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
|
# Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
"""Tests for matchers."""
from testtools import (
Matcher, # check that Matcher is exposed at the top level for docs.
TestCase,
)
from testtools.compat import (
text_repr,
)
from testtools.matchers import (
Equals,
MatchesException,
Raises,
)
from testtools.matchers._impl import (
Mismatch,
MismatchDecorator,
MismatchError,
)
from ..helpers import FullStackRunTest
# Silence pyflakes.
Matcher
class TestMismatch(TestCase):
run_tests_with = FullStackRunTest
def test_constructor_arguments(self):
mismatch = Mismatch("some description", {"detail": "things"})
self.assertEqual("some description", mismatch.describe())
self.assertEqual({"detail": "things"}, mismatch.get_details())
def test_constructor_no_arguments(self):
mismatch = Mismatch()
self.assertThat(
mismatch.describe, Raises(MatchesException(NotImplementedError))
)
self.assertEqual({}, mismatch.get_details())
class TestMismatchError(TestCase):
def test_is_assertion_error(self):
# MismatchError is an AssertionError, so that most of the time, it
# looks like a test failure, rather than an error.
def raise_mismatch_error():
raise MismatchError(2, Equals(3), Equals(3).match(2))
self.assertRaises(AssertionError, raise_mismatch_error)
def test_default_description_is_mismatch(self):
mismatch = Equals(3).match(2)
e = MismatchError(2, Equals(3), mismatch)
self.assertEqual(mismatch.describe(), str(e))
def test_default_description_unicode(self):
matchee = "\xa7"
matcher = Equals("a")
mismatch = matcher.match(matchee)
e = MismatchError(matchee, matcher, mismatch)
self.assertEqual(mismatch.describe(), str(e))
def test_verbose_description(self):
matchee = 2
matcher = Equals(3)
mismatch = matcher.match(2)
e = MismatchError(matchee, matcher, mismatch, True)
expected = (
f"Match failed. Matchee: {matchee!r}\nMatcher: {matcher}\n"
f"Difference: {matcher.match(matchee).describe()}\n"
)
self.assertEqual(expected, str(e))
def test_verbose_unicode(self):
# When assertThat is given matchees or matchers that contain non-ASCII
# unicode strings, we can still provide a meaningful error.
matchee = "\xa7"
matcher = Equals("a")
mismatch = matcher.match(matchee)
expected = (
f"Match failed. Matchee: {text_repr(matchee)}\nMatcher: {matcher}\n"
f"Difference: {mismatch.describe()}\n"
)
e = MismatchError(matchee, matcher, mismatch, True)
self.assertEqual(expected, str(e))
class TestMismatchDecorator(TestCase):
run_tests_with = FullStackRunTest
def test_forwards_description(self):
x = Mismatch("description", {"foo": "bar"})
decorated = MismatchDecorator(x)
self.assertEqual(x.describe(), decorated.describe())
def test_forwards_details(self):
x = Mismatch("description", {"foo": "bar"})
decorated = MismatchDecorator(x)
self.assertEqual(x.get_details(), decorated.get_details())
def test_repr(self):
x = Mismatch("description", {"foo": "bar"})
decorated = MismatchDecorator(x)
self.assertEqual(
f"<testtools.matchers.MismatchDecorator({x!r})>", repr(decorated)
)
def test_suite():
from unittest import TestLoader
return TestLoader().loadTestsFromName(__name__)
|