예제 #1
0
    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()))
예제 #2
0
def test_transform_try_except_with_multiple_exception_handlers():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            handlers=[
                cc.except_(cc.ref("AssertionError"), None, [cc.ret(cc.ref("y"))]),
                cc.except_(cc.ref("Exception"), None, [cc.ret(cc.ref("z"))]),
            ],
        ),
        """
            try {
                return x;
            } catch ($exception0) {
                if ($exception0.$nopeException === $nope.undefined) {
                    throw $exception0;
                } else {
                    if ($nope.builtins.isinstance($exception0.$nopeException, AssertionError)) {
                        return y;
                    } else {
                        if ($nope.builtins.isinstance($exception0.$nopeException, Exception)) {
                            return z;
                        } else {
                            throw $exception0;
                        }
                    }
                }
            }
        """,
    )
예제 #3
0
def test_transform_raise_without_exception_value():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            handlers=[
                cc.except_(cc.ref("AssertionError"), cc.ref("error"), [cc.raise_()]),
            ],
        ),
        """
            try {
                return x;
            } catch ($exception0) {
                if ($exception0.$nopeException === $nope.undefined) {
                    throw $exception0;
                } else {
                    if ($nope.builtins.isinstance($exception0.$nopeException, AssertionError)) {
                        var error = $exception0.$nopeException;
                        throw $exception0;
                    } else {
                        throw $exception0;
                    }
                }
            }
        """,
    )
예제 #4
0
 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")),
         ]),
     )
예제 #5
0
 def test_transform_boolean_or(self):
     _assert_transform(
         nodes.bool_or(nodes.ref("x"), nodes.ref("y")),
         cc.ternary_conditional(
             cc.call(cc.builtin("bool"), [cc.ref("x")]),
             cc.ref("x"),
             cc.ref("y")
         ),
     )
예제 #6
0
 def test_transform_call_with_positional_arguments(self):
     func_node = nodes.ref("f")
     type_lookup = [
         (func_node, types.func([types.str_type], types.none_type))
     ]
     _assert_transform(
         nodes.call(func_node, [nodes.ref("x")]),
         cc.call(cc.ref("f"), [cc.ref("x")]),
         type_lookup=type_lookup,
     )
예제 #7
0
 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"])),
         ])
     )
예제 #8
0
 def test_transform_while_loop(self):
     _assert_transform(
         nodes.while_(
             nodes.ref("x"),
             [nodes.ret(nodes.ref("y"))],
         ),
         cc.while_(
             cc.call(cc.builtin("bool"), [cc.ref("x")]),
             [cc.ret(cc.ref("y"))],
         )
     )
예제 #9
0
 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")),
         ]),
     )
예제 #10
0
 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"])),
         ])
     )
예제 #11
0
def test_transform_while_loop():
    _assert_transform(
        cc.while_(
            cc.ref("x"),
            [cc.ret(cc.ref("y"))],
        ),
        js.while_(
            js.ref("x"),
            [js.ret(js.ref("y"))],
        )
    )
예제 #12
0
 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")),
         ]),
     )
예제 #13
0
def test_transform_if_else():
    _assert_transform(
        cc.if_(
            cc.ref("x"),
            [cc.ret(cc.ref("y"))],
            [cc.ret(cc.ref("z"))],
        ),
        js.if_(
            js.ref("x"),
            [js.ret(js.ref("y"))],
            [js.ret(js.ref("z"))],
        )
    )
예제 #14
0
    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)))
예제 #15
0
 def test_statements_in_bodies_are_transformed(self):
     _assert_transform(
         nodes.try_(
             [nodes.ret(nodes.ref("x"))],
             handlers=[nodes.except_(nodes.ref("Exception"), nodes.ref("error"), [nodes.ref("y")])],
             finally_body=[nodes.ret(nodes.ref("z"))],
         ),
         cc.try_(
             [cc.ret(cc.ref("x"))],
             handlers=[cc.except_(cc.ref("Exception"), cc.ref("error"), [cc.ref("y")])],
             finally_body=[cc.ret(cc.ref("z"))],
         ),
     )
예제 #16
0
 def test_condition_is_transformed_using_bool_builtin(self):
     _assert_transform(
         nodes.if_(
             nodes.ref("x"),
             [nodes.ret(nodes.ref("y"))],
             [nodes.ret(nodes.ref("z"))],
         ),
         cc.if_(
             cc.call(cc.builtin("bool"), [cc.ref("x")]),
             [cc.ret(cc.ref("y"))],
             [cc.ret(cc.ref("z"))],
         )
     )
예제 #17
0
def test_transform_try_finally():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            finally_body=[cc.ret(cc.ref("y"))],
        ),
        """
            try {
                return x;
            } finally {
                return y;
            }
        """,
    )
예제 #18
0
 def test_generator_is_transformed_to_function_call_with_anonymous_function(self):
     _assert_transform(
         nodes.generator_expression(
             nodes.ref("y"),
             nodes.ref("x"),
             nodes.ref("xs")
         ),
         cc.call(
             cc.internal("generator_expression"),
             [
                 cc.function_expression([cc.arg("x")], [cc.ret(cc.ref("y"))]),
                 cc.ref("xs")
             ]
         )
     )
예제 #19
0
    def methods_are_set_as_members_on_object(self):
        node = cc.class_("A", methods=[
            cc.func("f", [cc.arg("self")], [
                cc.ret(cc.ref("self"))
            ])
        ], body=[])
        
        expected_aux = """internal class __A {
    internal dynamic f;
}
"""

        expected = """A = new
{
    __call__ = ((System.Func<dynamic>)(() =>
    {
        dynamic __self = null;
        __self = new __A {
            f = ((System.Func<dynamic>)(() =>
            {
                dynamic self = __self;
                return self;
            })),
        };
        return __self;
    })),
};
"""
        transformer = _create_transformer()
        assert_equal(expected, cs.dumps(transformer.transform(node)))
        assert_equal(expected_aux, cs.dumps(transformer.aux()))
예제 #20
0
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;
        """
    )
예제 #21
0
 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")),
         ])
     )
예제 #22
0
 def test_transform_call_with_keyword_arguments(self):
     func_node = nodes.ref("f")
     type_lookup = [
         (func_node, types.func(
             [
                 types.func_arg("first", types.str_type),
                 types.func_arg("second", types.str_type),
             ],
             types.none_type
         ))
     ]
     
     _assert_transform(
         nodes.call(func_node, [], {"first": nodes.ref("x"), "second": nodes.ref("y")}),
         cc.call(cc.ref("f"), [cc.ref("x"), cc.ref("y")]),
         type_lookup=type_lookup,
     )
예제 #23
0
 def test_condition_is_not_transformed_using_bool_builtin_if_already_a_bool(self):
     condition_node = nodes.ref("x")
     _assert_transform(
         nodes.if_(
             condition_node,
             [nodes.ret(nodes.ref("y"))],
             [nodes.ret(nodes.ref("z"))],
         ),
         cc.if_(
             cc.ref("x"),
             [cc.ret(cc.ref("y"))],
             [cc.ret(cc.ref("z"))],
         ),
         type_lookup=[
             (condition_node, types.bool_type),
         ],
     )
예제 #24
0
 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)]
     )
예제 #25
0
 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,
     )
예제 #26
0
def test_transform_try_with_empty_finally_body():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            finally_body=[],
        ),
        """
            return x;
        """,
    )
예제 #27
0
 def test_list_comprehension_is_transformed_as_with_generator_expression_but_wrapped_in_list_call(self):
     _assert_transform(
         nodes.list_comprehension(
             nodes.ref("y"),
             nodes.ref("x"),
             nodes.ref("xs")
         ),
         cc.call(
             cc.internal("iterator_to_list"),
             [
                 cc.call(
                     cc.internal("generator_expression"),
                     [
                         cc.function_expression([cc.arg("x")], [cc.ret(cc.ref("y"))]),
                         cc.ref("xs")
                     ]
                 )
             ]
         )
     )
예제 #28
0
    def type_of_nope_exception_is_checked_if_handler_has_exception_type(self):
        node = cc.try_(
            [],
            handlers=[
                cc.except_(cc.ref("Exception"), None, [
                    cc.ret(cc.ref("value"))
                ])
            ]
        )
        
        expected = """try {
} catch (__Nope.Internals.@__NopeException __exception) {
    if ((__Nope.Builtins.@isinstance(__exception.__Value, Exception)).__Value) {
        return value;
    } else {
        throw;
    }
}
"""
        assert_equal(expected, cs.dumps(transform(node)))
예제 #29
0
    def handlers_are_converted_to_if_statements_in_order(self):
        node = cc.try_(
            [],
            handlers=[
                cc.except_(cc.ref("AssertionError"), None, []),
                cc.except_(cc.ref("Exception"), None, []),
            ]
        )
        
        expected = """try {
} catch (__Nope.Internals.@__NopeException __exception) {
    if ((__Nope.Builtins.@isinstance(__exception.__Value, AssertionError)).__Value) {
    } else {
        if ((__Nope.Builtins.@isinstance(__exception.__Value, Exception)).__Value) {
        } else {
            throw;
        }
    }
}
"""
        assert_equal(expected, cs.dumps(transform(node)))
예제 #30
0
    def nope_exception_is_extracted_from_dotnet_exception_if_exception_is_named_in_catch(self):
        node = cc.try_(
            [],
            handlers=[
                cc.except_(cc.ref("Exception"), cc.ref("error"), [
                    cc.ret(cc.ref("error"))
                ])
            ]
        )
        
        expected = """try {
} catch (__Nope.Internals.@__NopeException __exception) {
    if ((__Nope.Builtins.@isinstance(__exception.__Value, Exception)).__Value) {
        error = __exception.__Value;
        return error;
    } else {
        throw;
    }
}
"""
        assert_equal(expected, cs.dumps(transform(node)))