-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_js_ast.py
155 lines (104 loc) · 4.27 KB
/
test_js_ast.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 unittest import TestCase
from js_ast import *
class JavascriptASTTest(TestCase):
def test_module(self):
module = Module(["test_statement1", "test_statement2"])
self.assertEqual(str(module), "test_statement1;\ntest_statement2;\n")
def test_function(self):
function = Function(["arg0", "arg1"], ["body0", "body1"])
self.assertEqual(str(function), "function (arg0, arg1) {\n body0\n body1\n}")
def test_function_no_args(self):
function = Function([], ["body0", "body1"])
self.assertEqual(str(function), "function () {\n body0\n body1\n}")
def test_return(self):
return_node = Return("1")
self.assertEqual(str(return_node), "return 1")
def test_declare_var(self):
declaration = DeclareVar(["a", "b", "c"])
self.assertEqual(str(declaration), "var a, b, c")
def test_assign(self):
assign = Assign("x", 1)
self.assertEqual(str(assign), "x = 1")
def test_aug_assign(self):
op = BinOp("+")
aug_assign = AugAssign("x", op, 1)
self.assertEqual(str(aug_assign), "x += 1")
def test_print_empty(self):
print_node = Print("")
self.assertEqual(str(print_node), "console.log()")
def test_print_int(self):
print_node = Print("1")
self.assertEqual(str(print_node), "console.log(1)")
def test_print_list(self):
print_node = Print("[1, 2, 3, 4]")
self.assertEqual(str(print_node), "console.log([1, 2, 3, 4])")
def test_for(self):
for_node = For("i", "iterable", ["func()"])
self.assertEqual(str(for_node), "for (i in iterable) {\n func()\n}")
def test_while(self):
while_node = While("a=b", ["func()"])
self.assertEqual(str(while_node), "while (a=b) {\n func()\n}")
def test_if(self):
if_node = If("a=b", ["func()"], ["func()"])
self.assertEqual(str(if_node), "if (a=b) {\n func()\n} else {\n func()\n}")
def test_bool(self):
op = BoolOp("&&")
bool_node = Bool(op, ["True", "False"])
self.assertEqual(str(bool_node), "True && False")
op = BoolOp("||")
bool_node = Bool(op, ["True", "False"])
self.assertEqual(str(bool_node), "True || False")
def test_bin(self):
op = BinOp("+")
bin_node = Bin(op, "4", "5")
self.assertEqual(str(bin_node), "(4 + 5)")
op = BinOp("-")
bin_node = Bin(op, "4", "5")
self.assertEqual(str(bin_node), "(4 - 5)")
def test_unary(self):
op = UnaryOp("!")
unary_node = Unary(op, "True")
self.assertEqual(str(unary_node), "!True")
op = UnaryOp("~")
unary_node = Unary(op, "1 == 1")
self.assertEqual(str(unary_node), "~(1 == 1)")
def test_empty_dict(self):
dict_node = Dict([], [])
self.assertEqual(str(dict_node), "{}")
def test_dict(self):
dict_node = Dict(["key0", "key1"], ["val0", "val1"])
self.assertEqual(str(dict_node), "{key0:val0, key1:val1}")
def test_compare(self):
op = CompareOp(">")
compare_node = Compare(op, "1", "2")
self.assertEqual(str(compare_node), "1 > 2")
op = CompareOp("==")
compare_node = Compare(op, "1", "2")
self.assertEqual(str(compare_node), "1 == 2")
def test_call(self):
call_node = Call("func", ["asdf", "asdf"])
self.assertEqual(str(call_node), "func (asdf, asdf)")
def test_num(self):
num_node = Num(9)
self.assertEqual(str(num_node), "9")
def test_str(self):
str_node = Str("asdf")
self.assertEqual(str(str_node), '"asdf"')
def test_attribute(self):
name = Name("a")
attr1 = Attribute(name, "b")
attr2 = Attribute(attr1, "c")
self.assertEqual(str(attr2), "a.b.c")
def test_subscript(self):
name = Name("a")
subscript_node = Subscript(name, 1)
self.assertEqual(str(subscript_node), "a[1]")
def test_name(self):
name_node = Name("a")
self.assertEqual(str(name_node), "a")
def test_empty_list(self):
list_node = List([])
self.assertEqual(str(list_node), "[]")
def test_list(self):
list_node = List(["a", "b", "c"])
self.assertEqual(str(list_node), "[a, b, c]")