Skip to content

Commit eb1f08e

Browse files
committed
add assertRaisesRegex to graalpytest
1 parent 928fdcd commit eb1f08e

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

graalpython/com.oracle.graal.python.test/src/graalpytest.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,27 +233,36 @@ def assertIsInstance(self, obj, cls, msg=None):
233233
def fail(self, msg):
234234
assert False, msg
235235

236-
class assertRaises():
237-
def __init__(self, exc_type, function=None, *args, **kwargs):
238-
self.function = function
239-
if self.function is None:
236+
def assertRaises(self, exc_type, function=None, *args, **kwargs):
237+
return self.assertRaisesRegex(exc_type, None, function, *args, **kwargs)
238+
239+
class assertRaisesRegex():
240+
def __init__(self, exc_type, exc_regex, function=None, *args, **kwargs):
241+
import re
242+
function = function
243+
if function is None:
240244
self.exc_type = exc_type
245+
self.exc_regex = exc_regex
241246
else:
242247
try:
243-
self.function(*args, **kwargs)
244-
except exc_type:
245-
pass
248+
function(*args, **kwargs)
249+
except exc_type as exc:
250+
if exc_regex:
251+
assert re.search(exc_regex, str(exc)), "%s does not match %s" % (exc_regex, exc)
246252
else:
247-
assert False, "expected '%r' to raise '%r'" % (self.function, exc_type)
253+
assert False, "expected '%r' to raise '%r'" % (function, exc_type)
248254

249255
def __enter__(self):
250256
return self
251257

252258
def __exit__(self, exc_type, exc, traceback):
259+
import re
253260
if not exc_type:
254-
assert False, "expected '%r' to raise '%r'" % (self.function, self.exc_type)
261+
assert False, "expected '%r' to be raised" % self.exc_type
255262
elif self.exc_type in exc_type.mro():
256263
self.exception = exc
264+
if self.exc_regex:
265+
assert re.search(self.exc_regex, str(exc)), "%s does not match %s" % (self.exc_regex, exc)
257266
return True
258267

259268
def assertIn(self, expected, in_str, msg=""):

graalpython/com.oracle.graal.python.test/src/tests/test_zlib.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,6 @@ def check_big_decompress_buffer(self, size, decompress_func):
122122
finally:
123123
data = None
124124

125-
def assertRaisesRegex(self, expected_exception, expected_regex, function,
126-
*args, **kwargs):
127-
"""Asserts that the message in a raised exception matches a regex.
128-
129-
Args:
130-
expected_exception: Exception class expected to be raised.
131-
expected_regex: Regex (re.Pattern object or string) expected
132-
to be found in error message.
133-
args: Function to be called and extra positional args.
134-
kwargs: Extra kwargs.
135-
msg: Optional message used in case of failure. Can only be used
136-
when assertRaisesRegex is used as a context manager.
137-
"""
138-
try:
139-
function(*args, **kwargs)
140-
except expected_exception as e:
141-
if not re.compile(expected_regex).search(str(e)):
142-
assert False, "exception message '%r' does not match '%r'" % (e, expected_regex)
143-
else:
144-
assert False, "expected '%r' to raise '%r'" % (self.function, exc_type)
145125

146126
class CompressTests(BaseCompressTestCase, unittest.TestCase):
147127
# Test compression in one go (whole message compression)

0 commit comments

Comments
 (0)