annotate test_pymonkey.py @ 61:1506350991d4

JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
author Atul Varma <varmaa@toolness.com>
date Sat, 25 Jul 2009 16:14:03 -0700
parents fb97bed55789
children 2b5696b91b01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
1 import sys
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
2 import unittest
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
3 import weakref
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
4
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
5 import pymonkey
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
6
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
7 class PymonkeyTests(unittest.TestCase):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
8 def _evaljs(self, code):
9
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
9 rt = pymonkey.Runtime()
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
10 cx = rt.new_context()
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
11 obj = cx.new_object()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
12 cx.init_standard_classes(obj)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
13 return cx.evaluate_script(obj, code, '<string>', 1)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
14
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
15 def _evalJsWrappedPyFunc(self, func, code):
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
16 cx = pymonkey.Runtime().new_context()
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
17 obj = cx.new_object()
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
18 cx.init_standard_classes(obj)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
19 jsfunc = cx.new_function(func, func.__name__)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
20 cx.define_property(obj, func.__name__, jsfunc)
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
21 return cx.evaluate_script(obj, code, '<string>', 1)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
22
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
23 def assertRaises(self, exctype, func, *args):
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
24 was_raised = False
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
25 try:
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
26 func(*args)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
27 except exctype, e:
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
28 self.last_exception = e
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
29 was_raised = True
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
30 self.assertTrue(was_raised)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
31
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
32 def testUndefinedStrIsUndefined(self):
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
33 self.assertEqual(str(pymonkey.undefined),
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
34 "pymonkey.undefined")
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
35
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
36 def testJsWrappedPythonFuncIsNotGCd(self):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
37 def define(cx, obj):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
38 def func(cx, this, args):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
39 return u'func was called'
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
40 jsfunc = cx.new_function(func, func.__name__)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
41 cx.define_property(obj, func.__name__, jsfunc)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
42 return weakref.ref(func)
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
43 rt = pymonkey.Runtime()
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
44 cx = rt.new_context()
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
45 obj = cx.new_object()
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
46 cx.init_standard_classes(obj)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
47 ref = define(cx, obj)
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
48 cx.gc()
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
49 self.assertNotEqual(ref(), None)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
50 result = cx.evaluate_script(obj, 'func()', '<string>', 1)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
51 self.assertEqual(result, u'func was called')
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
52
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
53 # Now ensure that the wrapped function is GC'd when it's
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
54 # no longer reachable from JS space.
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
55 cx.define_property(obj, 'func', 0)
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
56 cx.gc()
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
57 self.assertEqual(ref(), None)
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
58
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
59 def testJsWrappedPythonFuncPassesContext(self):
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
60 contexts = []
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
61
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
62 def func(cx, this, args):
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
63 contexts.append(cx)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
64 return True
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
65
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
66 code = "func()"
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
67 cx = pymonkey.Runtime().new_context()
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
68 obj = cx.new_object()
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
69 cx.init_standard_classes(obj)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
70 jsfunc = cx.new_function(func, func.__name__)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
71 cx.define_property(obj, func.__name__, jsfunc)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
72 cx.evaluate_script(obj, code, '<string>', 1)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
73 self.assertEqual(contexts[0], cx)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
74
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
75 def testJsWrappedPythonFuncPassesThisArg(self):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
76 thisObjs = []
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
77
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
78 def func(cx, this, args):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
79 thisObjs.append(this)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
80 return True
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
81
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
82 code = "func()"
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
83 cx = pymonkey.Runtime().new_context()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
84 obj = cx.new_object()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
85 cx.init_standard_classes(obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
86 jsfunc = cx.new_function(func, func.__name__)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
87 cx.define_property(obj, func.__name__, jsfunc)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
88 cx.evaluate_script(obj, code, '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
89 self.assertEqual(thisObjs[0], obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
90
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
91 def testJsWrappedPythonFuncPassesFuncArgs(self):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
92 funcArgs = []
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
93
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
94 def func(cx, this, args):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
95 funcArgs.append(args)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
96 return True
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
97
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
98 cx = pymonkey.Runtime().new_context()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
99 obj = cx.new_object()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
100 cx.init_standard_classes(obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
101 jsfunc = cx.new_function(func, func.__name__)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
102 cx.define_property(obj, func.__name__, jsfunc)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
103
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
104 cx.evaluate_script(obj, "func()", '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
105 self.assertEqual(len(funcArgs[0]), 0)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
106 self.assertTrue(isinstance(funcArgs[0], tuple))
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
107
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
108 cx.evaluate_script(obj, "func(1, 'foo')", '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
109 self.assertEqual(len(funcArgs[1]), 2)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
110 self.assertEqual(funcArgs[1][0], 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
111 self.assertEqual(funcArgs[1][1], u'foo')
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
112
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
113 def testJsWrappedPythonFunctionReturnsUnicode(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
114 def hai2u(cx, this, args):
29
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
115 return u"o hai"
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
116 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
117 u"o hai")
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
118
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
119 def testJsWrappedPythonFunctionThrowsException(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
120 def hai2u(cx, this, args):
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
121 raise Exception("hello")
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
122 self.assertRaises(pymonkey.error,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
123 self._evalJsWrappedPyFunc,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
124 hai2u, 'hai2u()')
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
125 self.assertEqual(self.last_exception.message,
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
126 "hello")
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
127
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
128 def testJsWrappedPythonFunctionReturnsNone(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
129 def hai2u(cx, this, args):
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
130 pass
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
131 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
132 None)
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
133
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
134 def testJsWrappedPythonFunctionReturnsTrue(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
135 def hai2u(cx, this, args):
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
136 return True
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
137 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
138 True)
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
139
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
140 def testJsWrappedPythonFunctionReturnsFalse(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
141 def hai2u(cx, this, args):
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
142 return False
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
143 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
144 False)
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
145
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
146 def testJsWrappedPythonFunctionReturnsSmallInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
147 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
148 return 5
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
149 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
150 5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
151
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
152 def testJsWrappedPythonFunctionReturnsFloat(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
153 def hai2u(cx, this, args):
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
154 return 5.1
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
155 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
156 5.1)
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
157
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
158 def testJsWrappedPythonFunctionReturnsNegativeInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
159 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
160 return -5
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
161 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
162 -5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
163
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
164 def testJsWrappedPythonFunctionReturnsBigInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
165 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
166 return 2147483647
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
167 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
168 2147483647)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
169
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
170 def testDefinePropertyWorksWithObject(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
171 cx = pymonkey.Runtime().new_context()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
172 obj = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
173 cx.init_standard_classes(obj)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
174 foo = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
175 cx.define_property(obj, "foo", foo)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
176 self.assertEqual(
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
177 cx.evaluate_script(obj, 'foo', '<string>', 1),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
178 foo
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
179 )
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
180
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
181 def testDefinePropertyWorksWithString(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
182 cx = pymonkey.Runtime().new_context()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
183 obj = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
184 cx.init_standard_classes(obj)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
185 foo = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
186 cx.define_property(obj, "foo", u"hello")
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
187 self.assertEqual(
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
188 cx.evaluate_script(obj, 'foo', '<string>', 1),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
189 u"hello"
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
190 )
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
191
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
192 def testObjectIsIdentityPreserving(self):
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
193 cx = pymonkey.Runtime().new_context()
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
194 obj = cx.new_object()
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
195 cx.init_standard_classes(obj)
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
196 cx.evaluate_script(obj, 'var foo = {bar: 1}', '<string>', 1)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
197 self.assertTrue(isinstance(cx.get_property(obj, u"foo"),
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
198 pymonkey.Object))
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
199 self.assertTrue(cx.get_property(obj, u"foo") is
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
200 cx.get_property(obj, u"foo"))
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
201
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
202 def testObjectGetattrWorks(self):
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
203 cx = pymonkey.Runtime().new_context()
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
204 obj = cx.new_object()
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
205 cx.init_standard_classes(obj)
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
206 cx.evaluate_script(obj, 'var boop = 5', '<string>', 1)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
207 cx.evaluate_script(obj, 'this["blarg\u2026"] = 5', '<string>', 1)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
208 self.assertEqual(cx.get_property(obj, u"beans"),
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
209 pymonkey.undefined)
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
210 self.assertEqual(cx.get_property(obj, u"blarg\u2026"), 5)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
211 self.assertEqual(cx.get_property(obj, u"boop"), 5)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
212
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
213 def testContextIsInstance(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
214 cx = pymonkey.Runtime().new_context()
9
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
215 self.assertTrue(isinstance(cx, pymonkey.Context))
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
216
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
217 def testContextTypeCannotBeInstantiated(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
218 self.assertRaises(TypeError, pymonkey.Context)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
219
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
220 def testObjectIsInstance(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
221 obj = pymonkey.Runtime().new_context().new_object()
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
222 self.assertTrue(isinstance(obj, pymonkey.Object))
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
223 self.assertFalse(isinstance(obj, pymonkey.Function))
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
224
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
225 def testObjectTypeCannotBeInstantiated(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
226 self.assertRaises(TypeError, pymonkey.Object)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
227
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
228 def testFunctionIsInstance(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
229 def boop():
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
230 pass
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
231 obj = pymonkey.Runtime().new_context().new_function(boop, "boop")
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
232 self.assertTrue(isinstance(obj, pymonkey.Object))
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
233 self.assertTrue(isinstance(obj, pymonkey.Function))
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
234
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
235 def testFunctionTypeCannotBeInstantiated(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
236 self.assertRaises(TypeError, pymonkey.Function)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
237
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
238 def testGetRuntimeWorks(self):
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
239 rt = pymonkey.Runtime()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
240 cx = rt.new_context()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
241 self.assertEqual(cx.get_runtime(), rt)
14
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
242
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
243 def testUndefinedCannotBeInstantiated(self):
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
244 self.assertRaises(TypeError, pymonkey.undefined)
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
245
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
246 def testEvaluateThrowsException(self):
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
247 self.assertRaises(pymonkey.error,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
248 self._evaljs, 'hai2u()')
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
249 self.assertEqual(self.last_exception.message,
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
250 'ReferenceError: hai2u is not defined')
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
251
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
252 def testEvaluateReturnsUndefined(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
253 retval = self._evaljs("")
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
254 self.assertTrue(retval is pymonkey.undefined)
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
255
28
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
256 def testEvaluateReturnsSMPUnicode(self):
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
257 # This is 'LINEAR B SYLLABLE B008 A', in the supplementary
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
258 # multilingual plane (SMP).
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
259 retval = self._evaljs("'\uD800\uDC00'")
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
260 self.assertEqual(retval, u'\U00010000')
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
261 self.assertEqual(retval.encode('utf-16'),
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
262 '\xff\xfe\x00\xd8\x00\xdc')
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
263
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
264 def testEvaluateReturnsBMPUnicode(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
265 retval = self._evaljs("'o hai\u2026'")
7
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
266 self.assertTrue(type(retval) == unicode)
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
267 self.assertEqual(retval, u'o hai\u2026')
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
268
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
269 def testEvaluateReturnsObject(self):
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
270 cx = pymonkey.Runtime().new_context()
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
271 obj = cx.new_object()
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
272 cx.init_standard_classes(obj)
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
273 obj = cx.evaluate_script(obj, '({boop: 1})', '<string>', 1)
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
274 self.assertTrue(isinstance(obj, pymonkey.Object))
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
275 self.assertEqual(cx.get_property(obj, u"boop"), 1)
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
276
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
277 def testEvaluateReturnsFunction(self):
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
278 cx = pymonkey.Runtime().new_context()
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
279 obj = cx.new_object()
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
280 cx.init_standard_classes(obj)
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
281 obj = cx.evaluate_script(obj, '(function boop() { return 1; })',
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
282 '<string>', 1)
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
283 self.assertTrue(isinstance(obj, pymonkey.Function))
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
284
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
285 def testCallFunctionRaisesErrorOnBadFuncArgs(self):
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
286 cx = pymonkey.Runtime().new_context()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
287 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
288 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
289 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
290 '(function boop(a, b) { return a+b+this.c; })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
291 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
292 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
293 self.assertRaises(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
294 NotImplementedError,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
295 cx.call_function,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
296 obj, obj, (1, self)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
297 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
298
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
299 def testCallFunctionRaisesErrorFromJS(self):
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
300 cx = pymonkey.Runtime().new_context()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
301 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
302 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
303 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
304 '(function boop(a, b) { blarg(); })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
305 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
306 )
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
307 self.assertRaises(pymonkey.error,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
308 cx.call_function,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
309 obj, obj, (1,))
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
310 self.assertEqual(self.last_exception.message,
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
311 'ReferenceError: blarg is not defined')
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
312
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
313 def testCallFunctionWorks(self):
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
314 cx = pymonkey.Runtime().new_context()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
315 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
316 thisArg = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
317 cx.define_property(thisArg, "c", 3)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
318 cx.init_standard_classes(obj)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
319 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
320 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
321 '(function boop(a, b) { return a+b+this.c; })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
322 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
323 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
324 self.assertEqual(cx.call_function(thisArg, obj, (1,2)), 6)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
325
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
326 def testEvaluateReturnsTrue(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
327 self.assertTrue(self._evaljs('true') is True)
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
328
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
329 def testEvaluateReturnsFalse(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
330 self.assertTrue(self._evaljs('false') is False)
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
331
4
2711b636f8e6 Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
332 def testEvaluateReturnsNone(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
333 self.assertTrue(self._evaljs('null') is None)
4
2711b636f8e6 Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
334
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
335 def testEvaluateReturnsIntegers(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
336 self.assertEqual(self._evaljs('1+3'), 4)
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
337
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
338 def testEvaluateReturnsNegativeIntegers(self):
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
339 self.assertEqual(self._evaljs('-5'), -5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
340
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
341 def testEvaluateReturnsBigIntegers(self):
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
342 self.assertEqual(self._evaljs('2147483647*2'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
343 2147483647*2)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
344
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
345 def testEvaluateReturnsFloats(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
346 self.assertEqual(self._evaljs('1.1+3'), 4.1)
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
347
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
348 if __name__ == '__main__':
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
349 unittest.main()