def init_method_is_called_if_present(self): node = cc.class_("A", methods=[ cc.func("__init__", [cc.arg("self"), cc.arg("value")], [ cc.expression_statement(cc.call(cc.ref("print"), [cc.ref("value")])) ]) ], body=[]) expected_aux = """internal class __A { internal dynamic __init__; } """ expected = """A = new { __call__ = ((System.Func<dynamic, dynamic>)((dynamic __value) => { dynamic __self = null; __self = new __A { __init__ = ((System.Func<dynamic, dynamic>)((dynamic value) => { dynamic self = __self; print(value); })), }; __self.__init__(__value); return __self; })), }; """ transformer = _create_transformer() assert_equal(expected, cs.dumps(transformer.transform(node))) assert_equal(expected_aux, cs.dumps(transformer.aux()))
def test_statements_in_module_body_are_transformed(self): module_node = nodes.module([nodes.expression_statement(nodes.ref("value"))], is_executable=True) module_type = types.module("blah", []) _assert_transform( module_node, cc.module([cc.expression_statement(cc.ref("value"))], is_executable=True, exported_names=[]), type_lookup=[(module_node, module_type)] )
def except_handler_is_converted_to_catch_for_nope_exceptions(self): node = cc.try_( [], handlers=[cc.except_(None, None, [cc.expression_statement(cc.ref("y"))])] ) expected = """try { } catch (__Nope.Internals.@__NopeException __exception) { y; } """ assert_equal(expected, cs.dumps(transform(node)))
def executable_module_is_converted_to_class_with_main_method(self): node = cc.module([ cc.expression_statement(cc.call(cc.ref("f"), [])) ], is_executable=True) module = LocalModule("blah.py", node) expected = """internal class Program { internal static void Main() { f(); } } """ assert_equal(expected, cs.dumps(transform(module)))
def try_with_finally_is_converted_to_try_with_finally(self): node = cc.try_( [cc.ret(cc.ref("x"))], finally_body=[cc.expression_statement(cc.ref("y"))] ) expected = """try { return x; } finally { y; } """ assert_equal(expected, cs.dumps(transform(node)))
def test_transform_expression_statement(): _assert_transform( cc.expression_statement(cc.ref("x")), js.expression_statement(js.ref("x")) )
def test_transform_module(): _assert_transform( cc.module([cc.expression_statement(cc.ref("x"))], is_executable=False, exported_names=[]), js.statements([js.expression_statement(js.ref("x"))]) )