move sections
[python/dscho.git] / Lib / test / test_exception_variations.py
blob8fe75e8fcc43e38ce9aa5be59dcbe1bafa0a952d
2 from test.test_support import run_unittest
3 import unittest
5 class ExceptionTestCase(unittest.TestCase):
6 def test_try_except_else_finally(self):
7 hit_except = False
8 hit_else = False
9 hit_finally = False
11 try:
12 raise Exception, 'nyaa!'
13 except:
14 hit_except = True
15 else:
16 hit_else = True
17 finally:
18 hit_finally = True
20 self.assertTrue(hit_except)
21 self.assertTrue(hit_finally)
22 self.assertFalse(hit_else)
24 def test_try_except_else_finally_no_exception(self):
25 hit_except = False
26 hit_else = False
27 hit_finally = False
29 try:
30 pass
31 except:
32 hit_except = True
33 else:
34 hit_else = True
35 finally:
36 hit_finally = True
38 self.assertFalse(hit_except)
39 self.assertTrue(hit_finally)
40 self.assertTrue(hit_else)
42 def test_try_except_finally(self):
43 hit_except = False
44 hit_finally = False
46 try:
47 raise Exception, 'yarr!'
48 except:
49 hit_except = True
50 finally:
51 hit_finally = True
53 self.assertTrue(hit_except)
54 self.assertTrue(hit_finally)
56 def test_try_except_finally_no_exception(self):
57 hit_except = False
58 hit_finally = False
60 try:
61 pass
62 except:
63 hit_except = True
64 finally:
65 hit_finally = True
67 self.assertFalse(hit_except)
68 self.assertTrue(hit_finally)
70 def test_try_except(self):
71 hit_except = False
73 try:
74 raise Exception, 'ahoy!'
75 except:
76 hit_except = True
78 self.assertTrue(hit_except)
80 def test_try_except_no_exception(self):
81 hit_except = False
83 try:
84 pass
85 except:
86 hit_except = True
88 self.assertFalse(hit_except)
90 def test_try_except_else(self):
91 hit_except = False
92 hit_else = False
94 try:
95 raise Exception, 'foo!'
96 except:
97 hit_except = True
98 else:
99 hit_else = True
101 self.assertFalse(hit_else)
102 self.assertTrue(hit_except)
104 def test_try_except_else_no_exception(self):
105 hit_except = False
106 hit_else = False
108 try:
109 pass
110 except:
111 hit_except = True
112 else:
113 hit_else = True
115 self.assertFalse(hit_except)
116 self.assertTrue(hit_else)
118 def test_try_finally_no_exception(self):
119 hit_finally = False
121 try:
122 pass
123 finally:
124 hit_finally = True
126 self.assertTrue(hit_finally)
128 def test_nested(self):
129 hit_finally = False
130 hit_inner_except = False
131 hit_inner_finally = False
133 try:
134 try:
135 raise Exception, 'inner exception'
136 except:
137 hit_inner_except = True
138 finally:
139 hit_inner_finally = True
140 finally:
141 hit_finally = True
143 self.assertTrue(hit_inner_except)
144 self.assertTrue(hit_inner_finally)
145 self.assertTrue(hit_finally)
147 def test_nested_else(self):
148 hit_else = False
149 hit_finally = False
150 hit_except = False
151 hit_inner_except = False
152 hit_inner_else = False
154 try:
155 try:
156 pass
157 except:
158 hit_inner_except = True
159 else:
160 hit_inner_else = True
162 raise Exception, 'outer exception'
163 except:
164 hit_except = True
165 else:
166 hit_else = True
167 finally:
168 hit_finally = True
170 self.assertFalse(hit_inner_except)
171 self.assertTrue(hit_inner_else)
172 self.assertFalse(hit_else)
173 self.assertTrue(hit_finally)
174 self.assertTrue(hit_except)
176 def test_main():
177 run_unittest(ExceptionTestCase)
179 if __name__ == '__main__':
180 test_main()