[Zope3-checkins] CVS: Zope3/src/zope/exceptions/tests - __init__.py:1.2 test_exceptionformatter.py:1.2

Jim Fulton jim@zope.com
Wed, 25 Dec 2002 09:14:10 -0500


Update of /cvs-repository/Zope3/src/zope/exceptions/tests
In directory cvs.zope.org:/tmp/cvs-serv15352/src/zope/exceptions/tests

Added Files:
	__init__.py test_exceptionformatter.py 
Log Message:
Grand renaming:

- Renamed most files (especially python modules) to lower case.

- Moved views and interfaces into separate hierarchies within each
  project, where each top-level directory under the zope package
  is a separate project.

- Moved everything to src from lib/python.

  lib/python will eventually go away. I need access to the cvs
  repository to make this happen, however.

There are probably some bits that are broken. All tests pass
and zope runs, but I haven't tried everything. There are a number
of cleanups I'll work on tomorrow.



=== Zope3/src/zope/exceptions/tests/__init__.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:09 2002
+++ Zope3/src/zope/exceptions/tests/__init__.py	Wed Dec 25 09:13:39 2002
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/exceptions/tests/test_exceptionformatter.py 1.1 => 1.2 ===
--- /dev/null	Wed Dec 25 09:14:09 2002
+++ Zope3/src/zope/exceptions/tests/test_exceptionformatter.py	Wed Dec 25 09:13:39 2002
@@ -0,0 +1,145 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+ExceptionFormatter tests.
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from zope.testing.cleanup import CleanUp # Base class w registry cleanup
+
+import sys
+from zope.exceptions.exceptionformatter import format_exception
+
+
+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
+
+
+class ExceptionForTesting (Exception):
+    pass
+
+
+
+class TestingTracebackSupplement:
+
+    source_url = '/somepath'
+    line = 634
+    column = 57
+    warnings = ['Repent, for the end is nigh']
+
+    def __init__(self, expression):
+        self.expression = expression
+
+
+
+class Test(CleanUp, TestCase):
+
+    def testBasicNamesText(self, as_html=0):
+        try:
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = tb(as_html)
+            # The traceback should include the name of this function.
+            self.assert_(s.find('testBasicNamesText') >= 0)
+            # The traceback should include the name of the exception.
+            self.assert_(s.find('ExceptionForTesting') >= 0)
+        else:
+            self.fail('no exception occurred')
+
+    def testBasicNamesHTML(self):
+        self.testBasicNamesText(1)
+
+    def testSupplement(self, as_html=0):
+        try:
+            __traceback_supplement__ = (TestingTracebackSupplement,
+                                        "You're one in a million")
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = tb(as_html)
+            # The source URL
+            self.assert_(s.find('/somepath') >= 0, s)
+            # The line number
+            self.assert_(s.find('634') >= 0, s)
+            # The column number
+            self.assert_(s.find('57') >= 0, s)
+            # The expression
+            self.assert_(s.find("You're one in a million") >= 0, s)
+            # The warning
+            self.assert_(s.find("Repent, for the end is nigh") >= 0, s)
+        else:
+            self.fail('no exception occurred')
+
+    def testSupplementHTML(self):
+        self.testSupplement(1)
+
+    def testTracebackInfo(self, as_html=0):
+        try:
+            __traceback_info__ = "Adam & Eve"
+            raise ExceptionForTesting
+        except ExceptionForTesting:
+            s = tb(as_html)
+            if as_html:
+                # Be sure quoting is happening.
+                self.assert_(s.find('Adam & Eve') >= 0, s)
+            else:
+                self.assert_(s.find('Adam & Eve') >= 0, s)
+        else:
+            self.fail('no exception occurred')
+
+    def testTracebackInfoHTML(self):
+        self.testTracebackInfo(1)
+
+    def testMultipleLevels(self):
+        # Makes sure many levels are shown in a traceback.
+        def f(n):
+            """Produces a (n + 1)-level traceback."""
+            __traceback_info__ = 'level%d' % n
+            if n > 0:
+                f(n - 1)
+            else:
+                raise ExceptionForTesting
+
+        try:
+            f(10)
+        except ExceptionForTesting:
+            s = tb()
+            for n in range(11):
+                self.assert_(s.find('level%d' % n) >= 0, s)
+        else:
+            self.fail('no exception occurred')
+
+    def testQuoteLastLine(self):
+        class C: pass
+        try: raise TypeError, C()
+        except:
+            s = tb(1)
+        else:
+            self.fail('no exception occurred')
+        self.assert_(s.find('<') >= 0, s)
+        self.assert_(s.find('>') >= 0, s)
+
+
+
+def test_suite():
+    return makeSuite(Test)
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')