1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
7 from testtools import TestCase
8 from testtools.compat import StringIO
9 from testtools.matchers import (
16 from testtools.matchers._datastructures import (
22 from testtools.tests.helpers import FullStackRunTest
23 from testtools.tests.matchers.helpers import TestMatchersInterface
26 def run_doctest(obj, name):
27 p = doctest.DocTestParser()
29 obj.__doc__, sys.modules[obj.__module__].__dict__, name, '', 0)
30 r = doctest.DocTestRunner()
32 r.run(t, out=output.write)
33 return r.failures, output.getvalue()
36 class TestMatchesListwise(TestCase):
38 run_tests_with = FullStackRunTest
40 def test_docstring(self):
41 failure_count, output = run_doctest(
42 MatchesListwise, "MatchesListwise")
44 self.fail("Doctest failed with %s" % output)
47 class TestMatchesStructure(TestCase, TestMatchersInterface):
50 def __init__(self, x, y):
54 matches_matcher = MatchesStructure(x=Equals(1), y=Equals(2))
55 matches_matches = [SimpleClass(1, 2)]
56 matches_mismatches = [
63 ("MatchesStructure(x=Equals(1))", MatchesStructure(x=Equals(1))),
64 ("MatchesStructure(y=Equals(2))", MatchesStructure(y=Equals(2))),
65 ("MatchesStructure(x=Equals(1), y=Equals(2))",
66 MatchesStructure(x=Equals(1), y=Equals(2))),
73 ]""", SimpleClass(1, 2), MatchesStructure(x=Equals(3), y=Equals(2))),
77 ]""", SimpleClass(1, 2), MatchesStructure(x=Equals(1), y=Equals(3))),
82 ]""", SimpleClass(1, 2), MatchesStructure(x=Equals(0), y=Equals(0))),
85 def test_fromExample(self):
87 self.SimpleClass(1, 2),
88 MatchesStructure.fromExample(self.SimpleClass(1, 3), 'x'))
90 def test_byEquality(self):
92 self.SimpleClass(1, 2),
93 MatchesStructure.byEquality(x=1))
95 def test_withStructure(self):
97 self.SimpleClass(1, 2),
98 MatchesStructure.byMatcher(LessThan, x=2))
100 def test_update(self):
102 self.SimpleClass(1, 2),
103 MatchesStructure(x=NotEquals(1)).update(x=Equals(1)))
105 def test_update_none(self):
107 self.SimpleClass(1, 2),
108 MatchesStructure(x=Equals(1), z=NotEquals(42)).update(
112 class TestMatchesSetwise(TestCase):
114 run_tests_with = FullStackRunTest
116 def assertMismatchWithDescriptionMatching(self, value, matcher,
117 description_matcher):
118 mismatch = matcher.match(value)
120 self.fail("%s matched %s" % (matcher, value))
121 actual_description = mismatch.describe()
125 "%s matching %s" % (matcher, value),
126 description_matcher))
128 def test_matches(self):
130 None, MatchesSetwise(Equals(1), Equals(2)).match([2, 1]))
132 def test_mismatches(self):
133 self.assertMismatchWithDescriptionMatching(
134 [2, 3], MatchesSetwise(Equals(1), Equals(2)),
135 MatchesRegex('.*There was 1 mismatch$', re.S))
137 def test_too_many_matchers(self):
138 self.assertMismatchWithDescriptionMatching(
139 [2, 3], MatchesSetwise(Equals(1), Equals(2), Equals(3)),
140 Equals('There was 1 matcher left over: Equals(1)'))
142 def test_too_many_values(self):
143 self.assertMismatchWithDescriptionMatching(
144 [1, 2, 3], MatchesSetwise(Equals(1), Equals(2)),
145 Equals('There was 1 value left over: [3]'))
147 def test_two_too_many_matchers(self):
148 self.assertMismatchWithDescriptionMatching(
149 [3], MatchesSetwise(Equals(1), Equals(2), Equals(3)),
151 'There were 2 matchers left over: Equals\([12]\), '
154 def test_two_too_many_values(self):
155 self.assertMismatchWithDescriptionMatching(
156 [1, 2, 3, 4], MatchesSetwise(Equals(1), Equals(2)),
158 'There were 2 values left over: \[[34], [34]\]'))
160 def test_mismatch_and_too_many_matchers(self):
161 self.assertMismatchWithDescriptionMatching(
162 [2, 3], MatchesSetwise(Equals(0), Equals(1), Equals(2)),
164 '.*There was 1 mismatch and 1 extra matcher: Equals\([01]\)',
167 def test_mismatch_and_too_many_values(self):
168 self.assertMismatchWithDescriptionMatching(
169 [2, 3, 4], MatchesSetwise(Equals(1), Equals(2)),
171 '.*There was 1 mismatch and 1 extra value: \[[34]\]',
174 def test_mismatch_and_two_too_many_matchers(self):
175 self.assertMismatchWithDescriptionMatching(
176 [3, 4], MatchesSetwise(
177 Equals(0), Equals(1), Equals(2), Equals(3)),
179 '.*There was 1 mismatch and 2 extra matchers: '
180 'Equals\([012]\), Equals\([012]\)', re.S))
182 def test_mismatch_and_two_too_many_values(self):
183 self.assertMismatchWithDescriptionMatching(
184 [2, 3, 4, 5], MatchesSetwise(Equals(1), Equals(2)),
186 '.*There was 1 mismatch and 2 extra values: \[[145], [145]\]',
190 class TestContainsAllInterface(TestCase, TestMatchersInterface):
192 matches_matcher = ContainsAll(['foo', 'bar'])
193 matches_matches = [['foo', 'bar'], ['foo', 'z', 'bar'], ['bar', 'foo']]
194 matches_mismatches = [['f', 'g'], ['foo', 'baz'], []]
197 "MatchesAll(Contains('foo'), Contains('bar'))",
198 ContainsAll(['foo', 'bar'])),
201 describe_examples = [("""Differences: [
204 'foo', ContainsAll(['foo', 'baz']))]
208 from unittest import TestLoader
209 return TestLoader().loadTestsFromName(__name__)