@@ -58,6 +58,48 @@ public Object call(Object input, List args) throws Throwable {
5858 Assertions .assertEquals ("abc" , expression .evaluate (Map .of ("a" , "a" , "b" , "b" , "c" , "c" )));
5959 }
6060
61+ @ Test
62+ public void testFunctionWithInputData () {
63+ JFunction addWithFactor = new JFunction ("addWithFactor" , (input , args ) -> {
64+ var inputMap = (Map <String , Object >)input ;
65+ String factor = inputMap .get ("factor" ).toString ();
66+ int factorInt = Integer .parseInt (factor );
67+ return ((Integer ) args .get (0 ) + (Integer ) args .get (1 )) * factorInt ;
68+ }, null );
69+
70+ var expression = Jsonata .jsonata ("$addWithFactor(1, 2)" );
71+ expression .registerFunction (addWithFactor .functionName , addWithFactor );
72+ Assertions .assertEquals (9 , expression .evaluate (Map .of ("factor" , "3" )));
73+ }
74+
75+ @ Test
76+ public void testFunctionWithInputData2 () {
77+ JFunction addWithFactor = new JFunction ("multiply" , (input , args ) -> {
78+ Object first = args .get (0 );
79+ Object second ;
80+ if (args .size () > 1 ) {
81+ second = args .get (1 );
82+ } else {
83+ var inputMap = (Map <String , Object >)input ;
84+ String factor = inputMap .get ("factor" ).toString ();
85+ second = Integer .parseInt (factor );
86+ }
87+ return (Integer ) first * (Integer ) second ;
88+ }, null , 2 );
89+
90+ var expression = Jsonata .jsonata ("$multiply(3) ~> $multiply(?, 2)" );
91+ expression .registerFunction (addWithFactor .functionName , addWithFactor );
92+ Assertions .assertEquals (18 , expression .evaluate (Map .of ("factor" , "3" )));
93+ }
94+
95+ @ Test
96+ public void testCustomFunctionPipe () {
97+ JFunction add = new JFunction ("add" , (input , args ) -> (Integer ) args .get (0 ) + (Integer ) args .get (1 ), null , 2 );
98+ var expression = Jsonata .jsonata ("$add(1, 2) ~> $add(?, 3)" );
99+ expression .registerFunction (add .functionName , add );
100+ Assertions .assertEquals (6 , expression .evaluate (null ));
101+ }
102+
61103 /**
62104 * Lambdas use no signature - in case of an error, a ClassCastException is thrown
63105 */
0 commit comments