-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
215 lines (153 loc) · 4.71 KB
/
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
from peg import *
import unittest
class ParseTest(unittest.TestCase):
def assertParse(self, parser, value, expected_result, position):
for result, pos in parser(value, 0):
if result == expected_result:
if pos == position:
pass
else:
self.fail("Parser stopped at %s instead of %s" % (pos, position))
else:
self.fail("Parser yielded %s instead of %s" % (result, expected_result))
break
def assertFail(self, parser, value):
for result, pos in parser(value, 0):
self.fail("Parser is expected to fail")
class ElementTest(ParseTest):
def test_element(self):
self.assertParse(element, [42], 42, 1)
def test_no_element(self):
self.assertFail(element, [])
class ReturnTest(ParseTest):
def test_return(self):
self.assertParse(Return(42), [], 42, 0)
class MonadTest(ParseTest):
def test_right_unit(self):
self.assertParse(Return(42) ** Return, [], 42, 0)
def test_first_monad_law(self):
self.assertParse(Return(42) ** (lambda x: Return(x / 2)), [], 21, 0)
def test_second_monad_law_1(self):
self.assertParse(
element ** (lambda a: Return(a / 2) ** (lambda b: Return(b * 3))), [42],
63, 1
)
def test_second_monad_law_2(self):
self.assertParse(
(element ** (lambda a: Return(a / 2))) ** (lambda b: Return(b * 3)), [42],
63, 1
)
class ZeroTest(ParseTest):
def test_zero(self):
self.assertFail(zero, [42])
class ItemTest(ParseTest):
def test_item(self):
self.assertParse(
item('a'), 'a',
'a', 1)
def test_item_fail(self):
self.assertFail(item('a'), 'b')
def test_item_numeric(self):
self.assertParse(
item(42), [42],
42, 1)
class ChainTest(ParseTest):
#
# The current behaviour of parsing a chain is returning the sum
# if '+' is implemented and the last element otherwise.
#
def test_chain(self):
self.assertParse(
item('a') + item('b'), 'ab',
'ab', 2)
def test_chain_too_short(self):
self.assertFail(item('a') + item('b'), 'a')
def test_chain_no_match(self):
self.assertFail(item('a') + item('b'), 'bb')
class BranchTest(ParseTest):
def test_branch_1(self):
self.assertParse(
item('a') | item('b'), 'a',
'a', 1
)
def test_branch_2(self):
self.assertParse(
item('a') | item('b'), 'b',
'b', 1
)
def test_branch_fail(self):
self.assertFail(item('a') | item('b'), 'c')
class BothTest(ParseTest):
def test_both(self):
self.assertParse(
item(1) & item(1), [1],
1, 1
)
def test_both_fail(self):
self.assertParse(
item(1) & item(0), [1],
1, 1
)
class RecurseTest(ParseTest):
def test_inside(self):
self.assertParse(
element [ element + element ], [['a', 'b']],
'ab', 1
)
class CutTest(ParseTest):
def test_cut(self):
p = -(Return(21) | Return(42))
count = 0
for result, pos in p([], 0):
count += 1
self.assertEqual(1, count)
def test_no_cut(self):
p = Return(21) | Return(42)
count = 0
for result, pos in p([], 0):
count += 1
self.assertEqual(2, count)
class WhenTest(ParseTest):
def test_even(self):
self.assertParse(
when(lambda x: x % 2 == 0), [42],
42, 1)
def test_not_even(self):
self.assertFail(
when(lambda x: x % 2 == 0), [21])
class SomeManyTest(ParseTest):
def test_some(self):
self.assertParse(
some(item('a') | item('b')), 'ab',
['a', 'b'], 2
)
def test_many(self):
self.assertParse(
many(item('a') | item('b')), 'ab',
['a', 'b'], 2
)
def test_some_empty(self):
self.assertFail(
some(item('a') | item('b')), [])
class SetTest(ParseTest):
def test_in_set(self):
self.assertParse(
Set(range(10)), [9],
9, 1
)
def test_set_union(self):
self.assertParse(
many(Set([1]) | Set([2])), [1, 2, 1],
[1, 2, 1], 3
)
def test_set_intersect_1(self):
self.assertFail(
Set([1,2,3]) & Set([2,3,4]), [1]
)
def test_set_intersect_2(self):
self.assertParse(
Set([1,2,3]) & Set([2,3,4]), [2],
2, 1
)
if __name__ == '__main__':
unittest.main()