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()))
Example #2
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")
         ),
     )
Example #3
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,
     )
Example #4
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")
                     ]
                 )
             ]
         )
     )
Example #5
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 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)))
Example #7
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"))],
         )
     )
Example #8
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")
             ]
         )
     )
Example #9
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,
     )
Example #10
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),
         ]),
     )
Example #11
0
def test_transform_call_with_positional_arguments():
    _assert_transform(
        cc.call(cc.ref("f"), [cc.ref("x"), cc.ref("y")]),
        js.call(js.ref("f"), [js.ref("x"), js.ref("y")]),
    )
Example #12
0
 def test_transform(self):
     _assert_transform(
         nodes.slice(nodes.ref("x"), nodes.ref("y"), nodes.none()),
         cc.call(cc.builtin("slice"), [cc.ref("x"), cc.ref("y"), cc.none]),
     )
Example #13
0
 def test_transform_getitem(self):
     _assert_transform(
         nodes.subscript(nodes.ref("x"), nodes.ref("y")),
         cc.call(cc.attr(cc.ref("x"), "__getitem__"), [cc.ref("y")])
     )
Example #14
0
 def test_transform_boolean_not(self):
     _assert_transform(
         nodes.bool_not(nodes.ref("x")),
         cc.not_(cc.call(cc.builtin("bool"), [cc.ref("x")])),
     )
Example #15
0
 def test_transform_unary_operation_is_converted_to_call_on_class(self):
     _assert_transform(
         nodes.neg(nodes.ref("x")),
         cc.call(cc.attr(cc.ref("x"), "__neg__"), [])
     )
Example #16
0
 def test_transform_binary_operation_is_converted_to_call_on_class(self):
     _assert_transform(
         nodes.add(nodes.ref("x"), nodes.ref("y")),
         cc.call(cc.attr(cc.ref("x"), "__add__"), [cc.ref("y")])
     )