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 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()))
Exemple #3
0
    def method_has_body(self):
        node = cs.method("f", [], [cs.ret(cs.ref("x"))])
        expected = """internal dynamic f() {
    return x;
}
"""
        assert_equal(expected, cs.dumps(node))
Exemple #4
0
    def lambda_without_arguments_has_body_serialized(self):
        node = cs.lambda_([], [cs.ret(cs.ref("x"))])
        expected = """(() =>
{
    return x;
})"""
        assert_equal(expected, cs.dumps(node))
Exemple #5
0
    def uses_braced_list_for_member_setters(self):
        node = cs.new(cs.ref("A"), [], [("X", cs.ref("x")), ("Y", cs.ref("y"))])
        expected = """new A {
    X = x,
    Y = y,
}"""
        assert_equal(expected, cs.dumps(node))
Exemple #6
0
    def class_body_is_written_out(self):
        node = cs.class_("A", [cs.method("Main", [], [])])
        expected = """internal class A {
    internal dynamic Main() {
    }
}
"""
        assert_equal(expected, cs.dumps(node))
 def path_is_normalised_before_hashing(self):
     module_resolver = FakeModuleResolver({
         ("os", "path"): LocalModule("x/../blah.py", None),
     })
     node = cc.module_ref(["os", "path"])
     
     expected = """__Nope.Internals.@Import(Module__blah_py.Init)"""
     assert_equal(expected, cs.dumps(transform(node, module_resolver=module_resolver)))
Exemple #8
0
    def object_members_are_assigned_with_equals_sign(self):
        node = cs.obj([("X", cs.ref("x")), ("Y", cs.ref("y"))])
        expected = """new
{
    X = x,
    Y = y,
}"""
        assert_equal(expected, cs.dumps(node))
 def absolute_module_reference_is_translated_to_get_module_with_reference_to_module_class(self):
     module_resolver = FakeModuleResolver({
         ("os", "path"): LocalModule("blah.py", None),
     })
     node = cc.module_ref(["os", "path"])
     
     expected = """__Nope.Internals.@Import(Module__blah_py.Init)"""
     assert_equal(expected, cs.dumps(transform(node, module_resolver=module_resolver)))
    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())
    def class_definition_creates_object_with_call_method_for_init(self):
        node = cc.class_("A", methods=[], body=[])
        
        expected_aux = """internal class __A {
}
"""
        
        expected = """A = new
{
    __call__ = ((System.Func<dynamic>)(() =>
    {
        dynamic __self = null;
        __self = new __A();
        return __self;
    })),
};
"""
        transformer = _create_transformer()
        assert_equal(expected, cs.dumps(transformer.transform(node)))
        assert_equal(expected_aux, cs.dumps(transformer.aux()))
    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)))
Exemple #14
0
    def test_serialize_try_with_finally_body(self):
        node = cs.try_(
            [cs.ret(cs.ref("x"))],
            finally_body=[cs.expression_statement(cs.ref("y"))],
        )
        expected = """try {
    return x;
} finally {
    y;
}
"""
        assert_equal(expected, cs.dumps(node))
Exemple #15
0
    def test_serialize_try_with_filtered_catch_with_name(self):
        node = cs.try_(
            [cs.ret(cs.ref("x"))],
            handlers=[cs.catch(cs.ref("Exception"), "exception", [cs.expression_statement(cs.ref("y"))])],
        )
        expected = """try {
    return x;
} catch (Exception exception) {
    y;
}
"""
        assert_equal(expected, cs.dumps(node))
Exemple #16
0
    def test_serialize_try_with_unfiltered_catch(self):
        node = cs.try_(
            [cs.ret(cs.ref("x"))],
            handlers=[cs.catch(None, None, [cs.expression_statement(cs.ref("y"))])],
        )
        expected = """try {
    return x;
} catch {
    y;
}
"""
        assert_equal(expected, cs.dumps(node))
    def non_executable_module_adds_value_to_modules_object(self):
        node = cc.module([], is_executable=False, exported_names=["x"])
        module = LocalModule("blah.py", node)
        
        expected = """internal class Module__blah_py {
    internal static dynamic Init() {
        dynamic __module = new System.Dynamic.ExpandoObject();
        __module.x = x;
        return __module;
    }
}
"""
        assert_equal(expected, cs.dumps(transform(module)))
    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 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 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)))
Exemple #23
0
    def arguments_have_dynamic_type(self):
        node = cs.lambda_([cs.arg("x")], [])
        expected = """((dynamic x) =>
{
})"""
        assert_equal(expected, cs.dumps(node))
Exemple #24
0
 def variable_with_initial_value_is_declared_as_dynamic_with_initial_value(self):
     node = cs.declare("x", cs.null)
     expected = """dynamic x = null;\n"""
     assert_equal(expected, cs.dumps(node))
Exemple #25
0
 def variable_without_initial_value_is_declared_as_dynamic(self):
     node = cs.declare("x", None)
     expected = """dynamic x;\n"""
     assert_equal(expected, cs.dumps(node))
Exemple #26
0
    def method_can_be_set_as_ststic(self):
        node = cs.method("f", [], [], static=True)
        expected = """internal static dynamic f() {
}
"""
        assert_equal(expected, cs.dumps(node))
Exemple #27
0
    def field_is_internal_and_dynamic(self):
        node = cs.field("X")
        expected = """internal dynamic X;
"""
        assert_equal(expected, cs.dumps(node))
Exemple #28
0
    def method_return_type_can_be_overridden(self):
        node = cs.method("f", [], [], returns=cs.void)
        expected = """internal void f() {
}
"""
        assert_equal(expected, cs.dumps(node))
Exemple #29
0
    def method_has_dynamic_arguments(self):
        node = cs.method("f", [cs.arg("x"), cs.arg("y")], [])
        expected = """internal dynamic f(dynamic x, dynamic y) {
}
"""
        assert_equal(expected, cs.dumps(node))
Exemple #30
0
    def method_has_internal_visibility(self):
        node = cs.method("f", [], [])
        expected = """internal dynamic f() {
}
"""
        assert_equal(expected, cs.dumps(node))