def test_transform_compound_assignments(self): _assert_transform( nodes.assign(["x", "y"], nodes.ref("z")), cc.statements([ cc.declare("__nope_u_tmp0", cc.ref("z")), cc.assign(cc.ref("x"), cc.ref("__nope_u_tmp0")), cc.assign(cc.ref("y"), cc.ref("__nope_u_tmp0")), ]), )
def test_import_of_module_in_package_assigns_values_for_both_package_and_module(self): _assert_transform( nodes.import_([ nodes.import_alias("os.path", None), ]), cc.statements([ cc.assign(cc.ref("os"), cc.module_ref(["os"])), cc.assign(cc.attr(cc.ref("os"), "path"), cc.module_ref(["os", "path"])), ]) )
def test_import_multiple_values_in_single_import_statement(self): _assert_transform( nodes.import_([ nodes.import_alias("os", None), nodes.import_alias("sys", None) ]), cc.statements([ cc.assign(cc.ref("os"), cc.module_ref(["os"])), cc.assign(cc.ref("sys"), cc.module_ref(["sys"])), ]) )
def test_multiple_imported_names_in_one_statement_generates_multiple_assignments(self): _assert_transform( nodes.import_from(["."], [ nodes.import_alias("x", None), nodes.import_alias("y", None), ]), cc.statements([ cc.assign(cc.ref("x"), cc.attr(cc.module_ref(["."]), "x")), cc.assign(cc.ref("y"), cc.attr(cc.module_ref(["."]), "y")), ]), )
def test_transform_module_with_exports(): _assert_transform( cc.module([ cc.declare("__all__"), cc.declare("x"), cc.assign(cc.ref("__all__"), cc.list_literal([cc.str_literal("x")])), cc.assign(cc.ref("x"), cc.none) ], exported_names=["x"]), """ var __all__; var x; __all__ = ["x"]; x = null; $exports.x = x; """ )
def test_importing_module_from_package_references_module_directly(self): module_resolver = FakeModuleResolver({ (("x", ), "y"): ResolvedImport(["x", "y"], _stub_module, None) }) _assert_transform( nodes.import_from(["x"], [nodes.import_alias("y", None)]), cc.assign(cc.ref("y"), cc.module_ref(["x", "y"])), module_resolver=module_resolver, )
def test_variables_are_declared(self): _assert_transform( nodes.func("f", nodes.args([]), [ nodes.assign([nodes.ref("x")], nodes.ref("y")), nodes.ret(nodes.ref("value")), ], type=None), cc.func("f", [], [ cc.declare("x"), cc.assign(cc.ref("x"), cc.ref("y")), cc.ret(cc.ref("value")), ]), )
def test_assignments_in_body_are_transformed(self): _assert_transform( nodes.class_("Blah", [nodes.assign([nodes.ref("value")], nodes.none())]), cc.class_( "Blah", methods=[], body=[ cc.declare("value"), cc.assign(cc.ref("value"), cc.none), ], ), )
def test_does_not_redeclare_variables_with_same_name_as_argument(self): _assert_transform( nodes.func( name="f", args=nodes.args([nodes.arg("x")]), body=[ nodes.assign(["x"], nodes.ref("y")), nodes.ret(nodes.ref("value")), ], type=None, ), cc.func("f", [cc.arg("x")], [ cc.assign(cc.ref("x"), cc.ref("y")), cc.ret(cc.ref("value")), ]) )
def test_module_exports_are_set_directly_on_module(self): module_node = nodes.module( [nodes.assign([nodes.ref("value")], nodes.none())], is_executable=False ) module_type = types.module("blah", [types.attr("value", types.none_type)]) _assert_transform( module_node, cc.module( [ cc.declare("value"), cc.assign(cc.ref("value"), cc.none) ], is_executable=False, exported_names=["value"], ), type_lookup=[(module_node, module_type)] )
def test_transform_class_with_attributes(): _assert_transform( cc.class_( name="User", methods=[], body=[ cc.declare("x"), cc.assign(cc.ref("x"), cc.none) ], ), """ User = function() { var $self0 = { "$nopeType": User }; var x; x = null; $self0.x = $nope.instanceAttribute($self0, x); return $self0; }; """ )
def test_import_from_assigns_value_to_asname_if_asname_is_set(self): _assert_transform( nodes.import_from(["os", "path"], [nodes.import_alias("join", "j")]), cc.assign(cc.ref("j"), cc.attr(cc.module_ref(["os", "path"]), "join")), )
def test_import_of_module_assigns_module_to_as_name_if_present(self): _assert_transform( nodes.import_([nodes.import_alias("message", "m")]), cc.assign(cc.ref("m"), cc.module_ref(["message"])) )
def test_transform_assigment_to_single_target(self): _assert_transform( nodes.assign([nodes.ref("x")], nodes.ref("y")), cc.assign(cc.ref("x"), cc.ref("y")), )
def test_import_of_module_assigns_module_to_name(self): _assert_transform( nodes.import_([nodes.import_alias("message", None)]), cc.assign(cc.ref("message"), cc.module_ref(["message"])) )
def test_transform_assignment(): _assert_transform( cc.assign(cc.ref("x"), cc.ref("z")), js.expression_statement(js.assign(js.ref("x"), js.ref("z"))), )
def test_transform_assignment_to_attribute_sets_property_directly(): _assert_transform( cc.assign(cc.attr(cc.ref("x"), "y"), cc.ref("z")), js.expression_statement(js.assign(js.property_access(js.ref("x"), "y"), js.ref("z"))), )
def test_import_from_uses_two_dots_to_indicate_import_from_parent_package(self): _assert_transform( nodes.import_from([".."], [nodes.import_alias("x", None)]), cc.assign(cc.ref("x"), cc.attr(cc.module_ref([".."]), "x")), )