diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-12-18 19:20:17 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-12-18 19:20:17 (GMT) |
commit | 165b1283ffe5922dd9f64ef7edb5534e6983d2f5 (patch) | |
tree | c857bb9f6aaa0c22476b9dfa457418d6758cb81e /Lib | |
parent | 92ed3877b471e5e285b5e76b6f13547e579daaa4 (diff) | |
download | cpython-165b1283ffe5922dd9f64ef7edb5534e6983d2f5.zip cpython-165b1283ffe5922dd9f64ef7edb5534e6983d2f5.tar.gz cpython-165b1283ffe5922dd9f64ef7edb5534e6983d2f5.tar.bz2 |
Followup to #7502: add __hash__ method and tests.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/doctest.py | 11 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 19 |
2 files changed, 30 insertions, 0 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 3daf17f..234733e 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -454,6 +454,10 @@ class Example: def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.source, self.want, self.lineno, self.indent, + self.exc_msg)) + class DocTest: """ @@ -517,6 +521,9 @@ class DocTest: def __ne__(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: def __lt__(self, other): if not isinstance(other, DocTest): @@ -2245,6 +2252,10 @@ class DocTestCase(unittest.TestCase): def __ne__(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): name = self._dt_test.name.split('.') return "%s (%s)" % (name[-1], '.'.join(name[:-1])) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 676d5de..5969ce2 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -258,6 +258,21 @@ unless it's `None`: >>> e = doctest.Example('raise X()', '', exc_msg) >>> e.exc_msg '\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""" @@ -361,6 +376,8 @@ Compare `DocTest`: True >>> test != same_test False + >>> hash(test) == hash(same_test) + True >>> docstring = ''' ... >>> print 42 ... 42 @@ -382,6 +399,8 @@ Compare `DocTestCase`: True >>> test_case != same_test_case False + >>> hash(test_case) == hash(same_test_case) + True >>> test == other_test_case False >>> test != other_test_case |