-
Notifications
You must be signed in to change notification settings - Fork 2
/
README
95 lines (67 loc) · 3.16 KB
/
README
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
Usage
=====
Call the `patternmatching.match()` function with two arguments:
* Argument 1: the pattern to match against
* Argument 2: the data structure to match
* Optional argument `flatten`: `True` to have the matched values
flattened, i.e. returned as a flat tuple regardless of their position
in the structure of the matched data.
Return value:
if flatten is true:
match_ok, [matched_value, matched_value, matched_value, ...]
if flatten is not true:
match_ok, matched_data_structure
Caveat:
if all matched values in the pattern are ignored, `match()` does not return a `tuple` but a boolean.
YES:
`match_ok = match(PATTERN, DATA)`
NO:
`match_ok, = match(PATTERN, DATA)`
Usage in (unit) tests (but also anywhere else)
==============================================
Comparing the return values of functions is tedious if you can't
simply use the `==` operator. For example when a function returns a
tuple and you only care about some parts of the tuple and not the
entire tuple:
retval = some_function_under_test()
assert retval and isinstance(retval, tuple) \
and len(retval) == 3 and retval[0] == 'foo' \
and isinstance(retval[1], tuple) and len(retval[1]) == 2 \
and isinstance(retval[1][1], SomeException)
With pattern matching:
retval = some_function_under_test()
assert retval == ('foo', (ANY, IS_INSTANCE(SomeException)), ANY)
Examples
========
MATCH OK
--------
match_ok, value = match(ANY, 'foobar')
assert match_ok and value == 'foobar'
match_ok = match(IGNORE(ANY), 'foobar')
assert match_ok
match_ok, value1, value2 match((ANY, ANY), ('foo', 'bar'))
assert match_ok and value1 == 'foo' and value2 == 'bar'
match_ok, (value1, value2) = match((ANY, ANY), ('foo', 'bar'), flatten=False)
assert match_ok and value1 == 'foo' and value2 == 'bar'
match_ok, (value1, ) = match((ANY, IGNORE(ANY)), ('foo', 'whatev'))
assert match_ok and value1 == 'foo'
match_ok, value1, value2 = match(('foo', ANY, ANY), ('foo', 1, 2))
assert match_ok and value1 == 1 and value2 == 2
match_ok, value1, value2, value3 = match(('foo', ANY, (ANY, ANY)), ('foo', 1, (2, 3)))
assert match_ok and value1 == 1 and value2 == 2 and value3 = 3
match_ok, (value1, (value2, value3)) = match(('foo', ANY, (ANY, ANY)), ('foo', 1, (2, 3)), flatten=False)
assert match_ok and value1 == 1 and value2 == 2 and value3 = 3
NO MATCH
--------
match_ok = match(3, 4)
assert not match_ok
# notice how you can still successfully do unpacking of return values and just ignore `value` if the match failed
match_ok, value = match(IS_INSTANCE(unicode), '123132')
assert not match_ok
# notice how you can still successfully do unpacking of return values and just ignore `value` if the match failed
match_ok, (value1, (value2, value3)) = match(('foo', (ANY, (ANY, ANY))), ('not-foo', (1, (2, 3))))
assert not match_ok
# ...even when the structure of the data completely mismatches
match_ok, (value1, (value2, value3)) = match(('foo', (ANY, (ANY, ANY))), ('foo', 'blabla'))
assert not match_ok
# ...don't rely on `value1`, `value2` and `value3` being `None` though--the matcher can still return whatever it wants there; you have to check `match_ok` yourself.