Пример #1
0
def test_running_file(temp_py_file):
    content = cf.get_file_content(temp_py_file.name)
    chain = setup_state("", "", pec="")

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(
                temp_py_file.name,
                solution_code=content).run().has_equal_value(expr_code="a")

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(temp_py_file.name,
                             solution_code=content).run().check_object(
                                 "a").has_equal_value()

    with pytest.raises(TF):
        with tempfile.TemporaryDirectory() as d:
            with ChDir(d):
                chain.check_file(temp_py_file.name,
                                 solution_code=content.replace(
                                     "1",
                                     "2")).run().has_equal_value(expr_code="a")

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(temp_py_file.name).run().has_equal_value(
                expr_code="a", override=1)

    with pytest.raises(TF):
        with tempfile.TemporaryDirectory() as d:
            with ChDir(d):
                chain.check_file(temp_py_file.name).run().has_equal_value(
                    expr_code="a", override=2)
Пример #2
0
def test_running_file_with_root_check(temp_py_file):
    content = cf.get_file_content(temp_py_file.name)
    chain = setup_state("", "", pec="")

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(temp_py_file.name,
                             solution_code=content).run().check_object(
                                 "a").has_equal_value()

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(temp_py_file.name,
                             solution_code=content).run().has_no_error()

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(temp_py_file.name,
                             solution_code=content).run().has_printout(0)

    with pytest.raises(TF):
        with tempfile.TemporaryDirectory() as d:
            with ChDir(d):
                chain.check_file(
                    temp_py_file.name,
                    solution_code="print('Bye!')").run().has_printout(0)

    with tempfile.TemporaryDirectory() as d:
        with ChDir(d):
            chain.check_file(temp_py_file.name,
                             solution_code=content).run().has_output("Hi")
Пример #3
0
def test_file_parsing(temp_py_file):
    expected_content = cf.get_file_content(temp_py_file.name)
    chain = setup_state("", "", pec="")

    file_chain = chain.check_file(temp_py_file.name,
                                  solution_code=expected_content)
    file_chain.check_if_else().check_test().has_equal_value()
    file_chain.check_if_else().check_test().has_equal_value(expr_code="False")
Пример #4
0
def test_file_content(temp_file):
    expected_content = cf.get_file_content(temp_file.name)
    chain = setup_state("", "", pec="")

    chain.check_file(temp_file.name,
                     parse=False).has_code(expected_content.split("\n")[0])
    chain.check_file(temp_file.name,
                     parse=False,
                     solution_code=expected_content).has_code(
                         expected_content.split("\n")[0])
Пример #5
0
def test_load_file(state, temp_file):
    expected_content = cf.get_file_content(temp_file.name)
    assert expected_content == cf.load_file(temp_file.name)

    filename = os.path.basename(os.path.normpath(temp_file.name))
    common_path = os.path.dirname(temp_file.name)

    load_file = partial(cf.load_file, prefix=common_path)
    assert expected_content == load_file(filename)

    assert expected_content == cf.load_file(filename, prefix=common_path)
    assert expected_content == cf.load_file(filename, prefix=common_path + "/")
Пример #6
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
Пример #7
0
def test_python_file_existence(temp_py_file):
    expected_content = cf.get_file_content(temp_py_file.name)
    chain = setup_state("", "", pec="")

    # test with just student content
    child = cf.check_file(chain._state, temp_py_file.name)
    assert expected_content in child.student_code
    assert child.student_ast is not None
    assert child.solution_code is None
    assert child.solution_ast is None

    # test with solution content
    child = cf.check_file(chain._state,
                          temp_py_file.name,
                          solution_code=expected_content)
    assert expected_content in child.student_code
    assert child.student_ast is not None
    assert expected_content in child.solution_code
    assert child.solution_ast is not None
Пример #8
0
def test_get_file_content_error(temp_file_sum):
    with patch("io.open") as mock_file:
        mock_file.side_effect = IOError()
        content = cf.get_file_content(temp_file_sum.name)
        assert content is None
Пример #9
0
def test_get_file_content_missing():
    content = cf.get_file_content("foo")
    assert content is None
Пример #10
0
def test_get_file_content_unicode(temp_file_unicode):
    content = cf.get_file_content(temp_file_unicode.name)
    assert content == "Hervé"
Пример #11
0
def test_get_file_content_simple(temp_file_sum):
    content = cf.get_file_content(temp_file_sum.name)
    assert content == "1 + 1"