-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-match.test
More file actions
304 lines (228 loc) · 5.31 KB
/
run-match.test
File metadata and controls
304 lines (228 loc) · 5.31 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
[case testTheBigMatch_python3_10]
class Person:
__match_args__ = ("name", "age")
name: str
age: int
def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age
def __str__(self) -> str:
return f"Person(name={self.name!r}, age={self.age})"
def f(x: object) -> None:
match x:
case 123:
print("test 1")
case 456 | 789:
print("test 2")
case True | False | None:
print("test 3")
case Person("bob" as name, age):
print(f"test 4 ({name=}, {age=})")
case num if num == 5:
print("test 5")
case 6 as num:
print(f"test 6 ({num=})")
case (7 | "7") as value:
print(f"test 7 ({value=})")
case Person("alice", age=123):
print("test 8")
case Person("charlie", age=123 | 456):
print("test 9")
case Person("dave", 123) as dave:
print(f"test 10 {dave}")
case {"test": 11}:
print("test 11")
case {"test": 12, **rest}:
print(f"test 12 (rest={rest})")
case {}:
print("test map final")
case ["test", 13]:
print("test 13")
case ["test", 13, _]:
print("test 13b")
case ["test", 14, *_]:
print("test 14")
# TODO: Fix "rest" being used here coliding with above "rest"
case ["test", 15, *rest2]:
print(f"test 15 ({rest2})")
case ["test", *rest3, 16]:
print(f"test 16 ({rest3})")
case [*rest4, "test", 17]:
print(f"test 17 ({rest4})")
case [*rest4, "test", 18, "some", "fluff"]:
print(f"test 18 ({rest4})")
case str("test 19"):
print("test 19")
case str(test_20) if test_20.startswith("test 20"):
print(f"test 20 ({test_20[7:]!r})")
case ("test 21" as value) | ("test 21 as well" as value):
print(f"test 21 ({value[7:]!r})")
case []:
print("test sequence final")
case _:
print("test final")
[file driver.py]
from native import f, Person
# test 1
f(123)
# test 2
f(456)
f(789)
# test 3
f(True)
f(False)
f(None)
# test 4
f(Person("bob", 123))
# test 5
f(5)
# test 6
f(6)
# test 7
f(7)
f("7")
# test 8
f(Person("alice", 123))
# test 9
f(Person("charlie", 123))
f(Person("charlie", 456))
# test 10
f(Person("dave", 123))
# test 11
f({"test": 11})
f({"test": 11, "some": "key"})
# test 12
f({"test": 12})
f({"test": 12, "key": "value"})
f({"test": 12, "key": "value", "abc": "123"})
# test map final
f({})
# test 13
f(["test", 13])
# test 13b
f(["test", 13, "fail"])
# test 14
f(["test", 14])
f(["test", 14, "something"])
# test 15
f(["test", 15])
f(["test", 15, "something"])
# test 16
f(["test", 16])
f(["test", "filler", 16])
f(["test", "more", "filler", 16])
# test 17
f(["test", 17])
f(["stuff", "test", 17])
f(["more", "stuff", "test", 17])
# test 18
f(["test", 18, "some", "fluff"])
f(["stuff", "test", 18, "some", "fluff"])
f(["more", "stuff", "test", 18, "some", "fluff"])
# test 19
f("test 19")
# test 20
f("test 20")
f("test 20 something else")
# test 21
f("test 21")
f("test 21 as well")
# test sequence final
f([])
# test final
f("")
[out]
test 1
test 2
test 2
test 3
test 3
test 3
test 4 (name='bob', age=123)
test 5
test 6 (num=6)
test 7 (value=7)
test 7 (value='7')
test 8
test 9
test 9
test 10 Person(name='dave', age=123)
test 11
test 11
test 12 (rest={})
test 12 (rest={'key': 'value'})
test 12 (rest={'key': 'value', 'abc': '123'})
test map final
test 13
test 13b
test 14
test 14
test 15 ([])
test 15 (['something'])
test 16 ([])
test 16 (['filler'])
test 16 (['more', 'filler'])
test 17 ([])
test 17 (['stuff'])
test 17 (['more', 'stuff'])
test 18 ([])
test 18 (['stuff'])
test 18 (['more', 'stuff'])
test 19
test 20 ('')
test 20 (' something else')
test 21 ('')
test 21 (' as well')
test sequence final
test final
[case testMatchOrSequencePattern_python3_10]
def f(x: tuple[str, str]) -> str:
match x:
case ("X", "Y") | ("X", "Z"):
return "THERE"
case _:
return "OTHER"
[file driver.py]
from native import f
print(f(("X", "Y")))
print(f(("X", "Z")))
print(f(("X", "A")))
[out]
THERE
THERE
OTHER
[case testCustomMappingAndSequenceObjects_python3_10]
def f(x: object) -> None:
match x:
case {"key": "value", **rest}:
print(rest, type(rest))
case [1, 2, *rest2]:
print(rest2, type(rest2))
[file driver.py]
from collections.abc import Mapping, Sequence
from native import f
class CustomMapping(Mapping):
inner: dict
def __init__(self, inner: dict) -> None:
self.inner = inner
def __getitem__(self, key):
return self.inner[key]
def __iter__(self):
return iter(self.inner)
def __len__(self) -> int:
return len(self.inner)
class CustomSequence(Sequence):
inner: list
def __init__(self, inner: list) -> None:
self.inner = inner
def __getitem__(self, index: int) -> None:
return self.inner[index]
def __len__(self) -> int:
return len(self.inner)
mapping = CustomMapping({"key": "value", "some": "data"})
sequence = CustomSequence([1, 2, 3])
f(mapping)
f(sequence)
[out]
{'some': 'data'} <class 'dict'>
[3] <class 'list'>