Merge lp:~jml/testtools/all-match-615108 into lp:~testtools-committers/testtools/trunk

Proposed by Jonathan Lange
Status: Merged
Approved by: Robert Collins
Approved revision: 206
Merged at revision: 208
Proposed branch: lp:~jml/testtools/all-match-615108
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 124 lines (+69/-1)
4 files modified
NEWS (+6/-1)
doc/for-test-authors.rst (+15/-0)
testtools/matchers.py (+20/-0)
testtools/tests/test_matchers.py (+28/-0)
To merge this branch: bzr merge lp:~jml/testtools/all-match-615108
Reviewer Review Type Date Requested Status
Robert Collins Approve
Review via email: mp+68532@code.launchpad.net

Description of the change

Adds a new matcher that matches many things against a single matcher.

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

Love the name, hate the name.

'ContentsMatch' ?

review: Approve
Revision history for this message
Jonathan Lange (jml) wrote :

I like the name. Merged as-is. Tbh, I reckon we should do a bit of a review of the names of the matchers anyway, maybe bringing them in-line with hamcrest.

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 11:59:23 +0000
@@ -27,7 +27,12 @@
27* New convenience assertions, ``assertIsNone`` and ``assertIsNotNone``.27* New convenience assertions, ``assertIsNone`` and ``assertIsNotNone``.
28 (Christian Kampka)28 (Christian Kampka)
2929
30* New matcher, ``GreaterThan``. (Christian Kampka)30* New matchers:
31
32 * ``AllMatch`` matches many values against a single matcher.
33 (Jonathan Lange, #615108)
34
35 * ``GreaterThan``. (Christian Kampka)
3136
3237
330.9.11380.9.11
3439
=== 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 @@
539 self.assertThat(42, MatchesAny(Equals(5), Not(Equals(6))))539 self.assertThat(42, MatchesAny(Equals(5), Not(Equals(6))))
540540
541541
542AllMatch
543~~~~~~~~
544
545Matches many values against a single matcher. Can be used to make sure that
546many things all meet the same condition::
547
548 def test_all_match_example(self):
549 self.assertThat([2, 3, 5, 7], AllMatch(LessThan(10)))
550
551If the match fails, then all of the values that fail to match will be included
552in the error message.
553
554In some ways, this is the converse of MatchesAll_.
555
556
542MatchesListwise557MatchesListwise
543~~~~~~~~~~~~~~~558~~~~~~~~~~~~~~~
544559
545560
=== 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 @@
13__metaclass__ = type13__metaclass__ = type
14__all__ = [14__all__ = [
15 'AfterPreproccessing',15 'AfterPreproccessing',
16 'AllMatch',
16 'Annotate',17 'Annotate',
17 'DocTestMatches',18 'DocTestMatches',
18 'EndsWith',19 'EndsWith',
@@ -841,3 +842,22 @@
841 return Annotate(842 return Annotate(
842 "after %s" % self._str_preprocessor(),843 "after %s" % self._str_preprocessor(),
843 self.matcher).match(value)844 self.matcher).match(value)
845
846
847class AllMatch(object):
848 """Matches if all provided values match the given matcher."""
849
850 def __init__(self, matcher):
851 self.matcher = matcher
852
853 def __str__(self):
854 return 'AllMatch(%s)' % (self.matcher,)
855
856 def match(self, values):
857 mismatches = []
858 for value in values:
859 mismatch = self.matcher.match(value)
860 if mismatch:
861 mismatches.append(mismatch)
862 if mismatches:
863 return MismatchesAll(mismatches)
844864
=== 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 @@
15 )15 )
16from testtools.matchers import (16from testtools.matchers import (
17 AfterPreproccessing,17 AfterPreproccessing,
18 AllMatch,
18 Annotate,19 Annotate,
19 AnnotatedMismatch,20 AnnotatedMismatch,
20 Equals,21 Equals,
@@ -739,6 +740,33 @@
739 repr(decorated))740 repr(decorated))
740741
741742
743class TestAllMatch(TestCase, TestMatchersInterface):
744
745 matches_matcher = AllMatch(LessThan(10))
746 matches_matches = [
747 [9, 9, 9],
748 (9, 9),
749 iter([9, 9, 9, 9, 9]),
750 ]
751 matches_mismatches = [
752 [11, 9, 9],
753 iter([9, 12, 9, 11]),
754 ]
755
756 str_examples = [
757 ("AllMatch(LessThan(12))", AllMatch(LessThan(12))),
758 ]
759
760 describe_examples = [
761 ('Differences: [\n'
762 '10 is not > 11\n'
763 '10 is not > 10\n'
764 ']',
765 [11, 9, 10],
766 AllMatch(LessThan(10))),
767 ]
768
769
742def test_suite():770def test_suite():
743 from unittest import TestLoader771 from unittest import TestLoader
744 return TestLoader().loadTestsFromName(__name__)772 return TestLoader().loadTestsFromName(__name__)

Subscribers

People subscribed via source and target branches