Created
October 15, 2014 22:03
-
-
Save abadger/62b5c3af5a715470f49f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (c) 2012-2014, Michael DeHaan <[email protected]> | |
# | |
# This file is part of Ansible | |
# | |
# Ansible is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# Ansible is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. | |
import unittest | |
# | |
# Compat for python2.6 | |
# | |
# Could get rid of _CompatTestCase if we use unittest2 | |
TESTCASE_METHODS = ('assertIsNone', 'assertIsNotNone', | |
'assertIs', 'assertIsNot', | |
'assertIn', 'assertNotIn',) | |
_NEED_COMPAT = False | |
for method in TESTCASE_METHODS: | |
if not hasattr(unittest.TestCase, method): | |
_NEED_COMPAT = True | |
class _CompatTestCase(unittest.TestCase): | |
def _assertIsNone(self, obj, msg=None): | |
"""Same as self.assertTrue(obj is None), with a nicer default message.""" | |
if obj is not None: | |
standardMsg = '%s is not None' % (safe_repr(obj),) | |
self.fail(self._formatMessage(msg, standardMsg)) | |
def _assertIsNotNone(self, obj, msg=None): | |
"""Included for symmetry with assertIsNone.""" | |
if obj is None: | |
standardMsg = 'unexpectedly None' | |
self.fail(self._formatMessage(msg, standardMsg)) | |
def _assertIs(self, expr1, expr2, msg=None): | |
"""Just like self.assertTrue(a is b), but with a nicer default message.""" | |
if expr1 is not expr2: | |
standardMsg = '%s is not %s' % (safe_repr(expr1), | |
safe_repr(expr2)) | |
self.fail(self._formatMessage(msg, standardMsg)) | |
def _assertIsNot(self, expr1, expr2, msg=None): | |
"""Just like self.assertTrue(a is not b), but with a nicer default message.""" | |
if expr1 is expr2: | |
standardMsg = 'unexpectedly identical: %s' % (safe_repr(expr1),) | |
self.fail(self._formatMessage(msg, standardMsg)) | |
def _assertIn(self, member, container, msg=None): | |
"""Just like self.assertTrue(a in b), but with a nicer default message.""" | |
if member not in container: | |
standardMsg = '%s not found in %s' % (safe_repr(member), | |
safe_repr(container)) | |
self.fail(self._formatMessage(msg, standardMsg)) | |
def _assertNotIn(self, member, container, msg=None): | |
"""Just like self.assertTrue(a not in b), but with a nicer default message.""" | |
if member in container: | |
standardMsg = '%s unexpectedly found in %s' % (safe_repr(member), | |
safe_repr(container)) | |
self.fail(self._formatMessage(msg, standardMsg)) | |
def __init__(self, *args, **kwargs): | |
# Python 2.6 compat | |
if not hasattr(self, 'assertIsNone'): | |
self.assertIsNone = self._assertIsNone | |
if not hasattr(self, 'assertIsNotNone'): | |
self.assertIsNotNone = self._assertIsNotNone | |
if not hasattr(self, 'assertIs'): | |
self.assertIs = self._assertIs | |
if not hasattr(self, 'assertIsNot'): | |
self.assertIsNot = self._assertIsNot | |
if not hasattr(self, 'assertIn'): | |
self.assertIn = self._assertIn | |
if not hasattr(self, 'assertNotIn'): | |
self.assertNotIn = self._assertNotIn | |
super(CompatTestCase, self).__init__(*args, **kwargs) | |
if _NEED_COMPAT: | |
CompatTestCase = _CompatTestCase | |
else: | |
CompatTestCase = unittest.TestCase | |
# | |
# Compat for python2.7 | |
# | |
# Could use the pypi mock library on py3 as well as py2. They are the same | |
try: | |
from unittest.mock import mock_open, patch | |
except ImportError: | |
# Python2 | |
from mock import mock_open, patch | |
try: | |
import __builtin__ | |
except ImportError: | |
BUILTINS = 'builtins' | |
else: | |
BUILTINS = '__builtin__' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment