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
=== modified file 'testtools/matchers.py'
--- testtools/matchers.py 2011-03-22 13:55:39 +0000
+++ testtools/matchers.py 2011-04-01 14:11:47 +0000
@@ -117,6 +117,31 @@
117 id(self), self.__dict__)117 id(self), self.__dict__)
118118
119119
120class MismatchDecorator(object):
121 """Decorate a ``Mismatch``.
122
123 Forwards all messages to the original mismatch object. Probably the best
124 way to use this is inherit from this class and then provide your own
125 custom decoration logic.
126 """
127
128 def __init__(self, original):
129 """Construct a `MismatchDecorator`.
130
131 :param original: A `Mismatch` object to decorate.
132 """
133 self.original = original
134
135 def __repr__(self):
136 return '<testtools.matchers.MismatchDecorator(%r)>' % (self.original,)
137
138 def describe(self):
139 return self.original.describe()
140
141 def get_details(self):
142 return self.original.get_details()
143
144
120class DocTestMatches(object):145class DocTestMatches(object):
121 """See if a string matches a doctest example."""146 """See if a string matches a doctest example."""
122147
@@ -480,15 +505,16 @@
480 return AnnotatedMismatch(self.annotation, mismatch)505 return AnnotatedMismatch(self.annotation, mismatch)
481506
482507
483class AnnotatedMismatch(Mismatch):508class AnnotatedMismatch(MismatchDecorator):
484 """A mismatch annotated with a descriptive string."""509 """A mismatch annotated with a descriptive string."""
485510
486 def __init__(self, annotation, mismatch):511 def __init__(self, annotation, mismatch):
512 super(AnnotatedMismatch, self).__init__(mismatch)
487 self.annotation = annotation513 self.annotation = annotation
488 self.mismatch = mismatch514 self.mismatch = mismatch
489515
490 def describe(self):516 def describe(self):
491 return '%s: %s' % (self.mismatch.describe(), self.annotation)517 return '%s: %s' % (self.original.describe(), self.annotation)
492518
493519
494class Raises(Matcher):520class Raises(Matcher):
495521
=== modified file 'testtools/tests/test_matchers.py'
--- testtools/tests/test_matchers.py 2011-01-22 17:56:00 +0000
+++ testtools/tests/test_matchers.py 2011-04-01 14:11:47 +0000
@@ -14,6 +14,7 @@
14from testtools.matchers import (14from testtools.matchers import (
15 AfterPreproccessing,15 AfterPreproccessing,
16 Annotate,16 Annotate,
17 AnnotatedMismatch,
17 Equals,18 Equals,
18 DocTestMatches,19 DocTestMatches,
19 DoesNotEndWith,20 DoesNotEndWith,
@@ -30,6 +31,7 @@
30 MatchesSetwise,31 MatchesSetwise,
31 MatchesStructure,32 MatchesStructure,
32 Mismatch,33 Mismatch,
34 MismatchDecorator,
33 Not,35 Not,
34 NotEquals,36 NotEquals,
35 Raises,37 Raises,
@@ -330,6 +332,14 @@
330 describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]332 describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]
331333
332334
335class TestAnnotatedMismatch(TestCase):
336
337 def test_forwards_details(self):
338 x = Mismatch('description', {'foo': 'bar'})
339 annotated = AnnotatedMismatch("annotation", x)
340 self.assertEqual(x.get_details(), annotated.get_details())
341
342
333class TestRaisesInterface(TestCase, TestMatchersInterface):343class TestRaisesInterface(TestCase, TestMatchersInterface):
334344
335 matches_matcher = Raises()345 matches_matcher = Raises()
@@ -660,6 +670,26 @@
660 ]670 ]
661671
662672
673class TestMismatchDecorator(TestCase):
674
675 def test_forwards_description(self):
676 x = Mismatch("description", {'foo': 'bar'})
677 decorated = MismatchDecorator(x)
678 self.assertEqual(x.describe(), decorated.describe())
679
680 def test_forwards_details(self):
681 x = Mismatch("description", {'foo': 'bar'})
682 decorated = MismatchDecorator(x)
683 self.assertEqual(x.get_details(), decorated.get_details())
684
685 def test_repr(self):
686 x = Mismatch("description", {'foo': 'bar'})
687 decorated = MismatchDecorator(x)
688 self.assertEqual(
689 '<testtools.matchers.MismatchDecorator(%r)>' % (x,),
690 repr(decorated))
691
692
663def test_suite():693def test_suite():
664 from unittest import TestLoader694 from unittest import TestLoader
665 return TestLoader().loadTestsFromName(__name__)695 return TestLoader().loadTestsFromName(__name__)

Subscribers

People subscribed via source and target branches