Mercurial > pymonkey
comparison 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 |
comparison
equal
deleted
inserted
replaced
45:03aec8572461 | 46:a0f677cfc679 |
---|---|
1 import sys | |
1 import unittest | 2 import unittest |
3 | |
2 import pymonkey | 4 import pymonkey |
3 | 5 |
4 class PymonkeyTests(unittest.TestCase): | 6 class PymonkeyTests(unittest.TestCase): |
5 def _evaljs(self, code): | 7 def _evaljs(self, code): |
6 rt = pymonkey.Runtime() | 8 rt = pymonkey.Runtime() |
15 cx.init_standard_classes(obj) | 17 cx.init_standard_classes(obj) |
16 jsfunc = cx.new_function(func, func.__name__) | 18 jsfunc = cx.new_function(func, func.__name__) |
17 cx.define_property(obj, func.__name__, jsfunc) | 19 cx.define_property(obj, func.__name__, jsfunc) |
18 return cx.evaluate_script(obj, code, '<string>', 1) | 20 return cx.evaluate_script(obj, code, '<string>', 1) |
19 | 21 |
22 def assertRaises(self, exctype, func, *args): | |
23 was_raised = False | |
24 try: | |
25 func(*args) | |
26 except exctype, e: | |
27 self.last_exception = e | |
28 was_raised = True | |
29 self.assertTrue(was_raised) | |
30 | |
20 def testJsWrappedPythonFuncPassesContext(self): | 31 def testJsWrappedPythonFuncPassesContext(self): |
21 contexts = [] | 32 contexts = [] |
22 | 33 |
23 def func(cx): | 34 def func(cx): |
24 contexts.append(cx) | 35 contexts.append(cx) |
36 def testJsWrappedPythonFunctionReturnsUnicode(self): | 47 def testJsWrappedPythonFunctionReturnsUnicode(self): |
37 def hai2u(cx): | 48 def hai2u(cx): |
38 return u"o hai" | 49 return u"o hai" |
39 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), | 50 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), |
40 u"o hai") | 51 u"o hai") |
52 | |
53 def testJsWrappedPythonFunctionThrowsException(self): | |
54 def hai2u(cx): | |
55 raise Exception("hello") | |
56 self.assertRaises(pymonkey.error, | |
57 self._evalJsWrappedPyFunc, | |
58 hai2u, 'hai2u()') | |
59 self.assertEqual(self.last_exception.message, | |
60 "uncaught exception: hello") | |
41 | 61 |
42 def testJsWrappedPythonFunctionReturnsNone(self): | 62 def testJsWrappedPythonFunctionReturnsNone(self): |
43 def hai2u(cx): | 63 def hai2u(cx): |
44 pass | 64 pass |
45 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), | 65 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), |
154 cx = rt.new_context() | 174 cx = rt.new_context() |
155 self.assertEqual(cx.get_runtime(), rt) | 175 self.assertEqual(cx.get_runtime(), rt) |
156 | 176 |
157 def testUndefinedCannotBeInstantiated(self): | 177 def testUndefinedCannotBeInstantiated(self): |
158 self.assertRaises(TypeError, pymonkey.undefined) | 178 self.assertRaises(TypeError, pymonkey.undefined) |
179 | |
180 def testEvaluateThrowsException(self): | |
181 self.assertRaises(pymonkey.error, | |
182 self._evaljs, 'hai2u()') | |
183 self.assertEqual(self.last_exception.message, | |
184 'File "<string>", line 1: ReferenceError: ' | |
185 'hai2u is not defined') | |
159 | 186 |
160 def testEvaluateReturnsUndefined(self): | 187 def testEvaluateReturnsUndefined(self): |
161 retval = self._evaljs("") | 188 retval = self._evaljs("") |
162 self.assertTrue(retval is pymonkey.undefined) | 189 self.assertTrue(retval is pymonkey.undefined) |
163 | 190 |
210 obj = cx.evaluate_script( | 237 obj = cx.evaluate_script( |
211 obj, | 238 obj, |
212 '(function boop(a, b) { blarg(); })', | 239 '(function boop(a, b) { blarg(); })', |
213 '<string>', 1 | 240 '<string>', 1 |
214 ) | 241 ) |
215 self.assertRaises( | 242 self.assertRaises(pymonkey.error, |
216 pymonkey.error, | 243 cx.call_function, |
217 cx.call_function, | 244 obj, obj, (1,)) |
218 obj, obj, (1,) | 245 self.assertEqual(self.last_exception.message, |
219 ) | 246 'File "<string>", line 1: ReferenceError: ' |
247 'blarg is not defined') | |
220 | 248 |
221 def testCallFunctionWorks(self): | 249 def testCallFunctionWorks(self): |
222 cx = pymonkey.Runtime().new_context() | 250 cx = pymonkey.Runtime().new_context() |
223 obj = cx.new_object() | 251 obj = cx.new_object() |
224 thisArg = cx.new_object() | 252 thisArg = cx.new_object() |