-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from vil02/add_basic_tests_of_create_puzzle
tests: test `create_puzzle`
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "puzzle-generator" | ||
version = "0.1.2" | ||
version = "0.1.3" | ||
description = "Generates python code representing a puzzle" | ||
authors = ["piotr.idzik <[email protected]>"] | ||
readme = "./puzzle_generator/README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pathlib | ||
import subprocess | ||
import pytest | ||
|
||
import puzzle_generator.create_puzzle as cp | ||
|
||
|
||
@pytest.fixture(name="puzzle") | ||
def fixture_puzzle(): | ||
return { | ||
"str": "Question 1?", | ||
"pass": "Answer 1", | ||
"rest": { | ||
"str": "Question 2?", | ||
"pass": "Is this the final answer?", | ||
"rest": {"str": "Congratulations!"}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture(name="puzzle_path") | ||
def fixture_puzzle_path(tmp_path) -> pathlib.Path: | ||
return tmp_path / "puzzle.py" | ||
|
||
|
||
def _run_puzzle_file( | ||
in_puzzle_path: pathlib.Path, answers: list[str] | ||
) -> subprocess.CompletedProcess[str]: | ||
assert in_puzzle_path.is_file() | ||
puzzle_dir = in_puzzle_path.parent | ||
puzzle_name = in_puzzle_path.name | ||
return subprocess.run( | ||
["python3", puzzle_name], | ||
cwd=puzzle_dir, | ||
input="\n".join(answers), | ||
text=True, | ||
capture_output=True, | ||
check=False, | ||
) | ||
|
||
|
||
def test_all_good_answers(puzzle, puzzle_path: pathlib.Path) -> None: | ||
cp.create(puzzle, puzzle_path) | ||
res = _run_puzzle_file(puzzle_path, ["Answer 1", "Is this the final answer?"]) | ||
|
||
assert res.returncode == 0 | ||
assert res.stdout == "Question 1?\nQuestion 2?\nCongratulations!\n" | ||
assert not res.stderr | ||
|
||
|
||
def test_second_answer_wrong(puzzle, puzzle_path: pathlib.Path) -> None: | ||
cp.create(puzzle, puzzle_path) | ||
res = _run_puzzle_file(puzzle_path, ["Answer 1", "This is a wrong answer"]) | ||
assert res.returncode == 1 | ||
assert ( | ||
res.stdout == "Question 1?\nQuestion 2?\nThis is a wrong answer. Try again!\n" | ||
) | ||
assert not res.stderr | ||
|
||
|
||
def test_first_answer_wrong(puzzle, puzzle_path: pathlib.Path) -> None: | ||
cp.create(puzzle, puzzle_path) | ||
res = _run_puzzle_file(puzzle_path, ["This is a wrong answer."]) | ||
assert res.returncode == 1 | ||
assert res.stdout == "Question 1?\nThis is a wrong answer. Try again!\n" | ||
assert not res.stderr |