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 (
22 class TestMatchersInterface:
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))
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))
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())
49 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
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"]
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)),
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))]
65 class TestDocTestMatchesSpecific(TestCase):
67 def test___init__simple(self):
68 matcher = DocTestMatches("foo")
69 self.assertEqual("foo\n", matcher.want)
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)
77 class TestEqualsInterface(TestCase, TestMatchersInterface):
79 matches_matcher = Equals(1)
81 matches_mismatches = [2]
83 str_examples = [("Equals(1)", Equals(1)), ("Equals('1')", Equals('1'))]
85 describe_examples = [("1 != 2", 2, Equals(1))]
88 class TestNotEqualsInterface(TestCase, TestMatchersInterface):
90 matches_matcher = NotEquals(1)
92 matches_mismatches = [1]
95 ("NotEquals(1)", NotEquals(1)), ("NotEquals('1')", NotEquals('1'))]
97 describe_examples = [("1 == 1", 1, NotEquals(1))]
100 class TestNotInterface(TestCase, TestMatchersInterface):
102 matches_matcher = Not(Equals(1))
103 matches_matches = [2]
104 matches_mismatches = [1]
107 ("Not(Equals(1))", Not(Equals(1))),
108 ("Not(Equals('1'))", Not(Equals('1')))]
110 describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
113 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
115 matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
116 matches_matches = ["1", "2"]
117 matches_mismatches = ["3"]
120 "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
121 MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
124 describe_examples = [("""Differences: [
137 "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
140 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
142 matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
143 matches_matches = [3, 4]
144 matches_mismatches = [1, 2]
147 ("MatchesAll(NotEquals(1), NotEquals(2))",
148 MatchesAll(NotEquals(1), NotEquals(2)))]
150 describe_examples = [("""Differences: [
154 1, MatchesAll(NotEquals(1), NotEquals(2)))]
157 class TestAnnotate(TestCase, TestMatchersInterface):
159 matches_matcher = Annotate("foo", Equals(1))
160 matches_matches = [1]
161 matches_mismatches = [2]
164 ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]
166 describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
170 from unittest import TestLoader
171 return TestLoader().loadTestsFromName(__name__)