Skip to content

Commit

Permalink
added Match assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
aodag-open-c committed Jul 9, 2018
1 parent e883d46 commit 3d6dcb6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ eqassertion

assertions in `__eq__` methods.

>>> from eqassertions import Any, NotNone, IsA, IsTrue, Match
>>> data = {"a": 1, "b": 2}
>>> assert {"a": 1, "b": Any()}
>>> assert {"a": 1, "b": NotNone()}
>>> assert {"a": 1, "b": IsA(int)}
>>> assert {"a": 1, "b": IsTrue()}
>>> assert v == {"a": 1, "b": Match(r"\d")}
>>> assert v == {"a": 1, "b": Match(r"[a-z]")}
Traceback (most recent call last):
...
AssertionError
20 changes: 19 additions & 1 deletion eqassertions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__version__ = '0.1.1'
import re

__version__ = '0.2'


class Any:
Expand Down Expand Up @@ -38,3 +40,19 @@ def __init__(self, typ):

def __eq__(self, o):
return isinstance(o, self.typ)


class Match:
"""
>>> v = {"a": 1, "b": 2}
>>> assert v == {"a": 1, "b": Match(r"\d")}
>>> assert v == {"a": 1, "b": Match(r"[a-z]")}
Traceback (most recent call last):
...
AssertionError
"""
def __init__(self, pattern):
self.pt = re.compile(pattern)

def __eq__(self, o):
return self.pt.match(str(o))

0 comments on commit 3d6dcb6

Please sign in to comment.