Beispiel #1
0
    def test_typeerror_on_object_call(self):
        c = dukpy.Context()

        ret = c.evaljs("({})")
        try:
            ret()
            assert False
        except TypeError:
            pass
Beispiel #2
0
    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']
Beispiel #3
0
    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
Beispiel #4
0
    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
Beispiel #5
0
    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
Beispiel #6
0
    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;
}
            """)
Beispiel #7
0
    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])
Beispiel #8
0
    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"
Beispiel #9
0
    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'
Beispiel #10
0
 def test_evaljs(self):
     c = dukpy.Context()
     ret = c.evaljs("5")
     assert ret == 5
Beispiel #11
0
 def test_can_construct_context(self):
     dukpy.Context()
Beispiel #12
0
    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
Beispiel #13
0
    def test_can_return_obj(self):
        c = dukpy.Context()

        obj = c.evaljs("({'hi': 'mum'})")
        assert obj['hi'] == 'mum'
Beispiel #14
0
    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
Beispiel #15
0
 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']
Beispiel #16
0
 def test_context_retained(self):
     c = dukpy.Context()
     c.evaljs("aardvark = 'lemon'")
     assert c.evaljs("aardvark") == 'lemon'
Beispiel #17
0
 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
Beispiel #18
0
 def test_can_set_obj(self):
     c = dukpy.Context()
     c.define_global("seven", 7)
     assert c.evaljs("seven") == 7