forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParsingTests.java
583 lines (477 loc) · 12.6 KB
/
ParsingTests.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Parse some expressions and check we get the AST we expect.
*
* <p>Rather than inspecting each node in the AST, we ask it to write itself to
* a string form and check that is as expected.
*
* @author Andy Clement
* @author Sam Brannen
*/
class ParsingTests {
private final SpelExpressionParser parser = new SpelExpressionParser();
@Nested
class Miscellaneous {
@Test
void literalNull() {
parseCheck("null");
}
@Test
void literalDate01() {
parseCheck("date('1974/08/24')");
}
@Test
void literalDate02() {
parseCheck("date('19740824T131030','yyyyMMddTHHmmss')");
}
@Test
void mixedOperators() {
parseCheck("true and 5>3", "(true and (5 > 3))");
}
@Test
void assignmentToVariables() {
parseCheck("#var1='value1'");
}
@Test
void collectionProcessorsCountStringArray() {
parseCheck("new String[] {'abc','def','xyz'}.count()");
}
@Test
void collectionProcessorsCountIntArray() {
parseCheck("new int[] {1,2,3}.count()");
}
@Test
void collectionProcessorsMax() {
parseCheck("new int[] {1,2,3}.max()");
}
@Test
void collectionProcessorsMin() {
parseCheck("new int[] {1,2,3}.min()");
}
@Test
void collectionProcessorsAverage() {
parseCheck("new int[] {1,2,3}.average()");
}
@Test
void collectionProcessorsSort() {
parseCheck("new int[] {3,2,1}.sort()");
}
@Test
void collectionProcessorsNonNull() {
parseCheck("{'a','b',null,'d',null}.nonNull()");
}
@Test
void collectionProcessorsDistinct() {
parseCheck("{'a','b','a','d','e'}.distinct()");
}
@Disabled("Unsupported syntax/feature")
@Test
void lambdaMax() {
parseCheck("(#max = {|x,y| $x > $y ? $x : $y }; #max(5,25))",
"(#max={|x,y| ($x > $y) ? $x : $y };#max(5,25))");
}
@Disabled("Unsupported syntax/feature")
@Test
void lambdaFactorial() {
parseCheck("(#fact = {|n| $n <= 1 ? 1 : $n * #fact($n-1) }; #fact(5))",
"(#fact={|n| ($n <= 1) ? 1 : ($n * #fact(($n - 1))) };#fact(5))");
}
@Disabled("Unsupported syntax/feature")
@Test
void projection() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.!{#isEven()}");
}
@Disabled("Unsupported syntax/feature")
@Test
void selection() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.?{#isEven(#this) == 'y'}",
"{1,2,3,4,5,6,7,8,9,10}.?{(#isEven(#this) == 'y')}");
}
@Disabled("Unsupported syntax/feature")
@Test
void selectionFirst() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.^{#isEven(#this) == 'y'}",
"{1,2,3,4,5,6,7,8,9,10}.^{(#isEven(#this) == 'y')}");
}
@Disabled("Unsupported syntax/feature")
@Test
void selectionLast() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.${#isEven(#this) == 'y'}",
"{1,2,3,4,5,6,7,8,9,10}.${(#isEven(#this) == 'y')}");
}
}
@Nested
class LiteralBooleans {
@Test
void literalBooleanFalse() {
parseCheck("false");
}
@Test
void literalBooleanTrue() {
parseCheck("true");
}
@Test
void literalBooleanNotTrue() {
parseCheck("!true");
parseCheck("not true", "!true");
}
}
@Nested
class LiteralNumbers {
@Test
void literalLong() {
parseCheck("37L", "37");
}
@Test
void literalIntegers() {
parseCheck("1");
parseCheck("1415");
}
@Test
void literalReal() {
parseCheck("6.0221415E+23", "6.0221415E23");
}
@Test
void literalHex() {
parseCheck("0x7FFFFFFF", "2147483647");
}
}
@Nested
class LiteralStrings {
@Test
void insideSingleQuotes() {
parseCheck("'hello'");
parseCheck("'hello world'");
}
@Test
void insideDoubleQuotes() {
parseCheck("\"hello\"", "'hello'");
parseCheck("\"hello world\"", "'hello world'");
}
@Test
void singleQuotesInsideSingleQuotes() {
parseCheck("'Tony''s Pizza'");
parseCheck("'big ''''pizza'''' parlor'");
}
@Test
void doubleQuotesInsideDoubleQuotes() {
parseCheck("\"big \"\"pizza\"\" parlor\"", "'big \"pizza\" parlor'");
parseCheck("\"big \"\"\"\"pizza\"\"\"\" parlor\"", "'big \"\"pizza\"\" parlor'");
}
@Test
void singleQuotesInsideDoubleQuotes() {
parseCheck("\"Tony's Pizza\"", "'Tony''s Pizza'");
parseCheck("\"big ''pizza'' parlor\"", "'big ''''pizza'''' parlor'");
}
@Test
void doubleQuotesInsideSingleQuotes() {
parseCheck("'big \"pizza\" parlor'");
parseCheck("'two double \"\" quotes'");
}
@Test
void inCompoundExpressions() {
parseCheck("'123''4' == '123''4'", "('123''4' == '123''4')");
parseCheck("('123''4'=='123''4')", "('123''4' == '123''4')");
parseCheck(
"""
"123""4" == "123""4"\
""",
"""
('123"4' == '123"4')\
""");
}
}
@Nested
class BooleanOperators {
@Test
void booleanOperatorsOr01() {
parseCheck("false or false", "(false or false)");
}
@Test
void booleanOperatorsOr02() {
parseCheck("false or true", "(false or true)");
}
@Test
void booleanOperatorsOr03() {
parseCheck("true or false", "(true or false)");
}
@Test
void booleanOperatorsOr04() {
parseCheck("true or false", "(true or false)");
}
@Test
void booleanOperatorsMix() {
parseCheck("false or true and false", "(false or (true and false))");
}
}
@Nested
class RelationalOperators {
@Test
void relOperatorsGT() {
parseCheck("3>6", "(3 > 6)");
}
@Test
void relOperatorsLT() {
parseCheck("3<6", "(3 < 6)");
}
@Test
void relOperatorsLE() {
parseCheck("3<=6", "(3 <= 6)");
}
@Test
void relOperatorsGE01() {
parseCheck("3>=6", "(3 >= 6)");
}
@Test
void relOperatorsGE02() {
parseCheck("3>=3", "(3 >= 3)");
}
@Disabled("Unsupported syntax/feature")
@Test
void relOperatorsIn() {
parseCheck("3 in {1,2,3,4,5}", "(3 in {1,2,3,4,5})");
}
@Test
void relOperatorsBetweenNumbers() {
parseCheck("1 between {1, 5}", "(1 between {1,5})");
}
@Test
void relOperatorsBetweenStrings() {
parseCheck("'efg' between {'abc', 'xyz'}", "('efg' between {'abc','xyz'})");
}
@Test
void relOperatorsInstanceOfInt() {
parseCheck("'xyz' instanceof int", "('xyz' instanceof int)");
}
@Test
void relOperatorsInstanceOfList() {
parseCheck("{1, 2, 3, 4, 5} instanceof List", "({1,2,3,4,5} instanceof List)");
}
@Test
void relOperatorsMatches() {
parseCheck("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'", "('5.0067' matches '^-?\\d+(\\.\\d{2})?$')");
parseCheck("'5.00' matches '^-?\\d+(\\.\\d{2})?$'", "('5.00' matches '^-?\\d+(\\.\\d{2})?$')");
}
}
@Nested
class MathematicalOperators {
@Test
void mathOperatorsAddIntegers() {
parseCheck("2+4", "(2 + 4)");
}
@Test
void mathOperatorsAddStrings() {
parseCheck("'a'+'b'", "('a' + 'b')");
}
@Test
void mathOperatorsAddMultipleStrings() {
parseCheck("'hello'+' '+'world'", "(('hello' + ' ') + 'world')");
}
@Test
void mathOperatorsSubtract() {
parseCheck("5-4", "(5 - 4)");
}
@Test
void mathOperatorsMultiply() {
parseCheck("7*4", "(7 * 4)");
}
@Test
void mathOperatorsDivide() {
parseCheck("8/4", "(8 / 4)");
}
@Test
void mathOperatorModulus() {
parseCheck("7 % 4", "(7 % 4)");
}
}
@Nested
class References {
@Test
void references() {
parseCheck("@foo");
parseCheck("@'foo.bar'");
parseCheck("@\"foo.bar.goo\"" , "@'foo.bar.goo'");
parseCheck("@$$foo");
}
}
@Nested
class Properties {
@Test
void propertiesSingle() {
parseCheck("name");
}
@Test
void propertiesDouble() {
parseCheck("placeofbirth.CitY");
}
@Test
void propertiesMultiple() {
parseCheck("a.b.c.d.e");
}
}
@Nested
class InlineCollections {
@Test
void inlineListOfIntegers() {
parseCheck("{1,2,3,4}");
parseCheck("{1, 2, 3, 4, 5}", "{1,2,3,4,5}");
}
@Test
void inlineListOfStrings() {
parseCheck("{'abc','xyz'}", "{'abc','xyz'}");
parseCheck("{\"abc\", 'xyz'}", "{'abc','xyz'}");
}
@Test
void inlineMapStringToObject() {
parseCheck("{'key1':'Value 1','today':DateTime.Today}");
}
@Test
void inlineMapIntegerToString() {
parseCheck("{1:'January',2:'February',3:'March'}");
}
}
@Nested
class MethodsConstructorsAndArrays {
@Test
void methods() {
parseCheck("echo()");
parseCheck("echo(12)");
parseCheck("echo(name)");
parseCheck("echo('Jane')");
parseCheck("echo('Jane',32)");
parseCheck("echo('Jane', 32)", "echo('Jane',32)");
parseCheck("age.doubleItAndAdd(12)");
}
@Test
void constructorWithNoArguments() {
parseCheck("new Foo()");
parseCheck("new example.Foo()");
}
@Test
void constructorWithOneArgument() {
parseCheck("new String('hello')");
parseCheck("new String( 'hello' )", "new String('hello')");
parseCheck("new String(\"hello\" )", "new String('hello')");
}
@Test
void constructorWithMultipleArguments() {
parseCheck("new example.Person('Jane',32,true)");
parseCheck("new example.Person('Jane', 32, true)", "new example.Person('Jane',32,true)");
parseCheck("new example.Person('Jane', 2 * 16, true)", "new example.Person('Jane',(2 * 16),true)");
}
@Test
void arrayConstructionWithOneDimensionalReferenceType() {
parseCheck("new String[3]");
}
@Test
void arrayConstructionWithOneDimensionalFullyQualifiedReferenceType() {
parseCheck("new java.lang.String[3]");
}
@Test
void arrayConstructionWithOneDimensionalPrimitiveType() {
parseCheck("new int[3]");
}
@Test
void arrayConstructionWithMultiDimensionalReferenceType() {
parseCheck("new Float[3][4]");
}
@Test
void arrayConstructionWithMultiDimensionalPrimitiveType() {
parseCheck("new int[3][4]");
}
@Test
void arrayConstructionWithOneDimensionalReferenceTypeWithInitializer() {
parseCheck("new String[] {'abc','xyz'}");
parseCheck("new String[] {'abc', 'xyz'}", "new String[] {'abc','xyz'}");
}
@Test
void arrayConstructionWithOneDimensionalPrimitiveTypeWithInitializer() {
parseCheck("new int[] {1,2,3,4,5}");
parseCheck("new int[] {1, 2, 3, 4, 5}", "new int[] {1,2,3,4,5}");
}
}
@Nested
class VariablesAndFunctions {
@Test
void variables() {
parseCheck("#foo");
}
@Test
void functions() {
parseCheck("#fn(1,2,3)");
parseCheck("#fn('hello')");
}
}
@Nested
class ElvisAndTernaryOperators {
@Test
void elvis() {
parseCheck("3?:1", "(3 ?: 1)");
parseCheck("(2*3)?:1*10", "((2 * 3) ?: (1 * 10))");
parseCheck("((2*3)?:1)*10", "(((2 * 3) ?: 1) * 10)");
}
@Test
void ternary() {
parseCheck("1>2?3:4", "((1 > 2) ? 3 : 4)");
parseCheck("(a ? 1 : 0) * 10", "((a ? 1 : 0) * 10)");
parseCheck("(a?1:0)*10", "((a ? 1 : 0) * 10)");
parseCheck("(4 % 2 == 0 ? 1 : 0) * 10", "((((4 % 2) == 0) ? 1 : 0) * 10)");
parseCheck("((4 % 2 == 0) ? 1 : 0) * 10", "((((4 % 2) == 0) ? 1 : 0) * 10)");
parseCheck("{1}.#isEven(#this) == 'y'?'it is even':'it is odd'",
"(({1}.#isEven(#this) == 'y') ? 'it is even' : 'it is odd')");
}
}
@Nested
class TypeReferences {
@Test
void typeReferences01() {
parseCheck("T(java.lang.String)");
}
@Test
void typeReferences02() {
parseCheck("T(String)");
}
}
/**
* Parse the supplied expression and then create a string representation of the resultant AST, it should be the same
* as the original expression.
*
* @param expression the expression to parse *and* the expected value of the string form of the resultant AST
*/
private void parseCheck(String expression) {
parseCheck(expression, expression);
}
/**
* Parse the supplied expression and then create a string representation of the resultant AST, it should be the
* expected value.
*
* @param expression the expression to parse
* @param expectedStringFormOfAST the expected string form of the AST
*/
private void parseCheck(String expression, String expectedStringFormOfAST) {
SpelExpression e = parser.parseRaw(expression);
assertThat(e).isNotNull();
assertThat(e.toStringAST()).isEqualTo(expectedStringFormOfAST);
}
}