-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gui_game_of_life.py
63 lines (52 loc) · 2.24 KB
/
test_gui_game_of_life.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
import pytest
from unittest.mock import patch
from io import StringIO
import sys
from gui_game_of_life import GameOfLife
@pytest.fixture
def game_instance():
return GameOfLife()
def test_init(game_instance):
assert game_instance.how_many_iterations == 0
assert len(game_instance.how_many_alive) == 1
assert game_instance.how_many_alive[0] == game_instance.chech_how_many_alive()
def test_show_board(game_instance, capsys):
game_instance.show_board()
captured = capsys.readouterr()
assert "Show Board" in captured.out
def test_end(game_instance, capsys):
with patch.object(sys, 'exit') as mock_exit:
game_instance.end()
mock_exit.assert_called_once_with()
def test_make_a_plot(game_instance):
with patch("matplotlib.pyplot.show") as mock_show:
game_instance.make_a_plot()
mock_show.assert_called_once()
def test_how_many_neighbours(game_instance):
game_instance.board = [["0", "0", "."], ["0", ".", "0"], [".", ".", "."]]
assert game_instance.how_many_neighbours(0, 0) == 3
assert game_instance.how_many_neighbours(1, 1) == 8
assert game_instance.how_many_neighbours(2, 2) == 0
def test_chech_how_many_alive(game_instance):
game_instance.board = [["0", ".", "."], [".", "0", "."], [".", ".", "."]]
assert game_instance.chech_how_many_alive() == 2
def test_one_iteration(game_instance):
initial_alive_cells = game_instance.chech_how_many_alive()
game_instance.one_iteration()
final_alive_cells = game_instance.chech_how_many_alive()
assert final_alive_cells != initial_alive_cells
def test_do_one_iteration(game_instance, capsys):
initial_alive_cells = game_instance.chech_how_many_alive()
game_instance.do_one_iteration()
final_alive_cells = game_instance.chech_how_many_alive()
captured = capsys.readouterr()
assert final_alive_cells != initial_alive_cells
assert "Show Board" in captured.out
def test_draw_board(game_instance):
with patch("tkinter.Canvas.create_rectangle") as mock_create_rectangle:
game_instance.draw_board()
mock_create_rectangle.assert_called_once()
def test_run_gui(game_instance):
with patch("tkinter.Tk.mainloop") as mock_mainloop:
game_instance.run_gui()
mock_mainloop.assert_called_once()