/
test_fun.py
254 lines (199 loc) · 5.16 KB
/
test_fun.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
245
246
247
248
249
250
251
252
253
254
import fun
def test1 ():
class B:
def __init__ (self, name):
self.name = name
def __str__ (self):
return self.name
classify = fun.Function (object, name="classify")
@classify.match (fun.catch (lambda a: a % 2 == 0))
def classify_even (a):
return "even"
@classify.match (fun.catch (lambda a: a % 3 == 0))
def classify_div3 (a, **kwargs):
return "divisible by 3"
@classify.match (fun.val == 0, priority=1)
def classify_zero (a):
return "zero"
@classify.match (lambda a: a > 5, priority=1)
def classify_big (a):
return "big"
@classify.match (fun.any)
def classify_unclassified (a):
return "unclassified"
@classify.match (fun.matchclass (B), priority=2)
def classify_string (a):
return "B"
@classify.match (fun.matchattr (name = "xxx"), priority=3)
def classify_xxx (a):
return "B(xxx)"
assert classify (0) == "zero"
assert classify (1) == "unclassified"
assert classify (2) == "even"
assert classify (3) == "divisible by 3"
assert classify (4) == "even"
assert classify (5) == "unclassified"
assert classify (6) == "big"
assert classify (7) == "big"
assert classify (8) == "big"
assert classify (9) == "big"
assert classify (B ("xx")) == "B"
assert classify (B ("xxx")) == "B(xxx)"
def test2 ():
class C:
def __init__ (self, name, value):
self.name = name
self.value = value
test = fun.Function (object, name="test")
@test.match (fun.bind (X = fun.if_else (fun.matchattr (name = "abc"),
lambda arg: arg.value)))
def do (arg):
return "1:" + X
@test.match (fun.bind (X = fun.if_else (fun.matchattr (name = "def"),
lambda arg: arg.value)))
def do (arg):
return "2:" + X
@test.match (fun.any)
def do (arg):
return "default"
assert test (C ("test", "")) == "default"
assert test (C ("abc", "yay!")) == "1:yay!"
assert test (C ("def", "meh!")) == "2:meh!"
def test3 ():
class A:
def __init__ (self, a):
self.m_a = a
def a (self):
return self.m_a
class B:
pass
x = fun.Function (object, object)
@x.match (fun.any, fun.any)
def do (i, j):
return "%s, %s" % (i, j)
@x.match (fun.matchclass (B), fun.any)
def do (i, j):
return "some B, %s" % j
@x.match (fun.any, fun.val == 5)
def do (i, j):
return "%s, value 5" % i
@x.match (fun.bind (X = fun.catch (lambda arg: arg.a ())), fun.any)
def do (i, j):
return "a=%s, %s" % (X, j)
assert (x (1, 2) == "1, 2")
assert (x (A (1), 2) == "a=1, 2")
assert (x (B (), 2) == "some B, 2")
assert (x (1, 5) == "1, value 5")
def test4 ():
x = fun.Function (object)
# Faulty branch. Calling x must assert.
@x.match (lambda arg: arg)
def do (a):
pass
try:
x (True)
except AssertionError:
assert False, "x (True) failed"
try:
x (8)
asserted = False
except AssertionError:
asserted = True
assert asserted
def test5 ():
x = fun.Function (object)
@x.match (fun.any)
def do (i):
return 1
@x.match (fun.val != 3)
def do (i):
return 2
assert x (0) == 2
assert x (1) == 2
assert x (2) == 2
assert x (3) == 1
assert x (4) == 2
assert x (5) == 2
assert x (6) == 2
def test6 ():
x = fun.Function (object)
@x.match (fun.any)
def do (i):
return 1
@x.match (fun.take_first (fun.val == 2))
def do (i):
return 2
assert x (0) == 1
assert x (0) == 1
assert x (1) == 1
assert x (1) == 1
assert x (2) == 2
assert x (2) == 1
assert x (3) == 1
assert x (3) == 1
def test7 ():
x = fun.Function (object)
@x.match (fun.any)
def do (i):
return 1
@x.match (fun.or_ (fun.val == 1, fun.val == 2))
def do (i):
return 2
assert x (0) == 1
assert x (0) == 1
assert x (1) == 2
assert x (1) == 2
assert x (2) == 2
assert x (2) == 2
assert x (3) == 1
assert x (3) == 1
def test8 ():
x = fun.Function (object)
@x.match (fun.any)
def do (i):
return 1
@x.match (fun.and_ (fun.val != 1, fun.val != 2))
def do (i):
return 2
assert x (0) == 2
assert x (0) == 2
assert x (1) == 1
assert x (1) == 1
assert x (2) == 1
assert x (2) == 1
assert x (3) == 2
assert x (3) == 2
def test9 ():
x = fun.Function ()
q = []
@x.match ()
def do ():
q.append (1)
@x.match (priority=1)
def do ():
next ()
q.append (2)
@x.match (priority=2)
def do ():
next ()
q.append (3)
x ()
assert q == [1, 2, 3]
def test10 ():
class X:
def __init__ (self):
self.x = fun.Function (int)
@self.x.match (int)
def do (i):
return i + 1
assert X ().x (1) == 2
test1 ()
test2 ()
test3 ()
test4 ()
test5 ()
test6 ()
test7 ()
test8 ()
test9 ()
test10 ()