diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-11-20 15:34:26 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-11-20 15:34:26 (GMT) |
commit | 8ca6d9884bcbdd05fe270e75e10f51af614e22a2 (patch) | |
tree | d6a9280c42e73fa1f344b415aacc759df3bae957 /Lib/unittest/case.py | |
parent | e5db2636f3fedd772e55ce55e4cf820475f29fe3 (diff) | |
download | cpython-8ca6d9884bcbdd05fe270e75e10f51af614e22a2.zip cpython-8ca6d9884bcbdd05fe270e75e10f51af614e22a2.tar.gz cpython-8ca6d9884bcbdd05fe270e75e10f51af614e22a2.tar.bz2 |
Issue 10326: TestCase instances can now be pickled (they store names of instance methods instead of references to the instance methods themselves).
Diffstat (limited to 'Lib/unittest/case.py')
-rw-r--r-- | Lib/unittest/case.py | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 5831a76..1df19dc 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -191,6 +191,27 @@ class _AssertWarnsContext(_AssertRaisesBaseContext): .format(exc_name)) +class _TypeEqualityDict(object): + + def __init__(self, testcase): + self.testcase = testcase + self._store = {} + + def __setitem__(self, key, value): + self._store[key] = value + + def __getitem__(self, key): + value = self._store[key] + if isinstance(value, str): + return getattr(self.testcase, value) + return value + + def get(self, key, default=None): + if key in self._store: + return self[key] + return default + + class TestCase(object): """A class whose instances are single test cases. @@ -253,13 +274,13 @@ class TestCase(object): # Map types to custom assertEqual functions that will compare # instances of said type in more detail to generate a more useful # error message. - self._type_equality_funcs = {} - self.addTypeEqualityFunc(dict, self.assertDictEqual) - self.addTypeEqualityFunc(list, self.assertListEqual) - self.addTypeEqualityFunc(tuple, self.assertTupleEqual) - self.addTypeEqualityFunc(set, self.assertSetEqual) - self.addTypeEqualityFunc(frozenset, self.assertSetEqual) - self.addTypeEqualityFunc(str, self.assertMultiLineEqual) + self._type_equality_funcs = _TypeEqualityDict(self) + self.addTypeEqualityFunc(dict, 'assertDictEqual') + self.addTypeEqualityFunc(list, 'assertListEqual') + self.addTypeEqualityFunc(tuple, 'assertTupleEqual') + self.addTypeEqualityFunc(set, 'assertSetEqual') + self.addTypeEqualityFunc(frozenset, 'assertSetEqual') + self.addTypeEqualityFunc(str, 'assertMultiLineEqual') def addTypeEqualityFunc(self, typeobj, function): """Add a type specific assertEqual style function to compare a type. |