-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht_runtime_access_test.go
More file actions
198 lines (171 loc) · 5.66 KB
/
Copy patht_runtime_access_test.go
File metadata and controls
198 lines (171 loc) · 5.66 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
package script
import (
"testing"
)
// ========== 索引访问测试 ==========
func Test_runtime_IndexAccess(t *testing.T) {
vm := newTestVM(t)
// 数组索引访问
arr := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
result, err := vm.indexAccess(arr, NewValue(1))
if err != nil || result.Int() != 2 {
t.Errorf("数组索引访问失败: %v", err)
}
// 字符串索引访问
str := NewValue("hello")
result, err = vm.indexAccess(str, NewValue(0))
if err != nil || result.String() != "h" {
t.Errorf("字符串索引访问失败: %v", err)
}
// Map索引访问
m := &MapValue{Pairs: map[string]Value{"key": NewValue("value")}}
result, err = vm.indexAccess(NewValue(m), NewValue("key"))
if err != nil || result.String() != "value" {
t.Errorf("Map索引访问失败: %v", err)
}
}
func Test_runtime_SliceAccess(t *testing.T) {
vm := newTestVM(t)
// 数组切片
arr := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3), NewValue(4)})
result, err := vm.sliceAccess(arr, NewValue(1), NewValue(3))
if err != nil {
t.Errorf("数组切片失败: %v", err)
return
}
slice := result.Array()
if len(slice.Elements) != 2 {
t.Errorf("切片长度错误: 期望2, 得到%d", len(slice.Elements))
}
// 字符串切片
str := NewValue("hello world")
result, err = vm.sliceAccess(str, NewValue(0), NewValue(5))
if err != nil || result.String() != "hello" {
t.Errorf("字符串切片失败: got=%s, want=hello", result.String())
}
}
func Test_runtime_Length(t *testing.T) {
vm := newTestVM(t)
// 数组长度
arr := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
if n, _ := vm.length(arr); n != 3 {
t.Errorf("数组长度错误")
}
// 字符串长度
str := NewValue("hello")
if n, _ := vm.length(str); n != 5 {
t.Errorf("字符串长度错误")
}
// Map长度
m := &MapValue{Pairs: map[string]Value{"a": NewValue(1), "b": NewValue(2)}}
if n, _ := vm.length(NewValue(m)); n != 2 {
t.Errorf("Map长度错误")
}
}
func Test_runtime_MapAccess(t *testing.T) {
t.Run("获取已存在的键", func(t *testing.T) {
runIntTest(t, `m := {"a": 100, "b": 200}
m["a"]`, 100)
})
t.Run("获取不存在的键返回nil", func(t *testing.T) {
result := runScript(t, `m := {"a": 100}
m["b"]`)
if !result.IsNil() {
t.Errorf("期望 nil,得到 %v", result)
}
})
}
func Test_runtime_ArrayIndexAccess2(t *testing.T) {
t.Run("正常访问", func(t *testing.T) {
runIntTest(t, `arr := [10, 20, 30]
arr[1]`, 20)
})
t.Run("越界访问返回nil", func(t *testing.T) {
result := runScript(t, `arr := [10, 20, 30]
arr[10]`)
if !result.IsNil() {
t.Errorf("期望 nil,得到 %v", result)
}
})
}
func Test_runtime_StringIndexAccess_EdgeCases(t *testing.T) {
vm := newTestVM(t)
t.Run("越界索引返回nil", func(t *testing.T) {
str := NewValue("hello")
result, err := vm.indexAccess(str, NewValue(100))
if err != nil {
t.Errorf("不应返回错误: %v", err)
}
if !result.IsNil() {
t.Errorf("越界索引应返回nil,得到: %v", result)
}
})
t.Run("空字符串越界", func(t *testing.T) {
str := NewValue("")
result, err := vm.indexAccess(str, NewValue(0))
if err != nil {
t.Errorf("不应返回错误: %v", err)
}
if !result.IsNil() {
t.Errorf("空字符串索引应返回nil")
}
})
}
func Test_runtime_SliceAccess_EdgeCases(t *testing.T) {
vm := newTestVM(t)
t.Run("省略结束索引使用对象长度", func(t *testing.T) {
arr := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
result, err := vm.sliceAccess(arr, NewValue(1), NewValue(SliceEndDefault))
if err != nil {
t.Errorf("不应返回错误: %v", err)
}
elements := result.Array().Elements
if len(elements) != 2 {
t.Errorf("期望2个元素,得到%d", len(elements))
}
})
t.Run("字符串切片正常", func(t *testing.T) {
str := NewValue("hello world")
result, err := vm.sliceAccess(str, NewValue(0), NewValue(5))
if err != nil {
t.Errorf("不应返回错误: %v", err)
}
if result.String() != "hello" {
t.Errorf("期望hello,得到%s", result.String())
}
})
t.Run("空数组切片", func(t *testing.T) {
arr := NewValue([]Value{})
result, err := vm.sliceAccess(arr, NewValue(0), NewValue(SliceEndDefault))
if err != nil {
t.Errorf("不应返回错误: %v", err)
}
elements := result.Array().Elements
if len(elements) != 0 {
t.Errorf("期望0个元素,得到%d", len(elements))
}
})
}
func Test_runtime_Length_EdgeCases(t *testing.T) {
vm := newTestVM(t)
tests := []struct {
name string
value Value
expectError bool
}{
{"整数报错", NewValue(42), true},
{"布尔报错", NewValue(true), true},
{"nil报错", NewValue(nil), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := vm.length(tt.value)
if tt.expectError && err == nil {
t.Errorf("期望错误,但未返回错误")
}
if !tt.expectError && err != nil {
t.Errorf("不期望错误,但返回: %v", err)
}
})
}
}