← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~jml/testtools/all-match-615108 into lp:testtools

 

Jonathan Lange has proposed merging lp:~jml/testtools/all-match-615108 into lp:testtools.

Requested reviews:
  testtools developers (testtools-dev)
Related bugs:
  Bug #615108 in testtools: "Please provide a way to match an iterable of matchees"
  https://bugs.launchpad.net/testtools/+bug/615108

For more details, see:
https://code.launchpad.net/~jml/testtools/all-match-615108/+merge/68532

Adds a new matcher that matches many things against a single matcher.
-- 
https://code.launchpad.net/~jml/testtools/all-match-615108/+merge/68532
Your team testtools developers is requested to review the proposed merge of lp:~jml/testtools/all-match-615108 into lp:testtools.
=== modified file 'NEWS'
--- NEWS	2011-07-20 08:48:46 +0000
+++ NEWS	2011-07-20 11:59:23 +0000
@@ -27,7 +27,12 @@
 * New convenience assertions, ``assertIsNone`` and ``assertIsNotNone``.
   (Christian Kampka)
 
-* New matcher, ``GreaterThan``. (Christian Kampka)
+* New matchers:
+
+  * ``AllMatch`` matches many values against a single matcher.
+    (Jonathan Lange, #615108)
+
+  * ``GreaterThan``. (Christian Kampka)
 
 
 0.9.11

=== modified file 'doc/for-test-authors.rst'
--- doc/for-test-authors.rst	2011-07-19 15:37:07 +0000
+++ doc/for-test-authors.rst	2011-07-20 11:59:23 +0000
@@ -539,6 +539,21 @@
       self.assertThat(42, MatchesAny(Equals(5), Not(Equals(6))))
 
 
+AllMatch
+~~~~~~~~
+
+Matches many values against a single matcher.  Can be used to make sure that
+many things all meet the same condition::
+
+  def test_all_match_example(self):
+      self.assertThat([2, 3, 5, 7], AllMatch(LessThan(10)))
+
+If the match fails, then all of the values that fail to match will be included
+in the error message.
+
+In some ways, this is the converse of MatchesAll_.
+
+
 MatchesListwise
 ~~~~~~~~~~~~~~~
 

=== modified file 'testtools/matchers.py'
--- testtools/matchers.py	2011-07-20 08:48:46 +0000
+++ testtools/matchers.py	2011-07-20 11:59:23 +0000
@@ -13,6 +13,7 @@
 __metaclass__ = type
 __all__ = [
     'AfterPreproccessing',
+    'AllMatch',
     'Annotate',
     'DocTestMatches',
     'EndsWith',
@@ -841,3 +842,22 @@
         return Annotate(
             "after %s" % self._str_preprocessor(),
             self.matcher).match(value)
+
+
+class AllMatch(object):
+    """Matches if all provided values match the given matcher."""
+
+    def __init__(self, matcher):
+        self.matcher = matcher
+
+    def __str__(self):
+        return 'AllMatch(%s)' % (self.matcher,)
+
+    def match(self, values):
+        mismatches = []
+        for value in values:
+            mismatch = self.matcher.match(value)
+            if mismatch:
+                mismatches.append(mismatch)
+        if mismatches:
+            return MismatchesAll(mismatches)

=== 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 11:59:23 +0000
@@ -15,6 +15,7 @@
     )
 from testtools.matchers import (
     AfterPreproccessing,
+    AllMatch,
     Annotate,
     AnnotatedMismatch,
     Equals,
@@ -739,6 +740,33 @@
             repr(decorated))
 
 
+class TestAllMatch(TestCase, TestMatchersInterface):
+
+    matches_matcher = AllMatch(LessThan(10))
+    matches_matches = [
+        [9, 9, 9],
+        (9, 9),
+        iter([9, 9, 9, 9, 9]),
+        ]
+    matches_mismatches = [
+        [11, 9, 9],
+        iter([9, 12, 9, 11]),
+        ]
+
+    str_examples = [
+        ("AllMatch(LessThan(12))", AllMatch(LessThan(12))),
+        ]
+
+    describe_examples = [
+        ('Differences: [\n'
+         '10 is not > 11\n'
+         '10 is not > 10\n'
+         ']',
+         [11, 9, 10],
+         AllMatch(LessThan(10))),
+        ]
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)


Follow ups