-
Notifications
You must be signed in to change notification settings - Fork 6
/
builtin_functions_test.go
99 lines (82 loc) · 2.75 KB
/
builtin_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
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
package dicescript
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestNativeFunctionCall(t *testing.T) {
vm := NewVM()
err := vm.Run("ceil(1.2)")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(2)))
}
vm = NewVM()
err = vm.Run("ceil('')")
assert.Error(t, err)
}
func TestNativeFunctionFloat(t *testing.T) {
vm := NewVM()
assert.True(t, valueEqual(funcCeil(vm, nil, []*VMValue{nf(1.1)}), ni(2)))
assert.True(t, valueEqual(funcCeil(vm, nil, []*VMValue{nf(1.0)}), ni(1)))
assert.True(t, valueEqual(funcRound(vm, nil, []*VMValue{nf(1.6)}), ni(2)))
assert.True(t, valueEqual(funcRound(vm, nil, []*VMValue{ni(2)}), ni(2)))
funcRound(vm, nil, []*VMValue{ns("1.6")})
assert.Error(t, vm.Error)
vm.Error = nil
assert.True(t, valueEqual(funcFloor(vm, nil, []*VMValue{nf(1.6)}), ni(1)))
assert.True(t, valueEqual(funcFloor(vm, nil, []*VMValue{ni(1)}), ni(1)))
funcFloor(vm, nil, []*VMValue{ns("1.6")})
assert.Error(t, vm.Error)
vm.Error = nil
}
func TestNativeFunctionConvert(t *testing.T) {
vm := NewVM()
assert.True(t, valueEqual(funcInt(vm, nil, []*VMValue{nf(1.1)}), ni(1)))
assert.True(t, valueEqual(funcInt(vm, nil, []*VMValue{ni(1)}), ni(1)))
assert.True(t, valueEqual(funcInt(vm, nil, []*VMValue{ns("1")}), ni(1)))
funcInt(vm, nil, []*VMValue{ns("xx")})
assert.Error(t, vm.Error)
vm.Error = nil
funcInt(vm, nil, []*VMValue{na()})
assert.Error(t, vm.Error)
vm.Error = nil
// float
assert.True(t, valueEqual(funcFloat(vm, nil, []*VMValue{nf(1.1)}), nf(1.1)))
assert.True(t, valueEqual(funcFloat(vm, nil, []*VMValue{ni(1)}), nf(1.0)))
assert.True(t, valueEqual(funcFloat(vm, nil, []*VMValue{ns("1")}), nf(1.0)))
funcFloat(vm, nil, []*VMValue{ns("xx")})
assert.Error(t, vm.Error)
vm.Error = nil
funcFloat(vm, nil, []*VMValue{na()})
assert.Error(t, vm.Error)
vm.Error = nil
// str
assert.True(t, valueEqual(funcStr(vm, nil, []*VMValue{nf(1.1)}), ns("1.1")))
assert.True(t, valueEqual(funcStr(vm, nil, []*VMValue{na(ni(1), ni(2))}), ns("[1, 2]")))
assert.True(t, valueEqual(funcStr(vm, nil, []*VMValue{na(na(), ni(2))}), ns("[[], 2]")))
}
func TestNativeFunctionLoad(t *testing.T) {
vm := NewVM()
err := vm.Run("val = '123'; load('val')")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ns("123")))
}
vm = NewVM()
err = vm.Run("load('load')")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, builtinValues["load"]))
}
}
func TestNativeFunctionTypeId(t *testing.T) {
vm := NewVM()
err := vm.Run("typeId(1)")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(IntType(VMTypeInt))))
}
}
func TestNativeFunctionStore(t *testing.T) {
vm := NewVM()
err := vm.Run("store('test', 123); test")
if assert.NoError(t, err) {
assert.True(t, valueEqual(vm.Ret, ni(123)))
}
}