001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.el.tree.impl;
017:
018: import de.odysseus.el.TestCase;
019: import de.odysseus.el.tree.Tree;
020: import de.odysseus.el.tree.impl.ast.AstBinary;
021:
022: public class ParserTest extends TestCase {
023: static Tree verifyLiteralExpression(String expression) {
024: Tree tree = parse(expression);
025: assertTrue(tree.getRoot().isLiteralText());
026: assertEquals(expression, tree.getRoot().getStructuralId(null));
027: return tree;
028: }
029:
030: static Tree verifyEvalExpression(String canonical) {
031: Tree tree = parse(canonical);
032: assertFalse(tree.getRoot().isLiteralText());
033: assertEquals(canonical, tree.getRoot().getStructuralId(null));
034: return tree;
035: }
036:
037: static Tree verifyEvalExpression(String canonical, String expression) {
038: Tree tree = parse(expression);
039: assertFalse(tree.getRoot().isLiteralText());
040: assertEquals(canonical, tree.getRoot().getStructuralId(null));
041: return verifyEvalExpression(canonical);
042: }
043:
044: static Tree verifyEvalExpression(String canonical,
045: String expression1, String expression2) {
046: Tree tree = parse(expression2);
047: assertFalse(tree.getRoot().isLiteralText());
048: assertEquals(canonical, tree.getRoot().getStructuralId(null));
049: return verifyEvalExpression(canonical, expression1);
050: }
051:
052: static Tree verifyCompositeExpression(String canonical) {
053: Tree tree = parse(canonical);
054: assertFalse(tree.getRoot().isLiteralText());
055: assertEquals(canonical, tree.getRoot().getStructuralId(null));
056: return tree;
057: }
058:
059: public void testLiteral() {
060: verifyLiteralExpression("");
061: verifyLiteralExpression("$");
062: verifyLiteralExpression("#");
063: verifyLiteralExpression("1");
064: verifyLiteralExpression("{1}");
065: verifyLiteralExpression("\\${1}");
066: verifyLiteralExpression("\\#{1}");
067: verifyLiteralExpression("\\${1}\\#{1}");
068: verifyLiteralExpression("\\");
069: verifyLiteralExpression("\\\\");
070: verifyLiteralExpression("foo");
071: verifyLiteralExpression("\\f\\o\\o\\");
072: verifyLiteralExpression("\"foo\"");
073: verifyLiteralExpression("'foo'");
074: }
075:
076: Tree verifyBinary(AstBinary.Operator op, String canonical) {
077: Tree tree = verifyEvalExpression(canonical);
078: assertTrue((tree.getRoot()).getChild(0) instanceof AstBinary);
079: assertEquals(op, ((AstBinary) tree.getRoot().getChild(0))
080: .getOperator());
081: return tree;
082: }
083:
084: public void testBinray() {
085: verifyEvalExpression("${a * a}");
086: verifyEvalExpression("${a / a}", "${a div a}");
087: verifyEvalExpression("${a % a}", "${a mod a}");
088: verifyEvalExpression("${a + a}");
089: verifyEvalExpression("${a - a}");
090: verifyEvalExpression("${a < a}", "${a lt a}");
091: verifyEvalExpression("${a > a}", "${a gt a}");
092: verifyEvalExpression("${a <= a}", "${a le a}");
093: verifyEvalExpression("${a >= a}", "${a ge a}");
094: verifyEvalExpression("${a == a}", "${a eq a}");
095: verifyEvalExpression("${a != a}", "${a ne a}");
096: verifyEvalExpression("${a && a}", "${a and a}");
097: verifyEvalExpression("${a || a}", "${a or a}");
098:
099: verifyBinary(AstBinary.DIV, "${a * a / a}");
100: verifyBinary(AstBinary.MUL, "${a / a * a}");
101: verifyBinary(AstBinary.MOD, "${a / a % a}");
102: verifyBinary(AstBinary.DIV, "${a % a / a}");
103: verifyBinary(AstBinary.ADD, "${a % a + a}");
104: verifyBinary(AstBinary.ADD, "${a + a % a}");
105: verifyBinary(AstBinary.SUB, "${a + a - a}");
106: verifyBinary(AstBinary.ADD, "${a - a + a}");
107: verifyBinary(AstBinary.LT, "${a - a < a}");
108: verifyBinary(AstBinary.LT, "${a < a - a}");
109: verifyBinary(AstBinary.GT, "${a < a > a}");
110: verifyBinary(AstBinary.LT, "${a > a < a}");
111: verifyBinary(AstBinary.LE, "${a > a <= a}");
112: verifyBinary(AstBinary.GT, "${a <= a > a}");
113: verifyBinary(AstBinary.GE, "${a <= a >= a}");
114: verifyBinary(AstBinary.LE, "${a >= a <= a}");
115: verifyBinary(AstBinary.EQ, "${a == a >= a}");
116: verifyBinary(AstBinary.EQ, "${a >= a == a}");
117: verifyBinary(AstBinary.NE, "${a == a != a}");
118: verifyBinary(AstBinary.EQ, "${a != a == a}");
119: verifyBinary(AstBinary.AND, "${a && a != a}");
120: verifyBinary(AstBinary.AND, "${a != a && a}");
121: verifyBinary(AstBinary.OR, "${a && a || a}");
122: verifyBinary(AstBinary.OR, "${a || a && a}");
123: verifyBinary(AstBinary.OR, "${! a || a}");
124: }
125:
126: public void testUnary() {
127: verifyEvalExpression("${- a}");
128: verifyEvalExpression("${- - a}");
129: verifyEvalExpression("${empty a}");
130: verifyEvalExpression("${empty empty a}");
131: verifyEvalExpression("${! a}", "${not a}");
132: verifyEvalExpression("${! ! a}", "${not not a}", "${not ! a}");
133: }
134:
135: public void testDeferredExpression() {
136: verifyEvalExpression("#{a}", "#{ a }");
137: }
138:
139: public void testComposite() {
140: verifyCompositeExpression("a${a}a");
141: verifyCompositeExpression("a ${a} a");
142: verifyCompositeExpression("${a}${a}");
143: try {
144: parse("#{a}${a}");
145: fail();
146: } catch (Exception e) {
147: }
148: }
149:
150: public void testInteger() {
151: verifyEvalExpression("${0}");
152: }
153:
154: public void testBoolean() {
155: verifyEvalExpression("${true}");
156: verifyEvalExpression("${false}");
157: }
158:
159: public void testNull() {
160: verifyEvalExpression("${null}");
161: }
162:
163: public void testString() {
164: verifyEvalExpression("${''}", "${\"\"}");
165: verifyEvalExpression("${'\\''}", "${\"'\"}");
166: verifyEvalExpression("${'\"'}", "${\"\\\"\"}");
167: verifyEvalExpression("${'a'}", "${\"a\"}");
168: }
169:
170: public void testFloat() {
171: verifyEvalExpression("${0.0}", "${0.0e0}");
172: verifyEvalExpression("${0.0}", "${0e0}", "${0E0}");
173: verifyEvalExpression("${0.0}", "${0.}", "${0.e0}");
174: verifyEvalExpression("${0.0}", "${.0}", "${.0e0}");
175: verifyEvalExpression("${0.0}", "${0e+0}", "${0e-0}");
176: }
177:
178: public void testChoice() {
179: verifyEvalExpression("${a ? a : a}", "${a?a:a}");
180: verifyEvalExpression("${a ? b ? b : b : a}", "${a?b?b:b:a}");
181: verifyEvalExpression("${a ? a : b ? b : b}", "${a?a:b?b:b}");
182: verifyEvalExpression("${c ? b : (f())}", "${c?b:(f())}");
183: verifyEvalExpression("${a ? f() : a}", "${a?f():a}");
184: verifyEvalExpression("${a ? a : a:f()}", "${a?a:a:f()}");
185: verifyEvalExpression("${a ? a:f() : a}", "${a?a:f():a}");
186: try {
187: parse("${a?a:f()}");
188: fail();
189: } catch (Exception e) {
190: }
191: }
192:
193: public void testNested() {
194: verifyEvalExpression("${(a)}", "${ ( a ) }");
195: verifyEvalExpression("${((a))}");
196: }
197:
198: public void testIdentifier() {
199: verifyEvalExpression("${a}", "${ a}", "${a }");
200: assertTrue(parse("${a}").getRoot().isLeftValue());
201: }
202:
203: public void testFunction() {
204: verifyEvalExpression("${a()}");
205: verifyEvalExpression("${a(a)}");
206: verifyEvalExpression("${a(a, a)}");
207: verifyEvalExpression("${a:a()}");
208: verifyEvalExpression("${a:a(a)}");
209: verifyEvalExpression("${a:a(a, a)}");
210: }
211:
212: public void testProperty() {
213: verifyEvalExpression("${a.a}", "${ a . a }");
214: verifyEvalExpression("${a.a.a}");
215: verifyEvalExpression("${a[a]}", "${ a [ a ] }");
216: verifyEvalExpression("${a[a][a]}");
217: verifyEvalExpression("${a[a[a]]}");
218:
219: assertTrue(parse("${a.a}").getRoot().isLeftValue());
220: assertFalse(parse("${1 . a}").getRoot().isLeftValue());
221: assertTrue(parse("${(1).a}").getRoot().isLeftValue());
222:
223: assertTrue(parse("${a[a]}").getRoot().isLeftValue());
224: assertFalse(parse("${1[a]}").getRoot().isLeftValue());
225: assertTrue(parse("${(1)[a]}").getRoot().isLeftValue());
226: }
227:
228: public void testIsDeferred() {
229: assertFalse(parse("foo").isDeferred());
230: assertFalse(parse("${foo}").isDeferred());
231: assertFalse(parse("${foo}bar${foo}").isDeferred());
232: assertTrue(parse("#{foo}").isDeferred());
233: assertTrue(parse("#{foo}bar#{foo}").isDeferred());
234: }
235: }
|