Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e8c757

Browse files
committed
Merged revisions 74865,75175,75180 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r74865 | georg.brandl | 2009-09-17 02:49:37 -0500 (Thu, 17 Sep 2009) | 1 line #6912: add "with" block support to pindent. ........ r75175 | georg.brandl | 2009-10-01 15:11:14 -0500 (Thu, 01 Oct 2009) | 1 line Fix some weird whitespace and two other overlong lines. ........ r75180 | georg.brandl | 2009-10-01 15:59:31 -0500 (Thu, 01 Oct 2009) | 1 line #7031: Add TestCase.assertIsInstance and negated method. ........
1 parent 23c5050 commit 6e8c757

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

Doc/library/unittest.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,22 @@ Test cases
952952
.. versionadded:: 3.1
953953

954954

955+
.. method:: assertIsInstance(obj, cls[, msg])
956+
957+
This signals a test failure if *obj* is not an instance of *cls* (which
958+
can be a class or a tuple of classes, as supported by :func:`isinstance`).
959+
960+
.. versionadded:: 3.2
961+
962+
963+
.. method:: assertNotIsInstance(obj, cls[, msg])
964+
965+
The inverse of the :meth:`assertIsInstance` method. This signals a test
966+
failure if *obj* is an instance of *cls*.
967+
968+
.. versionadded:: 3.2
969+
970+
955971
.. method:: assertFalse(expr, msg=None)
956972
failIf(expr, msg=None)
957973

Lib/test/test_unittest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,18 @@ def testAssertIsNot(self):
25102510
self.assertIsNot(thing, object())
25112511
self.assertRaises(self.failureException, self.assertIsNot, thing, thing)
25122512

2513+
def testAssertIsInstance(self):
2514+
thing = []
2515+
self.assertIsInstance(thing, list)
2516+
self.assertRaises(self.failureException, self.assertIsInstance,
2517+
thing, dict)
2518+
2519+
def testAssertNotIsInstance(self):
2520+
thing = []
2521+
self.assertNotIsInstance(thing, dict)
2522+
self.assertRaises(self.failureException, self.assertNotIsInstance,
2523+
thing, list)
2524+
25132525
def testAssertIn(self):
25142526
animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
25152527

Lib/unittest/case.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,9 @@ def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
620620
except (TypeError, IndexError, NotImplementedError):
621621
differing += ('Unable to index element %d '
622622
'of second %s\n' % (len1, seq_type_name))
623-
standardMsg = differing + '\n' + '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),
624-
pprint.pformat(seq2).splitlines()))
623+
standardMsg = differing + '\n' + '\n'.join(
624+
difflib.ndiff(pprint.pformat(seq1).splitlines(),
625+
pprint.pformat(seq2).splitlines()))
625626
msg = self._formatMessage(msg, standardMsg)
626627
self.fail(msg)
627628

@@ -734,7 +735,8 @@ def assertDictContainsSubset(self, expected, actual, msg=None):
734735
if key not in actual:
735736
missing.append(key)
736737
elif value != actual[key]:
737-
mismatched.append('%s, expected: %s, actual: %s' % (key, value, actual[key]))
738+
mismatched.append('%s, expected: %s, actual: %s' %
739+
(key, value, actual[key]))
738740

739741
if not (missing or mismatched):
740742
return
@@ -793,7 +795,8 @@ def assertMultiLineEqual(self, first, second, msg=None):
793795
'Second argument is not a string'))
794796

795797
if first != second:
796-
standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True), second.splitlines(True)))
798+
standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True),
799+
second.splitlines(True)))
797800
self.fail(self._formatMessage(msg, standardMsg))
798801

799802
def assertLess(self, a, b, msg=None):
@@ -832,6 +835,19 @@ def assertIsNotNone(self, obj, msg=None):
832835
standardMsg = 'unexpectedly None'
833836
self.fail(self._formatMessage(msg, standardMsg))
834837

838+
def assertIsInstance(self, obj, cls, msg=None):
839+
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer
840+
default message."""
841+
if not isinstance(obj, cls):
842+
standardMsg = '%r is not an instance of %r' % (obj, cls)
843+
self.fail(self._formatMessage(msg, standardMsg))
844+
845+
def assertNotIsInstance(self, obj, cls, msg=None):
846+
"""Included for symmetry with assertIsInstance."""
847+
if isinstance(obj, cls):
848+
standardMsg = '%r is an instance of %r' % (obj, cls)
849+
self.fail(self._formatMessage(msg, standardMsg))
850+
835851
def assertRaisesRegexp(self, expected_exception, expected_regexp,
836852
callable_obj=None, *args, **kwargs):
837853
"""Asserts that the message in a raised exception matches a regexp.

Tools/scripts/pindent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@
8888
next['if'] = next['elif'] = 'elif', 'else', 'end'
8989
next['while'] = next['for'] = 'else', 'end'
9090
next['try'] = 'except', 'finally'
91-
next['except'] = 'except', 'else', 'end'
91+
next['except'] = 'except', 'else', 'finally', 'end'
9292
next['else'] = next['finally'] = next['def'] = next['class'] = 'end'
9393
next['end'] = ()
94-
start = 'if', 'while', 'for', 'try', 'def', 'class'
94+
start = 'if', 'while', 'for', 'try', 'with', 'def', 'class'
9595

9696
class PythonIndenter:
9797

0 commit comments

Comments
 (0)