Skip to content

Commit 99af642

Browse files
authored
feat(matchers): add AnythingOrNone to match any value including None (#262)
1 parent cb399b7 commit 99af642

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

decoy/matchers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ def test_logger_called(decoy: Decoy):
4141
]
4242

4343

44+
class _AnythingOrNone:
45+
def __eq__(self, target: object) -> bool:
46+
return True
47+
48+
def __repr__(self) -> str:
49+
"""Return a string representation of the matcher."""
50+
return "<AnythingOrNone>"
51+
52+
53+
def AnythingOrNone() -> Any:
54+
"""Match anything including None.
55+
56+
!!! example
57+
```python
58+
assert "foobar" == AnythingOrNone()
59+
assert None == AnythingOrNone()
60+
```
61+
"""
62+
return _AnythingOrNone()
63+
64+
4465
class _Anything:
4566
def __eq__(self, target: object) -> bool:
4667
"""Return true if target is not None."""

tests/test_matchers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ def goodbye(self) -> str:
1818
_HelloTuple = namedtuple("_HelloTuple", ["hello"])
1919

2020

21+
def test_anything_or_none_matcher() -> None:
22+
"""It should have an "anything including None" matcher."""
23+
assert 1 == matchers.AnythingOrNone()
24+
assert False == matchers.AnythingOrNone() # noqa: E712
25+
assert {} == matchers.AnythingOrNone()
26+
assert [] == matchers.AnythingOrNone()
27+
assert ("hello", "world") == matchers.AnythingOrNone()
28+
assert SomeClass() == matchers.AnythingOrNone()
29+
assert None == matchers.AnythingOrNone() # noqa: E711
30+
31+
assert str(matchers.AnythingOrNone()) == "<AnythingOrNone>"
32+
33+
2134
def test_any_matcher() -> None:
2235
"""It should have an "anything except None" matcher."""
2336
assert 1 == matchers.Anything()
@@ -26,6 +39,7 @@ def test_any_matcher() -> None:
2639
assert [] == matchers.Anything()
2740
assert ("hello", "world") == matchers.Anything()
2841
assert SomeClass() == matchers.Anything()
42+
assert None != matchers.Anything() # noqa: E711
2943

3044

3145
def test_is_a_matcher() -> None:

0 commit comments

Comments
 (0)