-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtypes_functions_test.go
53 lines (42 loc) · 1.12 KB
/
types_functions_test.go
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
package dicescript
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestTypesFuncDict(t *testing.T) {
d := NewDictVal(nil)
d.Store("a", ni(1))
v, _ := d.Load("a")
assert.True(t, valueEqual(v, ni(1)))
v, ok := d.Load("b")
assert.True(t, v == nil)
assert.Equal(t, ok, false)
d2 := (*VMDictValue)(ni(2))
v, ok = d2.Load("a")
assert.True(t, v == nil)
assert.Equal(t, ok, false)
}
func TestTypesFuncDictToStr(t *testing.T) {
data := &ValueMap{}
data.Store("a", ni(1))
d := NewDictVal(data)
assert.Equal(t, d.ToString(), "{'a': 1}")
}
func TestTypesFuncArray(t *testing.T) {
vm := NewVM()
arr := na(ni(1), ni(2), ni(3))
assert.Equal(t, arr.ArrayItemGet(vm, 1), ni(2))
ni(1).ArrayItemGet(vm, 1)
assert.Error(t, vm.Error) // 此类型无法取下标
vm = NewVM()
arr = na(ni(1), ni(2), ni(3))
arr.ArrayItemSet(vm, 1, ni(4))
assert.Equal(t, arr.MustReadArray().List[1].MustReadInt(), IntType(4))
vm = NewVM()
arr = na(ni(1), ni(2), ni(3))
arr.ArrayItemSet(vm, 3, ni(4))
assert.Error(t, vm.Error)
vm = NewVM()
ni(1).ArrayItemSet(vm, 1, ni(2))
assert.Error(t, vm.Error) // 此类型无法赋值下标
}