Index: testing_support/auto_stub.py |
diff --git a/testing_support/auto_stub.py b/testing_support/auto_stub.py |
index 8ad4bbe74433a768011ea8fe595f2a5cc4f370c0..c5a3495c6fc7e6335fb24cd139f9359c56a7e78c 100644 |
--- a/testing_support/auto_stub.py |
+++ b/testing_support/auto_stub.py |
@@ -2,36 +2,13 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
+__version__ = '1.0' |
+ |
+import collections |
import inspect |
import unittest |
-class OrderedDict(object): |
- """Incomplete and inefficient implementation of collections.OrderedDict.""" |
- def __init__(self): |
- self._keys = [] |
- |
- def setdefault(self, key, value): |
- try: |
- self._getindex(key) |
- except KeyError: |
- self._keys.append((key, value)) |
- return self[key] |
- |
- def _getindex(self, key): |
- for i, v in enumerate(self._keys): |
- if v[0] == key: |
- return i |
- raise KeyError(key) |
- |
- def __getitem__(self, key): |
- return self._keys[self._getindex(key)][1] |
- |
- def iteritems(self): |
- for i in self._keys: |
- yield i |
- |
- |
class AutoStubMixIn(object): |
"""Automatically restores stubbed functions on unit test teardDown. |
@@ -40,9 +17,9 @@ class AutoStubMixIn(object): |
_saved = None |
def mock(self, obj, member, mock): |
- self._saved = self._saved or OrderedDict() |
+ self._saved = self._saved or collections.OrderedDict() |
old_value = self._saved.setdefault( |
- obj, OrderedDict()).setdefault(member, getattr(obj, member)) |
+ obj, collections.OrderedDict()).setdefault(member, getattr(obj, member)) |
setattr(obj, member, mock) |
return old_value |
@@ -86,43 +63,11 @@ class SimpleMock(object): |
class TestCase(unittest.TestCase, AutoStubMixIn): |
- """Adds python 2.7 functionality.""" |
- |
+ """Adds self.mock() and self.has_failed() to a TestCase.""" |
def tearDown(self): |
AutoStubMixIn.tearDown(self) |
unittest.TestCase.tearDown(self) |
def has_failed(self): |
"""Returns True if the test has failed.""" |
- if hasattr(self, '_exc_info'): |
- # Only present in python <= 2.6 |
- # pylint: disable=E1101 |
- return bool(self._exc_info()[0]) |
- |
- # Only present in python >= 2.7 |
- # pylint: disable=E1101 |
return not self._resultForDoCleanups.wasSuccessful() |
- |
- def assertIs(self, expr1, expr2, msg=None): |
- if hasattr(super(TestCase, self), 'assertIs'): |
- return super(TestCase, self).assertIs(expr1, expr2, msg) |
- if expr1 is not expr2: |
- self.fail(msg or '%r is not %r' % (expr1, expr2)) |
- |
- def assertIsNot(self, expr1, expr2, msg=None): |
- if hasattr(super(TestCase, self), 'assertIsNot'): |
- return super(TestCase, self).assertIsNot(expr1, expr2, msg) |
- if expr1 is expr2: |
- self.fail(msg or 'unexpectedly identical: %r' % expr1) |
- |
- def assertIn(self, expr1, expr2, msg=None): |
- if hasattr(super(TestCase, self), 'assertIn'): |
- return super(TestCase, self).assertIn(expr1, expr2, msg) |
- if expr1 not in expr2: |
- self.fail(msg or '%r not in %r' % (expr1, expr2)) |
- |
- def assertLess(self, a, b, msg=None): |
- if hasattr(super(TestCase, self), 'assertLess'): |
- return super(TestCase, self).assertLess(a, b, msg) |
- if not a < b: |
- self.fail(msg or '%r not less than %r' % (a, b)) |