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 org.junit.Assert;
20 import org.junit.Test;
21
22
23
24
25
26 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
27 public class BlockTest extends JexlTestCase {
28
29
30
31
32 public BlockTest() {
33 super("BlockTest");
34 }
35
36 @Test
37 public void testBlockSimple() throws Exception {
38 final JexlScript e = JEXL.createScript("if (true) { 'hello'; }");
39 final JexlContext jc = new MapContext();
40 final Object o = e.execute(jc);
41 Assert.assertEquals("Result is wrong", "hello", o);
42 }
43
44 @Test
45 public void testBlockExecutesAll() throws Exception {
46 final JexlScript e = JEXL.createScript("if (true) { x = 'Hello'; y = 'World';}");
47 final JexlContext jc = new MapContext();
48 final Object o = e.execute(jc);
49 Assert.assertEquals("First result is wrong", "Hello", jc.get("x"));
50 Assert.assertEquals("Second result is wrong", "World", jc.get("y"));
51 Assert.assertEquals("Block result is wrong", "World", o);
52 }
53
54 @Test
55 public void testEmptyBlock() throws Exception {
56 final JexlScript e = JEXL.createScript("if (true) { }");
57 final JexlContext jc = new MapContext();
58 final Object o = e.execute(jc);
59 Assert.assertNull("Result is wrong", o);
60 }
61
62 @Test
63 public void testBlockLastExecuted01() throws Exception {
64 final JexlScript e = JEXL.createScript("if (true) { x = 1; } else { x = 2; }");
65 final JexlContext jc = new MapContext();
66 final Object o = e.execute(jc);
67 Assert.assertEquals("Block result is wrong", new Integer(1), o);
68 }
69
70 @Test
71 public void testBlockLastExecuted02() throws Exception {
72 final JexlScript e = JEXL.createScript("if (false) { x = 1; } else { x = 2; }");
73 final JexlContext jc = new MapContext();
74 final Object o = e.execute(jc);
75 Assert.assertEquals("Block result is wrong", new Integer(2), o);
76 }
77
78 @Test
79 public void testNestedBlock() throws Exception {
80 final JexlScript e = JEXL.createScript("if (true) { x = 'hello'; y = 'world';" + " if (true) { x; } y; }");
81 final JexlContext jc = new MapContext();
82 final Object o = e.execute(jc);
83 Assert.assertEquals("Block result is wrong", "world", o);
84 }
85 }