summaryrefslogtreecommitdiff
path: root/mocker.py
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2010-09-18 17:36:24 -0300
committerGustavo Niemeyer <gustavo@niemeyer.net>2010-09-18 17:36:24 -0300
commitb64f0d59971eaa6f4d48ac3ebbad7eb867bec5f6 (patch)
treed8d56f6f9f5c05fbc8515f485f74063d4d7e7f15 /mocker.py
parentf28880cb3cb0642b96732644fc1a4b8c996d5a48 (diff)
downloadmocker-b64f0d59971eaa6f4d48ac3ebbad7eb867bec5f6.tar.gz
Added assertRaisesRegexp() to MockerTestCase. It works similarly to
the version in Python 2.7 and 3.2, except it will also return the error found. It also works as a context manager for with: statements.
Diffstat (limited to 'mocker.py')
-rw-r--r--mocker.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/mocker.py b/mocker.py
index 4935735..7f7a81a 100644
--- a/mocker.py
+++ b/mocker.py
@@ -39,6 +39,7 @@ import shutil
import types
import sys
import os
+import re
import gc
@@ -362,17 +363,40 @@ class MockerTestCase(unittest.TestCase):
with self.failUnlessRaises(ExcClass):
logic_which_should_raise()
"""
- excName = getattr(excClass, "__name__", str(excClass))
+ return self.failUnlessRaisesRegexp(excClass, None, *args, **kwargs)
+
+ def failUnlessRaisesRegexp(self, excClass, regexp, *args, **kwargs):
+ """
+ Fail unless an exception of class excClass is thrown by callableObj
+ when invoked with arguments args and keyword arguments kwargs, and
+ the str(error) value matches the provided regexp. If a different type
+ of exception is thrown, it will not be caught, and the test case will
+ be deemed to have suffered an error, exactly as for an unexpected
+ exception. It returns the exception instance if it matches the given
+ exception class.
+
+ This may also be used as a context manager when provided with a single
+ argument, as such:
+
+ with self.failUnlessRaisesRegexp(ExcClass, "something like.*happened"):
+ logic_which_should_raise()
+ """
+ def match_regexp(error):
+ error_str = str(error)
+ if regexp is not None and not re.search(regexp, error_str):
+ raise self.failureException("%r doesn't match %r" %
+ (error_str, regexp))
+ excName = self.__class_name(excClass)
if args:
callableObj = args[0]
try:
result = callableObj(*args[1:], **kwargs)
except excClass, e:
+ match_regexp(e)
return e
else:
raise self.failureException("%s not raised (%r returned)" %
- (self.__class_name(excClass),
- result))
+ (excName, result))
else:
test = self
class AssertRaisesContextManager(object):
@@ -383,6 +407,7 @@ class MockerTestCase(unittest.TestCase):
if value is None:
raise test.failureException("%s not raised" % excName)
elif isinstance(value, excClass):
+ match_regexp(value)
return True
return AssertRaisesContextManager()
@@ -417,6 +442,7 @@ class MockerTestCase(unittest.TestCase):
assertNotApproximates = failIfApproximates
assertMethodsMatch = failUnlessMethodsMatch
assertRaises = failUnlessRaises
+ assertRaisesRegexp = failUnlessRaisesRegexp
assertIsInstance = failUnlessIsInstance
assertNotIsInstance = failIfIsInstance
@@ -890,6 +916,9 @@ class MockerBase(object):
"""Make the last recorded event cause the given function to be called.
@param func: Function to be called.
+ @param with_object: If True, the called function will receive the
+ patched or proxied object so that its state may be used or verified
+ in checks.
The result of the function will be used as the event result.
"""