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.assertNull;
21
22 import org.junit.jupiter.api.Test;
23
24
25
26
27 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
28 class WhileTest extends JexlTestCase {
29
30 public WhileTest() {
31 super("WhileTest");
32 }
33
34 @Test
35 void testSimpleWhileFalse() throws Exception {
36 final JexlScript e = JEXL.createScript("while (false) ;");
37 final JexlContext jc = new MapContext();
38
39 final Object o = e.execute(jc);
40 assertNull(o);
41 }
42
43 @Test
44 void testWhileExecutesExpressionWhenLooping() throws Exception {
45 final JexlScript e = JEXL.createScript("while (x < 10) x = x + 1;");
46 final JexlContext jc = new MapContext();
47 jc.set("x", Integer.valueOf(1));
48
49 final Object o = e.execute(jc);
50 assertEquals(Integer.valueOf(10), o);
51 }
52
53 @Test
54 void testWhileWithBlock() throws Exception {
55 final JexlScript e = JEXL.createScript("while (x < 10) { x = x + 1; y = y * 2; }");
56 final JexlContext jc = new MapContext();
57 jc.set("x", Integer.valueOf(1));
58 jc.set("y", Integer.valueOf(1));
59
60 final Object o = e.execute(jc);
61 assertEquals(Integer.valueOf(512), o);
62 assertEquals(Integer.valueOf(10), jc.get("x"), "x is wrong");
63 assertEquals(Integer.valueOf(512), jc.get("y"), "y is wrong");
64 }
65 }