Ejemplo n.º 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()))
Ejemplo n.º 2
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()))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 7
0
 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)))
Ejemplo n.º 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))
Ejemplo n.º 9
0
 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)))
Ejemplo n.º 10
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())
Ejemplo n.º 11
0
    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())
Ejemplo n.º 12
0
    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()))
Ejemplo n.º 13
0
    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)))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 17
0
    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)))
Ejemplo n.º 18
0
    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)))
Ejemplo n.º 19
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)))
Ejemplo n.º 20
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)))
Ejemplo n.º 21
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)))
Ejemplo n.º 22
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)))
Ejemplo n.º 23
0
    def arguments_have_dynamic_type(self):
        node = cs.lambda_([cs.arg("x")], [])
        expected = """((dynamic x) =>
{
})"""
        assert_equal(expected, cs.dumps(node))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 27
0
    def field_is_internal_and_dynamic(self):
        node = cs.field("X")
        expected = """internal dynamic X;
"""
        assert_equal(expected, cs.dumps(node))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 30
0
    def method_has_internal_visibility(self):
        node = cs.method("f", [], [])
        expected = """internal dynamic f() {
}
"""
        assert_equal(expected, cs.dumps(node))