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 bodies_of_if_else_are_indented(self): node = js.if_( js.ref("condition"), [js.expression_statement(js.ref("y"))], [js.expression_statement(js.ref("x"))] ) assert_equal("if (condition) {\n y;\n} else {\n x;\n}\n", self._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_serialize_function_expression(): func = js.function_expression( args=["x", "y"], body = [ js.expression_statement(js.ref("y")), js.expression_statement(js.ref("x")), ], ) assert_equal("function(x, y) { y;x; }", _dumps(func))
def body_of_while_is_indented(self): node = js.while_( js.ref("condition"), [ js.expression_statement(js.ref("y")), js.expression_statement(js.ref("x")), ] ) assert_equal("while (condition) {\n y;\n x;\n}\n", 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_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_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 test_serialize_call_with_multiple_args(): assert_equal("f(x, y)", _dumps(js.call(js.ref("f"), [js.ref("x"), js.ref("y")])))
def test_serialize_property_access_with_dot_notation(): assert_equal("x.y", _dumps(js.property_access(js.ref("x"), "y")))
def test_serialize_call_with_no_args(): assert_equal("f()", _dumps(js.call(js.ref("f"), [])))
def test_serialize_call_with_one_arg(): assert_equal("f(x)", _dumps(js.call(js.ref("f"), [js.ref("x")])))
def test_serialize_var_declaration_with_immediate_assignment(): statement = js.var("x", js.ref("y")) assert_equal("var x = y;", _dumps(statement))
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 test_serialize_unary_operation(): assert_equal("-x", _dumps(js.unary_operation("-", js.ref("x"))))
def test_serialize_assignment(): assignment = js.assign("x", js.ref("y")) assert_equal("x = y", _dumps(assignment))
def test_serialize_return_statement(): statement = js.ret(js.ref("x")) assert_equal("return x;", _dumps(statement))
def test_serialize_property_access_with_subscript_notation(): assert_equal("x[y]", _dumps(js.property_access(js.ref("x"), js.ref("y"))))
def statements_are_separated_by_new_lines(self): statements = js.statements([ js.expression_statement(js.ref("y")), js.expression_statement(js.ref("x")), ]) assert_equal("y;\nx;\n", self._dumps(statements))
def test_parens_are_not_used_if_high_precedence_operation_is_inside_low_precedence_operation(): assert_equal("x.y || z", _dumps(js.or_(js.property_access(js.ref("x"), "y"), js.ref("z"))))
def test_serialize_throw(): node = js.throw(js.ref("error")) assert_equal("throw error;", _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_binary_operation(): assert_equal("x + y", _dumps(js.binary_operation("+", js.ref("x"), js.ref("y"))))
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_ternary_conditional_operation(): conditional = js.ternary_conditional(js.ref("x"), js.ref("y"), js.ref("z")) assert_equal("x ? y : z", _dumps(conditional))
def test_serialize_variable_reference(): assert_equal("flynn", _dumps(js.ref("flynn")))
def test_serialize_expression_statement(): assert_equal("x;", _dumps(js.expression_statement(js.ref("x"))))
def test_serialize_statements(): assert_equal("x;y;", _dumps(js.statements([ js.expression_statement(js.ref("x")), js.expression_statement(js.ref("y")), ])))