]> granicus.if.org Git - python/commitdiff
Merged revisions 74865,75175,75180 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Sun, 4 Oct 2009 20:19:21 +0000 (20:19 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sun, 4 Oct 2009 20:19:21 +0000 (20:19 +0000)
svn+ssh://pythondev@svn.python.org/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.
........

Doc/library/unittest.rst
Lib/test/test_unittest.py
Lib/unittest/case.py
Tools/scripts/pindent.py

index 494c2d7fbc0b9bb0809e472050ed7cee2dfe4a1e..aa763086bffbc16a539bba2a2289704f0fa2d410 100644 (file)
@@ -952,6 +952,22 @@ Test cases
       .. versionadded:: 3.1
 
 
+   .. method:: assertIsInstance(obj, cls[, msg])
+
+      This signals a test failure if *obj* is not an instance of *cls* (which
+      can be a class or a tuple of classes, as supported by :func:`isinstance`).
+
+      .. versionadded:: 3.2
+
+
+   .. method:: assertNotIsInstance(obj, cls[, msg])
+
+      The inverse of the :meth:`assertIsInstance` method.  This signals a test
+      failure if *obj* is an instance of *cls*.
+
+      .. versionadded:: 3.2
+
+
    .. method:: assertFalse(expr, msg=None)
                failIf(expr, msg=None)
 
index 108802db3d715a9a8b756b6993fdd5079bd42e77..120a90d759cadc0f611bc9c1ab2637babebf8129 100644 (file)
@@ -2510,6 +2510,18 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
         self.assertIsNot(thing, object())
         self.assertRaises(self.failureException, self.assertIsNot, thing, thing)
 
+    def testAssertIsInstance(self):
+        thing = []
+        self.assertIsInstance(thing, list)
+        self.assertRaises(self.failureException, self.assertIsInstance,
+                          thing, dict)
+
+    def testAssertNotIsInstance(self):
+        thing = []
+        self.assertNotIsInstance(thing, dict)
+        self.assertRaises(self.failureException, self.assertNotIsInstance,
+                          thing, list)
+
     def testAssertIn(self):
         animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
 
index 660ddebccae983baa192bd3a8efb00d0d2bb730c..88254be68b4e0538c7f37586d8c18e2832f3248a 100644 (file)
@@ -620,8 +620,9 @@ class TestCase(object):
                 except (TypeError, IndexError, NotImplementedError):
                     differing += ('Unable to index element %d '
                                   'of second %s\n' % (len1, seq_type_name))
-        standardMsg = differing + '\n' + '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),
-                                            pprint.pformat(seq2).splitlines()))
+        standardMsg = differing + '\n' + '\n'.join(
+            difflib.ndiff(pprint.pformat(seq1).splitlines(),
+                          pprint.pformat(seq2).splitlines()))
         msg = self._formatMessage(msg, standardMsg)
         self.fail(msg)
 
@@ -734,7 +735,8 @@ class TestCase(object):
             if key not in actual:
                 missing.append(key)
             elif value != actual[key]:
-                mismatched.append('%s, expected: %s, actual: %s' % (key, value,                                                                                                       actual[key]))
+                mismatched.append('%s, expected: %s, actual: %s' %
+                                  (key, value, actual[key]))
 
         if not (missing or mismatched):
             return
@@ -793,7 +795,8 @@ class TestCase(object):
                 'Second argument is not a string'))
 
         if first != second:
-            standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True), second.splitlines(True)))
+            standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True),
+                                                       second.splitlines(True)))
             self.fail(self._formatMessage(msg, standardMsg))
 
     def assertLess(self, a, b, msg=None):
@@ -832,6 +835,19 @@ class TestCase(object):
             standardMsg = 'unexpectedly None'
             self.fail(self._formatMessage(msg, standardMsg))
 
+    def assertIsInstance(self, obj, cls, msg=None):
+        """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
+        default message."""
+        if not isinstance(obj, cls):
+            standardMsg = '%r is not an instance of %r' % (obj, cls)
+            self.fail(self._formatMessage(msg, standardMsg))
+
+    def assertNotIsInstance(self, obj, cls, msg=None):
+        """Included for symmetry with assertIsInstance."""
+        if isinstance(obj, cls):
+            standardMsg = '%r is an instance of %r' % (obj, cls)
+            self.fail(self._formatMessage(msg, standardMsg))
+
     def assertRaisesRegexp(self, expected_exception, expected_regexp,
                            callable_obj=None, *args, **kwargs):
         """Asserts that the message in a raised exception matches a regexp.
index 3f3000d90c4d991a05f918af2effe33c844a882d..f80a8b7e23ad97770c44c2d0856a0c9a9a7cd3a1 100755 (executable)
@@ -88,10 +88,10 @@ next = {}
 next['if'] = next['elif'] = 'elif', 'else', 'end'
 next['while'] = next['for'] = 'else', 'end'
 next['try'] = 'except', 'finally'
-next['except'] = 'except', 'else', 'end'
+next['except'] = 'except', 'else', 'finally', 'end'
 next['else'] = next['finally'] = next['def'] = next['class'] = 'end'
 next['end'] = ()
-start = 'if', 'while', 'for', 'try', 'def', 'class'
+start = 'if', 'while', 'for', 'try', 'with', 'def', 'class'
 
 class PythonIndenter: