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;
                        }
                    }
                }
            }
        """,
    )
def test_transform_raise_without_exception_value():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            handlers=[
                cc.except_(cc.ref("AssertionError"), cc.ref("error"), [cc.raise_()]),
            ],
        ),
        """
            try {
                return x;
            } catch ($exception0) {
                if ($exception0.$nopeException === $nope.undefined) {
                    throw $exception0;
                } else {
                    if ($nope.builtins.isinstance($exception0.$nopeException, AssertionError)) {
                        var error = $exception0.$nopeException;
                        throw $exception0;
                    } else {
                        throw $exception0;
                    }
                }
            }
        """,
    )
def test_transform_try_with_empty_finally_body():
    _assert_transform(
        cc.try_(
            [cc.ret(cc.ref("x"))],
            finally_body=[],
        ),
        """
            return x;
        """,
    )
    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)))
    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)))
Exemple #6
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_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 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)))
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;
                    }
                }
            }
        """,
    )