Skip to content

Commit

Permalink
V0.2 (#4)
Browse files Browse the repository at this point in the history
Add ability to register multiple exception classes at once to the same comparer, by passing a tuple of exception types.
  • Loading branch information
wimglenn authored Feb 12, 2019
1 parent 8f93356 commit aedd73f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pytest-raisin 0.2 (2019-02-11)
==============================

- Add ability to register multiple exception classes at once to the same comparer, by passing a tuple of exception types.


pytest-raisin 0.1 (2018-12-16)
==============================

- Initial release
26 changes: 17 additions & 9 deletions pytest_raisin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pytest_raisin.compat import default_compare


__version__ = "0.1"
__version__ = "0.2"


log = logging.getLogger(__name__)
Expand All @@ -33,19 +33,24 @@ def my_error_compare(exc_actual, exc_expected):
useful context message should they fail to match. It should
return None if the exceptions should be considered equivalent.
"""
exception_classes = exception_class
if not isinstance(exception_class, tuple):
exception_classes = (exception_class,)

if not isinstance(exception_class, type) or not issubclass(exception_class, BaseException):
msg = "Can not register {!r}, it's not an Exception subclass"
msg = msg.format(exception_class)
raise TypeError(msg)
for exception_class in exception_classes:
if not isinstance(exception_class, type) or not issubclass(exception_class, BaseException):
msg = "Can not register {!r}, it's not an Exception subclass"
msg = msg.format(exception_class)
raise TypeError(msg)

def decorator(func):
if not callable(func):
raise TypeError('You are decorating a non callable: {!r}'.format(func))
if exception_class in exception_comparers:
log.warning("%r was registered multiple times", exception_class)
exception_comparers[exception_class] = func
log.debug("Registered %r to handle %r comparisons", func, exception_class)
for exception_class in exception_classes:
if exception_class in exception_comparers:
log.warning("%r was registered multiple times", exception_class)
exception_comparers[exception_class] = func
log.debug("Registered %r to handle %r comparisons", func, exception_class)
return func

return decorator
Expand Down Expand Up @@ -91,3 +96,6 @@ def pytest_configure(config):
def pytest_unconfigure(config):
pytest.raises = original
vars(pytest).pop("register_exception_compare", None)


pytest.register_exception_compare = register_exception_compare
2 changes: 1 addition & 1 deletion tests/test_register.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest_plugins = ["pytester"]
pytest_plugins = ["pytester", "pytest-raisin"]


def test_user_registered_error_hook_pass(testdir):
Expand Down

0 comments on commit aedd73f

Please sign in to comment.