Mercurial > pymonkey
comparison test_pymonkey.py @ 37:d4efcbb06964
Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 02 Jul 2009 22:42:31 -0700 |
parents | 5d3d3b25f23f |
children | 8a7abd0bb48d |
comparison
equal
deleted
inserted
replaced
36:04a6e9a67ae5 | 37:d4efcbb06964 |
---|---|
11 | 11 |
12 def _evalJsWrappedPyFunc(self, func, code): | 12 def _evalJsWrappedPyFunc(self, func, code): |
13 cx = pymonkey.Runtime().new_context() | 13 cx = pymonkey.Runtime().new_context() |
14 obj = cx.new_object() | 14 obj = cx.new_object() |
15 cx.init_standard_classes(obj) | 15 cx.init_standard_classes(obj) |
16 cx.define_function(obj, func, func.__name__) | 16 jsfunc = cx.new_function(func, func.__name__) |
17 cx.define_property(obj, func.__name__, jsfunc) | |
17 return cx.evaluate_script(obj, code, '<string>', 1) | 18 return cx.evaluate_script(obj, code, '<string>', 1) |
18 | 19 |
19 def testJsWrappedPythonFunctionReturnsUnicode(self): | 20 def testJsWrappedPythonFunctionReturnsUnicode(self): |
20 def hai2u(): | 21 def hai2u(): |
21 return u"o hai" | 22 return u"o hai" |
56 def hai2u(): | 57 def hai2u(): |
57 return 2147483647 | 58 return 2147483647 |
58 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), | 59 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'), |
59 2147483647) | 60 2147483647) |
60 | 61 |
62 def testDefinePropertyWorksWithObject(self): | |
63 cx = pymonkey.Runtime().new_context() | |
64 obj = cx.new_object() | |
65 cx.init_standard_classes(obj) | |
66 foo = cx.new_object() | |
67 cx.define_property(obj, "foo", foo) | |
68 self.assertEqual( | |
69 cx.evaluate_script(obj, 'foo', '<string>', 1), | |
70 foo | |
71 ) | |
72 | |
73 def testDefinePropertyWorksWithString(self): | |
74 cx = pymonkey.Runtime().new_context() | |
75 obj = cx.new_object() | |
76 cx.init_standard_classes(obj) | |
77 foo = cx.new_object() | |
78 cx.define_property(obj, "foo", u"hello") | |
79 self.assertEqual( | |
80 cx.evaluate_script(obj, 'foo', '<string>', 1), | |
81 u"hello" | |
82 ) | |
83 | |
61 def testObjectIsIdentityPreserving(self): | 84 def testObjectIsIdentityPreserving(self): |
62 cx = pymonkey.Runtime().new_context() | 85 cx = pymonkey.Runtime().new_context() |
63 obj = cx.new_object() | 86 obj = cx.new_object() |
64 cx.init_standard_classes(obj) | 87 cx.init_standard_classes(obj) |
65 cx.evaluate_script(obj, 'foo = {bar: 1}', '<string>', 1) | 88 cx.evaluate_script(obj, 'foo = {bar: 1}', '<string>', 1) |
87 self.assertRaises(TypeError, pymonkey.Context) | 110 self.assertRaises(TypeError, pymonkey.Context) |
88 | 111 |
89 def testObjectIsInstance(self): | 112 def testObjectIsInstance(self): |
90 obj = pymonkey.Runtime().new_context().new_object() | 113 obj = pymonkey.Runtime().new_context().new_object() |
91 self.assertTrue(isinstance(obj, pymonkey.Object)) | 114 self.assertTrue(isinstance(obj, pymonkey.Object)) |
115 self.assertFalse(isinstance(obj, pymonkey.Function)) | |
92 | 116 |
93 def testObjectTypeCannotBeInstantiated(self): | 117 def testObjectTypeCannotBeInstantiated(self): |
94 self.assertRaises(TypeError, pymonkey.Object) | 118 self.assertRaises(TypeError, pymonkey.Object) |
119 | |
120 def testFunctionIsInstance(self): | |
121 def boop(): | |
122 pass | |
123 obj = pymonkey.Runtime().new_context().new_function(boop, "boop") | |
124 self.assertTrue(isinstance(obj, pymonkey.Object)) | |
125 self.assertTrue(isinstance(obj, pymonkey.Function)) | |
126 | |
127 def testFunctionTypeCannotBeInstantiated(self): | |
128 self.assertRaises(TypeError, pymonkey.Function) | |
95 | 129 |
96 def testGetRuntimeWorks(self): | 130 def testGetRuntimeWorks(self): |
97 rt = pymonkey.Runtime() | 131 rt = pymonkey.Runtime() |
98 cx = rt.new_context() | 132 cx = rt.new_context() |
99 self.assertEqual(cx.get_runtime(), rt) | 133 self.assertEqual(cx.get_runtime(), rt) |