def test_typeerror_on_object_call(self): c = dukpy.Context() ret = c.evaljs("({})") try: ret() assert False except TypeError: pass
def test_can_pass_callable(self): c = dukpy.Context() called = {'called': False} def inner(): called['called'] = True c.define_global("func", inner) c.evaljs("func()") assert called['called']
def test_complex_pass(self): c = dukpy.Context() def call_first_argument_python(a, *args): return a(*args) call_first_argument_js = c.evaljs("(function(a) { return a.apply(undefined, arguments); })") ret = call_first_argument_js(call_first_argument_python, sum, [1, 7, 10]) assert ret == 18
def test_can_pass_class(self): c = dukpy.Context() class Test(object): def __init__(self, worked): self.worked = worked c.define_global("Test", Test) t = c.evaljs("Test(true)") assert t.worked
def test_can_pass_obj(self): c = dukpy.Context() class Test(object): def __init__(self): self.worked = False t = Test() c.define_global("t", t) c.evaljs("t.worked = true") assert t.worked
def test_pyargs_segfault(self): for _ in range(1, 10): c = dukpy.Context() c.define_global("test", range(1000)) c.evaljs(""" var iter = test.__iter__(); var i = (iter.__next__ || iter.next)(); while (i !== null) { i = (iter.__next__ || iter.next)(); break; } """)
def test_can_iterate_over_python(self): c = dukpy.Context() c.define_global("thing", [5, 8, 1, 6]) ret = c.evaljs(""" var count = 0; for (var i = 0; i < thing.length; i++) { count += thing[i]; } count; """) assert ret == sum([5, 8, 1, 6])
def test_tostring_calls_str(self): c = dukpy.Context() class Test(object): def __init__(self, str): self.s = str def __str__(self): return self.s c.define_global("t", Test('hi, mum')) ret = c.evaljs("t.toString()") assert ret == "hi, mum"
def test_can_return_complex_obj(self): c = dukpy.Context() obj = c.evaljs(""" ({'null': null, 'undefined': null, 'a': 'a', '1': 1, 'true': true, 'false': false, 'nested': {'object': true}, 'array': ['of', ['arrays'], {'and': 'objects'}]}) """) assert obj['null'] is None assert obj['undefined'] is None assert obj['a'] == 'a' assert obj['1'] == 1 assert obj['true'] is True assert obj['false'] is False assert obj['nested']['object'] assert len(obj['array']) == 3 assert obj['array'][0] == 'of' assert len(obj['array'][1]) == 1 assert obj['array'][1][0] == 'arrays' assert obj['array'][2]['and'] == 'objects'
def test_evaljs(self): c = dukpy.Context() ret = c.evaljs("5") assert ret == 5
def test_can_construct_context(self): dukpy.Context()
def test_binds_correctly(self): c = dukpy.Context() ret = c.evaljs("var r = ({x: 'ham', y: function() { return this.x; }}); r.z = r.y(); r;") assert ret.y() == ret.z
def test_can_return_obj(self): c = dukpy.Context() obj = c.evaljs("({'hi': 'mum'})") assert obj['hi'] == 'mum'
def test_can_return_callable(self): c = dukpy.Context() inc = c.evaljs("(function(x) { return x + 1; })") assert inc(1) == 2 assert inc(2) == 3
def test_can_pass_dict(self): c = dukpy.Context() d = {'worked': False} c.define_global("dict", d) c.evaljs("dict.worked = true") assert d['worked']
def test_context_retained(self): c = dukpy.Context() c.evaljs("aardvark = 'lemon'") assert c.evaljs("aardvark") == 'lemon'
def test_handles_none(self): o = dukpy._dukpy.ctx_eval_string dukpy._dukpy.ctx_eval_string = lambda *args, **kwargs: None c = dukpy.Context() c.evaljs("hi") dukpy._dukpy.ctx_eval_string = o
def test_can_set_obj(self): c = dukpy.Context() c.define_global("seven", 7) assert c.evaljs("seven") == 7