• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2from test.test_support import run_unittest
3import unittest
4
5class ExceptionTestCase(unittest.TestCase):
6    def test_try_except_else_finally(self):
7        hit_except = False
8        hit_else = False
9        hit_finally = False
10
11        try:
12            raise Exception, 'nyaa!'
13        except:
14            hit_except = True
15        else:
16            hit_else = True
17        finally:
18            hit_finally = True
19
20        self.assertTrue(hit_except)
21        self.assertTrue(hit_finally)
22        self.assertFalse(hit_else)
23
24    def test_try_except_else_finally_no_exception(self):
25        hit_except = False
26        hit_else = False
27        hit_finally = False
28
29        try:
30            pass
31        except:
32            hit_except = True
33        else:
34            hit_else = True
35        finally:
36            hit_finally = True
37
38        self.assertFalse(hit_except)
39        self.assertTrue(hit_finally)
40        self.assertTrue(hit_else)
41
42    def test_try_except_finally(self):
43        hit_except = False
44        hit_finally = False
45
46        try:
47            raise Exception, 'yarr!'
48        except:
49            hit_except = True
50        finally:
51            hit_finally = True
52
53        self.assertTrue(hit_except)
54        self.assertTrue(hit_finally)
55
56    def test_try_except_finally_no_exception(self):
57        hit_except = False
58        hit_finally = False
59
60        try:
61            pass
62        except:
63            hit_except = True
64        finally:
65            hit_finally = True
66
67        self.assertFalse(hit_except)
68        self.assertTrue(hit_finally)
69
70    def test_try_except(self):
71        hit_except = False
72
73        try:
74            raise Exception, 'ahoy!'
75        except:
76            hit_except = True
77
78        self.assertTrue(hit_except)
79
80    def test_try_except_no_exception(self):
81        hit_except = False
82
83        try:
84            pass
85        except:
86            hit_except = True
87
88        self.assertFalse(hit_except)
89
90    def test_try_except_else(self):
91        hit_except = False
92        hit_else = False
93
94        try:
95            raise Exception, 'foo!'
96        except:
97            hit_except = True
98        else:
99            hit_else = True
100
101        self.assertFalse(hit_else)
102        self.assertTrue(hit_except)
103
104    def test_try_except_else_no_exception(self):
105        hit_except = False
106        hit_else = False
107
108        try:
109            pass
110        except:
111            hit_except = True
112        else:
113            hit_else = True
114
115        self.assertFalse(hit_except)
116        self.assertTrue(hit_else)
117
118    def test_try_finally_no_exception(self):
119        hit_finally = False
120
121        try:
122            pass
123        finally:
124            hit_finally = True
125
126        self.assertTrue(hit_finally)
127
128    def test_nested(self):
129        hit_finally = False
130        hit_inner_except = False
131        hit_inner_finally = False
132
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
142
143        self.assertTrue(hit_inner_except)
144        self.assertTrue(hit_inner_finally)
145        self.assertTrue(hit_finally)
146
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
153
154        try:
155            try:
156                pass
157            except:
158                hit_inner_except = True
159            else:
160                hit_inner_else = True
161
162            raise Exception, 'outer exception'
163        except:
164            hit_except = True
165        else:
166            hit_else = True
167        finally:
168            hit_finally = True
169
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)
175
176def test_main():
177    run_unittest(ExceptionTestCase)
178
179if __name__ == '__main__':
180    test_main()
181