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
1=== modified file 'NEWS'
2--- NEWS 2011-07-20 08:48:46 +0000
3+++ NEWS 2011-07-20 11:59:23 +0000
4@@ -27,7 +27,12 @@
5 * New convenience assertions, ``assertIsNone`` and ``assertIsNotNone``.
6 (Christian Kampka)
7
8-* New matcher, ``GreaterThan``. (Christian Kampka)
9+* New matchers:
10+
11+ * ``AllMatch`` matches many values against a single matcher.
12+ (Jonathan Lange, #615108)
13+
14+ * ``GreaterThan``. (Christian Kampka)
15
16
17 0.9.11
18
19=== modified file 'doc/for-test-authors.rst'
20--- doc/for-test-authors.rst 2011-07-19 15:37:07 +0000
21+++ doc/for-test-authors.rst 2011-07-20 11:59:23 +0000
22@@ -539,6 +539,21 @@
23 self.assertThat(42, MatchesAny(Equals(5), Not(Equals(6))))
24
25
26+AllMatch
27+~~~~~~~~
28+
29+Matches many values against a single matcher. Can be used to make sure that
30+many things all meet the same condition::
31+
32+ def test_all_match_example(self):
33+ self.assertThat([2, 3, 5, 7], AllMatch(LessThan(10)))
34+
35+If the match fails, then all of the values that fail to match will be included
36+in the error message.
37+
38+In some ways, this is the converse of MatchesAll_.
39+
40+
41 MatchesListwise
42 ~~~~~~~~~~~~~~~
43
44
45=== modified file 'testtools/matchers.py'
46--- testtools/matchers.py 2011-07-20 08:48:46 +0000
47+++ testtools/matchers.py 2011-07-20 11:59:23 +0000
48@@ -13,6 +13,7 @@
49 __metaclass__ = type
50 __all__ = [
51 'AfterPreproccessing',
52+ 'AllMatch',
53 'Annotate',
54 'DocTestMatches',
55 'EndsWith',
56@@ -841,3 +842,22 @@
57 return Annotate(
58 "after %s" % self._str_preprocessor(),
59 self.matcher).match(value)
60+
61+
62+class AllMatch(object):
63+ """Matches if all provided values match the given matcher."""
64+
65+ def __init__(self, matcher):
66+ self.matcher = matcher
67+
68+ def __str__(self):
69+ return 'AllMatch(%s)' % (self.matcher,)
70+
71+ def match(self, values):
72+ mismatches = []
73+ for value in values:
74+ mismatch = self.matcher.match(value)
75+ if mismatch:
76+ mismatches.append(mismatch)
77+ if mismatches:
78+ return MismatchesAll(mismatches)
79
80=== modified file 'testtools/tests/test_matchers.py'
81--- testtools/tests/test_matchers.py 2011-07-20 08:46:46 +0000
82+++ testtools/tests/test_matchers.py 2011-07-20 11:59:23 +0000
83@@ -15,6 +15,7 @@
84 )
85 from testtools.matchers import (
86 AfterPreproccessing,
87+ AllMatch,
88 Annotate,
89 AnnotatedMismatch,
90 Equals,
91@@ -739,6 +740,33 @@
92 repr(decorated))
93
94
95+class TestAllMatch(TestCase, TestMatchersInterface):
96+
97+ matches_matcher = AllMatch(LessThan(10))
98+ matches_matches = [
99+ [9, 9, 9],
100+ (9, 9),
101+ iter([9, 9, 9, 9, 9]),
102+ ]
103+ matches_mismatches = [
104+ [11, 9, 9],
105+ iter([9, 12, 9, 11]),
106+ ]
107+
108+ str_examples = [
109+ ("AllMatch(LessThan(12))", AllMatch(LessThan(12))),
110+ ]
111+
112+ describe_examples = [
113+ ('Differences: [\n'
114+ '10 is not > 11\n'
115+ '10 is not > 10\n'
116+ ']',
117+ [11, 9, 10],
118+ AllMatch(LessThan(10))),
119+ ]
120+
121+
122 def test_suite():
123 from unittest import TestLoader
124 return TestLoader().loadTestsFromName(__name__)

Subscribers

People subscribed via source and target branches