Merge lp:~jml/testtools/mismatch-724691 into lp:~testtools-committers/testtools/trunk

Proposed by Jonathan Lange
Status: Merged
Merged at revision: 180
Proposed branch: lp:~jml/testtools/mismatch-724691
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 116 lines (+58/-2)
2 files modified
testtools/matchers.py (+28/-2)
testtools/tests/test_matchers.py (+30/-0)
To merge this branch: bzr merge lp:~jml/testtools/mismatch-724691
Reviewer Review Type Date Requested Status
testtools developers Pending
Review via email: mp+55938@code.launchpad.net

Description of the change

This branch fixes bug 724691 in the manner described on the bug.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testtools/matchers.py'
2--- testtools/matchers.py 2011-03-22 13:55:39 +0000
3+++ testtools/matchers.py 2011-04-01 14:11:47 +0000
4@@ -117,6 +117,31 @@
5 id(self), self.__dict__)
6
7
8+class MismatchDecorator(object):
9+ """Decorate a ``Mismatch``.
10+
11+ Forwards all messages to the original mismatch object. Probably the best
12+ way to use this is inherit from this class and then provide your own
13+ custom decoration logic.
14+ """
15+
16+ def __init__(self, original):
17+ """Construct a `MismatchDecorator`.
18+
19+ :param original: A `Mismatch` object to decorate.
20+ """
21+ self.original = original
22+
23+ def __repr__(self):
24+ return '<testtools.matchers.MismatchDecorator(%r)>' % (self.original,)
25+
26+ def describe(self):
27+ return self.original.describe()
28+
29+ def get_details(self):
30+ return self.original.get_details()
31+
32+
33 class DocTestMatches(object):
34 """See if a string matches a doctest example."""
35
36@@ -480,15 +505,16 @@
37 return AnnotatedMismatch(self.annotation, mismatch)
38
39
40-class AnnotatedMismatch(Mismatch):
41+class AnnotatedMismatch(MismatchDecorator):
42 """A mismatch annotated with a descriptive string."""
43
44 def __init__(self, annotation, mismatch):
45+ super(AnnotatedMismatch, self).__init__(mismatch)
46 self.annotation = annotation
47 self.mismatch = mismatch
48
49 def describe(self):
50- return '%s: %s' % (self.mismatch.describe(), self.annotation)
51+ return '%s: %s' % (self.original.describe(), self.annotation)
52
53
54 class Raises(Matcher):
55
56=== modified file 'testtools/tests/test_matchers.py'
57--- testtools/tests/test_matchers.py 2011-01-22 17:56:00 +0000
58+++ testtools/tests/test_matchers.py 2011-04-01 14:11:47 +0000
59@@ -14,6 +14,7 @@
60 from testtools.matchers import (
61 AfterPreproccessing,
62 Annotate,
63+ AnnotatedMismatch,
64 Equals,
65 DocTestMatches,
66 DoesNotEndWith,
67@@ -30,6 +31,7 @@
68 MatchesSetwise,
69 MatchesStructure,
70 Mismatch,
71+ MismatchDecorator,
72 Not,
73 NotEquals,
74 Raises,
75@@ -330,6 +332,14 @@
76 describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
77
78
79+class TestAnnotatedMismatch(TestCase):
80+
81+ def test_forwards_details(self):
82+ x = Mismatch('description', {'foo': 'bar'})
83+ annotated = AnnotatedMismatch("annotation", x)
84+ self.assertEqual(x.get_details(), annotated.get_details())
85+
86+
87 class TestRaisesInterface(TestCase, TestMatchersInterface):
88
89 matches_matcher = Raises()
90@@ -660,6 +670,26 @@
91 ]
92
93
94+class TestMismatchDecorator(TestCase):
95+
96+ def test_forwards_description(self):
97+ x = Mismatch("description", {'foo': 'bar'})
98+ decorated = MismatchDecorator(x)
99+ self.assertEqual(x.describe(), decorated.describe())
100+
101+ def test_forwards_details(self):
102+ x = Mismatch("description", {'foo': 'bar'})
103+ decorated = MismatchDecorator(x)
104+ self.assertEqual(x.get_details(), decorated.get_details())
105+
106+ def test_repr(self):
107+ x = Mismatch("description", {'foo': 'bar'})
108+ decorated = MismatchDecorator(x)
109+ self.assertEqual(
110+ '<testtools.matchers.MismatchDecorator(%r)>' % (x,),
111+ repr(decorated))
112+
113+
114 def test_suite():
115 from unittest import TestLoader
116 return TestLoader().loadTestsFromName(__name__)

Subscribers

People subscribed via source and target branches