def testErrorInfo(self): with STPyV8.JSContext(): with STPyV8.JSEngine() as engine: try: engine.compile( """ function hello() { throw Error("hello world"); } hello();""", "test", 10, 10).run() self.fail() except STPyV8.JSError as e: self.assertTrue( "JSError: Error: hello world ( test @ 14 : 28 ) ->" in str(e)) self.assertEqual("Error", e.name) self.assertEqual("hello world", e.message) self.assertEqual("test", e.scriptName) self.assertEqual(14, e.lineNum) self.assertEqual(96, e.startPos) self.assertEqual(97, e.endPos) self.assertEqual(28, e.startCol) self.assertEqual(29, e.endCol) self.assertEqual('throw Error("hello world");', e.sourceLine.strip()) self.assertEqual( 'Error: hello world\n' + ' at hello (test:14:35)\n' + ' at test:17:25', e.stackTrace)
def testCompile(self): with STPyV8.JSContext(): with STPyV8.JSEngine() as engine: s = engine.compile("1+2") self.assertTrue(isinstance(s, STPyV8.JSScript)) self.assertEqual("1+2", s.source) self.assertEqual(3, int(s.run())) self.assertRaises(SyntaxError, engine.compile, "1+")
def testUnicodeSource(self): class Global(STPyV8.JSClass): var = u'测试' def __getattr__(self, name): if name: return self.var return STPyV8.JSClass.__getattr__(self, name) g = Global() with STPyV8.JSContext(g) as ctxt: with STPyV8.JSEngine() as engine: src = u""" function 函数() { return 变量.length; } 函数(); var func = function () {}; """ s = engine.compile(src) self.assertTrue(isinstance(s, STPyV8.JSScript)) self.assertEqual(src, s.source) self.assertEqual(2, s.run()) func_name = u'函数' self.assertTrue(hasattr(ctxt.locals, func_name)) func = getattr(ctxt.locals, func_name) self.assertTrue(isinstance(func, STPyV8.JSFunction)) self.assertEqual(func_name, func.name) self.assertEqual("", func.resname) self.assertEqual(1, func.linenum) self.assertEqual(0, func.lineoff) self.assertEqual(0, func.coloff) var_name = u'变量' setattr(ctxt.locals, var_name, u'测试长字符串') self.assertEqual(6, func()) self.assertEqual("func", ctxt.locals.func.inferredname)