-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathELEval_test.dart
207 lines (179 loc) · 7.35 KB
/
ELEval_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//Copyright (C) 2012 Potix Corporation. All Rights Reserved.
//History: Tue, Sep 25, 2012 11:15:34 AM
// Author: hernichen
library ELEval_test;
import 'package:unittest/unittest.dart';
import 'package:rikulo_commons/mirrors.dart';
import 'package:rikulo_el/el.dart';
import 'package:rikulo_el/elimpl.dart' show ELSupport;
void testBug42565() {
expect(evaluateExpression("\${false?true:false}"), equals("false"));
expect(evaluateExpression("\${false?true: false}"), equals("false"));
expect(evaluateExpression("\${false?true :false}"), equals("false"));
expect(evaluateExpression("\${false?true : false}"), equals("false"));
expect(evaluateExpression("\${false? true:false}"), equals("false"));
expect(evaluateExpression("\${false? true: false}"), equals("false"));
expect(evaluateExpression("\${false? true :false}"), equals("false"));
expect(evaluateExpression("\${false? true : false}"), equals("false"));
expect(evaluateExpression("\${false ?true:false}"), equals("false"));
expect(evaluateExpression("\${false ?true: false}"), equals("false"));
expect(evaluateExpression("\${false ?true :false}"), equals("false"));
expect(evaluateExpression("\${false ?true : false}"), equals("false"));
expect(evaluateExpression("\${false ? true:false}"), equals("false"));
expect(evaluateExpression("\${false ? true: false}"), equals("false"));
expect(evaluateExpression("\${false ? true :false}"), equals("false"));
expect(evaluateExpression("\${false ? true : false}"), equals("false"));
}
void testBug44994() {
expect(evaluateExpression(
"\${0 lt 0 ? 1 lt 0 ? 'many': 'one': 'none'}"), equals("none"));
expect(evaluateExpression(
"\${0 lt 1 ? 1 lt 1 ? 'many': 'one': 'none'}"), equals("one"));
expect(evaluateExpression(
"\${0 lt 2 ? 1 lt 2 ? 'many': 'one': 'none'}"), equals("many"));
}
void testParserBug45511() {
// Test cases provided by OP
expect(evaluateExpression("\${empty ('')}"), equals("true"));
expect(evaluateExpression("\${empty('')}"), equals("true"));
expect(evaluateExpression("\${(true) and (false)}"), equals("false"));
expect(evaluateExpression("\${(true)and(false)}"), equals("false"));
}
void testBug48112() {
expect(evaluateExpression("\${trim('{world}')}"), equals("{world}"));
expect(evaluateExpression("\${fn:trim('{world}')}"), equals("{fn:trim}"));
}
void testFunctionMapper2() {
expect(evaluateExpression("\${aaa().name}"), equals("Rikulo"));
}
void testParserLiteralExpression() {
// Inspired by work on bug 45451, comments from kkolinko on the dev
// list and looking at the spec to find some edge cases
// '\' is only an escape character inside a StringLiteral
expect(evaluateExpression("\\\\"), equals("\\\\"));
/*
* LiteralExpresions can only contain ${ or #{ if escaped with \
* \ is not an escape character in any other circumstances including \\
*/
expect(evaluateExpression("\\"), equals("\\"));
expect(evaluateExpression("\$"), equals("\$"));
expect(evaluateExpression("#"), equals("#"));
expect(evaluateExpression("\\\$"), equals("\\\$"));
expect(evaluateExpression("\\#"), equals("\\#"));
expect(evaluateExpression("\\\\\$"), equals("\\\\\$"));
expect(evaluateExpression("\\\\#"), equals("\\\\#"));
expect(evaluateExpression("\\\${"), equals("\${"));
expect(evaluateExpression("\\#{"), equals("#{"));
expect(evaluateExpression("\\\\\${"), equals("\\\${"));
expect(evaluateExpression("\\\\#{"), equals("\\#{"));
}
void testParserStringLiteral() {
// Inspired by work on bug 45451, comments from kkolinko on the dev
// list and looking at the spec to find some edge cases
// The only characters that can be escaped inside a String literal
// are \ " and '. # and $ are not escaped inside a String literal.
expect(evaluateExpression("\${'\\\\'}"), equals("\\"));
expect(evaluateExpression("\${\"\\\\\"}"), equals("\\"));
expect(evaluateExpression("\${'\\\\\\\"\\'\$#'}"), equals("\\\"'\$#"));
expect(evaluateExpression("\${\"\\\\\\\"\\'\$#\"}"), equals("\\\"'\$#"));
// Trying to quote # or $ should throw an error
Exception e = null;
try {
evaluateExpression("\${'\\\$'}");
} on ELException catch (el) {
e = el;
}
expect(e, isNotNull);
expect(evaluateExpression("\${'\\\\\$'}"), equals("\\\$"));
expect(evaluateExpression("\${'\\\\\\\\\$'}"), equals("\\\\\$"));
// Can use ''' inside '"' when quoting with '"' and vice versa without
// escaping
expect(evaluateExpression("\${'\\\\\"'}"), equals("\\\""));
expect(evaluateExpression("\${'\"\\\\'}"), equals("\"\\"));
expect(evaluateExpression("\${'\\\\\\''}"), equals("\\'"));
expect(evaluateExpression("\${'\\'\\\\'}"), equals("'\\"));
expect(evaluateExpression("\${\"\\\\'\"}"), equals("\\'"));
expect(evaluateExpression("\${\"'\\\\\"}"), equals("'\\"));
expect(evaluateExpression("\${\"\\\\\\\"\"}"), equals("\\\""));
expect(evaluateExpression("\${\"\\\"\\\\\"}"), equals("\"\\"));
}
void compareBoth(String msg, int expected, Object o1, Object o2){
int i1 = ELSupport.compare(o1, o2);
int i2 = ELSupport.compare(o2, o1);
expect(i1, equals(expected), reason:msg);
expect(-i2, equals(expected), reason:msg);
}
void testElSupportCompare(){
compareBoth("Nulls should compare equal", 0, null, null);
compareBoth("Null should compare equal to \"\"", 0, "", null);
// compareBoth("Null should be less than File()",-1, null, new File(""));
compareBoth("Null should be less than DateTime.now()",-1, null, new DateTime.now());
compareBoth("DateTime.fromMillisecondsSinceEpoch(0) should be less than DateTime.fromMillisecondsSinceEpoch(1)",
-1, new DateTime.fromMillisecondsSinceEpoch(0), new DateTime.fromMillisecondsSinceEpoch(1));
try {
compareBoth("Should not compare",0, new DateTime.now(), new Map());
expect(false, isTrue, reason:"Expecting CastException");
} catch (expected) {
// Expected
}
expect(null, isNull);
}
/**
* Test mixing ${...} and #{...} in the same expression.
*/
void testMixedTypes() {
// Mixing types should throw an error
Exception e = null;
try {
evaluateExpression("\${1+1}#{1+1}");
} on ELException catch (el) {
e = el;
}
expect(e, isNotNull);
}
//----------------
String evaluateExpression(String expression) {
ELContext ctx = new ELContext.mapper(functionMapper: new FMapper());
ExpressionFactory exprFactory = new ExpressionFactory();
ValueExpression ve = exprFactory.createValueExpression(ctx, expression,
STRING_MIRROR);
return ve.getValue(ctx);
}
class Person {
String name;
Person(this.name);
}
Person person = new Person('Rikulo');
aaa() => person;
class FMapper extends FunctionMapper {
//@Override
Function resolveFunction(String name) {
switch (name) {
case "trim":
return (String input) => input.trim();
case "fn:trim":
return (String input) => "{fn:trim}";
case "aaa":
return aaa;
}
return null;
}
}
//class Inner$Class {
//
// static final String RETVAL = "Return from bug49555";
// static String bug49555() {
// return RETVAL;
// }
//}
void main() {
test('Bug42565', testBug42565);
test('Bug44994', testBug44994);
test('ParserBug45511', testParserBug45511);
test('Bug48112', testBug48112);
test('testFunctionMapper2', testFunctionMapper2);
test('parserLiteralExpression', testParserLiteralExpression);
test('parserStringLiteral', testParserStringLiteral);
test('ElSupportCompare', testElSupportCompare);
test('mixedTypes', testMixedTypes);
}