gopkg.in/olebedev/go-duktape.v1@v1.0.0-20151008052556-e2ae92f01e4a/api_test.go (about)

     1  package duktape
     2  
     3  import . "gopkg.in/check.v1"
     4  
     5  func (s *DuktapeSuite) TestPevalString(c *C) {
     6  	s.ctx.EvalString(`"Golang love Duktape!"`)
     7  	c.Assert(s.ctx.IsString(-1), Equals, true)
     8  	c.Assert(s.ctx.GetString(-1), Equals, "Golang love Duktape!")
     9  }
    10  
    11  func (s *DuktapeSuite) TestPevalString_Error(c *C) {
    12  	err := s.ctx.PevalString("var = 'foo';")
    13  	c.Assert(err.(*Error).Type, Equals, "SyntaxError")
    14  }
    15  
    16  func (s *DuktapeSuite) TestPevalFile_Error(c *C) {
    17  	err := s.ctx.PevalFile("foo.js")
    18  	c.Assert(err.(*Error).Message, Equals, "no sourcecode")
    19  }
    20  
    21  func (s *DuktapeSuite) TestPcompileString(c *C) {
    22  	err := s.ctx.PcompileString(CompileFunction, "foo")
    23  	c.Assert(err.(*Error).Type, Equals, "SyntaxError")
    24  	c.Assert(err.(*Error).LineNumber, Equals, 1)
    25  }
    26  
    27  func (s *DuktapeSuite) TestPushErrorObject(c *C) {
    28  	s.ctx.PushErrorObject(ErrType, "Got an error thingy: ", 5)
    29  	s.assertErrorInCtx(c, ErrType, "TypeError: Got an error thingy: 5")
    30  }
    31  
    32  func (s *DuktapeSuite) TestPushErrorObjectf(c *C) {
    33  	s.ctx.PushErrorObjectf(ErrURI, "Got an error thingy: %x", 0xdeadbeef)
    34  	s.assertErrorInCtx(c, ErrURI, "URIError: Got an error thingy: deadbeef")
    35  }
    36  
    37  func (s *DuktapeSuite) assertErrorInCtx(c *C, code int, msg string) {
    38  	c.Assert(s.ctx.IsError(-1), Equals, true)
    39  	c.Assert(s.ctx.GetErrorCode(-1), Equals, code)
    40  	c.Assert(s.ctx.SafeToString(-1), Equals, msg)
    41  }