1 # Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
3 """Tests for matchers."""
7 from testtools import (
8 Matcher, # check that Matcher is exposed at the top level for docs.
11 from testtools.matchers import (
28 class TestMismatch(TestCase):
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())
35 def test_constructor_no_arguments(self):
37 self.assertRaises(NotImplementedError, mismatch.describe)
38 self.assertEqual({}, mismatch.get_details())
41 class TestMatchersInterface(object):
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))
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))
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())
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)
77 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
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"]
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)),
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))]
93 class TestDocTestMatchesSpecific(TestCase):
95 def test___init__simple(self):
96 matcher = DocTestMatches("foo")
97 self.assertEqual("foo\n", matcher.want)
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)
105 class TestEqualsInterface(TestCase, TestMatchersInterface):
107 matches_matcher = Equals(1)
108 matches_matches = [1]
109 matches_mismatches = [2]
111 str_examples = [("Equals(1)", Equals(1)), ("Equals('1')", Equals('1'))]
113 describe_examples = [("1 != 2", 2, Equals(1))]
116 class TestNotEqualsInterface(TestCase, TestMatchersInterface):
118 matches_matcher = NotEquals(1)
119 matches_matches = [2]
120 matches_mismatches = [1]
123 ("NotEquals(1)", NotEquals(1)), ("NotEquals('1')", NotEquals('1'))]
125 describe_examples = [("1 == 1", 1, NotEquals(1))]
128 class TestIsInterface(TestCase, TestMatchersInterface):
133 matches_matcher = Is(foo)
134 matches_matches = [foo]
135 matches_mismatches = [bar, 1]
137 str_examples = [("Is(2)", Is(2))]
139 describe_examples = [("1 is not 2", 2, Is(1))]
142 class TestLessThanInterface(TestCase, TestMatchersInterface):
144 matches_matcher = LessThan(4)
145 matches_matches = [-5, 3]
146 matches_mismatches = [4, 5, 5000]
149 ("LessThan(12)", LessThan(12)),
152 describe_examples = [('4 is >= 4', 4, LessThan(4))]
155 class TestNotInterface(TestCase, TestMatchersInterface):
157 matches_matcher = Not(Equals(1))
158 matches_matches = [2]
159 matches_mismatches = [1]
162 ("Not(Equals(1))", Not(Equals(1))),
163 ("Not(Equals('1'))", Not(Equals('1')))]
165 describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
168 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
170 matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
171 matches_matches = ["1", "2"]
172 matches_mismatches = ["3"]
175 "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
176 MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
179 describe_examples = [("""Differences: [
192 "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
195 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
197 matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
198 matches_matches = [3, 4]
199 matches_mismatches = [1, 2]
202 ("MatchesAll(NotEquals(1), NotEquals(2))",
203 MatchesAll(NotEquals(1), NotEquals(2)))]
205 describe_examples = [("""Differences: [
209 1, MatchesAll(NotEquals(1), NotEquals(2)))]
212 class TestAnnotate(TestCase, TestMatchersInterface):
214 matches_matcher = Annotate("foo", Equals(1))
215 matches_matches = [1]
216 matches_mismatches = [2]
219 ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]
221 describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
225 from unittest import TestLoader
226 return TestLoader().loadTestsFromName(__name__)