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 (
21 class TestMatchersInterface:
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))
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))
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())
48 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
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"]
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)),
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))]
64 class TestDocTestMatchesSpecific(TestCase):
66 def test___init__simple(self):
67 matcher = DocTestMatches("foo")
68 self.assertEqual("foo\n", matcher.want)
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)
76 class TestEqualsInterface(TestCase, TestMatchersInterface):
78 matches_matcher = Equals(1)
80 matches_mismatches = [2]
82 str_examples = [("Equals(1)", Equals(1)), ("Equals('1')", Equals('1'))]
84 describe_examples = [("1 != 2", 2, Equals(1))]
87 class TestNotEqualsInterface(TestCase, TestMatchersInterface):
89 matches_matcher = NotEquals(1)
91 matches_mismatches = [1]
94 ("NotEquals(1)", NotEquals(1)), ("NotEquals('1')", NotEquals('1'))]
96 describe_examples = [("1 == 1", 1, NotEquals(1))]
99 class TestNotInterface(TestCase, TestMatchersInterface):
101 matches_matcher = Not(Equals(1))
102 matches_matches = [2]
103 matches_mismatches = [1]
106 ("Not(Equals(1))", Not(Equals(1))),
107 ("Not(Equals('1'))", Not(Equals('1')))]
109 describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
112 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
114 matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
115 matches_matches = ["1", "2"]
116 matches_mismatches = ["3"]
119 "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
120 MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
123 describe_examples = [("""Differences: [
136 "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
139 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
141 matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
142 matches_matches = [3, 4]
143 matches_mismatches = [1, 2]
146 ("MatchesAll(NotEquals(1), NotEquals(2))",
147 MatchesAll(NotEquals(1), NotEquals(2)))]
149 describe_examples = [("""Differences: [
153 1, MatchesAll(NotEquals(1), NotEquals(2)))]
157 from unittest import TestLoader
158 return TestLoader().loadTestsFromName(__name__)