Skip to content

Commit

Permalink
multiple exception types
Browse files Browse the repository at this point in the history
  • Loading branch information
iskandr committed Feb 27, 2024
1 parent a5ced39 commit be877ba
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,26 @@ def lte_(x, y, msg=None):
assert x <= y, msg

class assert_raises:
def __init__(self, exception_type):
self.exception_type = exception_type
def __init__(self, *exception_types):
self.exception_types = exception_types

def __enter__(self):
pass


def to_string(self):
return " or ".join(["%s" % e for e in self.exception_types])
def __exit__(self, type, value, traceback):
if type is None:
raise AssertionError("Expected exception %s not raised" % self.exception_type)
if type != self.exception_type:
raise AssertionError("Expected exception %s, got %s" % (self.exception_type, type))
raise AssertionError("Expected exception %s not raised" % self.to_string())
if type not in self.exception_types:
raise AssertionError("Expected exception %s, got %s" % (self.to_string(), type))
return True

def raises(exception_type):
def raises(*exception_types):
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
with assert_raises(exception_type):
with assert_raises(*exception_types):
f(*args, **kwargs)
return wrapper
return decorator

0 comments on commit be877ba

Please sign in to comment.