Skip to content

Commit 165b128

Browse files
committed
Followup to #7502: add __hash__ method and tests.
1 parent 92ed387 commit 165b128

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Lib/doctest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ 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))
460+
457461

458462
class DocTest:
459463
"""
@@ -517,6 +521,9 @@ def __eq__(self, other):
517521
def __ne__(self, other):
518522
return not self == other
519523

524+
def __hash__(self):
525+
return hash((self.docstring, self.name, self.filename, self.lineno))
526+
520527
# This lets us sort tests by name:
521528
def __lt__(self, other):
522529
if not isinstance(other, DocTest):
@@ -2245,6 +2252,10 @@ def __eq__(self, other):
22452252
def __ne__(self, other):
22462253
return not self == other
22472254

2255+
def __hash__(self):
2256+
return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
2257+
self._dt_checker))
2258+
22482259
def __repr__(self):
22492260
name = self._dt_test.name.split('.')
22502261
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
@@ -258,6 +258,21 @@ def test_Example(): r"""
258258
>>> e = doctest.Example('raise X()', '', exc_msg)
259259
>>> e.exc_msg
260260
'\n'
261+
262+
Compare `Example`:
263+
>>> example = doctest.Example('print 1', '1\n')
264+
>>> same_example = doctest.Example('print 1', '1\n')
265+
>>> other_example = doctest.Example('print 42', '42\n')
266+
>>> example == same_example
267+
True
268+
>>> example != same_example
269+
False
270+
>>> hash(example) == hash(same_example)
271+
True
272+
>>> example == other_example
273+
False
274+
>>> example != other_example
275+
True
261276
"""
262277

263278
def test_DocTest(): r"""
@@ -361,6 +376,8 @@ def test_DocTest(): r"""
361376
True
362377
>>> test != same_test
363378
False
379+
>>> hash(test) == hash(same_test)
380+
True
364381
>>> docstring = '''
365382
... >>> print 42
366383
... 42
@@ -382,6 +399,8 @@ def test_DocTest(): r"""
382399
True
383400
>>> test_case != same_test_case
384401
False
402+
>>> hash(test_case) == hash(same_test_case)
403+
True
385404
>>> test == other_test_case
386405
False
387406
>>> test != other_test_case

0 commit comments

Comments
 (0)