Issue #7502: Fix equality comparison for DocTestCase instances.
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 18 Dec 2011 18:27:45 +0000 (19:27 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 18 Dec 2011 18:27:45 +0000 (19:27 +0100)
Patch by Cédric Krier.

Lib/doctest.py
Lib/test/test_doctest.py
Misc/ACKS
Misc/NEWS

index f60b06d7c9acfa936d59b2376494687053f641e9..3daf17fa50d3746d9f0f0cf6e92d0bd4969f6e87 100644 (file)
@@ -440,6 +440,21 @@ class Example:
         self.options = options
         self.exc_msg = exc_msg
 
+    def __eq__(self, other):
+        if type(self) is not type(other):
+            return NotImplemented
+
+        return self.source == other.source and \
+               self.want == other.want and \
+               self.lineno == other.lineno and \
+               self.indent == other.indent and \
+               self.options == other.options and \
+               self.exc_msg == other.exc_msg
+
+    def __ne__(self, other):
+        return not self == other
+
+
 class DocTest:
     """
     A collection of doctest examples that should be run in a single
@@ -488,6 +503,19 @@ class DocTest:
         return ('<DocTest %s from %s:%s (%s)>' %
                 (self.name, self.filename, self.lineno, examples))
 
+    def __eq__(self, other):
+        if type(self) is not type(other):
+            return NotImplemented
+
+        return self.examples == other.examples and \
+               self.docstring == other.docstring and \
+               self.globs == other.globs and \
+               self.name == other.name and \
+               self.filename == other.filename and \
+               self.lineno == other.lineno
+
+    def __ne__(self, other):
+        return not self == other
 
     # This lets us sort tests by name:
     def __lt__(self, other):
@@ -2204,6 +2232,19 @@ class DocTestCase(unittest.TestCase):
     def id(self):
         return self._dt_test.name
 
+    def __eq__(self, other):
+        if type(self) is not type(other):
+            return NotImplemented
+
+        return self._dt_test == other._dt_test and \
+               self._dt_optionflags == other._dt_optionflags and \
+               self._dt_setUp == other._dt_setUp and \
+               self._dt_tearDown == other._dt_tearDown and \
+               self._dt_checker == other._dt_checker
+
+    def __ne__(self, other):
+        return not self == other
+
     def __repr__(self):
         name = self._dt_test.name.split('.')
         return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
index 13836bafa06686c8e6fa1d6d68b2a6cc9dde805e..676d5de6f7a6522d5362ba662f6931e61fb9473a 100644 (file)
@@ -347,6 +347,46 @@ will raise a ValueError:
     Traceback (most recent call last):
     ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print(1)'
 
+Compare `DocTest`:
+
+    >>> docstring = '''
+    ...     >>> print 12
+    ...     12
+    ... '''
+    >>> test = parser.get_doctest(docstring, globs, 'some_test',
+    ...                           'some_test', 20)
+    >>> same_test = parser.get_doctest(docstring, globs, 'some_test',
+    ...                                'some_test', 20)
+    >>> test == same_test
+    True
+    >>> test != same_test
+    False
+    >>> docstring = '''
+    ...     >>> print 42
+    ...     42
+    ... '''
+    >>> other_test = parser.get_doctest(docstring, globs, 'other_test',
+    ...                                 'other_file', 10)
+    >>> test == other_test
+    False
+    >>> test != other_test
+    True
+
+Compare `DocTestCase`:
+
+    >>> DocTestCase = doctest.DocTestCase
+    >>> test_case = DocTestCase(test)
+    >>> same_test_case = DocTestCase(same_test)
+    >>> other_test_case = DocTestCase(other_test)
+    >>> test_case == same_test_case
+    True
+    >>> test_case != same_test_case
+    False
+    >>> test == other_test_case
+    False
+    >>> test != other_test_case
+    True
+
 """
 
 def test_DocTestFinder(): r"""
index 10f138676d7f3dc7a796e0e21e735b4d421b25d1..86c78abde8ce144340690c3a5d19aa1d5914776d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -509,6 +509,7 @@ Bob Kras
 Holger Krekel
 Michael Kremer
 Fabian Kreutz
+Cédric Krier
 Hannu Krosing
 Andrej Krpic
 Ivan Krstić
index 72b33b99f3bd638a462fd9fa4c6f6d54ab6de3d6..7dbaaff5f6af7d58b80c0571f20d8b63f7a21bee 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7502: Fix equality comparison for DocTestCase instances.  Patch by
+  Cédric Krier.
+
 - Issue #8035: urllib: Fix a bug where the client could remain stuck after a
   redirection or an error.