def test_raise_python_error_from_js(self): ctx = DuktapeContext() def f(): raise Exception() ctx.set_globals(f=f) with self.assertRaises(JSError): ctx.eval_js('f()')
class TestBasicConversion(TestCase): def setUp(self): self.ctx = DuktapeContext() def _convert(self, val): self.ctx.set_globals(x=val) return self.ctx.eval_js('x') def test_None(self): self.assertIsNone(self._convert(None)) def test_int(self): self.assertEqual(self._convert(12), 12) def test_double(self): val = 1.23456789012345678909 self.assertEqual(self._convert(val), val) def test_big_int_overflows(self): val = 1 << 54 with self.assertRaises(OverflowError): self._convert(val) def test_int_fits_in_double(self): val = 1 << 53 self.assertEqual(self._convert(val), val) def test_utf8_string(self): val = u'\u05D4' self.assertEqual(self._convert(val), val) def test_ascii_string(self): val = 'hello world' self.assertEqual(self._convert(val), val) def test_js_null_and_undefined(self): self.assertIsNone(self.ctx.eval_js('undefined')) self.assertIsNone(self.ctx.eval_js('null'))
class TestPyProxy(TestCase): def setUp(self): self.ctx = DuktapeContext() def test_py_proxy_get(self): class X(object): def __init__(self): self.x = 42 self.ctx.set_globals(x=X()) res = self.ctx.eval_js('x.x') self.assertEqual(res, 42) res = self.ctx.eval_js('x.y') self.assertEqual(res, None) def test_py_proxy_get_list_index(self): self.ctx.set_globals(x=[1, 3]) res = self.ctx.eval_js('x[0]') self.assertEqual(res, 1) res = self.ctx.eval_js('x[3]') self.assertEqual(res, None) def test_py_proxy_get_dict_key(self): self.ctx.set_globals(x=dict(a=1, b=2)) res = self.ctx.eval_js('x.a') self.assertEqual(res, 1) res = self.ctx.eval_js('x.c') self.assertEqual(res, None) def test_py_proxy_get_dict_method(self): self.ctx.set_globals(x=dict(a=1, b=2)) res = self.ctx.eval_js('x.get("c", 42)') self.assertEqual(res, 42) def test_py_proxy_has(self): class X(object): def __init__(self): self.x = 42 self.ctx.set_globals(x=X()) res = self.ctx.eval_js('"x" in x') self.assertTrue(res) res = self.ctx.eval_js('"y" in x') self.assertFalse(res) def test_py_proxy_has_list_index(self): self.ctx.set_globals(x=[1, 3]) res = self.ctx.eval_js('0 in x') self.assertTrue(res) res = self.ctx.eval_js('3 in x') self.assertFalse(res) def test_py_proxy_has_dict_key(self): self.ctx.set_globals(x=dict(a=1, b=2)) res = self.ctx.eval_js('"a" in x') self.assertTrue(res) res = self.ctx.eval_js('"c" in x') self.assertFalse(res) def test_py_proxy_set(self): class X(object): def __init__(self): self.x = 42 x = X() self.ctx.set_globals(x=x) self.ctx.eval_js('x.x = 12') self.assertEqual(x.x, 12) def test_py_proxy_set_index(self): x = [1, 3] self.ctx.set_globals(x=x) self.ctx.eval_js('x[0] = 2') self.assertEqual(x[0], 2) with self.assertRaises(JSError): self.ctx.eval_js('x[2] = 0') def test_py_proxy_set_dict_key(self): x = dict(a=1, b=2) self.ctx.set_globals(x=x) self.ctx.eval_js('x.a = 3') self.assertEqual(x['a'], 3) self.assertEqual(x['b'], 2) def test_py_proxy_call_method(self): class X(object): def __init__(self): self.x = 42 def f(self, a): return self.x + a self.ctx.set_globals(x=X()) res = self.ctx.eval_js('f = x.f; f(1)') self.assertEqual(res, 43) def test_construct_python_object(self): class X(object): def __init__(self): self.x = 42 self.ctx.set_globals(X=X) res = self.ctx.eval_js('X().x') self.assertEqual(res, 42) def test_cant_call_new_on_py_proxy(self): class X(object): pass self.ctx.set_globals(X=X) with self.assertRaises(JSError) as err: self.ctx.eval_js('new X()') self.assertIn('can\'t use new on python objects', str(err.exception)) def test_return_py_proxy_to_python(self): class X(object): pass x = X() self.ctx.set_globals(x=x) returned_x = self.ctx.eval_js('x') self.assertIs(x, returned_x) def test_py_proxy_function(self): def test(x): return x + 1 self.ctx.set_globals(test=test) res = self.ctx.eval_js('test(41)') self.assertEqual(res, 42) def test_py_proxy_index_list(self): self.ctx.set_globals(x=[1, 2, 3]) self.assertEqual(self.ctx.eval_js('x[0]'), 1) self.assertEqual(self.ctx.eval_js('x[1]'), 2) self.assertEqual(self.ctx.eval_js('x[2]'), 3) self.assertEqual(self.ctx.eval_js('x[3]'), None) def test_py_proxy_index_dict(self): self.ctx.set_globals(x=dict(a=1, b=2)) self.assertEqual(self.ctx.eval_js('x["a"]'), 1) self.assertEqual(self.ctx.eval_js('x["b"]'), 2) self.assertEqual(self.ctx.eval_js('x["c"]'), None)
class TestPyProxy(TestCase): def setUp(self): self.ctx = DuktapeContext() def test_py_proxy_get(self): class X(object): def __init__(self): self.x = 42 self.ctx.set_globals(x=X()) res = self.ctx.eval_js('x.x') self.assertEqual(res, 42) res = self.ctx.eval_js('x.y') self.assertEqual(res, None) def test_py_proxy_get_list_index(self): self.ctx.set_globals(x=[1, 3]) res = self.ctx.eval_js('x[0]') self.assertEqual(res, 1) res = self.ctx.eval_js('x[3]') self.assertEqual(res, None) def test_py_proxy_get_dict_key(self): self.ctx.set_globals(x=dict(a=1, b=2)) res = self.ctx.eval_js('x.a') self.assertEqual(res, 1) res = self.ctx.eval_js('x.c') self.assertEqual(res, None) def test_py_proxy_get_dict_method(self): self.ctx.set_globals(x=dict(a=1, b=2)) res = self.ctx.eval_js('x.get("c", 42)') self.assertEqual(res, 42) def test_py_proxy_has(self): class X(object): def __init__(self): self.x = 42 self.ctx.set_globals(x=X()) res = self.ctx.eval_js('"x" in x') self.assertTrue(res) res = self.ctx.eval_js('"y" in x') self.assertFalse(res) def test_py_proxy_has_list_index(self): self.ctx.set_globals(x=[1, 3]) res = self.ctx.eval_js('0 in x') self.assertTrue(res) res = self.ctx.eval_js('3 in x') self.assertFalse(res) def test_py_proxy_has_dict_key(self): self.ctx.set_globals(x=dict(a=1, b=2)) res = self.ctx.eval_js('"a" in x') self.assertTrue(res) res = self.ctx.eval_js('"c" in x') self.assertFalse(res) def test_py_proxy_set(self): class X(object): def __init__(self): self.x = 42 x = X() self.ctx.set_globals(x=x) self.ctx.eval_js('x.x = 12') self.assertEqual(x.x, 12) def test_py_proxy_set_index(self): x = [1, 3] self.ctx.set_globals(x=x) self.ctx.eval_js('x[0] = 2') self.assertEqual(x[0], 2) with self.assertRaises(JSError): self.ctx.eval_js('x[2] = 0') def test_py_proxy_set_dict_key(self): x = dict(a=1, b=2) self.ctx.set_globals(x=x) self.ctx.eval_js('x.a = 3') self.assertEqual(x['a'], 3) self.assertEqual(x['b'], 2) def test_py_proxy_call_method(self): class X(object): def __init__(self): self.x = 42 def f(self, a): return self.x + a self.ctx.set_globals(x=X()) res = self.ctx.eval_js('f = x.f; f(1)') self.assertEqual(res, 43) def test_construct_python_object(self): class X(object): def __init__(self): self.x = 42 self.ctx.set_globals(X=X) res = self.ctx.eval_js('X().x') self.assertEqual(res, 42) def test_cant_call_new_on_py_proxy(self): class X(object): pass self.ctx.set_globals(X=X) with self.assertRaises(JSError) as err: self.ctx.eval_js('new X()') self.assertIn('can\'t use new on python objects', err.exception.message) def test_return_py_proxy_to_python(self): class X(object): pass x = X() self.ctx.set_globals(x=x) returned_x = self.ctx.eval_js('x') self.assertIs(x, returned_x) def test_py_proxy_function(self): def test(x): return x + 1 self.ctx.set_globals(test=test) res = self.ctx.eval_js('test(41)') self.assertEqual(res, 42) def test_py_proxy_index_list(self): self.ctx.set_globals(x=[1, 2, 3]) self.assertEqual(self.ctx.eval_js('x[0]'), 1) self.assertEqual(self.ctx.eval_js('x[1]'), 2) self.assertEqual(self.ctx.eval_js('x[2]'), 3) self.assertEqual(self.ctx.eval_js('x[3]'), None) def test_py_proxy_index_dict(self): self.ctx.set_globals(x=dict(a=1, b=2)) self.assertEqual(self.ctx.eval_js('x["a"]'), 1) self.assertEqual(self.ctx.eval_js('x["b"]'), 2) self.assertEqual(self.ctx.eval_js('x["c"]'), None)