c5cc44eb1d44548a7530ffd4a48468bef72ed00f
[kai/samba.git] / lib / testtools / testtools / tests / matchers / test_higherorder.py
1 # Copyright (c) 2008-2011 testtools developers. See LICENSE for details.
2
3 from testtools import TestCase
4 from testtools.matchers import (
5     DocTestMatches,
6     Equals,
7     LessThan,
8     MatchesStructure,
9     Mismatch,
10     NotEquals,
11     )
12 from testtools.matchers._higherorder import (
13     AfterPreprocessing,
14     AllMatch,
15     Annotate,
16     AnnotatedMismatch,
17     AnyMatch,
18     MatchesAny,
19     MatchesAll,
20     MatchesPredicate,
21     Not,
22     )
23 from testtools.tests.helpers import FullStackRunTest
24 from testtools.tests.matchers.helpers import TestMatchersInterface
25
26
27 class TestAllMatch(TestCase, TestMatchersInterface):
28
29     matches_matcher = AllMatch(LessThan(10))
30     matches_matches = [
31         [9, 9, 9],
32         (9, 9),
33         iter([9, 9, 9, 9, 9]),
34         ]
35     matches_mismatches = [
36         [11, 9, 9],
37         iter([9, 12, 9, 11]),
38         ]
39
40     str_examples = [
41         ("AllMatch(LessThan(12))", AllMatch(LessThan(12))),
42         ]
43
44     describe_examples = [
45         ('Differences: [\n'
46          '10 is not > 11\n'
47          '10 is not > 10\n'
48          ']',
49          [11, 9, 10],
50          AllMatch(LessThan(10))),
51         ]
52
53
54 class 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
86 class TestAfterPreprocessing(TestCase, TestMatchersInterface):
87
88     def parity(x):
89         return x % 2
90
91     matches_matcher = AfterPreprocessing(parity, Equals(1))
92     matches_matches = [3, 5]
93     matches_mismatches = [2]
94
95     str_examples = [
96         ("AfterPreprocessing(<function parity>, Equals(1))",
97          AfterPreprocessing(parity, Equals(1))),
98         ]
99
100     describe_examples = [
101         ("1 != 0: after <function parity> on 2", 2,
102          AfterPreprocessing(parity, Equals(1))),
103         ("1 != 0", 2,
104          AfterPreprocessing(parity, Equals(1), annotate=False)),
105         ]
106
107 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
108
109     matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
110     matches_matches = ["1", "2"]
111     matches_mismatches = ["3"]
112
113     str_examples = [(
114         "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
115         MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
116         ]
117
118     describe_examples = [("""Differences: [
119 Expected:
120     1
121 Got:
122     3
123
124 Expected:
125     2
126 Got:
127     3
128
129 ]""",
130         "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
131
132
133 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
134
135     matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
136     matches_matches = [3, 4]
137     matches_mismatches = [1, 2]
138
139     str_examples = [
140         ("MatchesAll(NotEquals(1), NotEquals(2))",
141          MatchesAll(NotEquals(1), NotEquals(2)))]
142
143     describe_examples = [
144         ("""Differences: [
145 1 == 1
146 ]""",
147          1, MatchesAll(NotEquals(1), NotEquals(2))),
148         ("1 == 1", 1,
149          MatchesAll(NotEquals(2), NotEquals(1), Equals(3), first_only=True)),
150         ]
151
152
153 class TestAnnotate(TestCase, TestMatchersInterface):
154
155     matches_matcher = Annotate("foo", Equals(1))
156     matches_matches = [1]
157     matches_mismatches = [2]
158
159     str_examples = [
160         ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]
161
162     describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
163
164     def test_if_message_no_message(self):
165         # Annotate.if_message returns the given matcher if there is no
166         # message.
167         matcher = Equals(1)
168         not_annotated = Annotate.if_message('', matcher)
169         self.assertIs(matcher, not_annotated)
170
171     def test_if_message_given_message(self):
172         # Annotate.if_message returns an annotated version of the matcher if a
173         # message is provided.
174         matcher = Equals(1)
175         expected = Annotate('foo', matcher)
176         annotated = Annotate.if_message('foo', matcher)
177         self.assertThat(
178             annotated,
179             MatchesStructure.fromExample(expected, 'annotation', 'matcher'))
180
181
182 class TestAnnotatedMismatch(TestCase):
183
184     run_tests_with = FullStackRunTest
185
186     def test_forwards_details(self):
187         x = Mismatch('description', {'foo': 'bar'})
188         annotated = AnnotatedMismatch("annotation", x)
189         self.assertEqual(x.get_details(), annotated.get_details())
190
191
192 class TestNotInterface(TestCase, TestMatchersInterface):
193
194     matches_matcher = Not(Equals(1))
195     matches_matches = [2]
196     matches_mismatches = [1]
197
198     str_examples = [
199         ("Not(Equals(1))", Not(Equals(1))),
200         ("Not(Equals('1'))", Not(Equals('1')))]
201
202     describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
203
204
205 def is_even(x):
206     return x % 2 == 0
207
208
209 class TestMatchesPredicate(TestCase, TestMatchersInterface):
210
211     matches_matcher = MatchesPredicate(is_even, "%s is not even")
212     matches_matches = [2, 4, 6, 8]
213     matches_mismatches = [3, 5, 7, 9]
214
215     str_examples = [
216         ("MatchesPredicate(%r, %r)" % (is_even, "%s is not even"),
217          MatchesPredicate(is_even, "%s is not even")),
218         ]
219
220     describe_examples = [
221         ('7 is not even', 7, MatchesPredicate(is_even, "%s is not even")),
222         ]
223
224
225 def test_suite():
226     from unittest import TestLoader
227     return TestLoader().loadTestsFromName(__name__)