Пример #1
0
def test_nested_operations():
    input = "x = 2 * ((33 + 2.2) / 2)"
    expected = """
    var x;
    x = 2 * ((33 + 2.2) / 2);
    """
    assert compile(input) == norm(expected)
Пример #2
0
def test_none_assignation():
    input = "x = None"
    expected = """
    var x;
    x = null;
    """
    assert compile(input) == norm(expected)
Пример #3
0
def test_basic_class():
    input = """
    class MyClass:
        def __init__(x):
            this.x = x

        def foo():
            return this.x
    """

    expected = """
    var MyClass, foo;
    MyClass = (function() {
        var classref_0;
        classref_0 = function(x) {
            this.x = x;
        };
        classref_0.prototype.foo = function() {
            return this.x;
        };
        return classref_0;
    })();
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #4
0
def test_simple_multiple_assignation():
    input = "x = y = 2"
    expected = """
    var x, y;
    x = y = 2;
    """
    assert compile(input) == norm(expected)
Пример #5
0
def test_new_import_and_try_overwrite():
    input = """
    import _new as new
    new = 2
    """

    with pytest.raises(RuntimeError):
        compiled = compile(input)
Пример #6
0
def test_tuple_expr():
    input = """
    (1, 2, 3)
    """
    expected = """
    [1,2,3];
    """
    assert compile(input) == norm(expected)
Пример #7
0
def test_list_expr():
    input = """
    [1, 2, 3]
    """
    expected = """
    [1,2,3];
    """
    assert compile(input) == norm(expected)
Пример #8
0
def test_global_import_and_try_overwrite():
    input = """
    import _global as g
    g = 2
    """

    with pytest.raises(RuntimeError):
        compiled = compile(input)
Пример #9
0
def test_delete_expr():
    input = """
    del x
    """
    expected = """
    var x;
    delete x;
    """
    assert compile(input) == norm(expected)
Пример #10
0
def test_basic_op_bitwise_shifts():
    input = """
    2 << 2
    2 >> 2
    """
    expected = """
    2 << 2;
    2 >> 2;
    """
    assert compile(input) == norm(expected)
Пример #11
0
def test_empty_return():
    input = """
    return
    """

    expected = """
    return;
    """
    compiled = compile(input)
    assert compiled == norm(expected)
Пример #12
0
def test_delete_expr_multiple():
    input = """
    del x, y
    """
    expected = """
    var x, y;
    delete x;
    delete y;
    """
    assert compile(input) == norm(expected)
Пример #13
0
def test_unary_operators():
    input = """
    x = +1
    y = -1
    """
    expected = """
    var x, y;
    x = +1;
    y = -1;
    """
    assert compile(input) == norm(expected)
Пример #14
0
def test_negative_slices():
    input = """
    testList[1:-1]
    """

    expected = """
    testList.slice(1, -1);
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #15
0
def test_dict_access():
    input = """
    xx = foo["22"]
    """

    expected = """
    var xx;
    xx = foo["22"];
    """
    compiled = compile(input)
    assert compiled == norm(expected)
Пример #16
0
def test_simple_function_call_with_multiple_args():
    input = """
    x = foo("Hello World", 2, 2.3)
    """

    expected = """
    var x;
    x = foo("Hello World", 2, 2.3);
    """

    assert compile(input) == norm(expected)
Пример #17
0
def test_auto_camel_case():
    input = """
    xx = foo_bar()
    """

    expected = """
    var xx;
    xx = fooBar();
    """
    compiled = compile(input, translate_options={"auto_camelcase": True})
    assert compiled == norm(expected)
Пример #18
0
def test_boolean_assignation():
    input = """
    x = True
    y = False
    """
    expected = """
    var x, y;
    x = true;
    y = false;
    """
    assert compile(input) == norm(expected)
Пример #19
0
def test_exceptions_raise():
    input = """
    raise "sample exception"
    """

    expected = """
    throw "sample exception";
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #20
0
def test_simple_slice():
    input = """
    testList[1:5]
    """

    expected = """
    testList.slice(1, 5);
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #21
0
def test_simple_function_call():
    input = """
    x = foo("Hello World")
    """

    expected = """
    var x;
    x = foo("Hello World");
    """

    assert compile(input) == norm(expected)
Пример #22
0
def test_new_import():
    input = """
    import _new as new_instance
    """

    expected = """
    var new_instance;
    new_instance = function() { var ___args_array = Array.apply(null, arguments); var ___clazz = ___args_array.slice(0, 1)[0]; return new (___clazz.bind.apply(___clazz, ___args_array))();};
    """

    compiled = compile(input)
    assert compiled == norm(expected)
Пример #23
0
def test_global_import():
    input = """
    import _global as g
    """

    expected = """
    var g;
    g = this;
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #24
0
def test_basic_while():
    input = """
    while True:
        console.log("test")
    """

    expected = """
    while (true) {
        console.log("test");
    }
    """
    assert compile(input) == norm(expected)
Пример #25
0
def test_module_as_closure():
    input = """
    xx = foo_bar()
    """

    expected = """
    (function() {
        var xx;
        xx = foo_bar();
    }).call(this);
    """
    compiled = compile(input, translate_options={"module_as_closure": True})
    assert compiled == norm(expected)
Пример #26
0
def test_partial_slices():
    input = """
    testList[1:]
    testList[:5]
    """

    expected = """
    testList.slice(1);
    testList.slice(0, 5);
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #27
0
def test_multiple_assignation():
    # FIXME: this seems generate inconsisten js?
    input = """
    x = a, b = 1, 2
    """
    expected = """
    var _ref_0, x;
    x = _ref_0 = [1,2];
    a = _ref_0[0];
    b = _ref_0[1];
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #28
0
def test_this_assignation():
    input = """
    def person(name):
        this.name = name
    """

    expected = """
    var person;
    person = function(name) {
        this.name = name;
    };
    """
    compiled = compile(input)
    assert compiled == norm(expected)
Пример #29
0
def test_break():
    input = """
    while True:
        break
    """

    expected = """
    while (true) {
        break;
    }
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)
Пример #30
0
def test_continue():
    input = """
    while True:
        continue
    """

    expected = """
    while (true) {
        continue;
    }
    """
    compiled = compile(input)
    print(compiled)
    assert compiled == norm(expected)