forked from olebedev/go-duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduktape_test.go
166 lines (135 loc) · 3.69 KB
/
duktape_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
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
package duktape
import (
goCtx "context"
"sync"
"testing"
"github.com/stretchr/testify/require"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
var _ = Suite(&DuktapeSuite{})
type DuktapeSuite struct {
ctx *Context
}
func (s *DuktapeSuite) SetUpTest(c *C) {
s.ctx = New()
}
func (s *DuktapeSuite) TestPushGlobalGoFunction_Call(c *C) {
var check bool
idx, err := s.ctx.PushGlobalGoFunction("test", func(c *Context) int {
check = !check
return 0
})
c.Assert(err, IsNil)
c.Assert(idx, Not(Equals), -1)
c.Assert(s.ctx.fnIndex.functions, HasLen, 1)
err = s.ctx.PevalString("test();")
c.Assert(err, IsNil)
c.Assert(check, Equals, true)
err = s.ctx.PevalString("test();")
c.Assert(err, IsNil)
c.Assert(check, Equals, false)
}
func (s *DuktapeSuite) TestPushGlobalGoFunction_Malformed(c *C) {
idx, err := s.ctx.PushGlobalGoFunction(".", func(c *Context) int {
return 0
})
c.Assert(err, ErrorMatches, "Malformed function name '.'")
c.Assert(idx, Equals, -1)
}
func (s *DuktapeSuite) TestPushGlobalGoFunction_Finalize(c *C) {
s.ctx.PushGlobalGoFunction("test", func(c *Context) int {
return 0
})
c.Assert(s.ctx.fnIndex.functions, HasLen, 1)
err := s.ctx.PevalString("test = undefined")
c.Assert(err, IsNil)
s.ctx.Gc(0)
c.Assert(s.ctx.fnIndex.functions, HasLen, 0)
}
func (s *DuktapeSuite) TestPushGoFunction_Call(c *C) {
var check bool
s.ctx.PushGlobalObject()
s.ctx.PushGoFunction(func(c *Context) int {
check = !check
return 0
})
s.ctx.PutPropString(-2, "test")
s.ctx.Pop()
c.Assert(s.ctx.fnIndex.functions, HasLen, 1)
err := s.ctx.PevalString("test();")
c.Assert(err, IsNil)
c.Assert(check, Equals, true)
err = s.ctx.PevalString("test();")
c.Assert(err, IsNil)
c.Assert(check, Equals, false)
}
func goTestfunc(ctx *Context) int {
top := ctx.GetTop()
a := ctx.GetNumber(top - 2)
b := ctx.GetNumber(top - 1)
ctx.PushNumber(a + b)
return 1
}
func (s *DuktapeSuite) TestMyAddTwo(c *C) {
s.ctx.PushGlobalGoFunction("adder", goTestfunc)
err := s.ctx.PevalString(`print("2 + 3 =", adder(2,3))`)
c.Assert(err, IsNil)
s.ctx.Pop()
err = s.ctx.PevalString(`adder(2,3)`)
c.Assert(err, IsNil)
c.Assert(s.ctx.GetNumber(-1), Equals, 5.0)
}
func (s *DuktapeSuite) TestPevalTimeout(c *C) {
ctx, ctxCancel := goCtx.WithCancel(goCtx.Background())
s.ctx.SetContextData(ctx)
s.ctx.SetExecTimeoutCheckHandler(func(dctx *Context) bool {
gctx := dctx.GetContextData().(goCtx.Context)
return gctx.Err() != nil
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := s.ctx.PevalString(`while(true){}`)
require.ErrorContains(c, err, "execution timeout")
}()
ctxCancel()
wg.Wait()
}
func (s *DuktapeSuite) TestPcallTimeout(c *C) {
ctx, ctxCancel := goCtx.WithCancel(goCtx.Background())
s.ctx.SetContextData(ctx)
s.ctx.SetExecTimeoutCheckHandler(func(dctx *Context) bool {
gctx := dctx.GetContextData().(goCtx.Context)
return gctx.Err() != nil
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := s.ctx.PcompileString(CompileFunction, `function(){ while(true){} }`)
require.NoError(c, err)
res := s.ctx.Pcall(0)
require.Equal(c, ExecError, res)
errMsg := s.ctx.SafeToString(-1)
require.Contains(c, errMsg, "execution timeout")
}()
ctxCancel()
wg.Wait()
}
func (s *DuktapeSuite) TestCreateContexts(c *C) {
dukCtx := New()
dukCtxFromMap := contexts.get(dukCtx.dukCtxId)
require.Equal(c, dukCtx, dukCtxFromMap)
require.Len(c, contexts.ctxs, 2)
dukCtx.DestroyHeap()
}
func (s *DuktapeSuite) TearDownTest(c *C) {
dukCtx := contexts.get(s.ctx.dukCtxId)
require.Equal(c, s.ctx, dukCtx)
require.Len(c, contexts.ctxs, 1)
s.ctx.DestroyHeap()
require.Len(c, contexts.ctxs, 0)
}