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}"
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}"
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)
def c(v): return Int(Token(TokenTypes.INT, v))
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}"
def test_block_str(): node = Block([Assign(t("="), Var(t("a")), Int(t("1")))]) assert str(node) == "{\n" + TAB + "a = 1\n}"
def test_assign_str(): node = Assign(t("="), Var(t("a")), Int(t("1"))) assert str(node) == "a = 1"
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"
def test_binaryop_str(): node = BinaryOp(t("+"), Int(t("1")), Var(t("a"))) assert str(node) == "1 + a"
def test_int_str(): node = Int(t("1")) s = str(node) assert s == "1" assert s == repr(node)
def test_ast_eq_same_type_diff_attr(): c1 = Int(t("1")) c2 = Int(t("1")) c2.left = "bla" assert c1 != c2
def test_ast_eq_dif_types(): assert Int(t("1")) != Var(t("1"))
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