-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodInvocation_test.go
More file actions
executable file
·470 lines (446 loc) · 14.5 KB
/
MethodInvocation_test.go
File metadata and controls
executable file
·470 lines (446 loc) · 14.5 KB
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
package main
// 测试用例参考:https://github.com/spring-projects/spring-framework/blob/main/spring-expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java
import (
"fmt"
"github.com/weaweawe01/ParserSpel/ast"
"testing"
)
// TestMethodInvocationAnalysis 测试SpEL方法调用表达式的AST树结构正确性
func TestMethodInvocationAnalysis(t *testing.T) {
fmt.Println("=== SpEL 方法调用 AST树结构测试 ===")
// 测试用例定义
testCases := []struct {
name string
expression string
expected ASTExpectation
}{
{
name: "链式方法调用",
expression: "getPlaceOfBirth().getCity()",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "getPlaceOfBirth().getCity()",
Children: []ASTExpectation{
{
NodeType: "MethodReference",
Value: "getPlaceOfBirth()",
Children: []ASTExpectation{},
},
{
NodeType: "MethodReference",
Value: ".getCity()",
Children: []ASTExpectation{},
},
},
},
},
{
name: "构造器创建字符串并调用方法",
expression: "new java.lang.String('hello').charAt(2)",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "new java.lang.String('hello').charAt(2)",
Children: []ASTExpectation{
{
NodeType: "ConstructorReference",
Value: "new java.lang.String('hello')",
Children: []ASTExpectation{
{
NodeType: "QualifiedIdentifier",
Value: "java.lang.String",
Children: []ASTExpectation{
{NodeType: "Identifier", Value: "java", Children: []ASTExpectation{}},
{NodeType: "Identifier", Value: "lang", Children: []ASTExpectation{}},
{NodeType: "Identifier", Value: "String", Children: []ASTExpectation{}},
},
},
{NodeType: "StringLiteral", Value: "'hello'", Children: []ASTExpectation{}},
},
},
{
NodeType: "MethodReference",
Value: ".charAt(2)",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "2", Children: []ASTExpectation{}},
},
},
},
},
},
{
name: "复杂链式方法调用与比较",
expression: "new java.lang.String('hello').charAt(2).equals('l'.charAt(0))",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "new java.lang.String('hello').charAt(2).equals('l'.charAt(0))",
Children: []ASTExpectation{
{
NodeType: "CompoundExpression",
Value: "new java.lang.String('hello').charAt(2)",
Children: []ASTExpectation{
{
NodeType: "ConstructorReference",
Value: "new java.lang.String('hello')",
Children: []ASTExpectation{
{
NodeType: "QualifiedIdentifier",
Value: "java.lang.String",
Children: []ASTExpectation{
{NodeType: "Identifier", Value: "java", Children: []ASTExpectation{}},
{NodeType: "Identifier", Value: "lang", Children: []ASTExpectation{}},
{NodeType: "Identifier", Value: "String", Children: []ASTExpectation{}},
},
},
{NodeType: "StringLiteral", Value: "'hello'", Children: []ASTExpectation{}},
},
},
{
NodeType: "MethodReference",
Value: ".charAt(2)",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "2", Children: []ASTExpectation{}},
},
},
},
},
{
NodeType: "MethodReference",
Value: ".equals('l'.charAt(0))",
Children: []ASTExpectation{
{
NodeType: "CompoundExpression",
Value: "'l'.charAt(0)",
Children: []ASTExpectation{
{NodeType: "StringLiteral", Value: "'l'", Children: []ASTExpectation{}},
{
NodeType: "MethodReference",
Value: ".charAt(0)",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "0", Children: []ASTExpectation{}},
},
},
},
},
},
},
},
},
},
{
name: "字符串转小写方法",
expression: "'HELLO'.toLowerCase()",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "'HELLO'.toLowerCase()",
Children: []ASTExpectation{
{NodeType: "StringLiteral", Value: "'HELLO'", Children: []ASTExpectation{}},
{
NodeType: "MethodReference",
Value: ".toLowerCase()",
Children: []ASTExpectation{},
},
},
},
},
{
name: "字符串去空格方法",
expression: "' abcba '.trim()",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "' abcba '.trim()",
Children: []ASTExpectation{
{NodeType: "StringLiteral", Value: "' abcba '", Children: []ASTExpectation{}},
{
NodeType: "MethodReference",
Value: ".trim()",
Children: []ASTExpectation{},
},
},
},
},
{
name: "Double构造器与比较方法",
expression: "new Double(3.0d).compareTo(8)",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "new Double(3.0).compareTo(8)",
Children: []ASTExpectation{
{
NodeType: "ConstructorReference",
Value: "new Double(3.0)",
Children: []ASTExpectation{
{
NodeType: "QualifiedIdentifier",
Value: "Double",
Children: []ASTExpectation{
{NodeType: "Identifier", Value: "Double", Children: []ASTExpectation{}},
},
},
{NodeType: "RealLiteral", Value: "3.0", Children: []ASTExpectation{}},
},
},
{
NodeType: "MethodReference",
Value: ".compareTo(8)",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "8", Children: []ASTExpectation{}},
},
},
},
},
},
{
name: "字符串startsWith方法",
expression: "new String('hello 2.0 to you').startsWith(7.0d)",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "new String('hello 2.0 to you').startsWith(7.0)",
Children: []ASTExpectation{
{
NodeType: "ConstructorReference",
Value: "new String('hello 2.0 to you')",
Children: []ASTExpectation{
{
NodeType: "QualifiedIdentifier",
Value: "String",
Children: []ASTExpectation{
{NodeType: "Identifier", Value: "String", Children: []ASTExpectation{}},
},
},
{NodeType: "StringLiteral", Value: "'hello 2.0 to you'", Children: []ASTExpectation{}},
},
},
{
NodeType: "MethodReference",
Value: ".startsWith(7.0)",
Children: []ASTExpectation{
{NodeType: "RealLiteral", Value: "7.0", Children: []ASTExpectation{}},
},
},
},
},
},
{
name: "可变参数方法调用-单参数",
expression: "aVarargsMethod(1)",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "aVarargsMethod(1)",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "1", Children: []ASTExpectation{}},
},
},
},
{
name: "可变参数方法调用-多参数",
expression: "aVarargsMethod('a',null,'b')",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "aVarargsMethod('a',null,'b')",
Children: []ASTExpectation{
{NodeType: "StringLiteral", Value: "'a'", Children: []ASTExpectation{}},
{NodeType: "NullLiteral", Value: "null", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'b'", Children: []ASTExpectation{}},
},
},
},
{
name: "可变参数方法调用-数组参数",
expression: "aVarargsMethod(new String[]{'a','b','c'})",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "aVarargsMethod(new String[] {'a','b','c'})",
Children: []ASTExpectation{
{
NodeType: "ConstructorReference",
Value: "new String[] {'a','b','c'}",
Children: []ASTExpectation{
{
NodeType: "QualifiedIdentifier",
Value: "String",
Children: []ASTExpectation{
{NodeType: "Identifier", Value: "String", Children: []ASTExpectation{}},
},
},
{
NodeType: "InlineList",
Value: "{'a','b','c'}",
Children: []ASTExpectation{
{NodeType: "StringLiteral", Value: "'a'", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'b'", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'c'", Children: []ASTExpectation{}},
},
},
},
},
},
},
},
{
name: "可变参数方法调用2-混合参数",
expression: "aVarargsMethod2(5,'a','b','c')",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "aVarargsMethod2(5,'a','b','c')",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "5", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'a'", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'b'", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'c'", Children: []ASTExpectation{}},
},
},
},
{
name: "可选可变参数方法调用",
expression: "optionalVarargsMethod(new String[]{'a','b','c'})",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "optionalVarargsMethod(new String[] {'a','b','c'})",
Children: []ASTExpectation{
{
NodeType: "ConstructorReference",
Value: "new String[] {'a','b','c'}",
Children: []ASTExpectation{
{
NodeType: "QualifiedIdentifier",
Value: "String",
Children: []ASTExpectation{
{NodeType: "Identifier", Value: "String", Children: []ASTExpectation{}},
},
},
{
NodeType: "InlineList",
Value: "{'a','b','c'}",
Children: []ASTExpectation{
{NodeType: "StringLiteral", Value: "'a'", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'b'", Children: []ASTExpectation{}},
{NodeType: "StringLiteral", Value: "'c'", Children: []ASTExpectation{}},
},
},
},
},
},
},
},
}
// 运行测试用例
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fmt.Printf("\n--- 测试用例: %s ---\n", tc.name)
fmt.Printf("表达式: %s\n", tc.expression)
parser := ast.NewSpelExpressionParser()
spelExpr, err := parser.ParseExpressionWithContext(tc.expression, nil)
if err != nil {
t.Logf("解析失败(预期可能失败): %v", err)
return // 继续下一个测试,不标记为失败
}
if spelExpr == nil || spelExpr.AST == nil {
t.Logf("解析结果为空(预期可能失败)")
return
}
// 打印实际的AST树结构
fmt.Println("实际AST结构:")
ast.PrintAST(spelExpr.AST, 0)
// 验证AST结构
fmt.Println("验证AST结构...")
if !validateASTStructure(spelExpr.AST.(ast.SpelNode), tc.expected) {
t.Logf("AST结构不匹配(预期可能不匹配)!\n期望: %+v\n实际AST见上方输出", tc.expected)
} else {
fmt.Println("✓ AST结构验证通过")
}
})
}
}
// TestMethodInvocationParsing 测试方法调用表达式的基本解析功能
func TestMethodInvocationParsing(t *testing.T) {
fmt.Println("\n=== 方法调用基本解析测试 ===")
testExpressions := []string{
"getPlaceOfBirth().getCity()",
"new java.lang.String('hello').charAt(2)",
"new java.lang.String('hello').charAt(2).equals('l'.charAt(0))",
"'HELLO'.toLowerCase()",
"' abcba '.trim()",
"new Double(3.0d).compareTo(8)",
"new String('hello 2.0 to you').startsWith(7.0d)",
"aVarargsMethod(1)",
"aVarargsMethod('a',null,'b')",
"aVarargsMethod(new String[]{'a','b','c'})",
"aVarargsMethod2(5,'a','b','c')",
"optionalVarargsMethod(new String[]{'a','b','c'})",
}
parser := ast.NewSpelExpressionParser()
for i, expr := range testExpressions {
t.Run(fmt.Sprintf("Expression_%d", i+1), func(t *testing.T) {
fmt.Printf("\n测试表达式 %d: %s\n", i+1, expr)
result, err := parser.ParseExpressionWithContext(expr, nil)
if err != nil {
fmt.Printf("❌ 解析错误: %v\n", err)
// 不强制失败,因为某些语法可能尚未实现
} else {
fmt.Printf("✅ 解析成功!\n")
if result != nil && result.AST != nil {
ast.PrintAST(result.AST, 0)
}
}
})
}
}
// TestMethodInvocationSpecialCases 测试方法调用的特殊情况
func TestMethodInvocationSpecialCases(t *testing.T) {
fmt.Println("\n=== 方法调用特殊情况测试 ===")
parser := ast.NewSpelExpressionParser()
// 测试链式方法调用
t.Run("ChainedMethods", func(t *testing.T) {
expr, err := parser.ParseExpression("getPlaceOfBirth().getCity()")
if err != nil {
fmt.Printf("链式方法调用解析失败: %v\n", err)
} else {
fmt.Printf("链式方法调用解析成功: %s\n", expr.AST.ToStringAST())
}
})
// 测试字符串字面量方法调用
t.Run("StringLiteralMethod", func(t *testing.T) {
expr, err := parser.ParseExpression("'HELLO'.toLowerCase()")
if err != nil {
fmt.Printf("字符串字面量方法调用解析失败: %v\n", err)
} else {
fmt.Printf("字符串字面量方法调用解析成功: %s\n", expr.AST.ToStringAST())
}
})
// 测试构造器+方法调用
t.Run("ConstructorAndMethod", func(t *testing.T) {
expr, err := parser.ParseExpression("new java.lang.String('hello').charAt(2)")
if err != nil {
fmt.Printf("构造器+方法调用解析失败: %v\n", err)
} else {
fmt.Printf("构造器+方法调用解析成功: %s\n", expr.AST.ToStringAST())
}
})
// 测试可变参数方法
t.Run("VarArgsMethod", func(t *testing.T) {
expr, err := parser.ParseExpression("aVarargsMethod('a',null,'b')")
if err != nil {
fmt.Printf("可变参数方法解析失败: %v\n", err)
} else {
fmt.Printf("可变参数方法解析成功: %s\n", expr.AST.ToStringAST())
}
})
// 测试复杂方法调用链
t.Run("ComplexMethodChain", func(t *testing.T) {
expr, err := parser.ParseExpression("new java.lang.String('hello').charAt(2).equals('l'.charAt(0))")
if err != nil {
fmt.Printf("复杂方法调用链解析失败: %v\n", err)
} else {
fmt.Printf("复杂方法调用链解析成功: %s\n", expr.AST.ToStringAST())
}
})
// 测试数组参数方法调用
t.Run("ArrayParameterMethod", func(t *testing.T) {
expr, err := parser.ParseExpression("aVarargsMethod(new String[]{'a','b','c'})")
if err != nil {
fmt.Printf("数组参数方法调用解析失败: %v\n", err)
} else {
fmt.Printf("数组参数方法调用解析成功: %s\n", expr.AST.ToStringAST())
}
})
}