|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
def test_cant_use_keyword(match): |
|
5
|
|
|
with pytest.raises(ValueError): |
|
6
|
|
|
assert not match.Pattern('def') |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def test_must_be_identifier(match): |
|
10
|
|
|
with pytest.raises(ValueError): |
|
11
|
|
|
assert not match.Pattern('1') |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def test_matching(enum, match): |
|
15
|
|
|
|
|
16
|
|
|
@enum.enum |
|
17
|
|
|
class TestClass: |
|
18
|
|
|
StrPair: enum.Ctor[str, str] |
|
19
|
|
|
matcher = match.ValueMatcher( |
|
20
|
|
|
((1, 2), TestClass.StrPair('a', 'b'))) |
|
21
|
|
|
assert not matcher.match(( |
|
22
|
|
|
(match.pat._, 4), |
|
23
|
|
|
match.pat._)) |
|
24
|
|
|
assert matcher.matches is None |
|
25
|
|
|
structure = ( |
|
26
|
|
|
match.pat.tup @ (1, match.pat.a), |
|
27
|
|
|
TestClass.StrPair( |
|
28
|
|
|
match.pat.b, match.pat.c)) |
|
29
|
|
|
assert matcher.match(structure) |
|
30
|
|
|
assert matcher.matches == dict(tup=(1, 2), a=2, b='a', c='b') |
|
31
|
|
|
assert matcher.matches[match.pat.a, match.pat.b, match.pat.c, match.pat.tup] == (2, 'a', 'b', (1, 2)) |
|
32
|
|
|
assert list(matcher.matches) == ['tup', 'a', 'b', 'c'] # Should preserve ordering. |
|
33
|
|
|
assert match.names(structure) == ['tup', 'a', 'b', 'c'] |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def test_as(match): |
|
37
|
|
|
pat = match.pat.hello |
|
38
|
|
|
assert pat @ match.pat._ is pat |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def test_map_interface(match): |
|
42
|
|
|
matcher = match.ValueMatcher((1, 2, 3, 4)) |
|
43
|
|
|
matcher.match((match.pat.a, match.pat._, match.pat._, match.pat.b)) |
|
44
|
|
|
assert len(matcher.matches) == 2 |
|
45
|
|
|
with pytest.raises(KeyError): |
|
46
|
|
|
assert not matcher.matches[None] |
|
47
|
|
|
|
|
48
|
|
|
matcher.matches[match.pat.c] = 7 |
|
49
|
|
|
del matcher.matches[match.pat.c] |
|
50
|
|
|
|
|
51
|
|
|
with pytest.raises(TypeError): |
|
52
|
|
|
matcher.matches[None] = None |
|
53
|
|
|
|
|
54
|
|
|
with pytest.raises(KeyError): |
|
55
|
|
|
del matcher.matches[None] |
|
56
|
|
|
|