-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCachedMethodExecutor_test.go
More file actions
executable file
·401 lines (354 loc) · 10.5 KB
/
Copy pathCachedMethodExecutor_test.go
File metadata and controls
executable file
·401 lines (354 loc) · 10.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
package main
// 参考 https://github.com/spring-projects/spring-framework/blob/main/spring-expression/src/test/java/org/springframework/expression/spel/CachedMethodExecutorTests.java
import (
"fmt"
"github.com/weaweawe01/ParserSpel/ast"
"testing"
)
// BaseObject represents the base class with echo method for strings
type BaseObject struct{}
func (b *BaseObject) Echo(value string) string {
return "String: " + value
}
// RootObject extends BaseObject and adds echo method for integers
type RootObject struct {
*BaseObject
}
func (r *RootObject) Echo(value int) string {
return fmt.Sprintf("int: %d", value)
}
// TestCachedMethodExecutor 测试方法执行器的缓存机制
func TestCachedMethodExecutor(t *testing.T) {
fmt.Println("=== 缓存方法执行器测试 ===")
// 测试用例定义
testCases := []struct {
name string
expression string
expected ASTExpectation
}{
{
name: "方法调用 - echo(#var)",
expression: "echo(#var)",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "echo(#var)",
Children: []ASTExpectation{
{
NodeType: "VariableReference",
Value: "#var",
Children: []ASTExpectation{},
},
},
},
},
{
name: "目标方法调用 - #var.echo(42)",
expression: "#var.echo(42)",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "#var.echo(42)",
Children: []ASTExpectation{
{
NodeType: "VariableReference",
Value: "#var",
Children: []ASTExpectation{},
},
{
NodeType: "MethodReference",
Value: "echo(42)",
Children: []ASTExpectation{
{
NodeType: "IntLiteral",
Value: "42",
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.ParseExpression(tc.expression)
if err != nil {
t.Fatalf("解析表达式失败: %v", err)
}
// 检查AST结构
fmt.Printf("AST-> %v\n", spelExpr.AST)
fmt.Printf("%s\n", spelExpr.GetExpressionString())
// 输出实际AST结构以便调试
fmt.Println("实际AST结构:")
ast.PrintAST(spelExpr.AST, 0)
// 验证AST结构
fmt.Println("验证AST结构...")
if !validateASTStructure(spelExpr.AST, tc.expected) {
t.Errorf("AST结构验证失败")
} else {
fmt.Println("✅ AST结构验证通过")
}
})
}
}
// TestCachedExecutionForParameters 测试参数缓存执行
func TestCachedExecutionForParameters(t *testing.T) {
fmt.Println("\n=== 参数缓存执行测试 ===")
// 模拟测试用例
testCases := []struct {
name string
expression string
variable interface{}
expected string
description string
}{
{
name: "整数参数 - 第一次",
expression: "echo(#var)",
variable: 42,
expected: "int: 42",
description: "使用整数参数调用echo方法",
},
{
name: "整数参数 - 第二次(缓存)",
expression: "echo(#var)",
variable: 42,
expected: "int: 42",
description: "重复使用相同整数参数,应该使用缓存",
},
{
name: "字符串参数",
expression: "echo(#var)",
variable: "Deep Thought",
expected: "String: Deep Thought",
description: "使用字符串参数调用echo方法",
},
{
name: "整数参数 - 第三次(缓存)",
expression: "echo(#var)",
variable: 42,
expected: "int: 42",
description: "再次使用整数参数,应该使用缓存",
},
}
parser := ast.NewSpelExpressionParser()
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)
fmt.Printf("变量值: %v\n", tc.variable)
fmt.Printf("描述: %s\n", tc.description)
expr, err := parser.ParseExpression(tc.expression)
if err != nil {
t.Fatalf("解析表达式失败: %v", err)
}
// 注意:在实际实现中,这里需要设置变量和上下文
// 由于当前的 Go 实现可能还没有完整的变量上下文支持,
// 这里主要验证表达式解析的正确性
fmt.Printf("解析结果: %s\n", expr.GetExpressionString())
fmt.Printf("期望结果: %s\n", tc.expected)
// 在有完整上下文支持后,可以添加实际的求值测试
// result, err := expr.GetValueWithContext(context)
// if err != nil {
// t.Fatalf("求值失败: %v", err)
// }
// if result != tc.expected {
// t.Errorf("结果不匹配: 期望 %v, 实际 %v", tc.expected, result)
// }
fmt.Println("✅ 表达式解析成功")
})
}
}
// TestCachedExecutionForTarget 测试目标对象缓存执行
func TestCachedExecutionForTarget(t *testing.T) {
fmt.Println("\n=== 目标对象缓存执行测试 ===")
// 模拟测试用例
testCases := []struct {
name string
expression string
target interface{}
expected string
description string
}{
{
name: "RootObject目标 - 第一次",
expression: "#var.echo(42)",
target: &RootObject{BaseObject: &BaseObject{}},
expected: "int: 42",
description: "使用RootObject调用echo方法",
},
{
name: "RootObject目标 - 第二次(缓存)",
expression: "#var.echo(42)",
target: &RootObject{BaseObject: &BaseObject{}},
expected: "int: 42",
description: "重复使用RootObject,应该使用缓存",
},
{
name: "BaseObject目标",
expression: "#var.echo(42)",
target: &BaseObject{},
expected: "String: 42",
description: "使用BaseObject调用echo方法",
},
{
name: "RootObject目标 - 第三次(缓存)",
expression: "#var.echo(42)",
target: &RootObject{BaseObject: &BaseObject{}},
expected: "int: 42",
description: "再次使用RootObject,应该使用缓存",
},
}
parser := ast.NewSpelExpressionParser()
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)
fmt.Printf("目标类型: %T\n", tc.target)
fmt.Printf("描述: %s\n", tc.description)
expr, err := parser.ParseExpression(tc.expression)
if err != nil {
t.Fatalf("解析表达式失败: %v", err)
}
// 注意:在实际实现中,这里需要设置目标对象和上下文
// 由于当前的 Go 实现可能还没有完整的对象上下文支持,
// 这里主要验证表达式解析的正确性
fmt.Printf("解析结果: %s\n", expr.GetExpressionString())
fmt.Printf("期望结果: %s\n", tc.expected)
// 在有完整上下文支持后,可以添加实际的求值测试
// context.SetVariable("var", tc.target)
// result, err := expr.GetValueWithContext(context)
// if err != nil {
// t.Fatalf("求值失败: %v", err)
// }
// if result != tc.expected {
// t.Errorf("结果不匹配: 期望 %v, 实际 %v", tc.expected, result)
// }
fmt.Println("✅ 表达式解析成功")
})
}
}
// TestMethodCachingBehavior 测试方法缓存行为
func TestMethodCachingBehavior(t *testing.T) {
fmt.Println("\n=== 方法缓存行为测试 ===")
testCases := []struct {
name string
expression string
expected ASTExpectation
}{
{
name: "简单方法调用",
expression: "toString()",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "toString()",
Children: []ASTExpectation{},
},
},
{
name: "带参数方法调用",
expression: "substring(1, 5)",
expected: ASTExpectation{
NodeType: "MethodReference",
Value: "substring(1, 5)",
Children: []ASTExpectation{
{NodeType: "IntLiteral", Value: "1", Children: []ASTExpectation{}},
{NodeType: "IntLiteral", Value: "5", Children: []ASTExpectation{}},
},
},
},
{
name: "链式方法调用",
expression: "getValue().toString()",
expected: ASTExpectation{
NodeType: "CompoundExpression",
Value: "getValue().toString()",
Children: []ASTExpectation{
{
NodeType: "MethodReference",
Value: "getValue()",
Children: []ASTExpectation{},
},
{
NodeType: "MethodReference",
Value: "toString()",
Children: []ASTExpectation{},
},
},
},
},
}
parser := ast.NewSpelExpressionParser()
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)
expr, err := parser.ParseExpression(tc.expression)
if err != nil {
t.Fatalf("解析表达式失败: %v", err)
}
fmt.Printf("AST-> %v\n", expr.AST)
fmt.Printf("%s\n", expr.GetExpressionString())
fmt.Println("实际AST结构:")
ast.PrintAST(expr.AST, 0)
fmt.Println("验证AST结构...")
if !validateASTStructure(expr.AST, tc.expected) {
t.Errorf("AST结构验证失败")
} else {
fmt.Println("✅ AST结构验证通过")
}
})
}
}
// TestMethodOverloading 测试方法重载
func TestMethodOverloading(t *testing.T) {
fmt.Println("\n=== 方法重载测试 ===")
// 模拟方法重载测试
overloadTests := []struct {
name string
expression string
description string
}{
{
name: "重载方法 - 整数参数",
expression: "echo(42)",
description: "调用接受整数参数的echo方法",
},
{
name: "重载方法 - 字符串参数",
expression: "echo('hello')",
description: "调用接受字符串参数的echo方法",
},
{
name: "重载方法 - 多个参数",
expression: "echo('test', 123)",
description: "调用接受多个参数的echo方法",
},
}
parser := ast.NewSpelExpressionParser()
for _, tc := range overloadTests {
t.Run(tc.name, func(t *testing.T) {
fmt.Printf("\n--- %s ---\n", tc.name)
fmt.Printf("表达式: %s\n", tc.expression)
fmt.Printf("描述: %s\n", tc.description)
expr, err := parser.ParseExpression(tc.expression)
if err != nil {
t.Fatalf("解析表达式失败: %v", err)
}
fmt.Printf("解析结果: %s\n", expr.GetExpressionString())
fmt.Println("实际AST结构:")
ast.PrintAST(expr.AST, 0)
// 验证方法调用是否正确解析
if expr.AST == nil {
t.Error("AST不应该为空")
} else {
fmt.Println("✅ 方法重载表达式解析成功")
}
})
}
}