22
class TestMatchersInterface:
35
class TestMismatch(TestCase):
37
def test_constructor_arguments(self):
38
mismatch = Mismatch("some description", {'detail': "things"})
39
self.assertEqual("some description", mismatch.describe())
40
self.assertEqual({'detail': "things"}, mismatch.get_details())
42
def test_constructor_no_arguments(self):
44
self.assertThat(mismatch.describe,
45
Raises(MatchesException(NotImplementedError)))
46
self.assertEqual({}, mismatch.get_details())
49
class TestMatchersInterface(object):
24
51
def test_matches_match(self):
25
52
matcher = self.matches_matcher
45
72
mismatch = matcher.match(matchee)
46
73
self.assertEqual(difference, mismatch.describe())
75
def test_mismatch_details(self):
76
# The mismatch object must provide get_details, which must return a
77
# dictionary mapping names to Content objects.
78
examples = self.describe_examples
79
for difference, matchee, matcher in examples:
80
mismatch = matcher.match(matchee)
81
details = mismatch.get_details()
82
self.assertEqual(dict(details), details)
49
85
class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
97
133
describe_examples = [("1 == 1", 1, NotEquals(1))]
136
class TestIsInterface(TestCase, TestMatchersInterface):
141
matches_matcher = Is(foo)
142
matches_matches = [foo]
143
matches_mismatches = [bar, 1]
145
str_examples = [("Is(2)", Is(2))]
147
describe_examples = [("1 is not 2", 2, Is(1))]
150
class TestLessThanInterface(TestCase, TestMatchersInterface):
152
matches_matcher = LessThan(4)
153
matches_matches = [-5, 3]
154
matches_mismatches = [4, 5, 5000]
157
("LessThan(12)", LessThan(12)),
160
describe_examples = [('4 is >= 4', 4, LessThan(4))]
163
def make_error(type, *args, **kwargs):
165
raise type(*args, **kwargs)
167
return sys.exc_info()
170
class TestMatchesExceptionInstanceInterface(TestCase, TestMatchersInterface):
172
matches_matcher = MatchesException(ValueError("foo"))
173
error_foo = make_error(ValueError, 'foo')
174
error_bar = make_error(ValueError, 'bar')
175
error_base_foo = make_error(Exception, 'foo')
176
matches_matches = [error_foo]
177
matches_mismatches = [error_bar, error_base_foo]
180
("MatchesException(Exception('foo',))",
181
MatchesException(Exception('foo')))
183
describe_examples = [
184
("<type 'exceptions.Exception'> is not a "
185
"<type 'exceptions.ValueError'>",
187
MatchesException(ValueError("foo"))),
188
("ValueError('bar',) has different arguments to ValueError('foo',).",
190
MatchesException(ValueError("foo"))),
194
class TestMatchesExceptionTypeInterface(TestCase, TestMatchersInterface):
196
matches_matcher = MatchesException(ValueError)
197
error_foo = make_error(ValueError, 'foo')
198
error_sub = make_error(UnicodeError, 'bar')
199
error_base_foo = make_error(Exception, 'foo')
200
matches_matches = [error_foo, error_sub]
201
matches_mismatches = [error_base_foo]
204
("MatchesException(<type 'exceptions.Exception'>)",
205
MatchesException(Exception))
207
describe_examples = [
208
("<type 'exceptions.Exception'> is not a "
209
"<type 'exceptions.ValueError'>",
211
MatchesException(ValueError)),
100
215
class TestNotInterface(TestCase, TestMatchersInterface):
102
217
matches_matcher = Not(Equals(1))
154
269
1, MatchesAll(NotEquals(1), NotEquals(2)))]
272
class TestKeysEqual(TestCase, TestMatchersInterface):
274
matches_matcher = KeysEqual('foo', 'bar')
276
{'foo': 0, 'bar': 1},
278
matches_mismatches = [
282
{'foo': 0, 'bar': 1, 'baz': 2},
283
{'a': None, 'b': None, 'c': None},
287
("KeysEqual('foo', 'bar')", KeysEqual('foo', 'bar')),
290
describe_examples = [
291
("['bar', 'foo'] does not match {'baz': 2, 'foo': 0, 'bar': 1}: "
293
{'foo': 0, 'bar': 1, 'baz': 2}, KeysEqual('foo', 'bar')),
157
297
class TestAnnotate(TestCase, TestMatchersInterface):
159
299
matches_matcher = Annotate("foo", Equals(1))
166
306
describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
309
class TestRaisesInterface(TestCase, TestMatchersInterface):
311
matches_matcher = Raises()
313
raise Exception('foo')
314
matches_matches = [boom]
315
matches_mismatches = [lambda:None]
317
# Tricky to get function objects to render constantly, and the interfaces
318
# helper uses assertEqual rather than (for instance) DocTestMatches.
321
describe_examples = []
324
class TestRaisesExceptionMatcherInterface(TestCase, TestMatchersInterface):
326
matches_matcher = Raises(
327
exception_matcher=MatchesException(Exception('foo')))
329
raise Exception('bar')
331
raise Exception('foo')
332
matches_matches = [boom_foo]
333
matches_mismatches = [lambda:None, boom_bar]
335
# Tricky to get function objects to render constantly, and the interfaces
336
# helper uses assertEqual rather than (for instance) DocTestMatches.
339
describe_examples = []
342
class TestRaisesBaseTypes(TestCase):
345
raise KeyboardInterrupt('foo')
347
def test_KeyboardInterrupt_matched(self):
348
# When KeyboardInterrupt is matched, it is swallowed.
349
matcher = Raises(MatchesException(KeyboardInterrupt))
350
self.assertThat(self.raiser, matcher)
352
def test_KeyboardInterrupt_propogates(self):
353
# The default 'it raised' propogates KeyboardInterrupt.
354
match_keyb = Raises(MatchesException(KeyboardInterrupt))
355
def raise_keyb_from_match():
357
matcher.match(self.raiser)
358
self.assertThat(raise_keyb_from_match, match_keyb)
360
def test_KeyboardInterrupt_match_Exception_propogates(self):
361
# If the raised exception isn't matched, and it is not a subclass of
362
# Exception, it is propogated.
363
match_keyb = Raises(MatchesException(KeyboardInterrupt))
364
def raise_keyb_from_match():
365
matcher = Raises(MatchesException(Exception))
366
matcher.match(self.raiser)
367
self.assertThat(raise_keyb_from_match, match_keyb)
370
class TestRaisesConvenience(TestCase):
372
def test_exc_type(self):
373
self.assertThat(lambda: 1/0, raises(ZeroDivisionError))
375
def test_exc_value(self):
376
e = RuntimeError("You lose!")
379
self.assertThat(raiser, raises(e))
382
class DoesNotStartWithTests(TestCase):
384
def test_describe(self):
385
mismatch = DoesNotStartWith("fo", "bo")
386
self.assertEqual("'fo' does not start with 'bo'.", mismatch.describe())
389
class StartsWithTests(TestCase):
392
matcher = StartsWith("bar")
393
self.assertEqual("Starts with 'bar'.", str(matcher))
395
def test_match(self):
396
matcher = StartsWith("bar")
397
self.assertIs(None, matcher.match("barf"))
399
def test_mismatch_returns_does_not_start_with(self):
400
matcher = StartsWith("bar")
401
self.assertIsInstance(matcher.match("foo"), DoesNotStartWith)
403
def test_mismatch_sets_matchee(self):
404
matcher = StartsWith("bar")
405
mismatch = matcher.match("foo")
406
self.assertEqual("foo", mismatch.matchee)
408
def test_mismatch_sets_expected(self):
409
matcher = StartsWith("bar")
410
mismatch = matcher.match("foo")
411
self.assertEqual("bar", mismatch.expected)
169
414
def test_suite():
170
415
from unittest import TestLoader
171
416
return TestLoader().loadTestsFromName(__name__)