1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.parser;
18
19 import org.apache.commons.jexl3.JexlException;
20 import org.apache.commons.jexl3.JexlFeatures;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24
25
26
27 public class ParserTest {
28 static final JexlFeatures FEATURES = new JexlFeatures();
29 public ParserTest() {}
30
31
32
33
34 @Test
35 public void testParse() throws Exception {
36 final Parser parser = new Parser(";");
37 JexlNode sn;
38 sn = parser.parse(null, FEATURES, "foo = 1;", null);
39 Assert.assertNotNull("parsed node is null", sn);
40
41 sn = parser.parse(null, FEATURES, "foo = \"bar\";", null);
42 Assert.assertNotNull("parsed node is null", sn);
43
44 sn = parser.parse(null, FEATURES, "foo = 'bar';", null);
45 Assert.assertNotNull("parsed node is null", sn);
46 }
47
48 @Test
49 public void testErrorAssign() throws Exception {
50 final String[] ops = { "=", "+=", "-=", "/=", "*=", "^=", "&=", "|=" };
51 for(final String op : ops) {
52 final Parser parser = new Parser(";");
53 try {
54 final JexlNode sn = parser.parse(null, FEATURES, "foo() "+op+" 1;", null);
55 Assert.fail("should have failed on invalid assignment " + op);
56 } catch (final JexlException.Parsing xparse) {
57
58 final String ss = xparse.getDetail();
59 final String sss = xparse.toString();
60 }
61 }
62 }
63
64 @Test
65 public void testErrorAmbiguous() throws Exception {
66 final Parser parser = new Parser(";");
67 try {
68 final JexlNode sn = parser.parse(null, FEATURES, "x = 1 y = 5", null);
69 Assert.fail("should have failed on ambiguous statement");
70 } catch (final JexlException.Ambiguous xambiguous) {
71
72 } catch(final JexlException xother) {
73 Assert.fail(xother.toString());
74 }
75 }
76
77 @Test
78 public void testIdentifierEscape() {
79 final String[] ids = new String[]{"a\\ b", "a\\ b\\ c", "a\\'b\\\"c", "a\\ \\ c"};
80 for(final String id : ids) {
81 final String esc0 = StringParser.unescapeIdentifier(id);
82 Assert.assertFalse(esc0.contains("\\"));
83 final String esc1 = StringParser.escapeIdentifier(esc0);
84 Assert.assertEquals(id, esc1);
85 }
86 }
87
88
89
90
91 @Test
92 public void testControlCharacters() {
93
94
95 final String[][] strings = new String[][] {
96 new String[] {"a\nb\tc", "'a\nb\tc'"},
97 new String[] {"a\nb\tc", "'a\\nb\\tc'"},
98 new String[] {"a\nb\tc", "\"a\\nb\\tc\""},
99 new String[] {"\b\t\n\f\r", "'\\b\\t\\n\\f\\r'"},
100 new String[] {"'hi'", "'\\'hi\\''"},
101 new String[] {"\"hi\"", "'\"hi\"'"},
102 new String[] {"\"hi\"", "'\"hi\"'"},
103 };
104 for(final String[] pair: strings) {
105 final String output = StringParser.buildString(pair[1], true);
106 Assert.assertEquals(pair[0], output);
107 }
108 }
109 }