1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 @SuppressWarnings({ "UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes" })
35 class DoWhileTest extends JexlTestCase {
36
37 public DoWhileTest() {
38 super("DoWhileTest");
39 }
40
41 @Test
42 void testEmptyBody() throws Exception {
43 final JexlScript e = JEXL.createScript("var i = 0; do ; while((i+=1) < 10); i");
44 final JexlContext jc = new MapContext();
45 final Object o = e.execute(jc);
46 assertEquals(10, o);
47 }
48
49 @Test
50 void testEmptyStmtBody() throws Exception {
51 final JexlScript e = JEXL.createScript("var i = 0; do {} while((i+=1) < 10); i");
52 final JexlContext jc = new MapContext();
53 final Object o = e.execute(jc);
54 assertEquals(10, o);
55 }
56
57 @Test
58 void testForEachBreakInsideFunction() throws Exception {
59 final JexlException.Parsing xparse = assertThrows(JexlException.Parsing.class, () -> JEXL.createScript("for (i : 1..2) { y = function() { break; } }"),
60 "break is out of loop!");
61 final String str = xparse.detailedMessage();
62 assertTrue(str.contains("break"));
63 }
64
65 @Test
66 void testForEachContinueInsideFunction() throws Exception {
67 final JexlException.Parsing xparse = assertThrows(JexlException.Parsing.class,
68 () -> JEXL.createScript("for (i : 1..2) { y = function() { continue; } }"), "break is out of loop!");
69 final String str = xparse.detailedMessage();
70 assertTrue(str.contains("continue"));
71 }
72
73 @Test
74 void testForEachLambda() throws Exception {
75 final JexlScript e = JEXL.createScript("(x)->{ for (i : 1..2) { continue; var y = function() { 42; } break; } }");
76 assertNotNull(e);
77 }
78
79 @Test
80 void testForLoop0() {
81 final String src = "(l)->{ for(let x = 0; x < 4; ++x) { l.add(x); } }";
82 final JexlEngine jexl = new JexlBuilder().safe(true).create();
83 final JexlScript script = jexl.createScript(src);
84 final List<Integer> l = new ArrayList<>();
85 final Object result = script.execute(null, l);
86 assertNotNull(result);
87 assertEquals(Arrays.asList(0, 1, 2, 3), l);
88 }
89
90 @Test
91 void testForLoop1() {
92 final String src = "(l)->{ for(var x = 0; x < 4; ++x) { l.add(x); } }";
93 final JexlEngine jexl = new JexlBuilder().safe(true).create();
94 final JexlScript script = jexl.createScript(src);
95 final List<Integer> l = new ArrayList<>();
96 final Object result = script.execute(null, l);
97 assertNotNull(result);
98 assertEquals(Arrays.asList(0, 1, 2, 3), l);
99 }
100
101 @Test
102 void testForLoop2() {
103 final String src = "(l)->{ for(x = 0; x < 4; ++x) { l.add(x); } }";
104 final JexlEngine jexl = new JexlBuilder().safe(true).create();
105 final JexlScript script = jexl.createScript(src);
106 final List<Integer> l = new ArrayList<>();
107 final JexlContext ctxt = new MapContext();
108 final Object result = script.execute(ctxt, l);
109 assertNotNull(result);
110 assertEquals(Arrays.asList(0, 1, 2, 3), l);
111 }
112
113 @Test
114 void testSimpleWhileFalse() throws Exception {
115 JexlScript e = JEXL.createScript("do {} while (false)");
116 final JexlContext jc = new MapContext();
117
118 Object o = e.execute(jc);
119 assertNull(o);
120 e = JEXL.createScript("do {} while (false); 23");
121 o = e.execute(jc);
122 assertEquals(23, o);
123
124 }
125
126 @Test
127 void testWhileEmptyBody() throws Exception {
128 final JexlScript e = JEXL.createScript("var i = 0; while((i+=1) < 10); i");
129 final JexlContext jc = new MapContext();
130 final Object o = e.execute(jc);
131 assertEquals(10, o);
132 }
133
134 @Test
135 void testWhileEmptyStmtBody() throws Exception {
136 final JexlScript e = JEXL.createScript("var i = 0; while((i+=1) < 10) {}; i");
137 final JexlContext jc = new MapContext();
138 final Object o = e.execute(jc);
139 assertEquals(10, o);
140 }
141
142 @Test
143 void testWhileExecutesExpressionWhenLooping() throws Exception {
144 JexlScript e = JEXL.createScript("do x = x + 1 while (x < 10)");
145 final JexlContext jc = new MapContext();
146 jc.set("x", 1);
147
148 Object o = e.execute(jc);
149 assertEquals(10, o);
150 assertEquals(10, jc.get("x"));
151
152 e = JEXL.createScript("var x = 0; do x += 1; while (x < 23)");
153 o = e.execute(jc);
154 assertEquals(23, o);
155
156 jc.set("x", 1);
157 e = JEXL.createScript("do x += 1; while (x < 23); return 42;");
158 o = e.execute(jc);
159 assertEquals(23, jc.get("x"));
160 assertEquals(42, o);
161 }
162
163 @Test
164 void testWhileWithBlock() throws Exception {
165 final JexlScript e = JEXL.createScript("do { x = x + 1; y = y * 2; } while (x < 10)");
166 final JexlContext jc = new MapContext();
167 jc.set("x", Integer.valueOf(1));
168 jc.set("y", Integer.valueOf(1));
169
170 final Object o = e.execute(jc);
171 assertEquals(Integer.valueOf(512), o, "Result is wrong");
172 assertEquals(Integer.valueOf(10), jc.get("x"), "x is wrong");
173 assertEquals(Integer.valueOf(512), jc.get("y"), "y is wrong");
174 }
175 }