-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.py
220 lines (192 loc) · 6.68 KB
/
parser_test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import textwrap
import os
import unittest
import parser
import identifier
import math_operations
write_golden_files = False
class TestParser(unittest.TestCase):
def test_empty(self):
self.assertEqual(parser.parse(""), parser.Queue([]))
def test_identifiers(self):
accepted = identifier.identifiers
for ident in accepted:
with self.subTest(ident):
actual = parser.parse(ident)
expected = parser.Queue([parser.Identifier(ident)])
self.assertEqual(actual, expected)
def test_unknown_identifier(self):
with self.assertRaises(parser.VisitationError):
parser.parse("func")
def test_unparseable_identifier(self):
with self.assertRaises(parser.IncompleteParseError):
parser.parse("123abc")
def test_strings(self):
accepted = {
'"Hello, world!"': "Hello, world!",
'""': "",
# r'"backslash-escaped quote \" characters"': 'backslash-escaped quote " characters',
r'"\b\f\n\r\t\v\\"': "\b\f\n\r\t\v\\",
r'"\""': '"',
}
for source, contents in accepted.items():
with self.subTest(source):
actual = parser.parse(source)
expected = parser.Queue([parser.String(contents)])
self.assertEqual(actual, expected)
rejected = ['"unterminated string']
for string in rejected:
with self.subTest(string):
with self.assertRaises(parser.IncompleteParseError):
parser.parse(string)
def test_numbers(self):
accepted = ["0", "1", "123", "-1", "-0"]
for number in accepted:
with self.subTest(number):
actual = parser.parse(number)
expected = parser.Queue([parser.Number(int(number))])
self.assertEqual(actual, expected)
rejected = ["0.", ".0"]
for number in rejected:
with self.assertRaises(parser.IncompleteParseError):
parser.parse(number)
floats = ["123.321", "12.0", "-123.321", "-12.0"]
for number in floats:
with self.assertRaises(parser.VisitationError):
parser.parse(number)
def test_simple_math_1(self):
source = textwrap.dedent(
"""\
1
1
+
3
-
"""
)
actual = parser.parse(source)
expected = parser.Queue(
[
parser.Number(1),
parser.Number(1),
math_operations.Add(),
parser.Number(3),
math_operations.Sub(),
]
)
self.assertEqual(actual, expected)
def test_simple_math_2(self):
source = textwrap.dedent(
"""\
1
1
3
# [ 1 1 3 ]
+
# [ 3 2 ]
-
# [ 1 ]
"""
)
actual = parser.parse(source)
expected = parser.Queue(
[
parser.Number(1),
parser.Number(1),
parser.Number(3),
parser.Identifier("+"),
parser.Identifier("-"),
]
)
self.assertEqual(actual, expected)
def test_single_number(self):
actual = parser.parse("1")
expected = parser.Queue([parser.Number(1)])
self.assertEqual(actual, expected)
def test_comment(self):
source = textwrap.dedent(
"""\
1 print # I am a comment
"""
)
actual = parser.parse(source)
expected = parser.Queue([parser.Number(1), parser.Identifier("print")])
self.assertEqual(actual, expected)
def test_comments_are_not_identifiers(self):
actual = parser.parse("# I am a comment")
expected = parser.Queue([])
self.assertEqual(actual, expected)
def test_block(self):
actual = parser.parse(textwrap.dedent("""\
[ 1 2 "a" ]
"""))
expected = parser.Queue([
parser.Block([
parser.Number(1.0),
parser.Number(2.0),
parser.String("a"),
])
])
self.assertEqual(actual, expected)
def test_empty_block(self):
actual = parser.parse("[ ]")
expected = parser.Queue([
parser.Block([])
])
self.assertEqual(actual, expected)
def test_blocks_need_spaces(self):
with self.assertRaises(parser.IncompleteParseError):
parser.parse("[a]")
actual = parser.parse("[ 1 ]")
expected = parser.Queue([parser.Block([parser.Number(1.0)])])
self.assertEqual(actual, expected)
def test_nested_block(self):
actual = parser.parse(textwrap.dedent("""\
[
1 2 "a"
[ "b" ]
]
"""))
expected = parser.Queue([
parser.Block([
parser.Number(1.0),
parser.Number(2.0),
parser.String("a"),
parser.Block([
parser.String("b"),
]),
]),
])
self.assertEqual(actual, expected)
def sample_programs(self):
input_dir = os.path.join(os.path.dirname(__file__), 'test_programs')
output_dir = os.path.join(os.path.dirname(__file__), 'test_parses')
pairs = []
for filename in os.listdir(input_dir):
base, _ = os.path.splitext(filename)
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, base + '.gold')
pairs.append((base, input_path, output_path))
return pairs
@unittest.skipIf(write_golden_files, 'writing golden files')
def test_sample_programs(self):
for base, input_path, output_path in self.sample_programs():
with self.subTest(base):
with open(input_path) as inp:
source = inp.read()
with open(output_path) as out:
expected = out.read()
asq = parser.parse(source)
actual = str(asq)
self.assertEqual(actual, expected)
@unittest.skipIf(not write_golden_files, 'not writing golden files')
def test_write_golden_files(self):
for base, input_path, output_path in self.sample_programs():
with self.subTest(base):
with open(input_path) as inp:
source = inp.read()
asq = parser.parse(source)
with open(output_path, 'w') as out:
out.write(str(asq))
if __name__ == "__main__":
unittest.main()