|
8 | 8 | from protowhat.checks import check_files as cf |
9 | 9 |
|
10 | 10 |
|
11 | | -@pytest.fixture(scope="function") |
12 | | -def tf(): |
| 11 | +@pytest.fixture |
| 12 | +def temp_py_file(): |
13 | 13 | with NamedTemporaryFile() as tmp: |
14 | | - tmp.file.write(b"1 + 1") |
| 14 | + tmp.file.write( |
| 15 | + b""" |
| 16 | +if True: |
| 17 | + a = 1""" |
| 18 | + ) |
15 | 19 | tmp.file.flush() |
16 | 20 | yield tmp |
17 | 21 |
|
18 | 22 |
|
19 | | -def test_file_existence(tf): |
20 | | - s = setup_state("", "", pec="") |
| 23 | +@pytest.fixture |
| 24 | +def temp_txt_file(): |
| 25 | + with NamedTemporaryFile() as tmp: |
| 26 | + tmp.file.write(b"this is a text file") |
| 27 | + tmp.file.flush() |
| 28 | + yield tmp |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(params=["temp_py_file", "temp_txt_file"]) |
| 32 | +def temp_file(request): |
| 33 | + return request.getfuncargvalue(request.param) |
| 34 | + |
| 35 | + |
| 36 | +def test_python_file_existence(temp_py_file): |
| 37 | + expected_content = cf.get_file_content(temp_py_file.name) |
| 38 | + chain = setup_state("", "", pec="") |
| 39 | + |
| 40 | + # test with just student content |
| 41 | + child = cf.check_file(chain._state, temp_py_file.name) |
| 42 | + assert expected_content in child.student_code |
| 43 | + assert child.student_ast is not None |
| 44 | + assert child.solution_code is None |
| 45 | + assert child.solution_ast is None |
21 | 46 |
|
22 | | - child = cf.check_file(s._state, tf.name) |
23 | | - assert "1 + 1" in child.student_code |
| 47 | + # test with solution content |
| 48 | + child = cf.check_file( |
| 49 | + chain._state, temp_py_file.name, solution_code=expected_content |
| 50 | + ) |
| 51 | + assert expected_content in child.student_code |
| 52 | + assert child.student_ast is not None |
| 53 | + assert expected_content in child.solution_code |
| 54 | + assert child.solution_ast is not None |
24 | 55 |
|
25 | | - file_chain = s.check_file(tf.name) |
26 | | - assert "1 + 1" in file_chain._state.student_code |
| 56 | + |
| 57 | +def test_file_existence_syntax(temp_py_file): |
| 58 | + """test integration of protowhat checks in pythonwhat""" |
| 59 | + expected_content = cf.get_file_content(temp_py_file.name) |
| 60 | + chain = setup_state("", "", pec="") |
| 61 | + |
| 62 | + file_chain = chain.check_file(temp_py_file.name) |
| 63 | + assert expected_content in file_chain._state.student_code |
27 | 64 |
|
28 | 65 | with helper.verify_sct(True): |
29 | | - s >> F(attr_scts={"check_file": cf.check_file}).check_file(tf.name) |
| 66 | + file_chain = chain >> F(attr_scts={"check_file": cf.check_file}).check_file( |
| 67 | + temp_py_file.name |
| 68 | + ) |
| 69 | + assert expected_content in file_chain._state.student_code |
| 70 | + |
| 71 | + |
| 72 | +def test_file_parsing(temp_py_file): |
| 73 | + expected_content = cf.get_file_content(temp_py_file.name) |
| 74 | + chain = setup_state("", "", pec="") |
| 75 | + |
| 76 | + file_chain = chain.check_file(temp_py_file.name, solution_code=expected_content) |
| 77 | + file_chain.check_if_else().check_test().has_equal_value() |
| 78 | + file_chain.check_if_else().check_test().has_equal_value(expr_code="False") |
| 79 | + |
| 80 | + |
| 81 | +def test_file_content(temp_file): |
| 82 | + expected_content = cf.get_file_content(temp_file.name) |
| 83 | + chain = setup_state("", "", pec="") |
| 84 | + |
| 85 | + chain.check_file(temp_file.name, parse=False).has_code(expected_content) |
| 86 | + chain.check_file( |
| 87 | + temp_file.name, parse=False, solution_code=expected_content |
| 88 | + ).has_code(expected_content) |
0 commit comments