-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtypes_methods_test.go
68 lines (55 loc) · 2.04 KB
/
types_methods_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package dicescript
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTypesMethodComputedCompute(t *testing.T) {
_attrs := &ValueMap{}
_attrs.Store("x", ni(1))
c := NewComputedValRaw(&ComputedData{Expr: "this.x + 10", Attrs: _attrs})
vm := NewVM()
ret := funcComputedCompute(vm, c, nil)
assert.Equal(t, ret.ToString(), "11")
}
func TestTypesMethodArraySum(t *testing.T) {
d := NewArrayVal(ni(1), nf(2.2), ni(3))
v := funcArraySum(nil, d, nil)
assert.Equal(t, v.ToString(), "6.2")
}
func TestTypesMethodArrayShuttle(t *testing.T) {
d := NewArrayVal(ni(1), ni(2), ni(3), ni(4))
v := funcArrayShuttle(nil, d, nil)
// 不知道怎么写测试,因为总是有概率打乱后与原本一致
assert.Equal(t, v.Length(nil), IntType(4))
}
func TestTypesMethodArrayRand(t *testing.T) {
d := NewArrayVal(ni(1), ni(1), ni(1), ni(1))
v := funcArrayRand(nil, d, nil)
assert.Equal(t, v.MustReadInt(), IntType(1))
}
func TestTypesMethodArrayRandSize(t *testing.T) {
d := NewArrayVal(ni(1), ni(1), ni(1), ni(1))
v := funcArrayRandSize(nil, d, []*VMValue{ni(1)})
assert.Equal(t, v.Length(nil), IntType(1))
}
func TestTypesMethodDictKeys(t *testing.T) {
d := NewDictValWithArrayMust(ns("a"), ni(1), ns("b"), ni(2))
v := funcDictKeys(nil, d.V(), nil)
assert.True(t, valueEqual(v, na(ns("a"), ns("b"))) || valueEqual(v, na(ns("b"), ns("a"))))
}
func TestTypesMethodDictValues(t *testing.T) {
d := NewDictValWithArrayMust(ns("a"), ni(1), ns("b"), ni(2))
v := funcDictValues(nil, d.V(), nil)
assert.True(t, valueEqual(v, na(ni(1), ni(2))) || valueEqual(v, na(ni(2), ni(1))))
}
func TestTypesMethodDictItems(t *testing.T) {
d := NewDictValWithArrayMust(ns("a"), ni(1), ns("b"), ni(2))
v := funcDictItems(nil, d.V(), nil)
assert.True(t, valueEqual(v, na(na(ns("a"), ni(1)), na(ns("b"), ni(2)))) ||
valueEqual(v, na(na(ns("b"), ni(2)), na(ns("a"), ni(1)))))
}
func TestTypesMethodDictLen(t *testing.T) {
d := NewDictValWithArrayMust(ns("a"), ni(1), ns("b"), ni(2))
v := funcDictLen(nil, d.V(), nil)
assert.Equal(t, v.MustReadInt(), IntType(2))
}