Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bc801c

Browse files
committed
Issue #7502: Fix equality comparison for DocTestCase instances.
Patch by Cédric Krier.
1 parent cf53ae2 commit 2bc801c

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lib/doctest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,21 @@ def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
440440
self.options = options
441441
self.exc_msg = exc_msg
442442

443+
def __eq__(self, other):
444+
if type(self) is not type(other):
445+
return NotImplemented
446+
447+
return self.source == other.source and \
448+
self.want == other.want and \
449+
self.lineno == other.lineno and \
450+
self.indent == other.indent and \
451+
self.options == other.options and \
452+
self.exc_msg == other.exc_msg
453+
454+
def __ne__(self, other):
455+
return not self == other
456+
457+
443458
class DocTest:
444459
"""
445460
A collection of doctest examples that should be run in a single
@@ -488,6 +503,19 @@ def __repr__(self):
488503
return ('<DocTest %s from %s:%s (%s)>' %
489504
(self.name, self.filename, self.lineno, examples))
490505

506+
def __eq__(self, other):
507+
if type(self) is not type(other):
508+
return NotImplemented
509+
510+
return self.examples == other.examples and \
511+
self.docstring == other.docstring and \
512+
self.globs == other.globs and \
513+
self.name == other.name and \
514+
self.filename == other.filename and \
515+
self.lineno == other.lineno
516+
517+
def __ne__(self, other):
518+
return not self == other
491519

492520
# This lets us sort tests by name:
493521
def __lt__(self, other):
@@ -2204,6 +2232,19 @@ def debug(self):
22042232
def id(self):
22052233
return self._dt_test.name
22062234

2235+
def __eq__(self, other):
2236+
if type(self) is not type(other):
2237+
return NotImplemented
2238+
2239+
return self._dt_test == other._dt_test and \
2240+
self._dt_optionflags == other._dt_optionflags and \
2241+
self._dt_setUp == other._dt_setUp and \
2242+
self._dt_tearDown == other._dt_tearDown and \
2243+
self._dt_checker == other._dt_checker
2244+
2245+
def __ne__(self, other):
2246+
return not self == other
2247+
22072248
def __repr__(self):
22082249
name = self._dt_test.name.split('.')
22092250
return "%s (%s)" % (name[-1], '.'.join(name[:-1]))

Lib/test/test_doctest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,46 @@ def test_DocTest(): r"""
347347
Traceback (most recent call last):
348348
ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print(1)'
349349
350+
Compare `DocTest`:
351+
352+
>>> docstring = '''
353+
... >>> print 12
354+
... 12
355+
... '''
356+
>>> test = parser.get_doctest(docstring, globs, 'some_test',
357+
... 'some_test', 20)
358+
>>> same_test = parser.get_doctest(docstring, globs, 'some_test',
359+
... 'some_test', 20)
360+
>>> test == same_test
361+
True
362+
>>> test != same_test
363+
False
364+
>>> docstring = '''
365+
... >>> print 42
366+
... 42
367+
... '''
368+
>>> other_test = parser.get_doctest(docstring, globs, 'other_test',
369+
... 'other_file', 10)
370+
>>> test == other_test
371+
False
372+
>>> test != other_test
373+
True
374+
375+
Compare `DocTestCase`:
376+
377+
>>> DocTestCase = doctest.DocTestCase
378+
>>> test_case = DocTestCase(test)
379+
>>> same_test_case = DocTestCase(same_test)
380+
>>> other_test_case = DocTestCase(other_test)
381+
>>> test_case == same_test_case
382+
True
383+
>>> test_case != same_test_case
384+
False
385+
>>> test == other_test_case
386+
False
387+
>>> test != other_test_case
388+
True
389+
350390
"""
351391

352392
def test_DocTestFinder(): r"""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ Bob Kras
509509
Holger Krekel
510510
Michael Kremer
511511
Fabian Kreutz
512+
Cédric Krier
512513
Hannu Krosing
513514
Andrej Krpic
514515
Ivan Krstić

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Core and Builtins
9797
Library
9898
-------
9999

100+
- Issue #7502: Fix equality comparison for DocTestCase instances. Patch by
101+
Cédric Krier.
102+
100103
- Issue #8035: urllib: Fix a bug where the client could remain stuck after a
101104
redirection or an error.
102105

0 commit comments

Comments
 (0)