forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_test_try_except.py
More file actions
282 lines (255 loc) · 9.01 KB
/
test_test_try_except.py
File metadata and controls
282 lines (255 loc) · 9.01 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import unittest
import helper
class TestTryExceptStepByStep(unittest.TestCase):
def setUp(self):
self.data = {
"DC_PEC": "import numpy as np",
"DC_SOLUTION": '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
except :
x = 'someerror'
else :
passed = True
finally:
print('done')
''',
"DC_SCT": '''
import collections
handlers = collections.OrderedDict()
handlers['TypeError'] = lambda: test_object_after_expression('x')
handlers['ValueError'] = lambda: test_object_after_expression('x')
handlers['ZeroDivisionError'] = lambda: test_object_after_expression('x', context_vals = ['anerror'])
handlers['IOError'] = lambda: test_object_after_expression('x', context_vals = ['anerror'])
handlers['all'] = lambda: test_object_after_expression('x')
test_try_except(index = 1,
body = lambda: test_function("max"),
handlers = handlers,
orelse = lambda: test_object_after_expression('passed'),
finalbody = lambda: test_function('print'))
'''
}
self.SCT_NO_LAM = helper.remove_lambdas(self.data['DC_SCT'])
def test_fail_01(self):
self.data["DC_CODE"] = ""
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "The system wants to check the first try-except block you defined but hasn't found it.")
def test_fail_02(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerrors'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the <code>TypeError</code> <code>except</code> block of the first try-except block. Are you sure you assigned the correct value to <code>x</code>?")
def test_fail_03(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Have you included a <code>ValueError</code> <code>except</code> block in your first try-except block?")
def test_fail_04(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerrors'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the <code>ValueError</code> <code>except</code> block of the first try-except block. Are you sure you assigned the correct value to <code>x</code>?")
def test_fail_05(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Have you included a <code>ZeroDivisionError</code> <code>except</code> block in your first try-except block?")
def test_fail_06(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except ZeroDivisionError as e:
x = 'test'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the <code>ZeroDivisionError</code> <code>except</code> block of the first try-except block. Are you sure you assigned the correct value to <code>x</code>?")
def test_fail_07(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except ZeroDivisionError as e:
x = e
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Have you included a <code>IOError</code> <code>except</code> block in your first try-except block?")
def test_fail_08(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = 'test'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the <code>ZeroDivisionError</code> <code>except</code> block of the first try-except block. Are you sure you assigned the correct value to <code>x</code>?")
def test_fail_09(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Have you included a general <code>except</code> block in your first try-except block?")
def test_fail_10(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
except :
x = 'someerrors'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the general <code>except</code> block of the first try-except block. Are you sure you assigned the correct value to <code>x</code>?")
def test_fail_11(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
except :
x = 'someerror'
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Have you included a <code>else</code> part in your first try-except block?")
def test_fail_12(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
except :
x = 'someerror'
else :
passed = False
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the <code>else</code> part of the first try-except block. Are you sure you assigned the correct value to <code>passed</code>?")
def test_fail_13(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
except :
x = 'someerror'
else :
passed = True
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Have you included a <code>finally</code> part in your first try-except block?")
def test_fail_14(self):
self.data["DC_CODE"] = '''
try:
x = max([1, 2, 'a'])
except TypeError as e:
x = 'typeerror'
except ValueError:
x = 'valueerror'
except (ZeroDivisionError, IOError) as e:
x = e
except :
x = 'someerror'
else :
passed = True
finally:
print('donessss')
'''
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your code in the <code>finally</code> part of the first try-except block. Did you call <code>print()</code> with the correct arguments? The first argument seems to be incorrect.")
def test_pass(self):
self.data["DC_CODE"] = self.data["DC_SOLUTION"]
sct_payload = helper.run(self.data)
self.assertTrue(sct_payload['correct'])
def test_fail_01_no_lam(self):
self.data["DC_SCT"] = self.SCT_NO_LAM
self.test_fail_01()
def test_fail_04_no_lam(self):
self.data["DC_SCT"] = self.SCT_NO_LAM
self.test_fail_04()
def test_fail_14_no_lam(self):
self.data["DC_SCT"] = self.SCT_NO_LAM
self.test_fail_14()
def test_pass_no_lam(self):
self.data["DC_SCT"] = self.SCT_NO_LAM
self.test_pass()
def test_fail_01_exchain(self):
self.data["DC_SCT"] = self.SCT_NO_LAM.replace('test_try_except', 'Ex().test_try_except')
self.test_fail_01()
def test_pass_exchain(self):
self.data["DC_SCT"] = self.SCT_NO_LAM.replace('test_try_except', 'Ex().test_try_except')
self.test_pass()
if __name__ == "__main__":
unittest.main()