-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsugers_test.go
58 lines (50 loc) · 1.04 KB
/
sugers_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
package gisp_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/ysmood/gisp"
)
func TestJSON(t *testing.T) {
sandbox := gisp.New(gisp.Box{
"+": func(ctx *gisp.Context) interface{} {
a := ctx.ArgNum(1)
b := ctx.ArgNum(2)
return a + b
},
"-": func(ctx *gisp.Context) interface{} {
a := ctx.ArgNum(1)
b := ctx.ArgNum(2)
return a - b
},
})
out, _ := gisp.RunJSON(`["-", ["+", 5, 1], ["+", 1, 1]]`, &gisp.Context{
Sandbox: sandbox,
})
assert.Equal(t, float64(4), out)
}
func TestTypes(t *testing.T) {
sandbox := gisp.New(gisp.Box{
"$": func(g *gisp.Context) interface{} {
return g.AST.([]interface{})[1]
},
"echo": func(g *gisp.Context) interface{} {
return []interface{}{
g.ArgNum(1),
g.ArgBool(2),
g.ArgArr(3),
g.ArgObj(4),
g.ArgStr(5),
}
},
})
out, _ := gisp.RunJSON(`["echo", 1.2, true, ["$", []], {}, "ok"]`, &gisp.Context{
Sandbox: sandbox,
})
assert.Equal(t, []interface{}{
1.2,
true,
[]interface{}{},
map[string]interface{}{},
"ok",
}, out)
}