1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
3 """Tests for matchers."""
5 from testtools import (
6 Matcher, # check that Matcher is exposed at the top level for docs.
9 from testtools.compat import (
14 from testtools.matchers import (
19 from testtools.matchers._impl import (
24 from testtools.tests.helpers import FullStackRunTest
30 class TestMismatch(TestCase):
32 run_tests_with = FullStackRunTest
34 def test_constructor_arguments(self):
35 mismatch = Mismatch("some description", {'detail': "things"})
36 self.assertEqual("some description", mismatch.describe())
37 self.assertEqual({'detail': "things"}, mismatch.get_details())
39 def test_constructor_no_arguments(self):
41 self.assertThat(mismatch.describe,
42 Raises(MatchesException(NotImplementedError)))
43 self.assertEqual({}, mismatch.get_details())
46 class TestMismatchError(TestCase):
48 def test_is_assertion_error(self):
49 # MismatchError is an AssertionError, so that most of the time, it
50 # looks like a test failure, rather than an error.
51 def raise_mismatch_error():
52 raise MismatchError(2, Equals(3), Equals(3).match(2))
53 self.assertRaises(AssertionError, raise_mismatch_error)
55 def test_default_description_is_mismatch(self):
56 mismatch = Equals(3).match(2)
57 e = MismatchError(2, Equals(3), mismatch)
58 self.assertEqual(mismatch.describe(), str(e))
60 def test_default_description_unicode(self):
62 matcher = Equals(_u('a'))
63 mismatch = matcher.match(matchee)
64 e = MismatchError(matchee, matcher, mismatch)
65 self.assertEqual(mismatch.describe(), str(e))
67 def test_verbose_description(self):
70 mismatch = matcher.match(2)
71 e = MismatchError(matchee, matcher, mismatch, True)
73 'Match failed. Matchee: %r\n'
75 'Difference: %s\n' % (
78 matcher.match(matchee).describe(),
80 self.assertEqual(expected, str(e))
82 def test_verbose_unicode(self):
83 # When assertThat is given matchees or matchers that contain non-ASCII
84 # unicode strings, we can still provide a meaningful error.
86 matcher = Equals(_u('a'))
87 mismatch = matcher.match(matchee)
89 'Match failed. Matchee: %s\n'
91 'Difference: %s\n' % (
96 e = MismatchError(matchee, matcher, mismatch, True)
101 # Using str() should still work, and return ascii only
103 expected.replace(matchee, matchee.encode("unicode-escape")),
104 str(e).decode("ascii"))
105 self.assertEqual(expected, actual)
108 class TestMismatchDecorator(TestCase):
110 run_tests_with = FullStackRunTest
112 def test_forwards_description(self):
113 x = Mismatch("description", {'foo': 'bar'})
114 decorated = MismatchDecorator(x)
115 self.assertEqual(x.describe(), decorated.describe())
117 def test_forwards_details(self):
118 x = Mismatch("description", {'foo': 'bar'})
119 decorated = MismatchDecorator(x)
120 self.assertEqual(x.get_details(), decorated.get_details())
123 x = Mismatch("description", {'foo': 'bar'})
124 decorated = MismatchDecorator(x)
126 '<testtools.matchers.MismatchDecorator(%r)>' % (x,),
131 from unittest import TestLoader
132 return TestLoader().loadTestsFromName(__name__)