@@ -34,6 +34,13 @@ def test_Pass(self):
3434 self .assertTrue (sct_payload ['correct' ])
3535 self .assertEqual (sct_payload ['message' ], "Great!" )
3636
37+ def test_Pass_spec2 (self ):
38+ self .data ['DC_SCT' ] = """
39+ Ex().check_function('print', 0).check_args(0).has_equal_ast()
40+ """
41+ sct_payload = helper .run (self .data )
42+ self .assertTrue (sct_payload ['correct' ])
43+
3744class TestFunctionExerciseNumpy (unittest .TestCase ):
3845
3946 def setUp (self ):
@@ -599,9 +606,7 @@ def setUp(self):
599606test_function("print", index = 3, highlight=True)
600607 '''
601608 }
602- self .DC_SCT_SPEC2 = '''
603- Ex().check_function("print", 0).check_arg(0).has_equal_value()
604- '''
609+
605610 def test_multiple_1 (self ):
606611 self .data ["DC_CODE" ] = 'print("abc")'
607612 sct_payload = helper .run (self .data )
@@ -659,6 +664,110 @@ def test_nohighlight_too_few_calls(self):
659664 self .assertFalse (sct_payload ['correct' ])
660665 self .assertEqual (sct_payload .get ('line_start' ), None )
661666
667+ class TestCheckFunction (unittest .TestCase ):
668+ def setUp (self ):
669+ self .data = {
670+ "DC_PEC" : "import numpy as np" ,
671+ "DC_CODE" : "np.array([1,2,3])" ,
672+ "DC_SOLUTION" : "np.array([1,2,3])" ,
673+ "DC_SCT" : "Ex().check_function('numpy.array', 0)"
674+ }
675+
676+ def run_append (self , sct ):
677+ self .data ["DC_SCT" ] += sct
678+ return helper .run (self .data )
679+
680+ def run_pass (self , sct ):
681+ sct_payload = self .run_append (sct )
682+ print (sct_payload )
683+ self .assertTrue (sct_payload ['correct' ])
684+ return sct_payload
685+
686+ def run_fail (self , sct ):
687+ self .assertFalse (self .run_append (sct )['correct' ])
688+
689+ def test_pass_np_call_exists (self ):
690+ sct_payload = helper .run (self .data )
691+ self .assertTrue (sct_payload ['correct' ])
692+
693+ def test_pass_test_student_typed (self ):
694+ self .run_pass (".test_student_typed(r'np\.array\(\[1,2,3\]\)')" )
695+
696+ def test_fail_test_student_typed (self ):
697+ self .data ["DC_CODE" ] = "np.array([1,2])"
698+ self .run_fail (".test_student_typed(r'np\.array\(\[1,2,3\]\)')" )
699+
700+ def test_pass_func_has_equal_ast (self ):
701+ self .run_pass (".has_equal_ast()" )
702+
703+ def test_fail_func_has_equal_ast (self ):
704+ self .data ["DC_CODE" ] = "np.array([1,2])"
705+ self .run_fail (".has_equal_ast()" )
706+
707+ def test_pass_check_args_pos_0 (self ):
708+ self .run_pass (".check_args(0)" )
709+
710+ def test_fail_check_args_pos_0 (self ):
711+ self .data ["DC_CODE" ] = "np.array()"
712+ self .run_fail (".check_args(0)" )
713+
714+ def test_pass_pos_0_test_student_typed (self ):
715+ self .run_pass (".check_args(0).test_student_typed(r'\[1,2,3\]')" )
716+
717+ def test_fail_pos_0_test_student_typed (self ):
718+ self .data ["DC_CODE" ] = "np.array([1,2])"
719+ self .run_fail (".check_args(0).test_student_typed(r'\[1,2,3\]')" )
720+
721+ def test_pass_pos_0_has_equal_ast (self ):
722+ self .run_pass (".check_args(0).has_equal_ast()" )
723+
724+ def test_fail_pos_0_has_equal_ast (self ):
725+ self .data ["DC_CODE" ] = "np.array([1,2])"
726+ self .run_fail (".check_args(0).has_equal_ast()" )
727+
728+ def test_pass_pos_0_has_equal_value (self ):
729+ self .run_pass (".check_args(0).has_equal_value()" )
730+
731+ def test_fail_pos_0_has_equal_value (self ):
732+ self .data ["DC_CODE" ] = "np.array([1,2])"
733+ self .run_fail (".check_args(0).has_equal_value()" )
734+
735+ def test_pass_pos_0_inline_if_body (self ):
736+ self .data ["DC_CODE" ] = "np.array([1,2,3] if True else [1])"
737+ self .data ["DC_SOLUTION" ] = "np.array([1,2,3] if False else [1])"
738+ self .run_pass (".check_args(0).check_if_exp(0).check_body().has_equal_ast()" )
739+
740+ def test_fail_pos_0_inline_if_body (self ):
741+ self .data ["DC_CODE" ] = "np.array([1,2,3] if True else [1])"
742+ self .data ["DC_SOLUTION" ] = "np.array([1,2] if False else [1])"
743+ self .run_fail (".check_args(0).check_if_exp(0).check_body().has_equal_ast()" )
744+
745+ class TestCheckFunctionCases (unittest .TestCase ):
746+ def setup_color (self ):
747+ self .data = {
748+ 'DC_PEC' : "def f(*args, **kwargs): pass" ,
749+ 'DC_CODE' : "f(color = 'blue')"
750+ }
751+ self .data ["DC_SOLUTION" ] = self .data ["DC_CODE" ]
752+
753+ def test_pass_sig_false (self ):
754+ self .setup_color ()
755+ self .data ['DC_SCT' ] = "Ex().check_function('f', 0, signature=False).check_args('color').has_equal_ast()"
756+
757+ sct_payload = helper .run (self .data )
758+ self .assertTrue (sct_payload ['correct' ])
759+
760+ @unittest .skip ("TODO: implement override" )
761+ def test_pass_sig_false_override (self ):
762+ self .setup_color ()
763+ self .data ["DC_SCT" ].replace ('color' , 'c' )
764+ self .data ['DC_SCT' ] = """
765+ Ex().check_function('f', 0, signature=False).override("f(c = 'blue')").check_args('c').has_equal_ast()
766+ """
767+
768+ sct_payload = helper .run (self .data )
769+ self .assertTrue (sct_payload ['correct' ])
770+
662771
663772class TestFunctionComplexArgs (unittest .TestCase ):
664773 def setUp (self ):
@@ -702,7 +811,5 @@ def test_fail_undillable_args(self):
702811 self .assertFalse (sct_payload ['correct' ])
703812
704813
705-
706-
707814if __name__ == "__main__" :
708815 unittest .main ()
0 commit comments