-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_space_game.py
91 lines (70 loc) · 1.94 KB
/
test_space_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import io
import pytest
from space_game import travel
# the actual solution to the game
SOLUTION = [
"2",
"2", # go to sirius and win quiz
"1",
"42", # hire copilot on orion
"1",
"yes", # go to centauri and buy GPU drive
"2",
"2",
"3",
"yes", # jump into black hole
]
DEATH_BY_BLACK_HOLE = [
"2",
"2", # go to sirius and win quiz
"1",
"41", # hire copilot on orion
"1",
"yes", # go to centauri and buy GPU drive
"1",
"2",
"3",
"yes", # jump into black hole
]
# text sniplets that should appear literally in the output
PHRASES = [
"The stars are waiting for you",
"Betelgeuse",
"credits",
"tech-savvy native",
"copilot",
"buy",
"life, the universe and everything",
"Black Hole",
"stupid idea",
"wonders beyond description",
"THE END",
]
@pytest.fixture
def solution_input():
"""helper function to hijack the keyboard for testing"""
return io.StringIO("\n".join(SOLUTION))
def test_travel(monkeypatch, solution_input):
"""game finishes"""
monkeypatch.setattr("sys.stdin", solution_input)
travel()
def test_output(monkeypatch, capsys, solution_input):
"""text output is not empty"""
monkeypatch.setattr("sys.stdin", solution_input)
travel()
captured = capsys.readouterr()
assert len(captured.out) > 0
def test_die(monkeypatch, capsys):
"""player dies"""
monkeypatch.setattr("sys.stdin", io.StringIO("\n".join(DEATH_BY_BLACK_HOLE)))
travel()
captured = capsys.readouterr()
assert "grain of dust" in captured.out
assert " wonders beyond description" not in captured.out
@pytest.mark.parametrize("phrase", PHRASES)
def test_output_phrases(monkeypatch, capsys, solution_input, phrase):
"""check for some key phrases in the output"""
monkeypatch.setattr("sys.stdin", solution_input)
travel()
captured = capsys.readouterr()
assert phrase in captured.out