testExceptionFormatter.py :  » Web-Frameworks » Zope » Zope-2.6.0 » lib » python » zExceptions » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » lib » python » zExceptions » tests » testExceptionFormatter.py
##############################################################################
#
# 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: testExceptionFormatter.py,v 1.4 2002/08/14 22:12:07 mj Exp $
"""

from __future__ import nested_scopes

from unittest import TestCase,TestSuite,main,makeSuite

try:
    from Testing.CleanUp import CleanUp# Base class w registry cleanup
exceptImportError:
    class CleanUp:
        pass

import sys
from zExceptions.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 TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.