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

Skip to content

Commit 92d4acb

Browse files
committed
Issue #7502: Fix equality comparison for DocTestCase instances.
Patch by Cédric Krier.
2 parents 0f694d7 + a742526 commit 92d4acb

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):
@@ -2206,6 +2234,19 @@ def debug(self):
22062234
def id(self):
22072235
return self._dt_test.name
22082236

2237+
def __eq__(self, other):
2238+
if type(self) is not type(other):
2239+
return NotImplemented
2240+
2241+
return self._dt_test == other._dt_test and \
2242+
self._dt_optionflags == other._dt_optionflags and \
2243+
self._dt_setUp == other._dt_setUp and \
2244+
self._dt_tearDown == other._dt_tearDown and \
2245+
self._dt_checker == other._dt_checker
2246+
2247+
def __ne__(self, other):
2248+
return not self == other
2249+
22092250
def __repr__(self):
22102251
name = self._dt_test.name.split('.')
22112252
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
@@ -348,6 +348,46 @@ def test_DocTest(): r"""
348348
Traceback (most recent call last):
349349
ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print(1)'
350350
351+
Compare `DocTest`:
352+
353+
>>> docstring = '''
354+
... >>> print 12
355+
... 12
356+
... '''
357+
>>> test = parser.get_doctest(docstring, globs, 'some_test',
358+
... 'some_test', 20)
359+
>>> same_test = parser.get_doctest(docstring, globs, 'some_test',
360+
... 'some_test', 20)
361+
>>> test == same_test
362+
True
363+
>>> test != same_test
364+
False
365+
>>> docstring = '''
366+
... >>> print 42
367+
... 42
368+
... '''
369+
>>> other_test = parser.get_doctest(docstring, globs, 'other_test',
370+
... 'other_file', 10)
371+
>>> test == other_test
372+
False
373+
>>> test != other_test
374+
True
375+
376+
Compare `DocTestCase`:
377+
378+
>>> DocTestCase = doctest.DocTestCase
379+
>>> test_case = DocTestCase(test)
380+
>>> same_test_case = DocTestCase(same_test)
381+
>>> other_test_case = DocTestCase(other_test)
382+
>>> test_case == same_test_case
383+
True
384+
>>> test_case != same_test_case
385+
False
386+
>>> test == other_test_case
387+
False
388+
>>> test != other_test_case
389+
True
390+
351391
"""
352392

353393
def test_DocTestFinder(): r"""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ Bob Kras
549549
Holger Krekel
550550
Michael Kremer
551551
Fabian Kreutz
552+
Cédric Krier
552553
Hannu Krosing
553554
Andrej Krpic
554555
Ivan Krstić

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ Core and Builtins
419419
Library
420420
-------
421421

422+
- Issue #7502: Fix equality comparison for DocTestCase instances. Patch by
423+
Cédric Krier.
424+
422425
- Issue #11870: threading: Properly reinitialize threads internal locks and
423426
condition variables to avoid deadlocks in child processes.
424427

0 commit comments

Comments
 (0)