Commit 165b1283 authored by Antoine Pitrou's avatar Antoine Pitrou

Followup to #7502: add __hash__ method and tests.

parent 92ed3877
...@@ -454,6 +454,10 @@ class Example: ...@@ -454,6 +454,10 @@ class Example:
def __ne__(self, other): def __ne__(self, other):
return not self == other return not self == other
def __hash__(self):
return hash((self.source, self.want, self.lineno, self.indent,
self.exc_msg))
class DocTest: class DocTest:
""" """
...@@ -517,6 +521,9 @@ class DocTest: ...@@ -517,6 +521,9 @@ class DocTest:
def __ne__(self, other): def __ne__(self, other):
return not self == other return not self == other
def __hash__(self):
return hash((self.docstring, self.name, self.filename, self.lineno))
# This lets us sort tests by name: # This lets us sort tests by name:
def __lt__(self, other): def __lt__(self, other):
if not isinstance(other, DocTest): if not isinstance(other, DocTest):
...@@ -2245,6 +2252,10 @@ class DocTestCase(unittest.TestCase): ...@@ -2245,6 +2252,10 @@ class DocTestCase(unittest.TestCase):
def __ne__(self, other): def __ne__(self, other):
return not self == other return not self == other
def __hash__(self):
return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
self._dt_checker))
def __repr__(self): def __repr__(self):
name = self._dt_test.name.split('.') name = self._dt_test.name.split('.')
return "%s (%s)" % (name[-1], '.'.join(name[:-1])) return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
......
...@@ -258,6 +258,21 @@ unless it's `None`: ...@@ -258,6 +258,21 @@ unless it's `None`:
>>> e = doctest.Example('raise X()', '', exc_msg) >>> e = doctest.Example('raise X()', '', exc_msg)
>>> e.exc_msg >>> e.exc_msg
'\n' '\n'
Compare `Example`:
>>> example = doctest.Example('print 1', '1\n')
>>> same_example = doctest.Example('print 1', '1\n')
>>> other_example = doctest.Example('print 42', '42\n')
>>> example == same_example
True
>>> example != same_example
False
>>> hash(example) == hash(same_example)
True
>>> example == other_example
False
>>> example != other_example
True
""" """
def test_DocTest(): r""" def test_DocTest(): r"""
...@@ -361,6 +376,8 @@ Compare `DocTest`: ...@@ -361,6 +376,8 @@ Compare `DocTest`:
True True
>>> test != same_test >>> test != same_test
False False
>>> hash(test) == hash(same_test)
True
>>> docstring = ''' >>> docstring = '''
... >>> print 42 ... >>> print 42
... 42 ... 42
...@@ -382,6 +399,8 @@ Compare `DocTestCase`: ...@@ -382,6 +399,8 @@ Compare `DocTestCase`:
True True
>>> test_case != same_test_case >>> test_case != same_test_case
False False
>>> hash(test_case) == hash(same_test_case)
True
>>> test == other_test_case >>> test == other_test_case
False False
>>> test != other_test_case >>> test != other_test_case
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment