1 # Copyright (c) 2008-2011 testtools developers. See LICENSE for details.
3 from testtools import TestCase
4 from testtools.matchers import (
12 from testtools.matchers._higherorder import (
23 from testtools.tests.helpers import FullStackRunTest
24 from testtools.tests.matchers.helpers import TestMatchersInterface
27 class TestAllMatch(TestCase, TestMatchersInterface):
29 matches_matcher = AllMatch(LessThan(10))
33 iter([9, 9, 9, 9, 9]),
35 matches_mismatches = [
41 ("AllMatch(LessThan(12))", AllMatch(LessThan(12))),
50 AllMatch(LessThan(10))),
54 class TestAnyMatch(TestCase, TestMatchersInterface):
56 matches_matcher = AnyMatch(Equals('elephant'))
58 ['grass', 'cow', 'steak', 'milk', 'elephant'],
60 ['elephant', 'elephant', 'elephant'],
61 set(['hippo', 'rhino', 'elephant']),
63 matches_mismatches = [
65 ['grass', 'cow', 'steak', 'milk'],
67 ['element', 'hephalump', 'pachyderm'],
68 set(['hippo', 'rhino', 'diplodocus']),
72 ("AnyMatch(Equals('elephant'))", AnyMatch(Equals('elephant'))),
86 class TestAfterPreprocessing(TestCase, TestMatchersInterface):
91 matches_matcher = AfterPreprocessing(parity, Equals(1))
92 matches_matches = [3, 5]
93 matches_mismatches = [2]
96 ("AfterPreprocessing(<function parity>, Equals(1))",
97 AfterPreprocessing(parity, Equals(1))),
100 describe_examples = [
101 ("1 != 0: after <function parity> on 2", 2,
102 AfterPreprocessing(parity, Equals(1))),
104 AfterPreprocessing(parity, Equals(1), annotate=False)),
107 class TestMatchersAnyInterface(TestCase, TestMatchersInterface):
109 matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
110 matches_matches = ["1", "2"]
111 matches_mismatches = ["3"]
114 "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
115 MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
118 describe_examples = [("""Differences: [
130 "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]
133 class TestMatchesAllInterface(TestCase, TestMatchersInterface):
135 matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
136 matches_matches = [3, 4]
137 matches_mismatches = [1, 2]
140 ("MatchesAll(NotEquals(1), NotEquals(2))",
141 MatchesAll(NotEquals(1), NotEquals(2)))]
143 describe_examples = [
147 1, MatchesAll(NotEquals(1), NotEquals(2))),
149 MatchesAll(NotEquals(2), NotEquals(1), Equals(3), first_only=True)),
153 class TestAnnotate(TestCase, TestMatchersInterface):
155 matches_matcher = Annotate("foo", Equals(1))
156 matches_matches = [1]
157 matches_mismatches = [2]
160 ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]
162 describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
164 def test_if_message_no_message(self):
165 # Annotate.if_message returns the given matcher if there is no
168 not_annotated = Annotate.if_message('', matcher)
169 self.assertIs(matcher, not_annotated)
171 def test_if_message_given_message(self):
172 # Annotate.if_message returns an annotated version of the matcher if a
173 # message is provided.
175 expected = Annotate('foo', matcher)
176 annotated = Annotate.if_message('foo', matcher)
179 MatchesStructure.fromExample(expected, 'annotation', 'matcher'))
182 class TestAnnotatedMismatch(TestCase):
184 run_tests_with = FullStackRunTest
186 def test_forwards_details(self):
187 x = Mismatch('description', {'foo': 'bar'})
188 annotated = AnnotatedMismatch("annotation", x)
189 self.assertEqual(x.get_details(), annotated.get_details())
192 class TestNotInterface(TestCase, TestMatchersInterface):
194 matches_matcher = Not(Equals(1))
195 matches_matches = [2]
196 matches_mismatches = [1]
199 ("Not(Equals(1))", Not(Equals(1))),
200 ("Not(Equals('1'))", Not(Equals('1')))]
202 describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]
209 class TestMatchesPredicate(TestCase, TestMatchersInterface):
211 matches_matcher = MatchesPredicate(is_even, "%s is not even")
212 matches_matches = [2, 4, 6, 8]
213 matches_mismatches = [3, 5, 7, 9]
216 ("MatchesPredicate(%r, %r)" % (is_even, "%s is not even"),
217 MatchesPredicate(is_even, "%s is not even")),
220 describe_examples = [
221 ('7 is not even', 7, MatchesPredicate(is_even, "%s is not even")),
226 from unittest import TestLoader
227 return TestLoader().loadTestsFromName(__name__)