예제 #1
0
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(),
        )
예제 #2
0
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))
예제 #3
0
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(),
        )
예제 #4
0
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(),
        )
예제 #6
0
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()
예제 #7
0
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")
예제 #8
0
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(),
        )
예제 #9
0
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()),
        )
예제 #11
0
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()
예제 #12
0
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(),
        )
예제 #13
0
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
예제 #14
0
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)")
예제 #15
0
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(),
        )
예제 #16
0
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)
예제 #17
0
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")
예제 #18
0
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])
예제 #19
0
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"])
예제 #20
0
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!")
예제 #21
0
def test_has_output_pattern(stu, passes):
    s = setup_state(stu, "")
    with helper.verify_sct(passes):
        s.has_output("Hi, there!", pattern=False)
예제 #22
0
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!")
예제 #23
0
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)
예제 #24
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()
예제 #25
0
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"])')
예제 #26
0
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)
예제 #27
0
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")
예제 #28
0
def test_basic(stu, passes):
    s = setup_state(stu, "", pec="c,a=0,0")
    with helper.verify_sct(passes):
        s.has_code("a|b")
예제 #29
0
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")
예제 #30
0
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"))