/
testExceptionFormatter.py
154 lines (124 loc) · 4.3 KB
/
testExceptionFormatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
##############################################################################
#
# 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 __future__ import nested_scopes
from unittest import TestCase, TestSuite, main, makeSuite
try:
from Testing.CleanUp import CleanUp # Base class w registry cleanup
except ImportError:
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')