-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
Copy pathtest_main.py
120 lines (82 loc) · 3.45 KB
/
test_main.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
import os
from pathlib import Path
from typing import Callable, List
import pytest
import requests
import requests_mock
from src.hangman.main import (
MainProcess,
Source,
parse_word_from_local,
parse_word_from_site,
print_coverage, # Ensure this is imported
)
class FkPrint:
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:
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]
def test_parse_word_from_local() -> None:
assert isinstance(parse_word_from_local(), str)
def test_parse_word_from_local_error() -> None:
data_path = Path(os.path.abspath('')) / 'Data'
real_name = 'local_words.txt'
time_name = 'local_words_not_exist.txt'
os.rename(data_path / real_name, data_path / time_name)
with pytest.raises(FileNotFoundError):
parse_word_from_local()
os.rename(data_path / time_name, data_path / real_name)
@pytest.mark.internet_required
def test_parse_word_from_site() -> None:
assert isinstance(parse_word_from_site(), str)
print_coverage()
def test_parse_word_from_site_no_internet() -> None:
with requests_mock.Mocker() as mock:
mock.get('https://random-word-api.herokuapp.com/word', exc=requests.exceptions.ConnectionError)
with pytest.raises(requests.exceptions.ConnectionError):
parse_word_from_site()
print_coverage()
def test_parse_word_from_site_success() -> 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'
print_coverage()
def test_parse_word_from_site_err() -> None:
with requests_mock.Mocker() as mock:
mock.get('https://random-word-api.herokuapp.com/word', status_code=404)
with pytest.raises(RuntimeError):
parse_word_from_site()
print_coverage()
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'])
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)
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]