def test_check_call_lambda(stu, passes): s = setup_state(stu, "lambda a, b: a + b") with helper.verify_sct(passes): s.check_lambda_function().multi( check_call("f(1,2)").has_equal_value(), check_call("f(1,2)").has_equal_output(), )
def test_test_custom_equality_func(tol, passes): s = setup_state("a = [1.011]", "a = [1.01]") import numpy as np with helper.verify_sct(passes): s.check_object("a").has_equal_value( func=lambda x, y: np.allclose(x, y, atol=tol))
def test_two_for_loops(stu, passes): s = setup_state(stu, "for i in range(1):\n pass\nfor j in range(4): print(j)") with helper.verify_sct(passes): s.check_for_loop(index=1).multi( check_iter().has_equal_value(), check_body().set_context(2).has_equal_output(), )
def test_check_function_def_basic(stu, passes): s = setup_state(stu, "def test(x): print(x)") with helper.verify_sct(passes): s.check_function_def("test").multi( check_args(0).has_equal_part("name", msg="wrong"), check_body().set_context(1).check_function("print").check_args( 0).has_equal_value(), )
def test_check_class_def_pass(stu, passes): sol = "class A(str):\n def __init__(self): pass" s = setup_state(stu, sol) with helper.verify_sct(passes): s.check_class_def("A").multi( check_bases(0).has_equal_ast(), check_body().check_function_def( "__init__").check_body().has_equal_ast(), )
def test_urlopen_in_process(sol_code, stu_code): with in_temp_dir(): chain = setup_state("", "", pec="") chain._state.solution_code = sol_code chain._state.student_code = stu_code with verify_sct(True): chain.run()
def test_running_code_isolation_run(sol_code, stu_code): # test that setup_state is isolated chain = setup_state(sol_code, stu_code, pec="") chain._state.solution_code = sol_code chain._state.student_code = stu_code with verify_sct(False): # test that run is isolated chain.run().has_equal_value(name="bar", override="bar")
def test_check_call(stu, passes): s = setup_state(stu, "def test(a, b): print(a + b); return a + b") with helper.verify_sct(passes): s.check_function_def("test").multi( check_call("f(1,2)").has_equal_value(), check_call("f(1,2)").has_equal_output(), check_call("f(3,1)").has_equal_value(), check_call("f(1, '2')").has_equal_error(), )
def test_check_function_def_args(stu, passes): s = setup_state(stu, "def f(a, b = 3): pass") with helper.verify_sct(passes): s.check_function_def("f").multi( check_args(0).has_equal_part("name", msg="wrong").has_equal_part( "is_default", msg="wrong"), check_args(1).has_equal_part("name", msg="wrong").has_equal_part( "is_default", msg="wrong").has_equal_value(), )
def test_for_loop_nested(stu, passes): s = setup_state( stu, "for i in range(3):\n for j in range(4):\n print(i + j)") with helper.verify_sct(passes): s.check_for_loop().multi( check_iter().has_equal_value(), check_body().set_context(2).check_for_loop().multi( check_iter(), check_body().set_context(3).has_equal_output()), )
def test_urlretrieve_in_process(sol_code, stu_code): # on mac an env var needs to be set: # OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES with in_temp_dir(): chain = setup_state("", "", pec="") chain._state.solution_code = sol_code chain._state.student_code = stu_code with verify_sct(True): chain.run()
def test_check_lambda_full_ast_based(stu, passes): s = setup_state(stu, "lambda x, y=2: print(x + y)") with helper.verify_sct(passes): s.check_lambda_function(0).multi( has_equal_part_len("args", unequal_msg="wrong"), check_args(0).has_equal_part("name", msg="wrong").has_equal_part( "is_default", msg="wrong"), check_args(1).has_equal_part("name", msg="wrong").has_equal_part( "is_default", msg="wrong").has_equal_value(), check_body().set_context(1, 2).has_equal_output(), )
def test_file_existence_syntax(temp_py_file): """test integration of protowhat checks in pythonwhat""" expected_content = cf.get_file_content(temp_py_file.name) chain = setup_state("", "", pec="") file_chain = chain.check_file(temp_py_file.name) assert expected_content in file_chain._state.student_code with helper.verify_sct(True): file_chain = chain >> F(attr_scts={ "check_file": cf.check_file }).check_file(temp_py_file.name) assert expected_content in file_chain._state.student_code
def test_has_equal_value_pickle(): """limit deep copy of env when just reading value""" sol = """a = 1 class NoPickle(list): def __getstate__(self): raise TypeError("Can't pickle this") b = NoPickle() """ s = setup_state(sol, sol) with helper.verify_sct(True): s.check_object("a").has_equal_value() with helper.verify_sct(True): s.check_object("a").has_equal_value(name="a") with helper.verify_sct(True): s.has_equal_value(expr_code="a") with pytest.raises(InstructorError): s.has_equal_value(expr_code="a", name="a") with pytest.raises(InstructorError): s.has_equal_value(expr_code="print(a)") with pytest.raises(InstructorError): s.has_equal_value(expr_code="print(a)", name="a") with pytest.raises(InstructorError): s.has_equal_value(expr_code="print(a)")
def test_check_list_comp_basic(stu, passes): pec = "x = {'a': 2, 'b':3, 'c':4, 'd':'test'}" sol = "[key + str(val) for key,val in x.items() if isinstance(key, str) if isinstance(val, int)]" s = setup_state(stu, sol, pec) with helper.verify_sct(passes): s.check_list_comp().multi( check_iter().has_equal_value(), check_ifs(0).check_function("isinstance").check_args( "obj").has_equal_ast(), check_ifs(1).check_function("isinstance").check_args( "obj").has_equal_ast(), check_body().has_context(exact_names=True).set_context( "a", 2).has_equal_value(), )
def test_basic_pattern(stu, passes): s = setup_state(stu, "", pec="a,c=0,0") with helper.verify_sct(passes): s.has_code("a|b", pattern=False)
def test_has_equal_value_name(stu, passes): s = setup_state(stu, "a = 0\nfor i in range(0): a = 1") with helper.verify_sct(passes): s.check_for_loop().check_body().has_equal_value(name="a")
def test_test_function_v2_do_eval(stu, do_eval, passes): s = setup_state(stu, "round(a)", pec="a,b = 1,2") with helper.verify_sct(passes): s.test_function_v2("round", params=["number"], do_eval=[do_eval])
def test_test_function_v2_print(stu, passes): s = setup_state(stu, "print(1)") with helper.verify_sct(passes): s.test_function_v2("print", params=["value"])
def test_test_output_contains(stu, passes): s = setup_state(stu, "") with helper.verify_sct(passes): s.test_output_contains(r"[H|h]i,*\s+there!")
def test_has_output_pattern(stu, passes): s = setup_state(stu, "") with helper.verify_sct(passes): s.has_output("Hi, there!", pattern=False)
def test_has_output_basic(stu, passes): s = setup_state(stu, "") with helper.verify_sct(passes): s.has_output(r"[H|h]i,*\s+there!")
def test_basic_has_printout(stu, correct): sol = "print(1, 2, 3)" s = setup_state(stu_code=stu, sol_code=sol) with helper.verify_sct(correct): s.has_printout(0)
def test_set_env_full_example(stu, passes): s = setup_state(stu, "print(a_list[1])", pec="a_list = [0, 1, 2]") with helper.verify_sct(passes): s.set_env(a_list=list(range(10))).has_equal_output()
def test_has_equal_output_basic(stu, passes): s = setup_state(stu, 'x = {"a":1, "b":2, "c": 3}') with helper.verify_sct(passes): s.test_expression_output(expr_code='print(x["a"])')
def test_has_equal_output_for(stu, passes, context_vals): s = setup_state(stu, "for i in range(10):\n print(i)") with helper.verify_sct(passes): s.check_for_loop().check_body().has_equal_output( context_vals=context_vals)
def test_has_equal_value_basic(stu, passes): s = setup_state(stu, "a = 2") with helper.verify_sct(passes): s.has_equal_value(expr_code="a")
def test_basic(stu, passes): s = setup_state(stu, "", pec="c,a=0,0") with helper.verify_sct(passes): s.has_code("a|b")
def test_copy_functionality(copy, passes): s = setup_state("a = [1]", "a = [2]") with helper.verify_sct(passes): s.has_equal_value(expr_code="a[0] = 3", name="a", copy=copy).has_equal_value(expr_code="a", name="a")
def test_set_env_fail(state): with helper.verify_sct(False): state.multi(set_env(x=4), set_env(y=5).has_equal_value(name="x"))