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)
def test_none_assignation(): input = "x = None" expected = """ var x; x = null; """ assert compile(input) == norm(expected)
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)
def test_simple_multiple_assignation(): input = "x = y = 2" expected = """ var x, y; x = y = 2; """ assert compile(input) == norm(expected)
def test_new_import_and_try_overwrite(): input = """ import _new as new new = 2 """ with pytest.raises(RuntimeError): compiled = compile(input)
def test_tuple_expr(): input = """ (1, 2, 3) """ expected = """ [1,2,3]; """ assert compile(input) == norm(expected)
def test_list_expr(): input = """ [1, 2, 3] """ expected = """ [1,2,3]; """ assert compile(input) == norm(expected)
def test_global_import_and_try_overwrite(): input = """ import _global as g g = 2 """ with pytest.raises(RuntimeError): compiled = compile(input)
def test_delete_expr(): input = """ del x """ expected = """ var x; delete x; """ assert compile(input) == norm(expected)
def test_basic_op_bitwise_shifts(): input = """ 2 << 2 2 >> 2 """ expected = """ 2 << 2; 2 >> 2; """ assert compile(input) == norm(expected)
def test_empty_return(): input = """ return """ expected = """ return; """ compiled = compile(input) assert compiled == norm(expected)
def test_delete_expr_multiple(): input = """ del x, y """ expected = """ var x, y; delete x; delete y; """ assert compile(input) == norm(expected)
def test_unary_operators(): input = """ x = +1 y = -1 """ expected = """ var x, y; x = +1; y = -1; """ assert compile(input) == norm(expected)
def test_negative_slices(): input = """ testList[1:-1] """ expected = """ testList.slice(1, -1); """ compiled = compile(input) print(compiled) assert compiled == norm(expected)
def test_dict_access(): input = """ xx = foo["22"] """ expected = """ var xx; xx = foo["22"]; """ compiled = compile(input) assert compiled == norm(expected)
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)
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)
def test_boolean_assignation(): input = """ x = True y = False """ expected = """ var x, y; x = true; y = false; """ assert compile(input) == norm(expected)
def test_exceptions_raise(): input = """ raise "sample exception" """ expected = """ throw "sample exception"; """ compiled = compile(input) print(compiled) assert compiled == norm(expected)
def test_simple_slice(): input = """ testList[1:5] """ expected = """ testList.slice(1, 5); """ compiled = compile(input) print(compiled) assert compiled == norm(expected)
def test_simple_function_call(): input = """ x = foo("Hello World") """ expected = """ var x; x = foo("Hello World"); """ assert compile(input) == norm(expected)
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)
def test_global_import(): input = """ import _global as g """ expected = """ var g; g = this; """ compiled = compile(input) print(compiled) assert compiled == norm(expected)
def test_basic_while(): input = """ while True: console.log("test") """ expected = """ while (true) { console.log("test"); } """ assert compile(input) == norm(expected)
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)
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)
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)
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)
def test_break(): input = """ while True: break """ expected = """ while (true) { break; } """ compiled = compile(input) print(compiled) assert compiled == norm(expected)
def test_continue(): input = """ while True: continue """ expected = """ while (true) { continue; } """ compiled = compile(input) print(compiled) assert compiled == norm(expected)