-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_game_loop_entity.py
105 lines (79 loc) · 3.87 KB
/
test_game_loop_entity.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
import unittest
from chess_library import ChessLibrary
from game_loop_entity import GameLoopEntity
from chess_position import ChessPosition
from chess_move import ChessMove
from led_interface import LedInterface
import webserver
from mock import MagicMock
from mock import patch
from mock import call
class TestGameLoopEntity(unittest.TestCase):
@classmethod
def setUpClass(cls):
webserver.run_webserver_in_thread()
@classmethod
def tearDownClass(cls):
webserver.stop_webserver_in_thread()
def setUp(self):
self.chess_library = ChessLibrary()
self.game_loop = GameLoopEntity()
self.led_interface = LedInterface()
def tearDown(self):
pass
@patch("chess_library.ChessLibrary.hand_off")
def test_give_to_chess_library(self,fn):
initial_pos = ChessPosition("b",4)
final_pos = ChessPosition("c",1)
chessMove = ChessMove(initial_pos, final_pos)
self.game_loop.give_to_chess_library(initial_pos, final_pos)
fn.assert_called_once_with(chessMove)
def test_get_opponent_move(self):
initial_pos = ChessPosition("b", 4)
final_pos = ChessPosition("c", 1)
chessMove = ChessMove(initial_pos, final_pos)
self.game_loop.chess_library.get_move = MagicMock(return_value=chessMove)
opponentMove = self.game_loop.get_opponent_move_from_library()
self.assertEqual(chessMove, opponentMove)
@patch("led_interface.LedInterface.start_blinking_led")
@patch("led_interface.LedInterface.turn_on_led")
def test_show_opponent_move(self, turn_on_led_checker, start_blinking_led_checker):
initial_pos = ChessPosition("b", 4)
final_pos = ChessPosition("c", 1)
chessMove = ChessMove(initial_pos, final_pos)
self.game_loop.show_opponent_move(initial_pos, final_pos)
turn_on_led_checker.assert_called_with(initial_pos)
start_blinking_led_checker.assert_called_with(final_pos, 1)
@patch("builtins.input", side_effect = ["b4","c5"])
def test_gather_user_input(self, input_checker):
result = self.game_loop.gather_user_input()
initial_pos = ChessPosition("b", 4)
final_pos = ChessPosition("c", 5)
chessMove = ChessMove(initial_pos, final_pos)
self.assertEqual(result, chessMove)
@patch("lcd_interface.LCDInterface.display")
def test_prompt_user_for_beginning_input(self, display_checker):
self.game_loop.prompt_user_for_input()
display_checker.assert_has_calls([call("Welcome to Auto Chess", ""), call("Enter initial then final position: ","")])
@patch("lcd_interface.LCDInterface.display")
def test_prompt_user_for_other_input(self, display_checker):
self.game_loop.prompt_user_for_input()
display_checker.assert_called_with("Enter initial then final position: ", "")
@patch("builtins.input", side_effect=["z20", "b4", "c5"])
def test_gather_user_input_initial_check(self, input_checker):
self.game_loop.lcd_interface.display = MagicMock()
result = self.game_loop.gather_user_input()
self.game_loop.lcd_interface.display.assert_called_once_with("Incorrect initial coordinate, try again.", "")
initial_pos = ChessPosition("b", 4)
final_pos = ChessPosition("c", 5)
chessMove = ChessMove(initial_pos, final_pos)
self.assertEqual(result, chessMove)
@patch("builtins.input", side_effect=["b4", "z20", "c5"])
def test_gather_user_input_final_check(self, input_checker):
self.game_loop.lcd_interface.display = MagicMock()
result = self.game_loop.gather_user_input()
self.game_loop.lcd_interface.display.assert_called_once_with("Incorrect final coordinate, try again.", "")
initial_pos = ChessPosition("b", 4)
final_pos = ChessPosition("c", 5)
chessMove = ChessMove(initial_pos, final_pos)
self.assertEqual(result, chessMove)