-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mhered_test_pkg.py
144 lines (115 loc) · 3.84 KB
/
test_mhered_test_pkg.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from pathlib import Path
import toml
from pytest import raises
from src.mhered_test_pkg import (
__version__,
compare_moves,
computer_play,
move_msg,
player_play,
rock_paper_scissors,
)
def test_versions_are_in_sync():
"""Checks if tool.poetry.version in pyproject.toml and
__version__ in mhered_test_pkg.__init__.py are in sync."""
path = Path(__file__).resolve().parents[1] / "pyproject.toml"
pyproject = toml.loads(open(str(path)).read())
pyproject_version = pyproject["tool"]["poetry"]["version"]
init_py_version = __version__
assert init_py_version == pyproject_version
def test_move_msg_r():
"""Checks message for r is ROCK"""
assert move_msg("r") == "ROCK"
def test_move_msg_p():
"""Checks message for p is PAPER"""
assert move_msg("p") == "PAPER"
def test_move_msg_s():
"""Checks message for s is SCISSORS"""
assert move_msg("s") == "SCISSORS"
def test_compare_moves():
"""Checks outcome for all move pairs"""
cases = [
{
"player": "r",
"computer": "s",
"outcome": "win",
},
{
"player": "r",
"computer": "p",
"outcome": "lose",
},
{
"player": "r",
"computer": "r",
"outcome": "tie",
},
{
"player": "p",
"computer": "r",
"outcome": "win",
},
{
"player": "p",
"computer": "s",
"outcome": "lose",
},
{
"player": "p",
"computer": "p",
"outcome": "tie",
},
{
"player": "s",
"computer": "p",
"outcome": "win",
},
{
"player": "s",
"computer": "r",
"outcome": "lose",
},
{
"player": "s",
"computer": "s",
"outcome": "tie",
},
]
for case in cases:
assert compare_moves(case["player"], case["computer"]) == case["outcome"]
def test_computer_play():
"""Test computer plays only 'r' 'p' 's'"""
for i in range(100):
assert computer_play() in ["r", "p", "s"]
def test_player_play_returns_r_p_s(monkeypatch):
"""Player_play() returns r p s"""
# Adapted from stack overflow: monkeypatching user input
# https://stackoverflow.com/questions/52248499/pytest-how-to-test-a-separate-function-with-input-call
# monkeypatch the "input" function, so that it returns "r".
# This simulates the user entering "r" in the terminal:
for user_input in ["r", "s", "p"]:
monkeypatch.setattr("builtins.input", lambda prompt="": user_input)
# go about using input() like you normally would:
player_move = player_play()
assert player_move == user_input
def test_player_play_exits_with_q(monkeypatch):
"""Player_play exits with q"""
# Adapted from python-pandemonium: testing for sys exit
# https://medium.com/python-pandemonium/testing-sys-exit-with-pytest-10c6e5f7726f
with raises(SystemExit) as pytest_wrapped_e:
monkeypatch.setattr("builtins.input", lambda prompt="": "q")
_ = player_play()
assert pytest_wrapped_e.type == SystemExit
# assert pytest_wrapped_e.value.code == 42
def test_rock_paper_scissors_overall(monkeypatch):
"""Full run"""
# Adapted from stack overflow: iterator to simulate a sequence of inputs
# https://stackoverflow.com/questions/53472142/pytest-user-input-simulation
# creating iterator object
user_inputs = iter(
["r", "s", "p", "r", "s", "p", "r", "s", "p", "r", "s", "p", "a", "q"]
)
with raises(SystemExit) as pytest_wrapped_e:
monkeypatch.setattr("builtins.input", lambda prompt="": next(user_inputs))
rock_paper_scissors()
assert pytest_wrapped_e.type == SystemExit