-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht_print_test.go
More file actions
161 lines (144 loc) · 4.1 KB
/
Copy patht_print_test.go
File metadata and controls
161 lines (144 loc) · 4.1 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
package script
import (
"bytes"
"strings"
"testing"
)
// runScriptWithOutput 执行脚本并捕获输出
func runScriptWithOutput(t *testing.T, input string) (Value, string) {
t.Helper()
parser := NewParser()
script, err := parser.Compile(input)
if err != nil {
t.Fatalf("编译失败: %v", err)
}
var buf bytes.Buffer
ctx := NewContext()
ctx.SetOutput(&buf)
engine := NewEngine()
result, err := engine.Run(ctx, script)
if err != nil {
t.Fatalf("执行失败: %v", err)
}
return result, buf.String()
}
// Test_Print_NoOutput 默认输出到stdout不panic
func Test_Print_NoOutput(t *testing.T) {
result := runScript(t, `print("hello")`)
if !result.IsNil() {
t.Errorf("print应返回nil, 得到%v", result)
}
}
// Test_Print_Output 输出到buffer
func Test_Print_Output(t *testing.T) {
_, output := runScriptWithOutput(t, `print("hello", 123)`)
expected := "hello 123"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_NoNewline print不加换行
func Test_Print_NoNewline(t *testing.T) {
_, output := runScriptWithOutput(t, `print("abc")`)
if strings.HasSuffix(output, "\n") {
t.Errorf("print不应有换行, 得到%q", output)
}
}
// Test_Println_Output println输出带换行
func Test_Println_Output(t *testing.T) {
_, output := runScriptWithOutput(t, `println("hello", 123)`)
expected := "hello 123\n"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_NoArgs print()无参数不报错
func Test_Print_NoArgs(t *testing.T) {
_, output := runScriptWithOutput(t, `print()`)
if output != "" {
t.Errorf("无参数print应输出空字符串, 得到%q", output)
}
}
// Test_Println_NoArgs println()无参数输出换行
func Test_Println_NoArgs(t *testing.T) {
_, output := runScriptWithOutput(t, `println()`)
if output != "\n" {
t.Errorf("无参数println应输出换行, 得到%q", output)
}
}
// Test_Print_MixedTypes 混合类型输出
func Test_Print_MixedTypes(t *testing.T) {
_, output := runScriptWithOutput(t, `print(1, "a", true, nil)`)
expected := "1 a true nil"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_StringNoQuote 字符串不加引号
func Test_Print_StringNoQuote(t *testing.T) {
_, output := runScriptWithOutput(t, `print("hello world")`)
expected := "hello world"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_ArrayType 数组输出
func Test_Print_ArrayType(t *testing.T) {
_, output := runScriptWithOutput(t, `print([1, 2, 3])`)
expected := "[1, 2, 3]"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_MapType map输出
func Test_Print_MapType(t *testing.T) {
_, output := runScriptWithOutput(t, `print({"a": 1})`)
if !strings.Contains(output, "a") || !strings.Contains(output, "1") {
t.Errorf("map输出应包含a和1, 得到%q", output)
}
}
// Test_Print_ReturnsNil print返回nil
func Test_Print_ReturnsNil(t *testing.T) {
result, _ := runScriptWithOutput(t, `print(1)
nil`)
if !result.IsNil() {
t.Errorf("print应返回nil")
}
}
// Test_Print_MultipleCalls 多次print连续输出
func Test_Print_MultipleCalls(t *testing.T) {
_, output := runScriptWithOutput(t, `print("a")
print("b")
print("c")
nil`)
expected := "abc"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Println_MultipleCalls 多次println各带换行
func Test_Println_MultipleCalls(t *testing.T) {
_, output := runScriptWithOutput(t, `println("a")
println("b")
nil`)
expected := "a\nb\n"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_FloatType 浮点数输出
func Test_Print_FloatType(t *testing.T) {
_, output := runScriptWithOutput(t, `print(1.5)`)
expected := "1.5"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}
// Test_Print_BoolType 布尔输出
func Test_Print_BoolType(t *testing.T) {
_, output := runScriptWithOutput(t, `print(true, false)`)
expected := "true false"
if output != expected {
t.Errorf("期望%q, 得到%q", expected, output)
}
}