[Checkins] SVN: zope.exceptions/branches/tseaver-no_2to3/src/zope/exceptions/tests/test_exceptionformatter.py Clean up existing unit tests.

Tres Seaver cvs-admin at zope.org
Fri Apr 6 19:36:05 UTC 2012


Log message for revision 125015:
  Clean up existing unit tests.

Changed:
  U   zope.exceptions/branches/tseaver-no_2to3/src/zope/exceptions/tests/test_exceptionformatter.py

-=-
Modified: zope.exceptions/branches/tseaver-no_2to3/src/zope/exceptions/tests/test_exceptionformatter.py
===================================================================
--- zope.exceptions/branches/tseaver-no_2to3/src/zope/exceptions/tests/test_exceptionformatter.py	2012-04-06 19:35:58 UTC (rev 125014)
+++ zope.exceptions/branches/tseaver-no_2to3/src/zope/exceptions/tests/test_exceptionformatter.py	2012-04-06 19:36:02 UTC (rev 125015)
@@ -13,166 +13,105 @@
 ##############################################################################
 """ExceptionFormatter tests.
 """
+import unittest
 
-import sys
-from unittest import TestCase, makeSuite
 
-from zope.exceptions.exceptionformatter import format_exception
-from zope.exceptions.exceptionformatter import extract_stack
+class Test_format_exception(unittest.TestCase):
 
+    def _callFUT(self, as_html=False):
+        import sys
+        from zope.exceptions.exceptionformatter import format_exception
+        t, v, b = sys.exc_info()
+        try:
+            return ''.join(format_exception(t, v, b, as_html=as_html))
+        finally:
+            del b
 
-def tb(as_html=0):
-    t, v, b = sys.exc_info()
-    try:
-        return ''.join(format_exception(t, v, b, as_html=as_html))
-    finally:
-        del b
-
-def st(as_html=0):
-    f = sys.exc_info()[2].tb_frame
-    try:
-        return ''.join(extract_stack(f, as_html=as_html))
-    finally:
-        del f
-
-class ExceptionForTesting (Exception):
-    pass
-
-
-class TestingTracebackSupplement(object):
-
-    source_url = '/somepath'
-    line = 634
-    column = 57
-    warnings = ['Repent, for the end is nigh']
-
-    def __init__(self, expression):
-        self.expression = expression
-
-
-class Test(TestCase):
-
-    def testBasicNamesText(self, as_html=0):
+    def test_basic_names_text(self):
         try:
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = tb(as_html)
-            # The traceback should include the name of this function.
-            self.assertTrue(s.find('testBasicNamesText') >= 0)
-            # The traceback should include the name of the exception.
-            self.assertTrue(s.find('ExceptionForTesting') >= 0)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(False)
+        # The traceback should include the name of this function.
+        self.assertTrue(s.find('test_basic_names_text') >= 0)
+        # The traceback should include the name of the exception.
+        self.assertTrue(s.find('ExceptionForTesting') >= 0)
 
-    def testBasicNamesHTML(self):
-        self.testBasicNamesText(1)
-
-    def testBasicNamesText_stack(self, as_html=0):
+    def test_basic_names_html(self):
         try:
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = st(as_html)
-            # The stack trace should include the name of this function.
-            self.assertTrue(s.find('testBasicNamesText_stack') >= 0)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(True)
+        # The traceback should include the name of this function.
+        self.assertTrue(s.find('test_basic_names_html') >= 0)
+        # The traceback should include the name of the exception.
+        self.assertTrue(s.find('ExceptionForTesting') >= 0)
 
-    def testBasicNamesHTML_stack(self):
-        self.testBasicNamesText_stack(1)
-
-    def testSupplement(self, as_html=0):
+    def test_traceback_info_text(self):
         try:
-            __traceback_supplement__ = (TestingTracebackSupplement,
-                                        "You're one in a million")
+            __traceback_info__ = "Adam & Eve"
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = tb(as_html)
-            # The source URL
-            self.assertTrue(s.find('/somepath') >= 0, s)
-            # The line number
-            self.assertTrue(s.find('634') >= 0, s)
-            # The column number
-            self.assertTrue(s.find('57') >= 0, s)
-            # The expression
-            self.assertTrue(s.find("You're one in a million") >= 0, s)
-            # The warning
-            self.assertTrue(s.find("Repent, for the end is nigh") >= 0, s)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(False)
+        self.assertTrue(s.find('Adam & Eve') >= 0, s)
 
-    def testSupplementHTML(self):
-        self.testSupplement(1)
-
-    def testSupplement_stack(self, as_html=0):
+    def test_traceback_info_html(self):
         try:
-            __traceback_supplement__ = (TestingTracebackSupplement,
-                                        "You're one in a million")
+            __traceback_info__ = "Adam & Eve"
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = st(as_html)
-            # The source URL
-            self.assertTrue(s.find('/somepath') >= 0, s)
-            # The line number
-            self.assertTrue(s.find('634') >= 0, s)
-            # The column number
-            self.assertTrue(s.find('57') >= 0, s)
-            # The expression
-            self.assertTrue(s.find("You're one in a million") >= 0, s)
-            # The warning
-            self.assertTrue(s.find("Repent, for the end is nigh") >= 0, s)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(True)
+        # Be sure quoting is happening.
+        self.assertTrue(s.find('Adam & Eve') >= 0, s)
 
-    def testSupplementHTML_stack(self):
-        self.testSupplement_stack(1)
-
-    def testTracebackInfo(self, as_html=0):
+    def test_traceback_info_is_tuple(self):
         try:
-            __traceback_info__ = "Adam & Eve"
+            __traceback_info__ = ("Adam", "Eve")
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = tb(as_html)
-            if as_html:
-                # Be sure quoting is happening.
-                self.assertTrue(s.find('Adam & Eve') >= 0, s)
-            else:
-                self.assertTrue(s.find('Adam & Eve') >= 0, s)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(False)
+        self.assertTrue(s.find('Adam') >= 0, s)
+        self.assertTrue(s.find('Eve') >= 0, s)
 
-    def testTracebackInfoHTML(self):
-        self.testTracebackInfo(1)
-
-    def testTracebackInfo_stack(self, as_html=0):
+    def test_supplement_text(self, as_html=0):
         try:
-            __traceback_info__ = "Adam & Eve"
+            __traceback_supplement__ = (TestingTracebackSupplement,
+                                        "You're one in a million")
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = st(as_html)
-            if as_html:
-                # Be sure quoting is happening.
-                self.assertTrue(s.find('Adam & Eve') >= 0, s)
-            else:
-                self.assertTrue(s.find('Adam & Eve') >= 0, s)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(as_html)
+        # The source URL
+        self.assertTrue(s.find('/somepath') >= 0, s)
+        # The line number
+        self.assertTrue(s.find('634') >= 0, s)
+        # The column number
+        self.assertTrue(s.find('57') >= 0, s)
+        # The expression
+        self.assertTrue(s.find("You're one in a million") >= 0, s)
+        # The warning
+        self.assertTrue(s.find("Repent, for the end is nigh") >= 0, s)
 
-    def testTracebackInfoHTML_stack(self):
-        self.testTracebackInfo_stack(1)
-
-    def testTracebackInfoTuple(self):
+    def test_supplement_html(self):
         try:
-            __traceback_info__ = ("Adam", "Eve")
+            __traceback_supplement__ = (TestingTracebackSupplement,
+                                        "You're one in a million")
             raise ExceptionForTesting
         except ExceptionForTesting:
-            s = tb()
-            self.assertTrue(s.find('Adam') >= 0, s)
-            self.assertTrue(s.find('Eve') >= 0, s)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(True)
+        # The source URL
+        self.assertTrue(s.find('/somepath') >= 0, s)
+        # The line number
+        self.assertTrue(s.find('634') >= 0, s)
+        # The column number
+        self.assertTrue(s.find('57') >= 0, s)
+        # The expression
+        self.assertTrue(s.find("You're one in a million") >= 0, s)
+        # The warning
+        self.assertTrue(s.find("Repent, for the end is nigh") >= 0, s)
 
-    def testMultipleLevels(self):
-        # Makes sure many levels are shown in a traceback.
+    def test_multiple_levels(self):
+        # Ensure many levels are shown in a traceback.
+        HOW_MANY = 10
         def f(n):
             """Produces a (n + 1)-level traceback."""
             __traceback_info__ = 'level%d' % n
@@ -180,39 +119,35 @@
                 f(n - 1)
             else:
                 raise ExceptionForTesting
-
         try:
-            f(10)
+            f(HOW_MANY)
         except ExceptionForTesting:
-            s = tb()
-            for n in range(11):
-                self.assertTrue(s.find('level%d' % n) >= 0, s)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(False)
+        for n in range(HOW_MANY+1):
+            self.assertTrue(s.find('level%d' % n) >= 0, s)
 
-    def testQuoteLastLine(self):
+    def test_quote_last_line(self):
         class C(object):
             pass
         try:
             raise TypeError(C())
         except:
-            s = tb(1)
-        else:
-            self.fail('no exception occurred')
+            s = self._callFUT(True)
         self.assertTrue(s.find('<') >= 0, s)
         self.assertTrue(s.find('>') >= 0, s)
 
-    def testMultilineException(self):
+    def test_multiline_exception(self):
         try:
             exec 'syntax error\n'
         except Exception:
-            s = tb()
+            s = self._callFUT(False)
         self.assertEqual(s.splitlines()[-3:],
                          ['    syntax error',
                           '               ^',
                           'SyntaxError: invalid syntax'])
 
-    def testRecursionFailure(self):
+    def test_recursion_failure(self):
+        import sys
         from zope.exceptions.exceptionformatter import TextExceptionFormatter
 
         class FormatterException(Exception):
@@ -229,13 +164,114 @@
             try:
                 fmt.formatException(*sys.exc_info())
             except FormatterException:
-                s = tb()
+                s = self._callFUT(False)
         # Recursion was detected
-        self.assertTrue('(Recursive formatException() stopped, trying traceback.format_tb)' in s, s)
+        self.assertTrue('(Recursive formatException() stopped, '
+                        'trying traceback.format_tb)' in s, s)
         # and we fellback to the stdlib rather than hid the real error
-        self.assertEqual(s.splitlines()[-2], '    raise FormatterException("Formatter failed")')
-        self.assertTrue('FormatterException: Formatter failed' in s.splitlines()[-1])
+        self.assertEqual(s.splitlines()[-2],
+                         '    raise FormatterException("Formatter failed")')
+        self.assertTrue('FormatterException: Formatter failed'
+                        in s.splitlines()[-1])
 
 
+class Test_extract_stack(unittest.TestCase):
+
+    def _callFUT(self, as_html=False):
+        import sys
+        from zope.exceptions.exceptionformatter import extract_stack
+        f = sys.exc_info()[2].tb_frame
+        try:
+            return ''.join(extract_stack(f, as_html=as_html))
+        finally:
+            del f
+
+    def test_basic_names_as_text(self, as_html=0):
+        try:
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = self._callFUT(False)
+        # The stack trace should include the name of this function.
+        self.assertTrue(s.find('test_basic_names_as_text') >= 0)
+
+    def test_basic_names_as_html(self):
+        try:
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = self._callFUT(True)
+        # The stack trace should include the name of this function.
+        self.assertTrue(s.find('test_basic_names_as_html') >= 0)
+
+    def test_traceback_info_text(self):
+        try:
+            __traceback_info__ = "Adam & Eve"
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = self._callFUT(False)
+        self.assertTrue(s.find('Adam & Eve') >= 0, s)
+
+    def test_traceback_info_html(self):
+        try:
+            __traceback_info__ = "Adam & Eve"
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = self._callFUT(True)
+        self.assertTrue(s.find('Adam & Eve') >= 0, s)
+
+    def test_traceback_supplement_text(self):
+        try:
+            __traceback_supplement__ = (TestingTracebackSupplement,
+                                        "You're one in a million")
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = self._callFUT(False)
+        # The source URL
+        self.assertTrue(s.find('/somepath') >= 0, s)
+        # The line number
+        self.assertTrue(s.find('634') >= 0, s)
+        # The column number
+        self.assertTrue(s.find('57') >= 0, s)
+        # The expression
+        self.assertTrue(s.find("You're one in a million") >= 0, s)
+        # The warning
+        self.assertTrue(s.find("Repent, for the end is nigh") >= 0, s)
+
+    def test_traceback_supplement_html(self):
+        try:
+            __traceback_supplement__ = (TestingTracebackSupplement,
+                                        "You're one in a million")
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = self._callFUT(True)
+        # The source URL
+        self.assertTrue(s.find('/somepath') >= 0, s)
+        # The line number
+        self.assertTrue(s.find('634') >= 0, s)
+        # The column number
+        self.assertTrue(s.find('57') >= 0, s)
+        # The expression
+        self.assertTrue(s.find("You're one in a million") >= 0, s)
+        # The warning
+        self.assertTrue(s.find("Repent, for the end is nigh") >= 0, s)
+
+
+class ExceptionForTesting (Exception):
+    pass
+
+
+class TestingTracebackSupplement(object):
+
+    source_url = '/somepath'
+    line = 634
+    column = 57
+    warnings = ['Repent, for the end is nigh']
+
+    def __init__(self, expression):
+        self.expression = expression
+
+
 def test_suite():
-    return makeSuite(Test)
+    return unittest.TestSuite((
+        unittest.makeSuite(Test_format_exception),
+        unittest.makeSuite(Test_extract_stack),
+        ))



More information about the checkins mailing list