示例#1
0
def test_for_loop_str():
    node = ForLoop(
        Assign(t("="), Var(t("i")), Int(t("0"))),
        BinaryOp(t("<"), Var(t("i")), Int(t("10"))),
        Assign(t("="), Var(t("i")), BinaryOp(t("+"), Var(t("i")),
                                             Int(t("1")))),
        Block([Assign(t("="), Var(t("a")), Int(t("1")))]))
    assert str(node) == "for i = 0; i < 10; i = i + 1 {\n" + TAB + "a = 1\n}"
示例#2
0
def test_control_block_str():
    node = ControlBlock([
        If(BinaryOp(t("<"), Int(t("1")), Var(t("a"))),
           Block([Assign(t("="), Var(t("a")), Int(t("1")))])),
        If(BinaryOp(t("<"), Int(t("1")), Var(t("a"))),
           Block([Assign(t("="), Var(t("a")), Int(t("1")))]))
    ], Block([Assign(t("="), Var(t("a")), Int(t("1")))]))
    assert str(
        node
    ) == "if 1 < a {\n" + TAB + "a = 1\n} elif 1 < a {\n" + TAB + "a = 1\n} else {\n" + TAB + "a = 1\n}"
示例#3
0
def test_array_str():
    node = Array([Int(t("1"))])
    s = str(node)
    assert s == "[1]"
    assert s == repr(node)

    node = Array([Int(t("1")), Int(t("2")), Int(t("3"))])
    s = str(node)
    assert s == "[1, 2, 3]"
    assert s == repr(node)

    node = Array([
        Int(t("1")),
        Int(t("2")),
        Int(t("3")),
        Array([Int(t("1")), Int(t("2")), Int(t("3"))])
    ])
    s = str(node)
    assert s == "[1, 2, 3, [1, 2, 3]]"
    assert s == repr(node)
示例#4
0
def c(v):
    return Int(Token(TokenTypes.INT, v))
示例#5
0
def test_if_str():
    node = If(BinaryOp(t("<"), Int(t("1")), Var(t("a"))),
              Block([Assign(t("="), Var(t("a")), Int(t("1")))]))
    assert str(node) == "if 1 < a {\n" + TAB + "a = 1\n}"
示例#6
0
def test_block_str():
    node = Block([Assign(t("="), Var(t("a")), Int(t("1")))])
    assert str(node) == "{\n" + TAB + "a = 1\n}"
示例#7
0
def test_assign_str():
    node = Assign(t("="), Var(t("a")), Int(t("1")))
    assert str(node) == "a = 1"
示例#8
0
def test_binaryop_complex_str():
    node = BinaryOp(t("+"), BinaryOp(t("+"), Int(t("1")), Var(t("a"))),
                    Int(t("1")))
    assert str(node) == "(1 + a) + 1"
示例#9
0
def test_binaryop_str():
    node = BinaryOp(t("+"), Int(t("1")), Var(t("a")))
    assert str(node) == "1 + a"
示例#10
0
def test_int_str():
    node = Int(t("1"))
    s = str(node)
    assert s == "1"
    assert s == repr(node)
示例#11
0
def test_ast_eq_same_type_diff_attr():
    c1 = Int(t("1"))
    c2 = Int(t("1"))
    c2.left = "bla"
    assert c1 != c2
示例#12
0
def test_ast_eq_dif_types():
    assert Int(t("1")) != Var(t("1"))
示例#13
0
def test_ast_eq_same_type_same_attr_diff_value():
    c1 = Int(t("1"))
    c2 = Int(t("1"))
    c1.left = "bla"
    c2.left = "bla1"
    assert c1 != c2