-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_patterns.py
244 lines (183 loc) · 5.54 KB
/
test_patterns.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import pytest
from patterns import patterns, Mismatch
def test_const():
@patterns
def const():
if 42: 'int'
if 'hi': 'str'
if True: 'bool'
if None: 'none'
assert const(42) == 'int'
assert const('hi') == 'str'
assert const(True) == 'bool'
assert const(None) == 'none'
with pytest.raises(Mismatch): const(2)
with pytest.raises(Mismatch): const({})
def test_container_const():
class L(list): pass
@patterns
def const():
if [1, 2]: 'list'
if (1, 2): 'tuple'
assert const([1, 2]) == 'list'
assert const((1, 2)) == 'tuple'
assert const(L([1, 2])) == 'list'
def test_complex_body():
@patterns
def const():
if 1:
x = 'int'
x
if 'hi':
return 'str'
assert const(1) == 'int'
assert const('hi') == 'str'
def test_global_ref():
@patterns
def _global():
if '': test_global_ref
assert _global('') is test_global_ref
def test_local_ref():
local_var = object()
@patterns
def _local():
if '': local_var
assert _local('') is local_var
local_var = object()
assert _local('') is local_var
def test_capture():
@patterns
def capture():
if y: y - 1
assert capture(41) == 40
def test_typing():
@patterns
def typing():
if n is int: n + 1
if s is (str, float): 'str_or_float'
assert typing(42) == 43
assert typing('42') == 'str_or_float'
assert typing(3.14) == 'str_or_float'
def test_destruct_tuple():
@patterns
def destruct():
if (x, 0): 0
if (x, 1): x
if (x, y): x + destruct((x, y - 1))
if (_,): raise ValueError('Give me pair')
assert destruct((2, 0)) == 0
assert destruct((2, 1)) == 2
assert destruct((2, 2)) == 4
assert destruct((5, 5)) == 25
with pytest.raises(ValueError): destruct((2,))
with pytest.raises(Mismatch): destruct(1)
def test_destruct_dict():
@patterns
def destruct():
if {}: 0
if {'a': a}: raise TypeError
if {'a': a, 'b': b}: a * b
# TODO: short form like this:
# if {a, b}: a * b
# how handle sets?
assert destruct({}) == 0
assert destruct({'a': 6, 'b': 7}) == 42
with pytest.raises(Mismatch): destruct({'a': 6, 'b': 7, 'c': None})
def test_nested_tuples():
@patterns
def destruct():
if ((1, 2), 3, x): x
if ((1, x), 3, 4): x
if ((1, x), 3, y): x + y
assert destruct(((1, 2), 3, 'world')) == 'world'
assert destruct(((1, 'hi'), 3, 4)) == 'hi'
assert destruct(((1, 'hi'), 3, 'world')) == 'hiworld'
assert destruct(((1, 2), 3, 4)) == 4
def test_swallow():
@patterns
def swallow():
if (1, x): 1 + '1'
with pytest.raises(TypeError): swallow((1, 2))
def test_nested_types():
@patterns
def destruct():
if (x is int, y is int): x * y
if (s is str, n is int): len(s) * n
if (s is str, t is str): s + t
assert destruct((6, 7)) == 42
assert destruct(('hi', 3)) == 6
assert destruct(('hi', 'world')) == 'hiworld'
def test_tail_destruct():
@patterns
def tail_destruct():
if []: 0
if [x] + xs: tail_destruct(xs) + 1
if (): ''
if (x is int,) + xs: 't' + tail_destruct(xs)
if (s is str,) + xs: s + tail_destruct(xs)
assert tail_destruct([]) == 0
assert tail_destruct([7]) == 1
assert tail_destruct([1, 2, 3]) == 3
assert tail_destruct(()) == ''
assert tail_destruct((1,)) == 't'
assert tail_destruct((1, 2, 3)) == 'ttt'
assert tail_destruct((1, 'X', 2)) == 'tXt'
def test_nested_capture():
@patterns
def answer():
# captute names should be diffrent here to get NameError
# if some structure is not handled properly
if [l]: 'list: %s' % l
if (t,): 'tuple: %s' % t
if {'key': d}: 'dict: %s' % d
assert answer(['alice']) == 'list: alice'
assert answer(('alice',)) == 'tuple: alice'
assert answer({'key': 'alice'}) == 'dict: alice'
def test_wildcard():
_ = 'XXX'
@patterns
def capture():
if _ is int: 'int ' + _
if [_ is int]: 'intlist ' + _
if [_]: 'list ' + _
if [_, _]: '2list ' + _
if _: 'wildcard ' + _
assert capture(42) == 'int XXX'
assert capture([42]) == 'intlist XXX'
assert capture(['hey']) == 'list XXX'
assert capture([42, 42]) == '2list XXX'
assert capture('hey') == 'wildcard XXX'
def test_same_names():
@patterns
def capture():
if [x is str, x]: 'str %s' % x
if [x, x]: 'double %s' % x
if [x, {'v': x}]: 'nested %s' % x
if [x, y]: '%s and %s' % (x, y)
assert capture([3, 3]) == 'double 3'
assert capture([1, 2]) == '1 and 2'
assert capture(['x', 'x']) == 'str x'
assert capture(['x', 'y']) == 'x and y'
assert capture([4, {'v': 4}]) == 'nested 4'
def test_wrong_pattern():
def wrong():
@patterns
def _wrong():
if map(): 1
with pytest.raises(TypeError): wrong()
def test_exception():
import sys, inspect
class E(Exception): pass
BASE_LINE = inspect.currentframe().f_lineno
@patterns
def exception():
if 1: raise E
if 2:
raise E
TESTS = [(1, E, BASE_LINE + 4), (2, E, BASE_LINE + 6), (3, Mismatch, BASE_LINE + 7)]
for value, exc, line in TESTS:
try:
exception(value)
except exc:
_, _, exc_traceback = sys.exc_info()
assert line == exc_traceback.tb_next.tb_lineno