def test_transform_try_except_with_exception_type_and_name():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            handlers=[
                cc.except_(cc.ref("AssertionError"), cc.ref("error"), [cc.ret(cc.ref("y"))]),
            ],
        ),
        """
            try {
                return x;
            } catch ($exception0) {
                if ($exception0.$nopeException === $nope.undefined) {
                    throw $exception0;
                } else {
                    if ($nope.builtins.isinstance($exception0.$nopeException, AssertionError)) {
                        var error = $exception0.$nopeException;
                        return y;
                    } else {
                        throw $exception0;
                    }
                }
            }
        """,
    )
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;
                        }
                    }
                }
            }
        """,
    )
Beispiel #3
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"))],
         )
     )
Beispiel #4
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"))],
         ),
     )
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"))],
        )
    )
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;
            }
        """,
    )
def test_transform_class_with_methods():
    _assert_transform(
        cc.class_(
            name="User",
            methods=[
                cc.func(
                    "f",
                    [cc.arg("self"), cc.arg("x")],
                    [cc.ret(cc.none)],
                )
            ],
            body=[]
        ),
        """
            function $f0(self, x) {
                return null;
            }
            User = function() {
                var $self1 = {
                    "$nopeType": User
                };
                $self1.f = $nope.instanceAttribute($self1, $f0);
                return $self1;
            };
        """
    )
    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()))
Beispiel #9
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),
         ],
     )
    def function_with_multiple_arguments_is_converted_to_csharp_lambda_assignment_with_dynamic_args(self):
        node = cc.func("f", [cc.arg("x"), cc.arg("y")], [cc.ret(cc.none)])
        
        expected = """
f = ((System.Func<dynamic, dynamic, dynamic>)((dynamic x, dynamic y) =>
{
    return __NopeNone.Value;
}));"""
        assert_equal(expected.strip(), cs.dumps(transform(node)).strip())
    def function_with_zero_arguments_is_converted_to_csharp_lambda_assignment(self):
        node = cc.func("f", [], [cc.ret(cc.none)])
        
        expected = """
f = ((System.Func<dynamic>)(() =>
{
    return __NopeNone.Value;
}));"""
        assert_equal(expected.strip(), cs.dumps(transform(node)).strip())
Beispiel #12
0
 def test_function_definitions_in_body_are_stored_as_methods(self):
     _assert_transform(
         nodes.class_("Blah", [nodes.func("f", nodes.args([]), [], type=None)]),
         cc.class_(
             "Blah",
             methods=[cc.func("f", [], [cc.ret(cc.none)])],
             body=[cc.declare("f")],
         ),
     )
def test_transform_try_with_empty_finally_body():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            finally_body=[],
        ),
        """
            return x;
        """,
    )
Beispiel #14
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"))],
         )
     )
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"))],
        )
    )
Beispiel #16
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")),
         ]),
     )
Beispiel #17
0
 def test_function_without_explicit_return_on_all_paths_returns_none_at_end(self):
     _assert_transform(
         nodes.func(
             name="f",
             args=nodes.args([]),
             body=[
                 nodes.if_(
                     nodes.ref("x"),
                     [nodes.ret(nodes.bool_literal(True))],
                     []
                 ),
             ],
             type=None
         ),
         cc.func("f", [], [
             cc.if_(
                 cc.call(cc.builtin("bool"), [cc.ref("x")]),
                 [cc.ret(cc.true)],
             ),
             cc.ret(cc.none),
         ]),
     )
    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_function_declaration():
    _assert_transform(
        cc.func(
            name="f",
            args=[cc.arg("x"), cc.arg("y")],
            body=[cc.ret(cc.ref("x"))],
        ),
        js.function_declaration(
            name="f",
            args=["x", "y"],
            body=[js.ret(js.ref("x"))],
        )
    )
Beispiel #20
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")
             ]
         )
     )
def test_transform_try_except_with_no_name():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            handlers=[
                cc.except_(None, None, [cc.ret(cc.ref("y"))]),
            ],
        ),
        """
            try {
                return x;
            } catch ($exception0) {
                if ($exception0.$nopeException === $nope.undefined) {
                    throw $exception0;
                } else {
                    if ($nope.builtins.isinstance($exception0.$nopeException, $nope.builtins.Exception)) {
                        return y;
                    } else {
                        throw $exception0;
                    }
                }
            }
        """,
    )
Beispiel #22
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")),
         ])
     )
Beispiel #23
0
 def test_does_not_redeclare_variables_with_same_name_as_type_parameter(self):
     _assert_transform(
         nodes.func(
             name="f",
             args=nodes.args([]),
             body=[],
             type=nodes.signature(
                 type_params=[nodes.formal_type_parameter("T")],
                 args=[],
                 returns=nodes.ref("T"),
             ),
         ),
         cc.func("f", [], [
             cc.ret(cc.none),
         ])
     )
Beispiel #24
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")
                     ]
                 )
             ]
         )
     )
    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)))
    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)))
def test_transform_function_expression_with_body():
    _assert_transform(
        cc.function_expression([], [cc.ret(cc.ref("x"))]),
        js.function_expression([], [js.ret(js.ref("x"))]),
    )
Beispiel #28
0
 def test_arguments_are_transformed(self):
     _assert_transform(
         nodes.func("f", nodes.args([nodes.arg("value")]), [nodes.ret(nodes.ref("value"))], type=None),
         cc.func("f", [cc.arg("value")], [cc.ret(cc.ref("value"))]),
     )
Beispiel #29
0
 def test_transform_return_statement_transforms_value(self):
     _assert_transform(
         nodes.ret(nodes.ref("value")),
         cc.ret(cc.ref("value"))
     )
def test_transform_return():
    _assert_transform(
        cc.ret(cc.ref("x")),
        js.ret(js.ref("x"))
    )