Remove bundled subunit.
[samba.git] / lib / testtools / testtools / tests / matchers / test_impl.py
1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
2
3 """Tests for matchers."""
4
5 from testtools import (
6     Matcher, # check that Matcher is exposed at the top level for docs.
7     TestCase,
8     )
9 from testtools.compat import (
10     str_is_unicode,
11     text_repr,
12     _u,
13     )
14 from testtools.matchers import (
15     Equals,
16     MatchesException,
17     Raises,
18     )
19 from testtools.matchers._impl import (
20     Mismatch,
21     MismatchDecorator,
22     MismatchError,
23     )
24 from testtools.tests.helpers import FullStackRunTest
25
26 # Silence pyflakes.
27 Matcher
28
29
30 class TestMismatch(TestCase):
31
32     run_tests_with = FullStackRunTest
33
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())
38
39     def test_constructor_no_arguments(self):
40         mismatch = Mismatch()
41         self.assertThat(mismatch.describe,
42             Raises(MatchesException(NotImplementedError)))
43         self.assertEqual({}, mismatch.get_details())
44
45
46 class TestMismatchError(TestCase):
47
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)
54
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))
59
60     def test_default_description_unicode(self):
61         matchee = _u('\xa7')
62         matcher = Equals(_u('a'))
63         mismatch = matcher.match(matchee)
64         e = MismatchError(matchee, matcher, mismatch)
65         self.assertEqual(mismatch.describe(), str(e))
66
67     def test_verbose_description(self):
68         matchee = 2
69         matcher = Equals(3)
70         mismatch = matcher.match(2)
71         e = MismatchError(matchee, matcher, mismatch, True)
72         expected = (
73             'Match failed. Matchee: %r\n'
74             'Matcher: %s\n'
75             'Difference: %s\n' % (
76                 matchee,
77                 matcher,
78                 matcher.match(matchee).describe(),
79                 ))
80         self.assertEqual(expected, str(e))
81
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.
85         matchee = _u('\xa7')
86         matcher = Equals(_u('a'))
87         mismatch = matcher.match(matchee)
88         expected = (
89             'Match failed. Matchee: %s\n'
90             'Matcher: %s\n'
91             'Difference: %s\n' % (
92                 text_repr(matchee),
93                 matcher,
94                 mismatch.describe(),
95                 ))
96         e = MismatchError(matchee, matcher, mismatch, True)
97         if str_is_unicode:
98             actual = str(e)
99         else:
100             actual = unicode(e)
101             # Using str() should still work, and return ascii only
102             self.assertEqual(
103                 expected.replace(matchee, matchee.encode("unicode-escape")),
104                 str(e).decode("ascii"))
105         self.assertEqual(expected, actual)
106
107
108 class TestMismatchDecorator(TestCase):
109
110     run_tests_with = FullStackRunTest
111
112     def test_forwards_description(self):
113         x = Mismatch("description", {'foo': 'bar'})
114         decorated = MismatchDecorator(x)
115         self.assertEqual(x.describe(), decorated.describe())
116
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())
121
122     def test_repr(self):
123         x = Mismatch("description", {'foo': 'bar'})
124         decorated = MismatchDecorator(x)
125         self.assertEqual(
126             '<testtools.matchers.MismatchDecorator(%r)>' % (x,),
127             repr(decorated))
128
129
130 def test_suite():
131     from unittest import TestLoader
132     return TestLoader().loadTestsFromName(__name__)