forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
97 lines (66 loc) · 2.54 KB
/
test_main.py
File metadata and controls
97 lines (66 loc) · 2.54 KB
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
from typing import Callable, List
import pytest
import requests_mock
from src.hangman.main import (
MainProcess,
Source,
parse_word_from_site,
)
class FkPrint(object):
def __init__(self) -> None:
self.container: List[str] = []
def __call__(self, value_to_print: str) -> None:
self.container.append(str(value_to_print))
class FkInput(object):
def __init__(self, values_to_input: List[str]) -> None:
self.values_to_input: List[str] = values_to_input
def __call__(self) -> str:
return self.values_to_input.pop(0)
@pytest.fixture
def choice_fn() -> Callable:
return lambda array: array[0] # noqa: E731
@pytest.mark.internet_required
def test_parse_word_from_site() -> None:
assert isinstance(parse_word_from_site(), str)
def test_parse_word_from_site_no_internet() -> None:
with requests_mock.Mocker() as mock:
mock.get("https://random-word-api.herokuapp.com/word", text='["some text"]')
assert parse_word_from_site() == "some text"
def test_parse_word_from_site_err() -> None:
with pytest.raises(RuntimeError):
parse_word_from_site(url="https://www.google.com/dsfsdfds/sdfsdf/sdfds")
def test_get_word(choice_fn: Callable) -> None:
fk_print = FkPrint()
fk_input = FkInput(["none"])
main_process = MainProcess(
Source(1), pr_func=fk_print, in_func=fk_input, ch_func=choice_fn
)
assert isinstance(main_process.get_word(), str)
def test_start_game_win(choice_fn: Callable) -> None:
fk_print = FkPrint()
fk_input = FkInput(["j", "a", "m"])
main_process = MainProcess(
Source(0), pr_func=fk_print, in_func=fk_input, ch_func=choice_fn
)
main_process.start_game()
assert "YOU WON" in fk_print.container[-1]
@pytest.mark.parametrize(
"input_str", [[letter] * 10 for letter in "qwertyuiopasdfghjklzxcvbnm"]
) # noqa: WPS435
def test_start_game_loose(input_str: List[str], choice_fn: Callable) -> None:
fk_print = FkPrint()
fk_input = FkInput(input_str)
main_process = MainProcess(
Source(0), pr_func=fk_print, in_func=fk_input, ch_func=choice_fn
)
main_process.start_game()
assert "YOU LOST" in fk_print.container[-1]
def test_wow_year(freezer, choice_fn: Callable) -> None:
freezer.move_to("2135-10-17")
fk_print = FkPrint()
fk_input = FkInput(["none"] * 100) # noqa: WPS435
main_process = MainProcess(
Source(0), pr_func=fk_print, in_func=fk_input, ch_func=choice_fn
)
main_process.start_game()
assert "this program" in fk_print.container[0]