-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht_base_lexer_basic_test.go
More file actions
462 lines (407 loc) · 12.5 KB
/
Copy patht_base_lexer_basic_test.go
File metadata and controls
462 lines (407 loc) · 12.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
package script
import (
"testing"
)
func Test_lexer_BasicTokens(t *testing.T) {
tests := []struct {
input string
expected []TokenType
}{
{
"x := 10",
[]TokenType{TokenIdent, TokenAssign, TokenInt},
},
{
"x + y",
[]TokenType{TokenIdent, TokenPlus, TokenIdent},
},
{
"if x > 10",
[]TokenType{TokenIf, TokenIdent, TokenGt, TokenInt},
},
{
"fn add()",
[]TokenType{TokenFn, TokenIdent, TokenLParen, TokenRParen},
},
{
"true false nil",
[]TokenType{TokenBool, TokenBool, TokenNil},
},
{
`"hello"`,
[]TokenType{TokenString},
},
{
"3.14",
[]TokenType{TokenFloat},
},
}
for _, tt := range tests {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Errorf("词法分析失败: %v", err)
continue
}
if len(tokens) != len(tt.expected) {
t.Errorf("Token数量不匹配: got=%d, want=%d", len(tokens), len(tt.expected))
continue
}
for i, tok := range tokens {
if tok.Type != tt.expected[i] {
t.Errorf("Token类型不匹配 [%d]: got=%v, want=%v", i, tok.Type, tt.expected[i])
}
}
}
}
func Test_lexer_Comments(t *testing.T) {
input := `
// 这是注释
x := 10
/* 多行
注释 */
y := 20
`
lexer := NewLexer(input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Fatalf("词法分析失败: %v", err)
}
// 注释应该被过滤掉
expected := []TokenType{TokenIdent, TokenAssign, TokenInt, TokenIdent, TokenAssign, TokenInt}
if len(tokens) != len(expected) {
t.Errorf("Token数量不匹配: got=%d, want=%d", len(tokens), len(expected))
}
for i, tok := range tokens {
if tok.Type != expected[i] {
t.Errorf("Token类型不匹配 [%d]: got=%v, want=%v", i, tok.Type, expected[i])
}
}
}
func Test_lexer_Numbers(t *testing.T) {
tests := []struct {
input string
expected string
}{
// 基础数字
{"123", "123"},
{"3.14", "3.14"},
// 下划线分隔
{"1_000", "1000"},
{"1_000_000", "1000000"},
// 十六进制
{"0xFF", "0xFF"},
{"0x10", "0x10"},
{"0xAB", "0xAB"},
{"0x00", "0x00"},
{"0XFF", "0XFF"},
{"0xFF_FF", "0xFFFF"},
}
for _, tt := range tests {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Errorf("词法分析失败: %v", err)
continue
}
if len(tokens) != 1 {
t.Errorf("期望1个token,got=%d", len(tokens))
continue
}
if tokens[0].Value != tt.expected {
t.Errorf("值不匹配: got=%s, want=%s", tokens[0].Value, tt.expected)
}
}
}
func Test_lexer_EscapeSequences(t *testing.T) {
tests := []struct {
input string
expected string
}{
{`"hello\nworld"`, "hello\nworld"},
{`"tab\there"`, "tab\there"},
{`"quote\"test"`, "quote\"test"},
{`"backslash\\"`, "backslash\\"},
{`"carriage\rreturn"`, "carriage\rreturn"},
}
for _, tt := range tests {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Errorf("词法分析失败: %v", err)
continue
}
if len(tokens) != 1 {
t.Errorf("期望1个token,got=%d", len(tokens))
continue
}
if tokens[0].Value != tt.expected {
t.Errorf("值不匹配: got=%q, want=%q", tokens[0].Value, tt.expected)
}
}
}
// ========== Lexer边缘场景测试(从lexer_edge_test.go合并) ==========
// TestLexer_EmptyInput 测试空输入
func Test_lexer_EmptyInput(t *testing.T) {
parser := NewParser()
script, err := parser.Compile("")
if err != nil {
t.Fatalf("空输入编译失败: %v", err)
}
ctx := NewContext()
engine := NewEngine()
_, err = engine.Run(ctx, script)
if err != nil {
t.Fatalf("空输入执行失败: %v", err)
}
}
// TestLexer_Whitespace 测试空白字符
func Test_lexer_Whitespace(t *testing.T) {
input := " \t 42 \n "
parser := NewParser()
script, err := parser.Compile(input)
if err != nil {
t.Fatalf("编译失败: %v", err)
}
ctx := NewContext()
engine := NewEngine()
result, err := engine.Run(ctx, script)
if err != nil {
t.Fatalf("执行失败: %v", err)
}
if result.Int() != 42 {
t.Errorf("期望 42, 得到 %d", result.Int())
}
}
// TestLexer_FloatUnderscore 测试浮点数中的下划线
func Test_lexer_FloatUnderscore(t *testing.T) {
input := "1_000.500_000"
parser := NewParser()
script, err := parser.Compile(input)
if err != nil {
t.Fatalf("编译失败: %v", err)
}
ctx := NewContext()
engine := NewEngine()
result, err := engine.Run(ctx, script)
if err != nil {
t.Fatalf("执行失败: %v", err)
}
expected := 1000.5
if result.Float() != expected {
t.Errorf("期望 %f, 得到 %f", expected, result.Float())
}
}
// TestLexer_LongString 测试长字符串
func Test_lexer_LongString(t *testing.T) {
longStr := ""
for i := 0; i < 100; i++ {
longStr += "a"
}
input := `"` + longStr + `"`
parser := NewParser()
script, err := parser.Compile(input)
if err != nil {
t.Fatalf("编译失败: %v", err)
}
ctx := NewContext()
engine := NewEngine()
result, err := engine.Run(ctx, script)
if err != nil {
t.Fatalf("执行失败: %v", err)
}
if len(result.String()) != 100 {
t.Errorf("期望长度100, 得到 %d", len(result.String()))
}
}
// TestLexer_Operators 测试操作符解析
func Test_lexer_Operators(t *testing.T) {
tests := []struct {
input string
expected []TokenType
}{
{"x == y", []TokenType{TokenIdent, TokenEq, TokenIdent}},
{"x != y", []TokenType{TokenIdent, TokenNeq, TokenIdent}},
{"x >= y", []TokenType{TokenIdent, TokenGe, TokenIdent}},
{"x <= y", []TokenType{TokenIdent, TokenLe, TokenIdent}},
{"x && y", []TokenType{TokenIdent, TokenAnd, TokenIdent}},
{"x || y", []TokenType{TokenIdent, TokenOr, TokenIdent}},
{"x >> 1", []TokenType{TokenIdent, TokenRShift, TokenInt}},
{"x << 1", []TokenType{TokenIdent, TokenLShift, TokenInt}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Fatalf("词法分析失败: %v", err)
}
if len(tokens) != len(tt.expected) {
t.Fatalf("Token数量不匹配: got=%d, want=%d", len(tokens), len(tt.expected))
}
for i, tok := range tokens {
if tok.Type != tt.expected[i] {
t.Errorf("Token类型不匹配 [%d]: got=%v, want=%v", i, tok.Type, tt.expected[i])
}
}
})
}
}
// TestLexer_ArrowOperator 测试箭头操作符
func Test_lexer_ArrowOperator(t *testing.T) {
input := "x => int"
lexer := NewLexer(input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Fatalf("词法分析失败: %v", err)
}
expected := []TokenType{TokenIdent, TokenArrow, TokenIdent}
if len(tokens) != len(expected) {
t.Fatalf("Token数量不匹配: got=%d, want=%d", len(tokens), len(expected))
}
for i, tok := range tokens {
if tok.Type != expected[i] {
t.Errorf("Token类型不匹配 [%d]: got=%v, want=%v", i, tok.Type, expected[i])
}
}
}
// TestLexer_EscapeSequences_EdgeCases 测试转义序列边缘情况
func Test_lexer_EscapeSequences_EdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// 无法识别的转义序列保留原样
{"未知转义", `"hello\xworld"`, "hello\\xworld"},
{"数字转义", `"test\1escape"`, "test\\1escape"},
{"字母转义", `"test\aescope"`, "test\\aescope"},
// 空字符串
{"空字符串", `""`, ""},
// 单字符字符串
{"单字符", `"a"`, "a"},
// 转义序列在结尾
{"结尾换行", `"test\n"`, "test\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Errorf("词法分析失败: %v", err)
return
}
if len(tokens) != 1 {
t.Errorf("期望1个token,got=%d", len(tokens))
return
}
if tokens[0].Value != tt.expected {
t.Errorf("值不匹配: got=%q, want=%q", tokens[0].Value, tt.expected)
}
})
}
}
// TestLexer_UnterminatedString2 测试未闭合字符串
func Test_lexer_UnterminatedString2(t *testing.T) {
input := `"hello world`
lexer := NewLexer(input)
_, err := lexer.Tokenize()
if err == nil {
t.Error("应该报告未闭合字符串错误")
}
}
// TestLexer_UnterminatedMultilineComment 测试未闭合多行注释
func Test_lexer_UnterminatedMultilineComment(t *testing.T) {
input := `/* 这是一个
未闭合的
注释`
lexer := NewLexer(input)
_, err := lexer.Tokenize()
if err == nil {
t.Error("应该报告未闭合多行注释错误")
}
}
// TestLexer_DefDirective 测试#fn指令
func Test_lexer_DefDirective(t *testing.T) {
tests := []struct {
input string
value string
}{
{"#fn myFunc()", "#fn myFunc()"},
{"#fn myFunc(x, y)", "#fn myFunc(x, y)"},
{"#fn myFunc(x)=>int", "#fn myFunc(x)=>int"},
}
for _, tt := range tests {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Errorf("词法分析失败: %v", err)
continue
}
if len(tokens) != 1 {
t.Errorf("期望1个token,got=%d", len(tokens))
continue
}
if tokens[0].Type != TokenDef {
t.Errorf("期望TokenDef,got=%v", tokens[0].Type)
}
if tokens[0].Value != tt.value {
t.Errorf("值不匹配: got=%q, want=%q", tokens[0].Value, tt.value)
}
}
}
// TestLexer_InvalidCharacter 测试无效字符
func Test_lexer_InvalidCharacter(t *testing.T) {
input := "x @ y"
lexer := NewLexer(input)
_, err := lexer.Tokenize()
if err == nil {
t.Error("应该报告无效字符错误")
}
}
// TestLexer_MultilineComment2 测试多行注释
func Test_lexer_MultilineComment2(t *testing.T) {
input := `/* 注释第一行
注释第二行
注释第三行 */
42`
lexer := NewLexer(input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Fatalf("词法分析失败: %v", err)
}
// 应该只有一个整数token(注释被过滤)
if len(tokens) != 1 {
t.Errorf("期望1个token,got=%d", len(tokens))
}
if tokens[0].Type != TokenInt {
t.Errorf("期望TokenInt,got=%v", tokens[0].Type)
}
}
// TestLexer_TypeAnnotation 测试类型注解
func Test_lexer_TypeAnnotation(t *testing.T) {
tests := []struct {
input string
expected []TokenType
}{
// >int 被正确解析为TokenGt + TokenIdent,TokenTypeAnnot需要紧凑语法如>int
{"x <string", []TokenType{TokenIdent, TokenLt, TokenIdent}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
lexer := NewLexer(tt.input)
tokens, err := lexer.Tokenize()
if err != nil {
t.Fatalf("词法分析失败: %v", err)
}
if len(tokens) != len(tt.expected) {
t.Fatalf("Token数量不匹配: got=%d, want=%d", len(tokens), len(tt.expected))
}
for i, tok := range tokens {
if tok.Type != tt.expected[i] {
t.Errorf("Token类型不匹配 [%d]: got=%v, want=%v", i, tok.Type, tt.expected[i])
}
}
})
}
}