-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht_error_messages_test.go
More file actions
66 lines (59 loc) · 1.61 KB
/
Copy patht_error_messages_test.go
File metadata and controls
66 lines (59 loc) · 1.61 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
package script
import (
"strings"
"testing"
)
// Test_FriendlyErrorMessages 测试友好的错误消息
func Test_FriendlyErrorMessages(t *testing.T) {
tests := []struct {
name string
input string
shouldContain string
shouldNotContain string
}{
{
name: "缺少右括号",
input: `if true { print(1 }`,
shouldContain: ")",
shouldNotContain: "47",
},
{
name: "缺少左大括号",
input: `if true print(1) }`,
shouldContain: "{",
shouldNotContain: "期望 53",
},
{
name: "缺少赋值运算符",
input: `for i + 0 { }`,
shouldContain: ":=",
shouldNotContain: "得到 54",
},
{
name: "缺少分号",
input: `for i := 0 i < 10 { }`,
shouldContain: ";",
shouldNotContain: "期望 68",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := NewParser()
_, err := parser.Compile(tt.input)
if err == nil {
t.Error("期望返回错误,但没有")
return
}
errMsg := err.Error()
// 检查错误消息包含期望的友好描述
if tt.shouldContain != "" && !strings.Contains(errMsg, tt.shouldContain) {
t.Errorf("错误消息应该包含 '%s'\n实际错误: %s", tt.shouldContain, errMsg)
}
// 检查错误消息不包含数字token ID
if tt.shouldNotContain != "" && strings.Contains(errMsg, tt.shouldNotContain) {
t.Errorf("错误消息不应该包含 '%s'\n实际错误: %s", tt.shouldNotContain, errMsg)
}
t.Logf("错误消息: %s", errMsg)
})
}
}