forked from dashjoin/jsonata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureTest.java
More file actions
71 lines (60 loc) · 2.2 KB
/
SignatureTest.java
File metadata and controls
71 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.dashjoin.jsonata;
import static com.dashjoin.jsonata.Jsonata.jsonata;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.dashjoin.jsonata.Jsonata.JFunction;
import com.dashjoin.jsonata.Jsonata.JFunctionCallable;
public class SignatureTest {
@Test
public void testParametersAreConvertedToArrays() {
Jsonata expr = jsonata("$greet(1,null,3)");
expr.registerFunction("greet", new JFunction(new JFunctionCallable() {
@Override
public Object call(Object input, @SuppressWarnings("rawtypes") List args) throws Throwable {
return args.toString();
}
}, "<a?a?a?a?:s>"));
Assertions.assertEquals("[[1], [null], [3], [null]]", expr.evaluate(null));
}
@Test
public void testError() {
Jsonata expr = jsonata("$foo()");
expr.registerFunction("foo", new JFunction(new JFunctionCallable() {
@Override
public Object call(Object input, @SuppressWarnings("rawtypes") List args) throws Throwable {
return null;
}
}, "(sao)"));
// null not allowed
Assertions.assertThrows(JException.class, ()->expr.evaluate(null));
// boolean not allowed
Assertions.assertThrows(JException.class, ()->expr.evaluate(true));
}
@Test
public void testVarArg() {
var expression = Jsonata.jsonata("$sumvar(1,2,3)");
expression.registerFunction("sumvar", new JFunction(new JFunctionCallable() {
@SuppressWarnings("rawtypes")
@Override
public Object call(Object input, List args) throws Throwable {
int sum = 0;
for (Object i : args)
sum += (int) i;
return sum;
}
}, "<n+:n>"));
Assertions.assertEquals(6, expression.evaluate(null));
}
@Test
public void testVarArgMany(){
Jsonata expr = jsonata("$customArgs('test',[1,2,3,4],3)");
expr.registerFunction("customArgs", new JFunction(new JFunctionCallable() {
@Override
public Object call(Object input, @SuppressWarnings("rawtypes") List args) throws Throwable {
return args.toString();
}
}, "<sa<n>n:s>"));
Assertions.assertEquals("[test, [1, 2, 3, 4], 3]", expr.evaluate(null));
}
}