@@ -4084,4 +4084,114 @@ public void shouldTransformStringToBase64() {
40844084 );
40854085 }
40864086
4087+ @ Test // checkstyle-disable-line JavaNCSS
4088+ public void shouldCreateVariableFromLiteralValue () {
4089+ MetafixTestHelpers .assertFix (streamReceiver , Arrays .asList (
4090+ "to_var('data.title', 'testVar')" ,
4091+ "add_field('testResult', 'This is a $[testVar]')"
4092+ ),
4093+ i -> {
4094+ i .startRecord ("1" );
4095+ i .startEntity ("data" );
4096+ i .literal ("title" , "test" );
4097+ i .endEntity ();
4098+ i .endRecord ();
4099+ i .startRecord ("2" );
4100+ i .endRecord ();
4101+ i .startRecord ("3" );
4102+ i .startEntity ("data" );
4103+ i .literal ("title" , "final-test" );
4104+ i .endEntity ();
4105+ i .endRecord ();
4106+ },
4107+ o -> {
4108+ o .get ().startRecord ("1" );
4109+ o .get ().startEntity ("data" );
4110+ o .get ().literal ("title" , "test" );
4111+ o .get ().endEntity ();
4112+ o .get ().literal ("testResult" , "This is a test" );
4113+ o .get ().endRecord ();
4114+ o .get ().startRecord ("2" );
4115+ o .get ().literal ("testResult" , "This is a " );
4116+ o .get ().endRecord ();
4117+ o .get ().startRecord ("3" );
4118+ o .get ().startEntity ("data" );
4119+ o .get ().literal ("title" , "final-test" );
4120+ o .get ().endEntity ();
4121+ o .get ().literal ("testResult" , "This is a final-test" );
4122+ o .get ().endRecord ();
4123+ }
4124+ );
4125+ }
4126+
4127+ @ Test
4128+ public void shouldCreateVariableFromLiteralValueWithDefault () {
4129+ MetafixTestHelpers .assertFix (streamReceiver , Arrays .asList (
4130+ "to_var('data.title', 'testVar', 'default': 'n/a')" ,
4131+ "add_field('testResult', 'This is a $[testVar]')"
4132+ ),
4133+ i -> {
4134+ i .startRecord ("1" );
4135+ i .startEntity ("data" );
4136+ i .literal ("title" , "test" );
4137+ i .endEntity ();
4138+ i .endRecord ();
4139+ i .startRecord ("2" );
4140+ i .endRecord ();
4141+ },
4142+ o -> {
4143+ o .get ().startRecord ("1" );
4144+ o .get ().startEntity ("data" );
4145+ o .get ().literal ("title" , "test" );
4146+ o .get ().endEntity ();
4147+ o .get ().literal ("testResult" , "This is a test" );
4148+ o .get ().endRecord ();
4149+ o .get ().startRecord ("2" );
4150+ o .get ().literal ("testResult" , "This is a n/a" );
4151+ o .get ().endRecord ();
4152+ }
4153+ );
4154+ }
4155+
4156+ @ Test
4157+ public void shouldNotCreateVariableFromArrayValue () {
4158+ MetafixTestHelpers .assertExecutionException (IllegalStateException .class , "Expected String, got Array" , () ->
4159+ MetafixTestHelpers .assertFix (streamReceiver , Arrays .asList (
4160+ "to_var('data.title', 'testVar')"
4161+ ),
4162+ i -> {
4163+ i .startRecord ("1" );
4164+ i .startEntity ("data" );
4165+ i .literal ("title" , "test1" );
4166+ i .literal ("title" , "test2" );
4167+ i .endEntity ();
4168+ i .endRecord ();
4169+ },
4170+ o -> {
4171+ }
4172+ )
4173+ );
4174+ }
4175+
4176+ @ Test
4177+ public void shouldNotCreateVariableFromHashValue () {
4178+ MetafixTestHelpers .assertExecutionException (IllegalStateException .class , "Expected String, got Hash" , () ->
4179+ MetafixTestHelpers .assertFix (streamReceiver , Arrays .asList (
4180+ "to_var('data.title', 'testVar')"
4181+ ),
4182+ i -> {
4183+ i .startRecord ("1" );
4184+ i .startEntity ("data" );
4185+ i .startEntity ("title" );
4186+ i .literal ("key" , "value" );
4187+ i .endEntity ();
4188+ i .endEntity ();
4189+ i .endRecord ();
4190+ },
4191+ o -> {
4192+ }
4193+ )
4194+ );
4195+ }
4196+
40874197}
0 commit comments