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