Mercurial > pymonkey
view test_pymonkey.py @ 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.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 29 Jun 2009 13:33:07 -0700 |
parents | 74b7ad049542 |
children | bd30f5c02fc3 |
line wrap: on
line source
import unittest import pymonkey class PymonkeyTests(unittest.TestCase): def _evaljs(self, code): rt = pymonkey.Runtime() cx = rt.new_context() obj = cx.new_object() cx.init_standard_classes(obj) return cx.evaluate_script(obj, code, '<string>', 1) def testDefineFunctionWorks(self): cx = pymonkey.Runtime().new_context() obj = cx.new_object() cx.init_standard_classes(obj) result = {'wasCalled': False} def hai2u(): result['wasCalled'] = True cx.define_function(obj, hai2u, "hai2u") cx.evaluate_script(obj, 'hai2u()', '<string>', 1) self.assertTrue(result['wasCalled']) def testObjectIsIdentityPreserving(self): cx = pymonkey.Runtime().new_context() obj = cx.new_object() cx.init_standard_classes(obj) cx.evaluate_script(obj, 'foo = {bar: 1}', '<string>', 1) self.assertTrue(isinstance(cx.get_property(obj, u"foo"), pymonkey.Object)) self.assertTrue(cx.get_property(obj, u"foo") is cx.get_property(obj, u"foo")) def testObjectGetattrWorks(self): cx = pymonkey.Runtime().new_context() obj = cx.new_object() cx.init_standard_classes(obj) cx.evaluate_script(obj, 'boop = 5', '<string>', 1) cx.evaluate_script(obj, 'this["blarg\u2026"] = 5', '<string>', 1) self.assertEqual(cx.get_property(obj, u"beans"), pymonkey.undefined) self.assertEqual(cx.get_property(obj, u"blarg\u2026"), 5) self.assertEqual(cx.get_property(obj, u"boop"), 5) def testContextIsInstance(self): cx = pymonkey.Runtime().new_context() self.assertTrue(isinstance(cx, pymonkey.Context)) def testContextTypeCannotBeInstantiated(self): self.assertRaises(TypeError, pymonkey.Context) def testObjectIsInstance(self): obj = pymonkey.Runtime().new_context().new_object() self.assertTrue(isinstance(obj, pymonkey.Object)) def testObjectTypeCannotBeInstantiated(self): self.assertRaises(TypeError, pymonkey.Object) def testGetRuntimeWorks(self): rt = pymonkey.Runtime() cx = rt.new_context() self.assertEqual(cx.get_runtime(), rt) def testUndefinedCannotBeInstantiated(self): self.assertRaises(TypeError, pymonkey.undefined) def testEvaluateReturnsUndefined(self): retval = self._evaljs("") self.assertTrue(retval is pymonkey.undefined) def testEvaluateReturnsUnicode(self): retval = self._evaljs("'o hai\u2026'") self.assertTrue(type(retval) == unicode) self.assertEqual(retval, u'o hai\u2026') def testEvaluateReturnsObject(self): rt = pymonkey.Runtime() cx = rt.new_context() obj = cx.new_object() cx.init_standard_classes(obj) obj = cx.evaluate_script(obj, '({boop: 1})', '<string>', 1) self.assertTrue(isinstance(obj, pymonkey.Object)) self.assertEqual(cx.get_property(obj, u"boop"), 1) def testEvaluateReturnsTrue(self): self.assertTrue(self._evaljs('true') is True) def testEvaluateReturnsFalse(self): self.assertTrue(self._evaljs('false') is False) def testEvaluateReturnsNone(self): self.assertTrue(self._evaljs('null') is None) def testEvaluateReturnsIntegers(self): self.assertEqual(self._evaljs('1+3'), 4) def testEvaluateReturnsFloats(self): self.assertEqual(self._evaljs('1.1+3'), 4.1) if __name__ == '__main__': unittest.main()