Merge lp:~jml/testtools/matches-exception-791889 into lp:~testtools-committers/testtools/trunk

Proposed by Jonathan Lange
Status: Merged
Approved by: Robert Collins
Approved revision: 211
Merged at revision: 210
Proposed branch: lp:~jml/testtools/matches-exception-791889
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 132 lines (+43/-15)
3 files modified
NEWS (+6/-0)
testtools/matchers.py (+11/-11)
testtools/tests/test_matchers.py (+26/-4)
To merge this branch: bzr merge lp:~jml/testtools/matches-exception-791889
Reviewer Review Type Date Requested Status
Robert Collins Approve
Review via email: mp+68595@code.launchpad.net

Commit message

Update MatchesException to allow taking a Matcher as well as a regular expression.

Description of the change

Allow MatchesException to take a Matcher instead of an regular expression.

Updates AfterPreprocessing to show the original, unprocessed value.

Changes the mismatch description from MatchesRegex.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) :
review: Approve
Revision history for this message
Robert Collins (lifeless) wrote :

It might be nicer to say 'value was not matched by regex'; blargh, I dunno :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'NEWS'
--- NEWS 2011-07-20 08:48:46 +0000
+++ NEWS 2011-07-20 20:06:42 +0000
@@ -4,6 +4,12 @@
4NEXT4NEXT
5~~~~5~~~~
66
7Changes
8-------
9
10* ``MatchesRegex`` mismatch now says "<value> does not match <regex>" rather
11 than "<regex> did not match <value>"
12
7Improvements13Improvements
8------------14------------
915
1016
=== modified file 'testtools/matchers.py'
--- testtools/matchers.py 2011-07-20 08:48:46 +0000
+++ testtools/matchers.py 2011-07-20 20:06:42 +0000
@@ -412,11 +412,15 @@
412 are checked. If a type is given only the type of the exception is412 are checked. If a type is given only the type of the exception is
413 checked.413 checked.
414 :param value_re: If 'exception' is a type, and the matchee exception414 :param value_re: If 'exception' is a type, and the matchee exception
415 is of the right type, then the 'str()' of the matchee exception415 is of the right type, then match against this. If value_re is a
416 is matched against this regular expression.416 string, then assume value_re is a regular expression and match
417 the str() of the exception against it. Otherwise, assume value_re
418 is a matcher, and match the exception against it.
417 """419 """
418 Matcher.__init__(self)420 Matcher.__init__(self)
419 self.expected = exception421 self.expected = exception
422 if istext(value_re):
423 value_re = AfterPreproccessing(str, MatchesRegex(value_re))
420 self.value_re = value_re424 self.value_re = value_re
421 self._is_instance = type(self.expected) not in classtypes()425 self._is_instance = type(self.expected) not in classtypes()
422426
@@ -433,11 +437,7 @@
433 return Mismatch('%s has different arguments to %s.' % (437 return Mismatch('%s has different arguments to %s.' % (
434 _error_repr(other[1]), _error_repr(self.expected)))438 _error_repr(other[1]), _error_repr(self.expected)))
435 elif self.value_re is not None:439 elif self.value_re is not None:
436 str_exc_value = str(other[1])440 return self.value_re.match(other[1])
437 if not re.match(self.value_re, str_exc_value):
438 return Mismatch(
439 '"%s" does not match "%s".'
440 % (str_exc_value, self.value_re))
441441
442 def __str__(self):442 def __str__(self):
443 if self._is_instance:443 if self._is_instance:
@@ -729,7 +729,7 @@
729729
730 def match(self, value):730 def match(self, value):
731 if not re.match(self.pattern, value, self.flags):731 if not re.match(self.pattern, value, self.flags):
732 return Mismatch("%r did not match %r" % (self.pattern, value))732 return Mismatch("%r does not match %r" % (value, self.pattern))
733733
734734
735class MatchesSetwise(object):735class MatchesSetwise(object):
@@ -837,7 +837,7 @@
837 self._str_preprocessor(), self.matcher)837 self._str_preprocessor(), self.matcher)
838838
839 def match(self, value):839 def match(self, value):
840 value = self.preprocessor(value)840 after = self.preprocessor(value)
841 return Annotate(841 return Annotate(
842 "after %s" % self._str_preprocessor(),842 "after %s on %r" % (self._str_preprocessor(), value),
843 self.matcher).match(value)843 self.matcher).match(after)
844844
=== modified file 'testtools/tests/test_matchers.py'
--- testtools/tests/test_matchers.py 2011-07-20 08:46:46 +0000
+++ testtools/tests/test_matchers.py 2011-07-20 20:06:42 +0000
@@ -254,14 +254,36 @@
254254
255 str_examples = [255 str_examples = [
256 ("MatchesException(%r)" % Exception,256 ("MatchesException(%r)" % Exception,
257 MatchesException(Exception))257 MatchesException(Exception, 'fo.'))
258 ]258 ]
259 describe_examples = [259 describe_examples = [
260 ('"bar" does not match "fo.".',260 # XXX: This is kind of a crappy message. Need to change
261 # AfterPreproccessing.
262 ("'bar' does not match 'fo.': after <type 'str'> on ValueError('bar',)",
261 error_bar, MatchesException(ValueError, "fo.")),263 error_bar, MatchesException(ValueError, "fo.")),
262 ]264 ]
263265
264266
267class TestMatchesExceptionTypeMatcherInterface(TestCase, TestMatchersInterface):
268
269 matches_matcher = MatchesException(
270 ValueError, AfterPreproccessing(str, Equals('foo')))
271 error_foo = make_error(ValueError, 'foo')
272 error_sub = make_error(UnicodeError, 'foo')
273 error_bar = make_error(ValueError, 'bar')
274 matches_matches = [error_foo, error_sub]
275 matches_mismatches = [error_bar]
276
277 str_examples = [
278 ("MatchesException(%r)" % Exception,
279 MatchesException(Exception, Equals('foo')))
280 ]
281 describe_examples = [
282 ("5 != ValueError('bar',)",
283 error_bar, MatchesException(ValueError, Equals(5))),
284 ]
285
286
265class TestNotInterface(TestCase, TestMatchersInterface):287class TestNotInterface(TestCase, TestMatchersInterface):
266288
267 matches_matcher = Not(Equals(1))289 matches_matcher = Not(Equals(1))
@@ -618,7 +640,7 @@
618 ]640 ]
619641
620 describe_examples = [642 describe_examples = [
621 ("'a|b' did not match 'c'", 'c', MatchesRegex('a|b')),643 ("'c' does not match 'a|b'", 'c', MatchesRegex('a|b')),
622 ]644 ]
623645
624646
@@ -713,7 +735,7 @@
713 ]735 ]
714736
715 describe_examples = [737 describe_examples = [
716 ("1 != 0: after <function parity>",738 ("1 != 0: after <function parity> on 2",
717 2,739 2,
718 AfterPreproccessing(parity, Equals(1))),740 AfterPreproccessing(parity, Equals(1))),
719 ]741 ]

Subscribers

People subscribed via source and target branches