forked from jamsesso/json-logic-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableTests.java
More file actions
216 lines (169 loc) · 7.06 KB
/
VariableTests.java
File metadata and controls
216 lines (169 loc) · 7.06 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package io.github.jamsesso.jsonlogic;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class VariableTests {
private static final JsonLogic jsonLogic = new JsonLogic();
@Test
public void testEmptyString() throws JsonLogicException {
assertEquals(3.14, jsonLogic.apply("{\"var\": \"\"}", 3.14));
}
@Test
public void testMapAccess() throws JsonLogicException {
Map<String, Double> data = new HashMap<String, Double>() {{
put("pi", 3.14);
}};
assertEquals(3.14, jsonLogic.apply("{\"var\": \"pi\"}", data));
}
@Test
public void testDefaultValue() throws JsonLogicException {
assertEquals(3.14, jsonLogic.apply("{\"var\": [\"pi\", 3.14]}", null));
}
@Test
public void testUndefined() throws JsonLogicException {
assertNull(jsonLogic.apply("{\"var\": [\"pi\"]}", null));
assertNull(jsonLogic.apply("{\"var\": \"\"}", null));
assertNull(jsonLogic.apply("{\"var\": 0}", null));
}
@Test
public void testArrayAccess() throws JsonLogicException {
String[] data = new String[] {"hello", "world"};
assertEquals("hello", jsonLogic.apply("{\"var\": 0}", data));
assertEquals("world", jsonLogic.apply("{\"var\": 1}", data));
assertNull(jsonLogic.apply("{\"var\": 2}", data));
assertNull(jsonLogic.apply("{\"var\": 3}", data));
}
@Test
public void testArrayAccessWithStringKeys() throws JsonLogicException {
String[] data = new String[] {"hello", "world"};
assertEquals("hello", jsonLogic.apply("{\"var\": \"0\"}", data));
assertEquals("world", jsonLogic.apply("{\"var\": \"1\"}", data));
assertNull(jsonLogic.apply("{\"var\": \"2\"}", data));
assertNull(jsonLogic.apply("{\"var\": \"3\"}", data));
}
@Test
public void testListAccess() throws JsonLogicException {
List<String> data = Arrays.asList("hello", "world");
assertEquals("hello", jsonLogic.apply("{\"var\": 0}", data));
assertEquals("world", jsonLogic.apply("{\"var\": 1}", data));
assertNull(jsonLogic.apply("{\"var\": 2}", data));
assertNull(jsonLogic.apply("{\"var\": 3}", data));
}
@Test
public void testListAccessWithStringKeys() throws JsonLogicException {
List<String> data = Arrays.asList("hello", "world");
assertEquals("hello", jsonLogic.apply("{\"var\": \"0\"}", data));
assertEquals("world", jsonLogic.apply("{\"var\": \"1\"}", data));
assertNull(jsonLogic.apply("{\"var\": \"2\"}", data));
assertNull(jsonLogic.apply("{\"var\": \"3\"}", data));
}
@Test
public void testComplexAccess() throws JsonLogicException {
Map<String, Object> data = new HashMap<String, Object>() {{
put("users", Arrays.asList(
new HashMap<String, Object>() {{
put("name", "John");
put("followers", 1337);
}},
new HashMap<String, Object>() {{
put("name", "Jane");
put("followers", 2048);
}}
));
}};
assertEquals("John", jsonLogic.apply("{\"var\": \"users.0.name\"}", data));
assertEquals(1337.0, jsonLogic.apply("{\"var\": \"users.0.followers\"}", data));
assertEquals("Jane", jsonLogic.apply("{\"var\": \"users.1.name\"}", data));
assertEquals(2048.0, jsonLogic.apply("{\"var\": \"users.1.followers\"}", data));
}
@Test
public void missingNestedMapKey_returnsDefault() throws JsonLogicException {
// data.a.b is missing -> should use default
String rule = "{\"var\": [\"a.b.c\", \"fallback\"]}";
Map<String, Object> data = map("a", map("b", new HashMap<>()));
Object result = jsonLogic.apply(rule, data);
assertEquals("fallback", result);
}
@Test
public void presentNullLeaf_returnsNull_notDefault() throws JsonLogicException {
// data.user.age present with value null -> should return null (no default)
String rule = "{\"var\": [\"user.age\", 42]}";
Map<String, Object> user = new HashMap<>();
user.put("age", null);
Map<String, Object> data = map("user", user);
Object result = jsonLogic.apply(rule, data);
assertNull(result);
}
@Test
public void intermediateNull_returnsNull_notDefault() throws JsonLogicException {
// data.a.b is null before finishing path -> should return null (no default)
String rule = "{\"var\": [\"a.b.c\", \"fallback\"]}";
Map<String, Object> data = map("a", map("b", null));
Object result = jsonLogic.apply(rule, data);
assertNull(result);
}
@Test
public void nonTraversableIntermediate_returnsNull_notDefault() throws JsonLogicException {
// data.a is a number; trying to access a.b -> should return null (no default)
String rule = "{\"var\": [\"a.b\", \"fallback\"]}";
Map<String, Object> data = map("a", 5);
Object result = jsonLogic.apply(rule, data);
assertNull(result);
}
@Test
public void arrayIndexWithinBounds_returnsElement_asDoubleForNumbers() throws JsonLogicException {
// items.1 exists -> should return 20 (as a double per evaluator.transform)
String rule = "{\"var\": [\"items.1\", 999]}";
Map<String, Object> data = map("items", Arrays.asList(10, 20));
Object result = jsonLogic.apply(rule, data);
assertTrue(result instanceof Number);
assertEquals(20.0, ((Number) result).doubleValue(), 0.0);
}
@Test
public void arrayIndexOutOfBounds_returnsDefault() throws JsonLogicException {
// items.2 missing -> use default
String rule = "{\"var\": [\"items.2\", \"missing\"]}";
Map<String, Object> data = map("items", Arrays.asList(10, 20));
Object result = jsonLogic.apply(rule, data);
assertEquals("missing", result);
}
@Test
public void arrayElementPresentButNull_returnsNull_notDefault() throws JsonLogicException {
// items.0 exists and is null -> should return null (no default)
String rule = "{\"var\": [\"items.0\", \"missing\"]}";
Map<String, Object> data = map("items", Collections.singletonList(null));
Object result = jsonLogic.apply(rule, data);
assertNull(result);
}
@Test
public void topLevelNumericIndex_overList_works() throws JsonLogicException {
// {"var": [1, "missing"]} over a top-level list -> "banana"
String rule = "{\"var\": [1, \"missing\"]}";
List<String> data = Arrays.asList("apple", "banana", "carrot");
Object result = jsonLogic.apply(rule, data);
assertEquals("banana", result);
}
@Test
public void emptyVarKey_returnsWholeDataObject() throws JsonLogicException {
// {"var": ""} should return the entire data object (same instance)
String rule = "{\"var\": \"\"}";
Map<String, Object> data = map("x", 1);
Object result = jsonLogic.apply(rule, data);
assertSame("Should return the same data instance", data, result);
}
/** Helper to make small maps concisely. */
private static Map<String, Object> map(Object... kv) {
Map<String, Object> m = new HashMap<>();
for (int i = 0; i < kv.length; i += 2) {
m.put((String) kv[i], kv[i + 1]);
}
return m;
}
}