Example #1
0
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))
Example #2
0
 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))
Example #3
0
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))
Example #4
0
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))
Example #5
0
 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))
Example #6
0
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"))],
        )
    )
Example #7
0
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"))],
        )
    )
Example #8
0
    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))
Example #9
0
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"))],
        )
    )
Example #10
0
def test_serialize_call_with_multiple_args():
    assert_equal("f(x, y)", _dumps(js.call(js.ref("f"), [js.ref("x"), js.ref("y")])))
Example #11
0
def test_serialize_property_access_with_dot_notation():
    assert_equal("x.y", _dumps(js.property_access(js.ref("x"), "y")))
Example #12
0
def test_serialize_call_with_no_args():
    assert_equal("f()", _dumps(js.call(js.ref("f"), [])))
Example #13
0
def test_serialize_call_with_one_arg():
    assert_equal("f(x)", _dumps(js.call(js.ref("f"), [js.ref("x")])))
Example #14
0
def test_serialize_var_declaration_with_immediate_assignment():
    statement = js.var("x", js.ref("y"))
    assert_equal("var x = y;", _dumps(statement))
Example #15
0
 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))
Example #16
0
def test_serialize_unary_operation():
    assert_equal("-x", _dumps(js.unary_operation("-", js.ref("x"))))
Example #17
0
def test_serialize_assignment():
    assignment = js.assign("x", js.ref("y"))
    assert_equal("x = y", _dumps(assignment))
Example #18
0
def test_serialize_return_statement():
    statement = js.ret(js.ref("x"))
    assert_equal("return x;", _dumps(statement))
Example #19
0
def test_serialize_property_access_with_subscript_notation():
    assert_equal("x[y]", _dumps(js.property_access(js.ref("x"), js.ref("y"))))
Example #20
0
 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))
Example #21
0
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"))))
Example #22
0
def test_serialize_throw():
    node = js.throw(js.ref("error"))
    assert_equal("throw error;", _dumps(node))
Example #23
0
def test_serialize_while_loop():
    node = js.while_(
        js.ref("condition"),
        [js.ret(js.ref("value"))]
    )
    assert_equal("while (condition) { return value; }", _dumps(node))
Example #24
0
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))
Example #25
0
def test_serialize_binary_operation():
    assert_equal("x + y", _dumps(js.binary_operation("+", js.ref("x"), js.ref("y"))))
Example #26
0
 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))
Example #27
0
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))
Example #28
0
def test_serialize_variable_reference():
    assert_equal("flynn", _dumps(js.ref("flynn")))
Example #29
0
def test_serialize_expression_statement():
    assert_equal("x;", _dumps(js.expression_statement(js.ref("x"))))
Example #30
0
def test_serialize_statements():
    assert_equal("x;y;", _dumps(js.statements([
        js.expression_statement(js.ref("x")),
        js.expression_statement(js.ref("y")),
    ])))