forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_test_lambda_function.py
More file actions
192 lines (157 loc) · 8.13 KB
/
test_test_lambda_function.py
File metadata and controls
192 lines (157 loc) · 8.13 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
import unittest
import helper
class TestLambdaFunctionStepByStep(unittest.TestCase):
def setUp(self):
self.data = {
"DC_PEC": "",
"DC_SOLUTION": "echo_word = lambda word, echo = 1: word * echo",
"DC_SCT": '''
test_lambda_function(1,
body = lambda: test_student_typed('word'),
results = ["lam('test', 2)"],
errors = ["lam('a', '2')"])
'''
}
def test_fail_1(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 lambda function you defined but hasn't found it.")
def test_fail_2(self):
self.data["DC_CODE"] = "echo_word = lambda wrd: wrd * 1"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "You should define the first lambda function with 2 arguments, instead got 1.")
helper.test_lines(self, sct_payload, 1, 1, 13, 31)
def test_fail_3(self):
self.data["DC_CODE"] = "echo_word = lambda wrd, echo: wrd * echo"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "In your definition of the first lambda function, the first argument should be called <code>word</code>, instead got <code>wrd</code>.")
helper.test_lines(self, sct_payload, 1, 1, 20, 22)
def test_fail_4(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 2: word * echo"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "In your definition of the first lambda function, the argument <code>echo</code> does not have the correct default.")
helper.test_lines(self, sct_payload, 1, 1, 33, 33)
def test_fail_5(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: 2 * echo"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your definition of the first lambda function. Could not find the correct pattern in your code.")
helper.test_lines(self, sct_payload, 1, 1, 36, 43)
def test_fail_6(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * echo + 1"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your definition of the first lambda function. Calling it with arguments <code>('test', 2)</code> should result in <code>testtest</code>, instead got an error.")
helper.test_lines(self, sct_payload, 1, 1, 13, 50)
def test_fail_7(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * echo * 2"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your definition of the first lambda function. Calling it with arguments <code>('test', 2)</code> should result in <code>testtest</code>, instead got <code>testtesttesttest</code>.")
helper.test_lines(self, sct_payload, 1, 1, 13, 50)
def test_fail_8(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * int(echo)"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Check your definition of the first lambda function. Calling it with arguments <code>('a', '2')</code> doesn't result in an error, but it should.")
helper.test_lines(self, sct_payload, 1, 1, 13, 51)
def test_pass(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * echo"
sct_payload = helper.run(self.data)
self.assertTrue(sct_payload['correct'])
def test_fail_8_no_lam(self):
self.data["DC_SCT"] = helper.remove_lambdas(self.data["DC_SCT"])
self.test_fail_8()
def test_pass_no_lam(self):
self.data["DC_SCT"] = helper.remove_lambdas(self.data["DC_SCT"])
self.test_pass()
class TestLambdaFunctionStepByStepCustom(unittest.TestCase):
def setUp(self):
self.data = {
"DC_PEC": "",
"DC_SOLUTION": "echo_word = lambda word, echo = 1: word * echo",
"DC_SCT": '''
test_lambda_function(1,
body = lambda: test_student_typed('word'),
results = ["lam('test', 2)"],
errors = ["lam('a', '2')"],
not_called_msg='notcalled',
nb_args_msg='nbargs',
arg_names_msg='argnames',
arg_defaults_msg='argdefaults',
wrong_result_msg='wrongresult',
no_error_msg='noerror',
expand_message=False)
'''
}
def test_fail_1(self):
self.data["DC_CODE"] = ""
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "notcalled")
def test_fail_2(self):
self.data["DC_CODE"] = "echo_word = lambda wrd: wrd * 1"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "nbargs")
def test_fail_3(self):
self.data["DC_CODE"] = "echo_word = lambda wrd, echo: wrd * echo"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "argnames")
def test_fail_4(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 2: word * echo"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "argdefaults")
def test_fail_5(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: 2 * echo"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "Could not find the correct pattern in your code.")
def test_fail_6(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * echo + 1"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "wrongresult")
def test_fail_7(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * echo * 2"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "wrongresult")
def test_fail_8(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * int(echo)"
sct_payload = helper.run(self.data)
self.assertFalse(sct_payload['correct'])
self.assertEqual(sct_payload['message'], "noerror")
def test_pass(self):
self.data["DC_CODE"] = "echo_word = lambda word, echo = 1: word * echo"
sct_payload = helper.run(self.data)
self.assertTrue(sct_payload['correct'])
def test_fail_8_no_lam(self):
self.data["DC_SCT"] = helper.remove_lambdas(self.data["DC_SCT"])
self.test_fail_8()
def test_pass_no_lam(self):
self.data["DC_SCT"] = helper.remove_lambdas(self.data["DC_SCT"])
self.test_pass()
class TestLambdaFunctionDifferentform(unittest.TestCase):
def test_pass(self):
self.data = {
"DC_PEC": "",
"DC_SOLUTION": "echo_word = lambda word, echo = 1: word * echo",
"DC_CODE": "(lambda word, echo = 1: word * echo)('test', 3)",
"DC_SCT": '''
test_lambda_function(1,
body = lambda: test_student_typed('word'),
results = ["lam('test', 2)"],
errors = ["lam('a', '2')"])
'''
}
sct_payload = helper.run(self.data)
self.assertTrue(sct_payload['correct'])
if __name__ == "__main__":
unittest.main()