示例#1
0
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;
            };
        """
    )
示例#2
0
def test_transform_class_with_init_method():
    class_node = cc.class_(
        name="User",
        methods=[
            cc.func(
                "__init__",
                [cc.arg("self"), cc.arg("x")],
                [],
            )
        ],
        body=[],
    )
    _assert_transform(
        class_node,
        """
            function $__init__0(self, x) {
            }
            User = function($arg1) {
                var $self2 = {
                    "$nopeType": User
                };
                $__init__0($self2, $arg1);
                return $self2;
            };
        """,
    )
示例#3
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()))
示例#4
0
    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())
示例#5
0
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"))],
        )
    )
示例#6
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()))
示例#7
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")
             ]
         )
     )
示例#8
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")),
         ])
     )
示例#9
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")
                     ]
                 )
             ]
         )
     )
示例#10
0
def test_transform_function_expression_with_arguments():
    _assert_transform(
        cc.function_expression([cc.arg("x"), cc.arg("y")], []),
        js.function_expression(["x", "y"], []),
    )
示例#11
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"))]),
     )