forked from olebedev/go-duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
42 lines (34 loc) · 1.38 KB
/
api_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
package duktape
import . "gopkg.in/check.v1"
func (s *DuktapeSuite) TestPevalString(c *C) {
s.ctx.EvalString(`"Golang love Duktape!"`)
c.Assert(s.ctx.IsString(-1), Equals, true)
c.Assert(s.ctx.GetString(-1), Equals, "Golang love Duktape!")
}
func (s *DuktapeSuite) TestPevalString_Error(c *C) {
err := s.ctx.PevalString("var = 'foo';")
c.Assert(err.(*Error).Type, Equals, "SyntaxError")
}
func (s *DuktapeSuite) TestPevalFile_Error(c *C) {
err := s.ctx.PevalFile("foo.js")
c.Assert(err.(*Error).Message, Equals, "no sourcecode")
}
// This test panics.
// func (s *DuktapeSuite) TestPcompileString(c *C) {
// err := s.ctx.PcompileString(CompileFunction, "foo")
// c.Assert(err.(*Error).Type, Equals, "SyntaxError")
// c.Assert(err.(*Error).LineNumber, Equals, 1)
// }
func (s *DuktapeSuite) TestPushErrorObject(c *C) {
s.ctx.PushErrorObject(ErrType, "Got an error thingy: %v", 5)
s.assertErrorInCtx(c, ErrType, "TypeError: Got an error thingy: 5")
}
func (s *DuktapeSuite) TestPushErrorObjectVa(c *C) {
s.ctx.PushErrorObjectVa(ErrURI, "Got an error thingy: %x %s %s", int64(0xdeadbeef), "is", "tasty")
s.assertErrorInCtx(c, ErrURI, "URIError: Got an error thingy: deadbeef is tasty")
}
func (s *DuktapeSuite) assertErrorInCtx(c *C, code int, msg string) {
c.Assert(s.ctx.IsError(-1), Equals, true)
c.Assert(s.ctx.GetErrorCode(-1), Equals, code)
c.Assert(s.ctx.SafeToString(-1), Equals, msg)
}