-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ast_nodes.py
155 lines (100 loc) · 3.36 KB
/
test_ast_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from wac.ast.nodes import *
def test_single_expr_type_is_comparable():
a = SingleExprType(int)
b = SingleExprType(int)
assert a == b
def test_create_tuple_expr_type():
i = SingleExprType(int)
f = SingleExprType(float)
t = TupleExprType((i, f))
assert t.type == (i, f)
def test_create_empty_tuple_expr_type():
t = TupleExprType(())
assert t.type == ()
def test_compare_tuple_expr_type():
a = TupleExprType((SingleExprType(int),))
b = TupleExprType((SingleExprType(int),))
assert a == b
def test_create_int_expr():
i = IntExpr(1234)
assert isinstance(i, Expr)
assert isinstance(i, IntExpr)
assert i.rtype == SingleExprType(int)
assert i.value == 1234
def test_create_bool_expr():
b = BoolExpr(True)
assert isinstance(b, Expr)
assert isinstance(b, BoolExpr)
assert b.rtype == SingleExprType(bool)
assert b.value is True
def test_create_str_expr():
s = StrExpr("hello world")
assert isinstance(s, Expr)
assert isinstance(s, StrExpr)
assert s.rtype == SingleExprType(str)
assert s.value == "hello world"
def test_create_float_expr():
f = FloatExpr(3.14)
assert isinstance(f, Expr)
assert isinstance(f, FloatExpr)
assert f.rtype == SingleExprType(float)
assert f.value == 3.14
def test_create_identifier_expr():
i = IdentifierExpr(SingleExprType(int), "x")
assert isinstance(i, Expr)
assert isinstance(i, IdentifierExpr)
assert i.rtype == SingleExprType(int)
assert i.name == "x"
def test_create_tuple_expr():
i = IntExpr(1234)
f = FloatExpr(3.14)
t = TupleExpr.of(i, f)
assert isinstance(t, TupleExpr)
assert isinstance(t, Expr)
assert t.rtype == TupleExprType(
(SingleExprType(int), SingleExprType(float))
)
assert t.values == (i, f)
def test_create_binary_expr():
lhs = IntExpr(1)
rhs = IntExpr(2)
expr = BinaryExpr.of(lhs, BinaryExprOper.ADD, rhs)
assert isinstance(expr, Expr)
assert isinstance(expr, BinaryExpr)
assert expr.rtype == SingleExprType(int)
assert expr.lhs is lhs
assert expr.rhs is rhs
def test_create_binary_expr_auto_deduce_bool_type():
lhs = IntExpr(1)
rhs = IntExpr(2)
expr = BinaryExpr.of(lhs, BinaryExprOper.EQUALS, rhs)
assert expr.rtype == SingleExprType(bool)
assert expr.lhs is lhs
assert expr.rhs is rhs
def test_create_unary_expr():
rhs = IntExpr(1234)
expr = UnaryExpr.of(UnaryExprOper.NEGATIVE, rhs)
assert isinstance(expr, Expr)
assert isinstance(expr, UnaryExpr)
assert expr.rtype == SingleExprType(int)
assert expr.rhs is rhs
def test_create_unary_expr_auto_deduce_bool_type():
rhs = BoolExpr(False)
expr = UnaryExpr.of(UnaryExprOper.NOT, rhs)
assert expr.rtype == SingleExprType(bool)
assert expr.rhs is rhs
def test_create_var_def_stmt():
expr = IntExpr(1234)
var = VarDefStmtExpr("x", expr)
assert isinstance(var, Expr)
assert isinstance(var, VarDefStmtExpr)
assert var.name == "x"
assert var.expr is expr
def test_create_module_def_stmt():
var_def = VarDefStmtExpr("x", IntExpr(1234))
mod = ModuleDefStmtExpr(stmts=(var_def,), name="some_module")
assert isinstance(mod, Stmt)
assert isinstance(mod, Expr)
assert isinstance(mod, ModuleDefStmtExpr)
assert mod.name == "some_module"
assert mod.stmts == (var_def,)