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
1=== modified file 'NEWS'
2--- NEWS 2011-07-20 08:48:46 +0000
3+++ NEWS 2011-07-20 20:06:42 +0000
4@@ -4,6 +4,12 @@
5 NEXT
6 ~~~~
7
8+Changes
9+-------
10+
11+* ``MatchesRegex`` mismatch now says "<value> does not match <regex>" rather
12+ than "<regex> did not match <value>"
13+
14 Improvements
15 ------------
16
17
18=== modified file 'testtools/matchers.py'
19--- testtools/matchers.py 2011-07-20 08:48:46 +0000
20+++ testtools/matchers.py 2011-07-20 20:06:42 +0000
21@@ -412,11 +412,15 @@
22 are checked. If a type is given only the type of the exception is
23 checked.
24 :param value_re: If 'exception' is a type, and the matchee exception
25- is of the right type, then the 'str()' of the matchee exception
26- is matched against this regular expression.
27+ is of the right type, then match against this. If value_re is a
28+ string, then assume value_re is a regular expression and match
29+ the str() of the exception against it. Otherwise, assume value_re
30+ is a matcher, and match the exception against it.
31 """
32 Matcher.__init__(self)
33 self.expected = exception
34+ if istext(value_re):
35+ value_re = AfterPreproccessing(str, MatchesRegex(value_re))
36 self.value_re = value_re
37 self._is_instance = type(self.expected) not in classtypes()
38
39@@ -433,11 +437,7 @@
40 return Mismatch('%s has different arguments to %s.' % (
41 _error_repr(other[1]), _error_repr(self.expected)))
42 elif self.value_re is not None:
43- str_exc_value = str(other[1])
44- if not re.match(self.value_re, str_exc_value):
45- return Mismatch(
46- '"%s" does not match "%s".'
47- % (str_exc_value, self.value_re))
48+ return self.value_re.match(other[1])
49
50 def __str__(self):
51 if self._is_instance:
52@@ -729,7 +729,7 @@
53
54 def match(self, value):
55 if not re.match(self.pattern, value, self.flags):
56- return Mismatch("%r did not match %r" % (self.pattern, value))
57+ return Mismatch("%r does not match %r" % (value, self.pattern))
58
59
60 class MatchesSetwise(object):
61@@ -837,7 +837,7 @@
62 self._str_preprocessor(), self.matcher)
63
64 def match(self, value):
65- value = self.preprocessor(value)
66+ after = self.preprocessor(value)
67 return Annotate(
68- "after %s" % self._str_preprocessor(),
69- self.matcher).match(value)
70+ "after %s on %r" % (self._str_preprocessor(), value),
71+ self.matcher).match(after)
72
73=== modified file 'testtools/tests/test_matchers.py'
74--- testtools/tests/test_matchers.py 2011-07-20 08:46:46 +0000
75+++ testtools/tests/test_matchers.py 2011-07-20 20:06:42 +0000
76@@ -254,14 +254,36 @@
77
78 str_examples = [
79 ("MatchesException(%r)" % Exception,
80- MatchesException(Exception))
81+ MatchesException(Exception, 'fo.'))
82 ]
83 describe_examples = [
84- ('"bar" does not match "fo.".',
85+ # XXX: This is kind of a crappy message. Need to change
86+ # AfterPreproccessing.
87+ ("'bar' does not match 'fo.': after <type 'str'> on ValueError('bar',)",
88 error_bar, MatchesException(ValueError, "fo.")),
89 ]
90
91
92+class TestMatchesExceptionTypeMatcherInterface(TestCase, TestMatchersInterface):
93+
94+ matches_matcher = MatchesException(
95+ ValueError, AfterPreproccessing(str, Equals('foo')))
96+ error_foo = make_error(ValueError, 'foo')
97+ error_sub = make_error(UnicodeError, 'foo')
98+ error_bar = make_error(ValueError, 'bar')
99+ matches_matches = [error_foo, error_sub]
100+ matches_mismatches = [error_bar]
101+
102+ str_examples = [
103+ ("MatchesException(%r)" % Exception,
104+ MatchesException(Exception, Equals('foo')))
105+ ]
106+ describe_examples = [
107+ ("5 != ValueError('bar',)",
108+ error_bar, MatchesException(ValueError, Equals(5))),
109+ ]
110+
111+
112 class TestNotInterface(TestCase, TestMatchersInterface):
113
114 matches_matcher = Not(Equals(1))
115@@ -618,7 +640,7 @@
116 ]
117
118 describe_examples = [
119- ("'a|b' did not match 'c'", 'c', MatchesRegex('a|b')),
120+ ("'c' does not match 'a|b'", 'c', MatchesRegex('a|b')),
121 ]
122
123
124@@ -713,7 +735,7 @@
125 ]
126
127 describe_examples = [
128- ("1 != 0: after <function parity>",
129+ ("1 != 0: after <function parity> on 2",
130 2,
131 AfterPreproccessing(parity, Equals(1))),
132 ]

Subscribers

People subscribed via source and target branches