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

Skip to content

Commit 7bfe899

Browse files
committed
Followup to #7502: add __hash__ method and tests.
2 parents 83250bb + 165b128 commit 7bfe899

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lib/doctest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ def __eq__(self, other):
454454
def __ne__(self, other):
455455
return not self == other
456456

457+
def __hash__(self):
458+
return hash((self.source, self.want, self.lineno, self.indent,
459+
self.exc_msg))
457460

458461
class DocTest:
459462
"""
@@ -517,6 +520,9 @@ def __eq__(self, other):
517520
def __ne__(self, other):
518521
return not self == other
519522

523+
def __hash__(self):
524+
return hash((self.docstring, self.name, self.filename, self.lineno))
525+
520526
# This lets us sort tests by name:
521527
def __lt__(self, other):
522528
if not isinstance(other, DocTest):
@@ -2247,6 +2253,10 @@ def __eq__(self, other):
22472253
def __ne__(self, other):
22482254
return not self == other
22492255

2256+
def __hash__(self):
2257+
return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
2258+
self._dt_checker))
2259+
22502260
def __repr__(self):
22512261
name = self._dt_test.name.split('.')
22522262
return "%s (%s)" % (name[-1], '.'.join(name[:-1]))

Lib/test/test_doctest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,21 @@ def test_Example(): r"""
259259
>>> e = doctest.Example('raise X()', '', exc_msg)
260260
>>> e.exc_msg
261261
'\n'
262+
263+
Compare `Example`:
264+
>>> example = doctest.Example('print 1', '1\n')
265+
>>> same_example = doctest.Example('print 1', '1\n')
266+
>>> other_example = doctest.Example('print 42', '42\n')
267+
>>> example == same_example
268+
True
269+
>>> example != same_example
270+
False
271+
>>> hash(example) == hash(same_example)
272+
True
273+
>>> example == other_example
274+
False
275+
>>> example != other_example
276+
True
262277
"""
263278

264279
def test_DocTest(): r"""
@@ -362,6 +377,8 @@ def test_DocTest(): r"""
362377
True
363378
>>> test != same_test
364379
False
380+
>>> hash(test) == hash(same_test)
381+
True
365382
>>> docstring = '''
366383
... >>> print 42
367384
... 42
@@ -383,6 +400,8 @@ def test_DocTest(): r"""
383400
True
384401
>>> test_case != same_test_case
385402
False
403+
>>> hash(test_case) == hash(same_test_case)
404+
True
386405
>>> test == other_test_case
387406
False
388407
>>> test != other_test_case

0 commit comments

Comments
 (0)