← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~jml/testtools/matches-exception-791889 into lp:testtools

 

Jonathan Lange has proposed merging lp:~jml/testtools/matches-exception-791889 into lp:testtools.

Requested reviews:
  testtools developers (testtools-dev)
Related bugs:
  Bug #791889 in testtools: "The value_re argument for ExpectedException only fits some situations"
  https://bugs.launchpad.net/testtools/+bug/791889

For more details, see:
https://code.launchpad.net/~jml/testtools/matches-exception-791889/+merge/68595

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.
-- 
https://code.launchpad.net/~jml/testtools/matches-exception-791889/+merge/68595
Your team testtools developers is requested to review the proposed merge of lp:~jml/testtools/matches-exception-791889 into lp:testtools.
=== modified file 'NEWS'
--- NEWS	2011-07-20 08:48:46 +0000
+++ NEWS	2011-07-20 20:06:42 +0000
@@ -4,6 +4,12 @@
 NEXT
 ~~~~
 
+Changes
+-------
+
+* ``MatchesRegex`` mismatch now says "<value> does not match <regex>" rather
+  than "<regex> did not match <value>"
+
 Improvements
 ------------
 

=== 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 @@
             are checked. If a type is given only the type of the exception is
             checked.
         :param value_re: If 'exception' is a type, and the matchee exception
-            is of the right type, then the 'str()' of the matchee exception
-            is matched against this regular expression.
+            is of the right type, then match against this.  If value_re is a
+            string, then assume value_re is a regular expression and match
+            the str() of the exception against it.  Otherwise, assume value_re
+            is a matcher, and match the exception against it.
         """
         Matcher.__init__(self)
         self.expected = exception
+        if istext(value_re):
+            value_re = AfterPreproccessing(str, MatchesRegex(value_re))
         self.value_re = value_re
         self._is_instance = type(self.expected) not in classtypes()
 
@@ -433,11 +437,7 @@
                 return Mismatch('%s has different arguments to %s.' % (
                         _error_repr(other[1]), _error_repr(self.expected)))
         elif self.value_re is not None:
-            str_exc_value = str(other[1])
-            if not re.match(self.value_re, str_exc_value):
-                return Mismatch(
-                    '"%s" does not match "%s".'
-                    % (str_exc_value, self.value_re))
+            return self.value_re.match(other[1])
 
     def __str__(self):
         if self._is_instance:
@@ -729,7 +729,7 @@
 
     def match(self, value):
         if not re.match(self.pattern, value, self.flags):
-            return Mismatch("%r did not match %r" % (self.pattern, value))
+            return Mismatch("%r does not match %r" % (value, self.pattern))
 
 
 class MatchesSetwise(object):
@@ -837,7 +837,7 @@
             self._str_preprocessor(), self.matcher)
 
     def match(self, value):
-        value = self.preprocessor(value)
+        after = self.preprocessor(value)
         return Annotate(
-            "after %s" % self._str_preprocessor(),
-            self.matcher).match(value)
+            "after %s on %r" % (self._str_preprocessor(), value),
+            self.matcher).match(after)

=== 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 @@
 
     str_examples = [
         ("MatchesException(%r)" % Exception,
-         MatchesException(Exception))
+         MatchesException(Exception, 'fo.'))
         ]
     describe_examples = [
-        ('"bar" does not match "fo.".',
+        # XXX: This is kind of a crappy message. Need to change
+        # AfterPreproccessing.
+        ("'bar' does not match 'fo.': after <type 'str'> on ValueError('bar',)",
          error_bar, MatchesException(ValueError, "fo.")),
         ]
 
 
+class TestMatchesExceptionTypeMatcherInterface(TestCase, TestMatchersInterface):
+
+    matches_matcher = MatchesException(
+        ValueError, AfterPreproccessing(str, Equals('foo')))
+    error_foo = make_error(ValueError, 'foo')
+    error_sub = make_error(UnicodeError, 'foo')
+    error_bar = make_error(ValueError, 'bar')
+    matches_matches = [error_foo, error_sub]
+    matches_mismatches = [error_bar]
+
+    str_examples = [
+        ("MatchesException(%r)" % Exception,
+         MatchesException(Exception, Equals('foo')))
+        ]
+    describe_examples = [
+        ("5 != ValueError('bar',)",
+         error_bar, MatchesException(ValueError, Equals(5))),
+        ]
+
+
 class TestNotInterface(TestCase, TestMatchersInterface):
 
     matches_matcher = Not(Equals(1))
@@ -618,7 +640,7 @@
         ]
 
     describe_examples = [
-        ("'a|b' did not match 'c'", 'c', MatchesRegex('a|b')),
+        ("'c' does not match 'a|b'", 'c', MatchesRegex('a|b')),
         ]
 
 
@@ -713,7 +735,7 @@
         ]
 
     describe_examples = [
-        ("1 != 0: after <function parity>",
+        ("1 != 0: after <function parity> on 2",
          2,
          AfterPreproccessing(parity, Equals(1))),
         ]


Follow ups