testtools: Import latest upstream.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / test_matchers.py
1 # Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
2
3 """Tests for matchers."""
4
5 import doctest
6
7 from testtools import (
8     Matcher, # check that Matcher is exposed at the top level for docs.
9     TestCase,
10     )
11 from testtools.matchers import (
12     Annotate,
13     Equals,
14     DocTestMatches,
15     Is,
16     LessThan,
17     MatchesAny,
18     MatchesAll,
19     Mismatch,
20     Not,
21     NotEquals,
22     )
23
24 # Silence pyflakes.
25 Matcher
26
27
28 class TestMismatch(TestCase):
29
30     def test_constructor_arguments(self):
31         mismatch = Mismatch("some description", {'detail': "things"})
32         self.assertEqual("some description", mismatch.describe())
33         self.assertEqual({'detail': "things"}, mismatch.get_details())
34
35     def test_constructor_no_arguments(self):
36         mismatch = Mismatch()
37         self.assertRaises(NotImplementedError, mismatch.describe)
38         self.assertEqual({}, mismatch.get_details())
39
40
41 class TestMatchersInterface(object):
42
43     def test_matches_match(self):
44         matcher = self.matches_matcher
45         matches = self.matches_matches
46         mismatches = self.matches_mismatches
47         for candidate in matches:
48             self.assertEqual(None, matcher.match(candidate))
49         for candidate in mismatches:
50             mismatch = matcher.match(candidate)
51             self.assertNotEqual(None, mismatch)
52             self.assertNotEqual(None, getattr(mismatch, 'describe', None))
53
54     def test__str__(self):
55         # [(expected, object to __str__)].
56         examples = self.str_examples
57         for expected, matcher in examples:
58             self.assertThat(matcher, DocTestMatches(expected))
59
60     def test_describe_difference(self):
61         # [(expected, matchee, matcher), ...]
62         examples = self.describe_examples
63         for difference, matchee, matcher in examples:
64             mismatch = matcher.match(matchee)
65             self.assertEqual(difference, mismatch.describe())
66
67     def test_mismatch_details(self):
68         # The mismatch object must provide get_details, which must return a
69         # dictionary mapping names to Content objects.
70         examples = self.describe_examples
71         for difference, matchee, matcher in examples:
72             mismatch = matcher.match(matchee)
73             details = mismatch.get_details()
74             self.assertEqual(dict(details), details)
75
76
77 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
78
79     matches_matcher = DocTestMatches("Ran 1 test in ...s", doctest.ELLIPSIS)
80     matches_matches = ["Ran 1 test in 0.000s", "Ran 1 test in 1.234s"]
81     matches_mismatches = ["Ran 1 tests in 0.000s", "Ran 2 test in 0.000s"]
82
83     str_examples = [("DocTestMatches('Ran 1 test in ...s\\n')",
84         DocTestMatches("Ran 1 test in ...s")),
85         ("DocTestMatches('foo\\n', flags=8)", DocTestMatches("foo", flags=8)),
86         ]
87
88     describe_examples = [('Expected:\n    Ran 1 tests in ...s\nGot:\n'
89         '    Ran 1 test in 0.123s\n', "Ran 1 test in 0.123s",
90         DocTestMatches("Ran 1 tests in ...s", doctest.ELLIPSIS))]
91
92
93 class TestDocTestMatchesSpecific(TestCase):
94
95     def test___init__simple(self):
96         matcher = DocTestMatches("foo")
97         self.assertEqual("foo\n", matcher.want)
98
99     def test___init__flags(self):
100         matcher = DocTestMatches("bar\n", doctest.ELLIPSIS)
101         self.assertEqual("bar\n", matcher.want)
102         self.assertEqual(doctest.ELLIPSIS, matcher.flags)
103
104
105 class TestEqualsInterface(TestCase, TestMatchersInterface):
106
107     matches_matcher = Equals(1)
108     matches_matches = [1]
109     matches_mismatches = [2]
110
111     str_examples = [("Equals(1)", Equals(1)), ("Equals('1')", Equals('1'))]
112
113     describe_examples = [("1 != 2", 2, Equals(1))]
114
115
116 class TestNotEqualsInterface(TestCase, TestMatchersInterface):
117
118     matches_matcher = NotEquals(1)
119     matches_matches = [2]
120     matches_mismatches = [1]
121
122     str_examples = [
123         ("NotEquals(1)", NotEquals(1)), ("NotEquals('1')", NotEquals('1'))]
124
125     describe_examples = [("1 == 1", 1, NotEquals(1))]
126
127
128 class TestIsInterface(TestCase, TestMatchersInterface):
129
130     foo = object()
131     bar = object()
132
133     matches_matcher = Is(foo)
134     matches_matches = [foo]
135     matches_mismatches = [bar, 1]
136
137     str_examples = [("Is(2)", Is(2))]
138
139     describe_examples = [("1 is not 2", 2, Is(1))]
140
141
142 class TestLessThanInterface(TestCase, TestMatchersInterface):
143
144     matches_matcher = LessThan(4)
145     matches_matches = [-5, 3]
146     matches_mismatches = [4, 5, 5000]
147
148     str_examples = [
149         ("LessThan(12)", LessThan(12)),
150         ]
151
152     describe_examples = [('4 is >= 4', 4, LessThan(4))]
153
154
155 class TestNotInterface(TestCase, TestMatchersInterface):
156
157     matches_matcher = Not(Equals(1))
158     matches_matches = [2]
159     matches_mismatches = [1]
160
161     str_examples = [
162         ("Not(Equals(1))", Not(Equals(1))),
163         ("Not(Equals('1'))", Not(Equals('1')))]
164
165     describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
166
167
168 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
169
170     matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
171     matches_matches = ["1", "2"]
172     matches_mismatches = ["3"]
173
174     str_examples = [(
175         "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
176         MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
177         ]
178
179     describe_examples = [("""Differences: [
180 Expected:
181     1
182 Got:
183     3
184
185 Expected:
186     2
187 Got:
188     3
189
190 ]
191 """,
192         "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
193
194
195 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
196
197     matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
198     matches_matches = [3, 4]
199     matches_mismatches = [1, 2]
200
201     str_examples = [
202         ("MatchesAll(NotEquals(1), NotEquals(2))",
203          MatchesAll(NotEquals(1), NotEquals(2)))]
204
205     describe_examples = [("""Differences: [
206 1 == 1
207 ]
208 """,
209                           1, MatchesAll(NotEquals(1), NotEquals(2)))]
210
211
212 class TestAnnotate(TestCase, TestMatchersInterface):
213
214     matches_matcher = Annotate("foo", Equals(1))
215     matches_matches = [1]
216     matches_mismatches = [2]
217
218     str_examples = [
219         ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]
220
221     describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
222
223
224 def test_suite():
225     from unittest import TestLoader
226     return TestLoader().loadTestsFromName(__name__)