Mercurial > pymonkey
diff test_pymonkey.py @ 46:a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 06 Jul 2009 01:37:16 -0700 |
parents | 03aec8572461 |
children | 3f4982759e55 |
line wrap: on
line diff
--- a/test_pymonkey.py Mon Jul 06 00:09:42 2009 -0700 +++ b/test_pymonkey.py Mon Jul 06 01:37:16 2009 -0700 @@ -1,4 +1,6 @@ +import sys import unittest + import pymonkey class PymonkeyTests(unittest.TestCase): @@ -17,6 +19,15 @@ cx.define_property(obj, func.__name__, jsfunc) return cx.evaluate_script(obj, code, '<string>', 1) + def assertRaises(self, exctype, func, *args): + was_raised = False + try: + func(*args) + except exctype, e: + self.last_exception = e + was_raised = True + self.assertTrue(was_raised) + def testJsWrappedPythonFuncPassesContext(self): contexts = [] @@ -39,6 +50,15 @@ self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), u"o hai") + def testJsWrappedPythonFunctionThrowsException(self): + def hai2u(cx): + raise Exception("hello") + self.assertRaises(pymonkey.error, + self._evalJsWrappedPyFunc, + hai2u, 'hai2u()') + self.assertEqual(self.last_exception.message, + "uncaught exception: hello") + def testJsWrappedPythonFunctionReturnsNone(self): def hai2u(cx): pass @@ -157,6 +177,13 @@ def testUndefinedCannotBeInstantiated(self): self.assertRaises(TypeError, pymonkey.undefined) + def testEvaluateThrowsException(self): + self.assertRaises(pymonkey.error, + self._evaljs, 'hai2u()') + self.assertEqual(self.last_exception.message, + 'File "<string>", line 1: ReferenceError: ' + 'hai2u is not defined') + def testEvaluateReturnsUndefined(self): retval = self._evaljs("") self.assertTrue(retval is pymonkey.undefined) @@ -212,11 +239,12 @@ '(function boop(a, b) { blarg(); })', '<string>', 1 ) - self.assertRaises( - pymonkey.error, - cx.call_function, - obj, obj, (1,) - ) + self.assertRaises(pymonkey.error, + cx.call_function, + obj, obj, (1,)) + self.assertEqual(self.last_exception.message, + 'File "<string>", line 1: ReferenceError: ' + 'blarg is not defined') def testCallFunctionWorks(self): cx = pymonkey.Runtime().new_context()