This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
duktape_test.go
111 lines (83 loc) · 2.22 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
package duktape
import (
"testing"
. "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) TearDownTest(c *C) {
s.ctx.DestroyHeap()
}