Commit 7861ac1c authored by maruel@chromium.org's avatar maruel@chromium.org

Reduce auto_stub.py by removing python 2.6 support code.

This makes this file even easier to read and python 2.6 support is not needed
anymore.

R=iannucci@chromium.org
BUG=

Review URL: https://codereview.chromium.org/238233002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@265799 0039d316-1c4b-4281-b951-d872f2087c98
parent f182f61c
......@@ -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))
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