forked from testing-cabal/testtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_assert_that.py
154 lines (132 loc) · 5.2 KB
/
test_assert_that.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from doctest import ELLIPSIS
from testtools import (
TestCase,
)
from testtools.assertions import (
assert_that,
)
from testtools.compat import (
_u,
)
from testtools.content import (
TracebackContent,
)
from testtools.matchers import (
Annotate,
DocTestMatches,
Equals,
)
class AssertThatTests(object):
"""
A mixin containing shared tests for assertThat and assert_that.
"""
def assert_that_callable(self, *args, **kwargs):
raise NotImplementedError
def assertFails(self, message, function, *args, **kwargs):
"""Assert that function raises a failure with the given message."""
failure = self.assertRaises(
self.failureException, function, *args, **kwargs)
self.assert_that_callable(failure, DocTestMatches(message, ELLIPSIS))
def test_assertThat_matches_clean(self):
class Matcher(object):
def match(self, foo):
return None
self.assert_that_callable("foo", Matcher())
def test_assertThat_mismatch_raises_description(self):
calls = []
class Mismatch(object):
def __init__(self, thing):
self.thing = thing
def describe(self):
calls.append(('describe_diff', self.thing))
return "object is not a thing"
def get_details(self):
return {}
class Matcher(object):
def match(self, thing):
calls.append(('match', thing))
return Mismatch(thing)
def __str__(self):
calls.append(('__str__',))
return "a description"
class Test(type(self)):
def test(self):
self.assert_that_callable("foo", Matcher())
result = Test("test").run()
self.assertEqual([
('match', "foo"),
('describe_diff', "foo"),
], calls)
self.assertFalse(result.wasSuccessful())
def test_assertThat_output(self):
matchee = 'foo'
matcher = Equals('bar')
expected = matcher.match(matchee).describe()
self.assertFails(expected, self.assert_that_callable, matchee, matcher)
def test_assertThat_message_is_annotated(self):
matchee = 'foo'
matcher = Equals('bar')
expected = Annotate('woo', matcher).match(matchee).describe()
self.assertFails(expected,
self.assert_that_callable, matchee, matcher, 'woo')
def test_assertThat_verbose_output(self):
matchee = 'foo'
matcher = Equals('bar')
expected = (
'Match failed. Matchee: %r\n'
'Matcher: %s\n'
'Difference: %s\n' % (
matchee,
matcher,
matcher.match(matchee).describe(),
))
self.assertFails(
expected,
self.assert_that_callable, matchee, matcher, verbose=True)
def get_error_string(self, e):
"""Get the string showing how 'e' would be formatted in test output.
This is a little bit hacky, since it's designed to give consistent
output regardless of Python version.
In testtools, TestResult._exc_info_to_unicode is the point of dispatch
between various different implementations of methods that format
exceptions, so that's what we have to call. However, that method cares
about stack traces and formats the exception class. We don't care
about either of these, so we take its output and parse it a little.
"""
error = TracebackContent((e.__class__, e, None), self).as_text()
# We aren't at all interested in the traceback.
if error.startswith('Traceback (most recent call last):\n'):
lines = error.splitlines(True)[1:]
for i, line in enumerate(lines):
if not line.startswith(' '):
break
error = ''.join(lines[i:])
# We aren't interested in how the exception type is formatted.
exc_class, error = error.split(': ', 1)
return error
def test_assertThat_verbose_unicode(self):
# When assertThat is given matchees or matchers that contain non-ASCII
# unicode strings, we can still provide a meaningful error.
matchee = _u('\xa7')
matcher = Equals(_u('a'))
expected = (
'Match failed. Matchee: %s\n'
'Matcher: %s\n'
'Difference: %s\n\n' % (
repr(matchee).replace("\\xa7", matchee),
matcher,
matcher.match(matchee).describe(),
))
e = self.assertRaises(
self.failureException, self.assert_that_callable, matchee, matcher,
verbose=True)
self.assertEqual(expected, self.get_error_string(e))
class TestAssertThatFunction(AssertThatTests, TestCase):
def assert_that_callable(self, *args, **kwargs):
return assert_that(*args, **kwargs)
class TestAssertThatMethod(AssertThatTests, TestCase):
def assert_that_callable(self, *args, **kwargs):
return self.assertThat(*args, **kwargs)
def test_suite():
from unittest import TestLoader
return TestLoader().loadTestsFromName(__name__)