def test_serialize_try_catch(): node = js.try_( [js.ret(js.ref("x"))], "error", [js.ret(js.ref("y"))] ) assert_equal("try { return x; } catch (error) { return y; }", _dumps(node))
def test_serialize_if_else(): if_else = js.if_( js.ref("x"), [js.ret(js.ref("y"))], [js.ret(js.ref("z"))], ) assert_equal("if (x) { return y; } else { return z; }", _dumps(if_else))
def test_transform_if_else(): _assert_transform( cc.if_( cc.ref("x"), [cc.ret(cc.ref("y"))], [cc.ret(cc.ref("z"))], ), js.if_( js.ref("x"), [js.ret(js.ref("y"))], [js.ret(js.ref("z"))], ) )
def body_of_try_catch_are_indented(self): node = js.try_( [js.ret(js.ref("x"))], "error", [js.ret(js.ref("y"))], [js.ret(js.ref("z"))], ) expected = """try { return x; } catch (error) { return y; } finally { return z; } """ assert_equal(expected, self._dumps(node))
def test_transform_while_loop(): _assert_transform( cc.while_( cc.ref("x"), [cc.ret(cc.ref("y"))], ), js.while_( js.ref("x"), [js.ret(js.ref("y"))], ) )
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"))], ) )
def closing_brace_of_function_expression_is_on_same_line_as_closing_paren_of_call(self): node = js.call( js.ref("f"), [js.function_expression([], [js.ret(js.ref("x"))])] ) assert_equal("f(function() {\n return x;\n})", self._dumps(node))
def body_of_function_declaration_is_indented(self): node = js.function_declaration("f", [], [js.ret(js.ref("x"))]) assert_equal("function f() {\n return x;\n}\n", self._dumps(node))
def test_serialize_while_loop(): node = js.while_( js.ref("condition"), [js.ret(js.ref("value"))] ) assert_equal("while (condition) { return value; }", _dumps(node))
def test_serialize_try_finally(): node = js.try_( [js.ret(js.ref("x"))], finally_body=[js.ret(js.ref("z"))], ) assert_equal("try { return x; } finally { return z; }", _dumps(node))
def test_serialize_return_statement(): statement = js.ret(js.ref("x")) assert_equal("return x;", _dumps(statement))
def test_transform_function_expression_with_body(): _assert_transform( cc.function_expression([], [cc.ret(cc.ref("x"))]), js.function_expression([], [js.ret(js.ref("x"))]), )
def test_transform_return(): _assert_transform( cc.ret(cc.ref("x")), js.ret(js.ref("x")) )