diff --git a/tests/test_test_function_definition.py b/tests/test_test_function_definition.py index 19ff99ac..4dd46378 100644 --- a/tests/test_test_function_definition.py +++ b/tests/test_test_function_definition.py @@ -743,6 +743,8 @@ def my_fun(x, y = 4, z = ('a', 'b'), *args, **kwargs): self.SCT_CHECK_ONE = "Ex().check_function_def('my_fun').check_args(1)" self.SCT_CHECK_Y = "Ex().check_function_def('my_fun').check_args('y')" self.SCT_CHECK_X = "Ex().check_function_def('my_fun').check_args('x')" + self.SCT_CHECK_ARGS = "Ex().check_function_def('my_fun').check_args('*args')" + self.SCT_CHECK_KWARGS = "Ex().check_function_def('my_fun').check_args('**kwargs')" def when_code_is_sol(self): self.data['DC_CODE'] = self.data['DC_SOLUTION'] @@ -782,6 +784,30 @@ def test_fail_kw_not_default(self): self.data['DC_SCT'] = self.SCT_CHECK_X + ".is_default()" self.assertFalse(self.when_replace('x, y = 4', 'x = 2, y = 4')) + def test_fail_star_args_undef(self): + self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), args=2, **kwargs): pass""" + self.data['DC_SCT'] = self.SCT_CHECK_ARGS + sct_payload = helper.run(self.data) + self.assertFalse(sct_payload['correct']) + + def test_fail_star_args_name(self): + self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), *wrongargsname, **kwargs): pass""" + self.data['DC_SCT'] = self.SCT_CHECK_ARGS + '.has_equal_name()' + sct_payload = helper.run(self.data) + self.assertFalse(sct_payload['correct']) + + def test_fail_kwargs_undef(self): + self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), args=2, kwargs=2): pass""" + self.data['DC_SCT'] = self.SCT_CHECK_KWARGS + sct_payload = helper.run(self.data) + self.assertFalse(sct_payload['correct']) + + def test_fail_kwargs_name(self): + self.data['DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), *args, **wrongkwargsname): pass""" + self.data['DC_SCT'] = self.SCT_CHECK_KWARGS + '.has_equal_name()' + sct_payload = helper.run(self.data) + self.assertFalse(sct_payload['correct']) + def test_pass_equal_value(self): self.data['DC_SCT'] = self.SCT_CHECK_Y + ".has_equal_value('unequal values')" self.assertTrue(self.when_code_is_sol()) @@ -811,11 +837,12 @@ def test_fail_multi(self): self.data['DC_SCT'] = self.MULTI_SCT self.assertFalse(self.when_replace('x', 'x2')) + class TestLambdaFunctionSpec2(TestFunctionSpec2): def setUp(self): super().setUp() self.data['DC_SOLUTION'] = "lambda x, y = 4, z = ('a', 'b'), *args, **kwargs: [x, y, *z, *args]" - for attr in ['MULTI_SCT', 'SCT_CHECK', 'SCT_KW', 'SCT_POS', 'SCT_CHECK_ONE', 'SCT_CHECK_Y', 'SCT_CHECK_X']: + 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']: lam_sct = getattr(self, attr).replace("check_function_def('my_fun')", 'check_lambda_function(0)') setattr(self, attr, lam_sct) diff --git a/tests/test_test_wiki.py b/tests/test_test_wiki.py index 5b28ad3a..6f3faf3b 100644 --- a/tests/test_test_wiki.py +++ b/tests/test_test_wiki.py @@ -186,7 +186,67 @@ def test_for_body(): sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct']) +class TestPartChecks(unittest.TestCase): + def test_pass_simple(self): + self.data = { + "DC_SOLUTION": ''' +L2 = [i*2 for i in range(0,10) if i>2] +''', + "DC_SCT": ''' +list_comp = Ex().check_list_comp(0, missing_msg="Did you include a list comprehension?") +list_comp.check_body().test_student_typed('i\*2') +list_comp.check_iter().has_equal_value() +list_comp.check_ifs(0).multi([has_equal_value(context_vals=[i]) for i in range(0,10)]) +''' + } + self.data["DC_CODE"] = "L2 = [i*2 for i in range(10) if i>2]" + sct_payload = helper.run(self.data) + self.assertTrue(sct_payload['correct']) + + def test_pass_complex1(self): + self.data = { + "DC_SOLUTION": """L3 = [i*2 if i> 5 else 0 for i in range(0,10)]""", + "DC_SCT": """ +(Ex().check_list_comp(0) # first comptehension + .check_body() # comp's body + .set_context(i=6) + .check_if_exp(0) # body's inline if + .has_equal_value() + ) +""" + } + self.data['DC_CODE'] = self.data['DC_SOLUTION'] + sct_payload = helper.run(self.data) + self.assertTrue(sct_payload['correct']) + + def test_fail_complex1(self): + self.data = { + "DC_SOLUTION": """L3 = [i*2 if i> 5 else 0 for i in range(0,10)]""", + "DC_SCT": """ +(Ex().check_list_comp(0) # first comptehension + .check_body() # comp's body + .set_context(i=6) + .check_if_exp(0) # body's inline if + .has_equal_value() + ) +""" + } + self.data['DC_CODE'] = """L3 = [i*2 for i in range(0,10)]""" # no inline if + sct_payload = helper.run(self.data) + self.assertFalse(sct_payload['correct']) + def test_pass_complex2(self): + self.data = { + "DC_SOLUTION": """L3 = [i*2 if i> 5 else 0 for i in range(0,10)]""", + "DC_SCT": """ +(Ex().check_list_comp(0) # first comptehension + .check_body().set_context(i=6).has_equal_value() + ) +""" + } + self.data['DC_CODE'] = self.data['DC_SOLUTION'] + sct_payload = helper.run(self.data) + self.assertTrue(sct_payload['correct']) if __name__ == "__main__": unittest.main()