Skip to content

Commit a4ad134

Browse files
author
Filip Schouwenaars
authored
Merge pull request #163 from datacamp/spec-unit-tests
Spec unit tests
2 parents 2ed1b6f + a0caf03 commit a4ad134

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

tests/test_test_function_definition.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ def my_fun(x, y = 4, z = ('a', 'b'), *args, **kwargs):
743743
self.SCT_CHECK_ONE = "Ex().check_function_def('my_fun').check_args(1)"
744744
self.SCT_CHECK_Y = "Ex().check_function_def('my_fun').check_args('y')"
745745
self.SCT_CHECK_X = "Ex().check_function_def('my_fun').check_args('x')"
746+
self.SCT_CHECK_ARGS = "Ex().check_function_def('my_fun').check_args('*args')"
747+
self.SCT_CHECK_KWARGS = "Ex().check_function_def('my_fun').check_args('**kwargs')"
746748

747749
def when_code_is_sol(self):
748750
self.data['DC_CODE'] = self.data['DC_SOLUTION']
@@ -782,6 +784,30 @@ def test_fail_kw_not_default(self):
782784
self.data['DC_SCT'] = self.SCT_CHECK_X + ".is_default()"
783785
self.assertFalse(self.when_replace('x, y = 4', 'x = 2, y = 4'))
784786

787+
def test_fail_star_args_undef(self):
788+
self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), args=2, **kwargs): pass"""
789+
self.data['DC_SCT'] = self.SCT_CHECK_ARGS
790+
sct_payload = helper.run(self.data)
791+
self.assertFalse(sct_payload['correct'])
792+
793+
def test_fail_star_args_name(self):
794+
self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), *wrongargsname, **kwargs): pass"""
795+
self.data['DC_SCT'] = self.SCT_CHECK_ARGS + '.has_equal_name()'
796+
sct_payload = helper.run(self.data)
797+
self.assertFalse(sct_payload['correct'])
798+
799+
def test_fail_kwargs_undef(self):
800+
self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), args=2, kwargs=2): pass"""
801+
self.data['DC_SCT'] = self.SCT_CHECK_KWARGS
802+
sct_payload = helper.run(self.data)
803+
self.assertFalse(sct_payload['correct'])
804+
805+
def test_fail_kwargs_name(self):
806+
self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), *args, **wrongkwargsname): pass"""
807+
self.data['DC_SCT'] = self.SCT_CHECK_KWARGS + '.has_equal_name()'
808+
sct_payload = helper.run(self.data)
809+
self.assertFalse(sct_payload['correct'])
810+
785811
def test_pass_equal_value(self):
786812
self.data['DC_SCT'] = self.SCT_CHECK_Y + ".has_equal_value('unequal values')"
787813
self.assertTrue(self.when_code_is_sol())
@@ -811,11 +837,12 @@ def test_fail_multi(self):
811837
self.data['DC_SCT'] = self.MULTI_SCT
812838
self.assertFalse(self.when_replace('x', 'x2'))
813839

840+
814841
class TestLambdaFunctionSpec2(TestFunctionSpec2):
815842
def setUp(self):
816843
super().setUp()
817844
self.data['DC_SOLUTION'] = "lambda x, y = 4, z = ('a', 'b'), *args, **kwargs: [x, y, *z, *args]"
818-
for attr in ['MULTI_SCT', 'SCT_CHECK', 'SCT_KW', 'SCT_POS', 'SCT_CHECK_ONE', 'SCT_CHECK_Y', 'SCT_CHECK_X']:
845+
for attr in ['MULTI_SCT', 'SCT_CHECK', 'SCT_KW', 'SCT_POS', 'SCT_CHECK_ONE', 'SCT_CHECK_Y', 'SCT_CHECK_X', 'SCT_CHECK_ARGS', 'SCT_CHECK_KWARGS']:
819846
lam_sct = getattr(self, attr).replace("check_function_def('my_fun')", 'check_lambda_function(0)')
820847
setattr(self, attr, lam_sct)
821848

tests/test_test_wiki.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,67 @@ def test_for_body():
186186
sct_payload = helper.run(self.data)
187187
self.assertTrue(sct_payload['correct'])
188188

189+
class TestPartChecks(unittest.TestCase):
190+
def test_pass_simple(self):
191+
self.data = {
192+
"DC_SOLUTION": '''
193+
L2 = [i*2 for i in range(0,10) if i>2]
194+
''',
195+
"DC_SCT": '''
196+
list_comp = Ex().check_list_comp(0, missing_msg="Did you include a list comprehension?")
197+
list_comp.check_body().test_student_typed('i\*2')
198+
list_comp.check_iter().has_equal_value()
199+
list_comp.check_ifs(0).multi([has_equal_value(context_vals=[i]) for i in range(0,10)])
200+
'''
201+
}
202+
self.data["DC_CODE"] = "L2 = [i*2 for i in range(10) if i>2]"
203+
sct_payload = helper.run(self.data)
204+
self.assertTrue(sct_payload['correct'])
205+
206+
def test_pass_complex1(self):
207+
self.data = {
208+
"DC_SOLUTION": """L3 = [i*2 if i> 5 else 0 for i in range(0,10)]""",
209+
"DC_SCT": """
210+
(Ex().check_list_comp(0) # first comptehension
211+
.check_body() # comp's body
212+
.set_context(i=6)
213+
.check_if_exp(0) # body's inline if
214+
.has_equal_value()
215+
)
216+
"""
217+
}
218+
self.data['DC_CODE'] = self.data['DC_SOLUTION']
219+
sct_payload = helper.run(self.data)
220+
self.assertTrue(sct_payload['correct'])
221+
222+
def test_fail_complex1(self):
223+
self.data = {
224+
"DC_SOLUTION": """L3 = [i*2 if i> 5 else 0 for i in range(0,10)]""",
225+
"DC_SCT": """
226+
(Ex().check_list_comp(0) # first comptehension
227+
.check_body() # comp's body
228+
.set_context(i=6)
229+
.check_if_exp(0) # body's inline if
230+
.has_equal_value()
231+
)
232+
"""
233+
}
234+
self.data['DC_CODE'] = """L3 = [i*2 for i in range(0,10)]""" # no inline if
235+
sct_payload = helper.run(self.data)
236+
self.assertFalse(sct_payload['correct'])
189237

238+
def test_pass_complex2(self):
239+
self.data = {
240+
"DC_SOLUTION": """L3 = [i*2 if i> 5 else 0 for i in range(0,10)]""",
241+
"DC_SCT": """
242+
(Ex().check_list_comp(0) # first comptehension
243+
.check_body().set_context(i=6).has_equal_value()
244+
)
245+
"""
246+
}
247+
self.data['DC_CODE'] = self.data['DC_SOLUTION']
248+
sct_payload = helper.run(self.data)
249+
self.assertTrue(sct_payload['correct'])
190250

191251
if __name__ == "__main__":
192252
unittest.main()

0 commit comments

Comments
 (0)