testtools: Fix included testtools, for systems that don't have it.
[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     MatchesAny,
16     MatchesAll,
17     Not,
18     NotEquals,
19     )
20
21
22 class TestMatchersInterface:
23
24     def test_matches_match(self):
25         matcher = self.matches_matcher
26         matches = self.matches_matches
27         mismatches = self.matches_mismatches
28         for candidate in matches:
29             self.assertEqual(None, matcher.match(candidate))
30         for candidate in mismatches:
31             mismatch = matcher.match(candidate)
32             self.assertNotEqual(None, mismatch)
33             self.assertNotEqual(None, getattr(mismatch, 'describe', None))
34
35     def test__str__(self):
36         # [(expected, object to __str__)].
37         examples = self.str_examples
38         for expected, matcher in examples:
39             self.assertThat(matcher, DocTestMatches(expected))
40
41     def test_describe_difference(self):
42         # [(expected, matchee, matcher), ...]
43         examples = self.describe_examples
44         for difference, matchee, matcher in examples:
45             mismatch = matcher.match(matchee)
46             self.assertEqual(difference, mismatch.describe())
47
48
49 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
50
51     matches_matcher = DocTestMatches("Ran 1 test in ...s", doctest.ELLIPSIS)
52     matches_matches = ["Ran 1 test in 0.000s", "Ran 1 test in 1.234s"]
53     matches_mismatches = ["Ran 1 tests in 0.000s", "Ran 2 test in 0.000s"]
54
55     str_examples = [("DocTestMatches('Ran 1 test in ...s\\n')",
56         DocTestMatches("Ran 1 test in ...s")),
57         ("DocTestMatches('foo\\n', flags=8)", DocTestMatches("foo", flags=8)),
58         ]
59
60     describe_examples = [('Expected:\n    Ran 1 tests in ...s\nGot:\n'
61         '    Ran 1 test in 0.123s\n', "Ran 1 test in 0.123s",
62         DocTestMatches("Ran 1 tests in ...s", doctest.ELLIPSIS))]
63
64
65 class TestDocTestMatchesSpecific(TestCase):
66
67     def test___init__simple(self):
68         matcher = DocTestMatches("foo")
69         self.assertEqual("foo\n", matcher.want)
70
71     def test___init__flags(self):
72         matcher = DocTestMatches("bar\n", doctest.ELLIPSIS)
73         self.assertEqual("bar\n", matcher.want)
74         self.assertEqual(doctest.ELLIPSIS, matcher.flags)
75
76
77 class TestEqualsInterface(TestCase, TestMatchersInterface):
78
79     matches_matcher = Equals(1)
80     matches_matches = [1]
81     matches_mismatches = [2]
82
83     str_examples = [("Equals(1)", Equals(1)), ("Equals('1')", Equals('1'))]
84
85     describe_examples = [("1 != 2", 2, Equals(1))]
86
87
88 class TestNotEqualsInterface(TestCase, TestMatchersInterface):
89
90     matches_matcher = NotEquals(1)
91     matches_matches = [2]
92     matches_mismatches = [1]
93
94     str_examples = [
95         ("NotEquals(1)", NotEquals(1)), ("NotEquals('1')", NotEquals('1'))]
96
97     describe_examples = [("1 == 1", 1, NotEquals(1))]
98
99
100 class TestNotInterface(TestCase, TestMatchersInterface):
101
102     matches_matcher = Not(Equals(1))
103     matches_matches = [2]
104     matches_mismatches = [1]
105
106     str_examples = [
107         ("Not(Equals(1))", Not(Equals(1))),
108         ("Not(Equals('1'))", Not(Equals('1')))]
109
110     describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
111
112
113 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
114
115     matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
116     matches_matches = ["1", "2"]
117     matches_mismatches = ["3"]
118
119     str_examples = [(
120         "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
121         MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
122         ]
123
124     describe_examples = [("""Differences: [
125 Expected:
126     1
127 Got:
128     3
129
130 Expected:
131     2
132 Got:
133     3
134
135 ]
136 """,
137         "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
138
139
140 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
141
142     matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
143     matches_matches = [3, 4]
144     matches_mismatches = [1, 2]
145
146     str_examples = [
147         ("MatchesAll(NotEquals(1), NotEquals(2))",
148          MatchesAll(NotEquals(1), NotEquals(2)))]
149
150     describe_examples = [("""Differences: [
151 1 == 1
152 ]
153 """,
154                           1, MatchesAll(NotEquals(1), NotEquals(2)))]
155
156
157 class TestAnnotate(TestCase, TestMatchersInterface):
158
159     matches_matcher = Annotate("foo", Equals(1))
160     matches_matches = [1]
161     matches_mismatches = [2]
162
163     str_examples = [
164         ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]
165
166     describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
167
168
169 def test_suite():
170     from unittest import TestLoader
171     return TestLoader().loadTestsFromName(__name__)