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

Proposed by Jonathan Lange
Status: Merged
Merged at revision: 294
Proposed branch: lp:~jml/testtools/any-match
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 99 lines (+60/-0)
3 files modified
NEWS (+7/-0)
testtools/matchers/_higherorder.py (+20/-0)
testtools/tests/matchers/test_higherorder.py (+33/-0)
To merge this branch: bzr merge lp:~jml/testtools/any-match
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+139720@code.launchpad.net

Commit message

Add new matcher, AnyMatch

Description of the change

Deep in the yak stack I go.

This adds a new matcher, AnyMatch. It's a lot like AllMatch, but where AllMatch
guarantees that all items in a collection match a given matcher, this checks that
just one of them do.

Contains(x) is equivalent to AnyMatch(Equals(x)).

Feels a lot like we're dancing around a deeper issue in Python.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

Looks good. The name is somewhat confusing considering we also have MatchesAny, but it is consistent with the already existing AllMatch (and MatchesAll).

I agree there's a deeper issue here, but until we put a finger on what it is exactly and what the alternatives are this seems reasonable.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'NEWS'
--- NEWS 2012-12-10 23:48:16 +0000
+++ NEWS 2012-12-13 15:10:43 +0000
@@ -6,6 +6,13 @@
6NEXT6NEXT
7~~~~7~~~~
88
9Improvements
10------------
11
12* ``AnyMatch`` added, a new matcher that matches when any item in a collection
13 matches the given matcher. (Jonathan Lange)
14
15
90.9.22160.9.22
10~~~~~~17~~~~~~
1118
1219
=== modified file 'testtools/matchers/_higherorder.py'
--- testtools/matchers/_higherorder.py 2012-09-10 11:37:46 +0000
+++ testtools/matchers/_higherorder.py 2012-12-13 15:10:43 +0000
@@ -236,6 +236,26 @@
236 return MismatchesAll(mismatches)236 return MismatchesAll(mismatches)
237237
238238
239class AnyMatch(object):
240 """Matches if any of the provided values match the given matcher."""
241
242 def __init__(self, matcher):
243 self.matcher = matcher
244
245 def __str__(self):
246 return 'AnyMatch(%s)' % (self.matcher,)
247
248 def match(self, values):
249 mismatches = []
250 for value in values:
251 mismatch = self.matcher.match(value)
252 if mismatch:
253 mismatches.append(mismatch)
254 else:
255 return None
256 return MismatchesAll(mismatches)
257
258
239class MatchesPredicate(Matcher):259class MatchesPredicate(Matcher):
240 """Match if a given function returns True.260 """Match if a given function returns True.
241261
242262
=== modified file 'testtools/tests/matchers/test_higherorder.py'
--- testtools/tests/matchers/test_higherorder.py 2012-09-08 17:21:06 +0000
+++ testtools/tests/matchers/test_higherorder.py 2012-12-13 15:10:43 +0000
@@ -14,6 +14,7 @@
14 AllMatch,14 AllMatch,
15 Annotate,15 Annotate,
16 AnnotatedMismatch,16 AnnotatedMismatch,
17 AnyMatch,
17 MatchesAny,18 MatchesAny,
18 MatchesAll,19 MatchesAll,
19 MatchesPredicate,20 MatchesPredicate,
@@ -50,6 +51,38 @@
50 ]51 ]
5152
5253
54class TestAnyMatch(TestCase, TestMatchersInterface):
55
56 matches_matcher = AnyMatch(Equals('elephant'))
57 matches_matches = [
58 ['grass', 'cow', 'steak', 'milk', 'elephant'],
59 (13, 'elephant'),
60 ['elephant', 'elephant', 'elephant'],
61 set(['hippo', 'rhino', 'elephant']),
62 ]
63 matches_mismatches = [
64 [],
65 ['grass', 'cow', 'steak', 'milk'],
66 (13, 12, 10),
67 ['element', 'hephalump', 'pachyderm'],
68 set(['hippo', 'rhino', 'diplodocus']),
69 ]
70
71 str_examples = [
72 ("AnyMatch(Equals('elephant'))", AnyMatch(Equals('elephant'))),
73 ]
74
75 describe_examples = [
76 ('Differences: [\n'
77 '7 != 11\n'
78 '7 != 9\n'
79 '7 != 10\n'
80 ']',
81 [11, 9, 10],
82 AnyMatch(Equals(7))),
83 ]
84
85
53class TestAfterPreprocessing(TestCase, TestMatchersInterface):86class TestAfterPreprocessing(TestCase, TestMatchersInterface):
5487
55 def parity(x):88 def parity(x):

Subscribers

People subscribed via source and target branches