Skip to content

Commit e40b325

Browse files
committed
Add new test cases for script
1 parent 7805892 commit e40b325

File tree

9 files changed

+2766
-0
lines changed

9 files changed

+2766
-0
lines changed
Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
package com.relogiclabs.jschema.test.negative;
2+
3+
import com.relogiclabs.jschema.JsonAssert;
4+
import com.relogiclabs.jschema.JsonSchema;
5+
import com.relogiclabs.jschema.exception.ScriptRuntimeException;
6+
import com.relogiclabs.jschema.exception.SystemOperationException;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static com.relogiclabs.jschema.message.ErrorCode.DIVD02;
10+
import static com.relogiclabs.jschema.message.ErrorCode.ITER01;
11+
import static com.relogiclabs.jschema.message.ErrorCode.VARD01;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
15+
public class ScriptBasicTests {
16+
@Test
17+
public void When_DuplicateVariableDefinition_ExceptionThrown() {
18+
var schema =
19+
"""
20+
%schema:
21+
{
22+
"variableTest": @variableTest #integer
23+
}
24+
%script: {
25+
constraint variableTest() {
26+
var test = 10;
27+
if(true) {
28+
// Variable shadowing in inner scope
29+
var test = 30;
30+
if(test != 30) return fail("Invalid: " + test);
31+
}
32+
if(test != 10) return fail("Invalid: " + test);
33+
// Variable 'test' already defined in this scope
34+
var test = 20;
35+
}
36+
}
37+
""";
38+
var json =
39+
"""
40+
{
41+
"variableTest": 2
42+
}
43+
""";
44+
JsonSchema.isValid(schema, json);
45+
var exception = assertThrows(ScriptRuntimeException.class,
46+
() -> JsonAssert.isValid(schema, json));
47+
assertEquals(VARD01, exception.getCode());
48+
exception.printStackTrace();
49+
}
50+
51+
@Test
52+
public void When_ForeachWithNonIterableValue_ExceptionThrown() {
53+
var schema =
54+
"""
55+
%schema:
56+
{
57+
"iteratorTest": @iteratorTest #integer
58+
}
59+
%script: {
60+
constraint iteratorTest() {
61+
foreach(var i in target) print(i);
62+
}
63+
}
64+
""";
65+
var json =
66+
"""
67+
{
68+
"iteratorTest": 2
69+
}
70+
""";
71+
JsonSchema.isValid(schema, json);
72+
var exception = assertThrows(ScriptRuntimeException.class,
73+
() -> JsonAssert.isValid(schema, json));
74+
assertEquals(ITER01, exception.getCode());
75+
exception.printStackTrace();
76+
}
77+
78+
@Test
79+
public void When_WrongIndexAndRangeOutOfBounds_ExceptionThrown() {
80+
var schema =
81+
"""
82+
%schema:
83+
{
84+
"indexTest1": @indexArrayTest #array,
85+
"indexTest2": @indexStringTest #string
86+
}
87+
%script: {
88+
constraint indexArrayTest() {
89+
var result1 = tryof(target[10]);
90+
if(find(result1.error, "[INDX04]") < 0)
91+
return fail("Invalid: " + result1);
92+
var result2 = tryof(target[-1]);
93+
if(find(result2.error, "[INDX05]") < 0)
94+
return fail("Invalid: " + result2);
95+
var result3 = tryof(target[10..]);
96+
if(find(result3.error, "[RNGS07]") < 0)
97+
return fail("Invalid: " + result3);
98+
var result4 = tryof(target[..10]);
99+
if(find(result4.error, "[RNGS08]") < 0)
100+
return fail("Invalid: " + result4);
101+
var array = [10, 20];
102+
// only assign at the end of array to add
103+
// use fill function for array of specific size
104+
var result5 = tryof(array[10] = 10);
105+
if(find(result5.error, "[INDX04]") < 0)
106+
return fail("Invalid: " + result5);
107+
}
108+
109+
constraint indexStringTest() {
110+
var result1 = tryof(target[100]);
111+
if(find(result1.error, "[INDX02]") < 0)
112+
return fail("Invalid: " + result1);
113+
var result2 = tryof(target[-1]);
114+
if(find(result2.error, "[INDX03]") < 0)
115+
return fail("Invalid: " + result2);
116+
var result3 = tryof(target[100..]);
117+
if(find(result3.error, "[RNGS04]") < 0)
118+
return fail("Invalid: " + result3);
119+
var result4 = tryof(target[..100]);
120+
if(find(result4.error, "[RNGS05]") < 0)
121+
return fail("Invalid: " + result4);
122+
}
123+
}
124+
""";
125+
var json =
126+
"""
127+
{
128+
"indexTest1": [1, 2, 3],
129+
"indexTest2": "This is a test"
130+
}
131+
""";
132+
//JsonSchema.isValid(schema, json);
133+
JsonAssert.isValid(schema, json);
134+
}
135+
136+
@Test
137+
public void When_WrongTypeForDifferentOperations_ExceptionThrown() {
138+
var schema =
139+
"""
140+
%schema:
141+
{
142+
"operationTest": @operationTest #object
143+
}
144+
%script: {
145+
constraint operationTest() {
146+
var result1 = tryof(target + 10);
147+
if(find(result1.error, "[ADDI01]") < 0)
148+
return fail("Invalid: " + result1);
149+
var result2 = tryof(target - 10);
150+
if(find(result2.error, "[SUBT01]") < 0)
151+
return fail("Invalid: " + result2);
152+
var result3 = tryof(target * 10);
153+
if(find(result3.error, "[MULT01]") < 0)
154+
return fail("Invalid: " + result3);
155+
var result4 = tryof(target / 10);
156+
if(find(result4.error, "[DIVD01]") < 0)
157+
return fail("Invalid: " + result4);
158+
var result5 = tryof(target > 10);
159+
if(find(result5.error, "[RELA01]") < 0)
160+
return fail("Invalid: " + result5);
161+
var result6 = tryof(target >= 10);
162+
if(find(result6.error, "[RELA02]") < 0)
163+
return fail("Invalid: " + result6);
164+
var result7 = tryof(target < 10);
165+
if(find(result7.error, "[RELA03]") < 0)
166+
return fail("Invalid: " + result7);
167+
var result8 = tryof(target <= 10);
168+
if(find(result8.error, "[RELA04]") < 0)
169+
return fail("Invalid: " + result8);
170+
}
171+
}
172+
""";
173+
var json =
174+
"""
175+
{
176+
"operationTest": {}
177+
}
178+
""";
179+
//JsonSchema.isValid(schema, json);
180+
JsonAssert.isValid(schema, json);
181+
}
182+
183+
@Test
184+
public void When_WrongLValueForIncrementDecrement_ExceptionThrown() {
185+
var schema =
186+
"""
187+
%schema:
188+
{
189+
"incDecTest1": @incDecTest1 #object,
190+
"incDecTest2": @incDecTest1 #array
191+
}
192+
%script: {
193+
constraint incDecTest1() {
194+
var t = target;
195+
var result1 = tryof(t++);
196+
if(find(result1.error, "[INCR02]") < 0)
197+
return fail("Invalid: " + result1);
198+
var result2 = tryof(++t);
199+
if(find(result2.error, "[INCR04]") < 0)
200+
return fail("Invalid: " + result2);
201+
var result3 = tryof(t--);
202+
if(find(result3.error, "[DECR02]") < 0)
203+
return fail("Invalid: " + result3);
204+
var result4 = tryof(--t);
205+
if(find(result4.error, "[DECR04]") < 0)
206+
return fail("Invalid: " + result4);
207+
}
208+
}
209+
""";
210+
var json =
211+
"""
212+
{
213+
"incDecTest1": {},
214+
"incDecTest2": []
215+
}
216+
""";
217+
//JsonSchema.isValid(schema, json);
218+
JsonAssert.isValid(schema, json);
219+
}
220+
221+
@Test
222+
public void When_ReadonlyLValueForOperations_ExceptionThrown() {
223+
var schema =
224+
"""
225+
%schema:
226+
{
227+
"lvalueTest1": @lvalueTest1 #object,
228+
"lvalueTest2": @lvalueTest2 #array
229+
}
230+
%script: {
231+
constraint lvalueTest1() {
232+
var result1 = tryof(target.test = 10);
233+
if(find(result1.error, "[PRPS02]") < 0)
234+
return fail("Invalid: " + result1);
235+
var result2 = tryof(target.k1 = 10);
236+
if(find(result2.error, "[ASIN01]") < 0)
237+
return fail("Invalid: " + result2);
238+
}
239+
240+
constraint lvalueTest2() {
241+
var result1 = tryof(target[2] = 10);
242+
if(find(result1.error, "[INDX04]") < 0)
243+
return fail("Invalid: " + result1);
244+
var result2 = tryof(target[0] = 10);
245+
if(find(result2.error, "[ASIN01]") < 0)
246+
return fail("Invalid: " + result2);
247+
}
248+
}
249+
""";
250+
var json =
251+
"""
252+
{
253+
"lvalueTest1": {"k1": 100},
254+
"lvalueTest2": [100, 200]
255+
}
256+
""";
257+
//JsonSchema.isValid(schema, json);
258+
JsonAssert.isValid(schema, json);
259+
}
260+
261+
@Test
262+
public void When_WrongLValueForMemberAndIndexRangeOperations_ExceptionThrown() {
263+
var schema =
264+
"""
265+
%schema:
266+
{
267+
"lvalueTest1": @lvalueTest1 #array,
268+
"lvalueTest2": @lvalueTest2 #object,
269+
"lvalueTest3": @lvalueTest3 #integer
270+
}
271+
%script: {
272+
constraint lvalueTest1() {
273+
var result1 = tryof(target.test = 100);
274+
if(find(result1.error, "[PRPS01]") < 0)
275+
return fail("Invalid: " + result1);
276+
var result2 = tryof(target["test"] = 100);
277+
if(find(result2.error, "[INDX07]") < 0)
278+
return fail("Invalid: " + result2);
279+
}
280+
281+
constraint lvalueTest2() {
282+
var result1 = tryof(target[0] = 100);
283+
if(find(result1.error, "[INDX06]") < 0)
284+
return fail("Invalid: " + result1);
285+
var result2 = tryof(target[0..] = 100);
286+
if(find(result2.error, "[RNGS10]") < 0)
287+
return fail("Invalid: " + result2);
288+
}
289+
290+
constraint lvalueTest3() {
291+
var result1 = tryof(target.test = 10);
292+
if(find(result1.error, "[PRPS01]") < 0)
293+
return fail("Invalid: " + result1);
294+
var result2 = tryof(target[0] = 10);
295+
if(find(result2.error, "[INDX06]") < 0)
296+
return fail("Invalid: " + result2);
297+
var result3 = tryof(target[0..] = 100);
298+
if(find(result3.error, "[RNGS10]") < 0)
299+
return fail("Invalid: " + result3);
300+
}
301+
}
302+
""";
303+
var json =
304+
"""
305+
{
306+
"lvalueTest1": [10, 20],
307+
"lvalueTest2": { "k1": 10 },
308+
"lvalueTest3": 10
309+
}
310+
""";
311+
//JsonSchema.isValid(schema, json);
312+
JsonAssert.isValid(schema, json);
313+
}
314+
315+
@Test
316+
public void When_RuntimeSystemException_ExceptionThrown() {
317+
// Exception like OutOfMemoryError / DivideByZero etc.
318+
var schema =
319+
"""
320+
%schema:
321+
{
322+
"exceptionTest": @exceptionTest #integer
323+
}
324+
%script: {
325+
constraint exceptionTest() {
326+
var result = target / 0;
327+
}
328+
}
329+
""";
330+
var json =
331+
"""
332+
{
333+
"exceptionTest": 10
334+
}
335+
""";
336+
JsonSchema.isValid(schema, json);
337+
var exception = assertThrows(SystemOperationException.class,
338+
() -> JsonAssert.isValid(schema, json));
339+
assertEquals(DIVD02, exception.getCode());
340+
exception.printStackTrace();
341+
}
342+
}

0 commit comments

Comments
 (0)