1
|
|
|
"""Chess Board unit test module.""" |
2
|
|
|
|
3
|
|
|
import unittest |
4
|
|
|
|
5
|
|
|
from chess.board import ChessBoard |
6
|
|
|
|
7
|
|
|
starting_fen_board = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TestBoard(unittest.TestCase): |
11
|
|
|
|
12
|
|
|
"""Chess unit test class.""" |
13
|
|
|
|
14
|
|
|
def setUp(self): |
15
|
|
|
self.chess_board = ChessBoard() |
16
|
|
|
|
17
|
|
|
@staticmethod |
18
|
|
|
def convert_default_white_spaces_to_black(white_positions): |
|
|
|
|
19
|
|
|
return [(7 - row, column) for row, column in white_positions] |
20
|
|
|
|
21
|
|
|
def verify_pieces_at_locations_are_correct_piece_and_color(self, white_positions, piece): |
|
|
|
|
22
|
|
|
for location in white_positions: |
23
|
|
|
assert self.chess_board[location].kind == piece |
24
|
|
|
assert self.chess_board[location].color == "white" |
25
|
|
|
|
26
|
|
|
black_positions = self.convert_default_white_spaces_to_black(white_positions) |
27
|
|
|
for location in black_positions: |
28
|
|
|
assert self.chess_board[location].kind == piece |
29
|
|
|
assert self.chess_board[location].color == "black" |
30
|
|
|
|
31
|
|
|
def test_init(self): |
|
|
|
|
32
|
|
|
assert len(self.chess_board) == 64 |
33
|
|
|
|
34
|
|
|
def test_has_pawn_at_1_3(self): |
|
|
|
|
35
|
|
|
piece = self.chess_board[(1, 3)] |
36
|
|
|
assert len(piece.moves) > 0 |
37
|
|
|
|
38
|
|
|
def test_initial_pawn_positions(self): |
|
|
|
|
39
|
|
|
expected_white_pawn_positions = [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)] |
40
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color(expected_white_pawn_positions, "pawn") |
41
|
|
|
|
42
|
|
|
def test_initial_knight_positions(self): |
|
|
|
|
43
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 1), (0, 6)], "knight") |
44
|
|
|
|
45
|
|
|
def test_initial_rook_positions(self): |
|
|
|
|
46
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 0), (0, 7)], "rook") |
47
|
|
|
|
48
|
|
|
def test_initial_bishop_positions(self): |
|
|
|
|
49
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 2), (0, 5)], 'bishop') |
50
|
|
|
|
51
|
|
|
def test_initial_queen_positions(self): |
|
|
|
|
52
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 3)], "queen") |
53
|
|
|
|
54
|
|
|
def test_initial_king_positions(self): |
|
|
|
|
55
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 4)], "king") |
56
|
|
|
|
57
|
|
|
def test_starting_board_fen_export(self): |
|
|
|
|
58
|
|
|
assert self.chess_board.generate_fen() == starting_fen_board |
59
|
|
|
|
60
|
|
|
def test_one_move_fen_export(self): |
|
|
|
|
61
|
|
|
self.chess_board[(6, 0)] = None |
62
|
|
|
expected_fen = "rnbqkbnr/1ppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" |
63
|
|
|
assert expected_fen == self.chess_board.generate_fen() |
64
|
|
|
|
65
|
|
|
def test_clear_board_removes_all_pieces(self): |
|
|
|
|
66
|
|
|
self.chess_board.clear_board() |
67
|
|
|
for location in self.chess_board.board: |
68
|
|
|
assert self.chess_board[location] is None |
69
|
|
|
|
70
|
|
|
def test_starting_board_fen_import(self): |
|
|
|
|
71
|
|
|
self.chess_board.clear_board() |
72
|
|
|
self.chess_board.import_fen_board(starting_fen_board) |
73
|
|
|
|
74
|
|
|
expected_white_pawn_positions = [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)] |
75
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color(expected_white_pawn_positions, "pawn") |
76
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 1), (0, 6)], "knight") |
77
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 0), (0, 7)], "rook") |
78
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 2), (0, 5)], 'bishop') |
79
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 3)], "queen") |
80
|
|
|
self.verify_pieces_at_locations_are_correct_piece_and_color([(0, 4)], "king") |
81
|
|
|
|
82
|
|
|
def test_starting_board_custom_export(self): |
|
|
|
|
83
|
|
|
expected_json = self.chess_board.load_json() |
84
|
|
|
exported_json = self.chess_board.export() |
85
|
|
|
self.compare_boards(exported_json['board'], expected_json['board']) |
86
|
|
|
exported_json.pop('board') |
87
|
|
|
for key, value in exported_json.items(): |
88
|
|
|
assert expected_json[key] == value |
89
|
|
|
assert len(expected_json) == len(exported_json) + 1 |
90
|
|
|
|
91
|
|
|
@staticmethod |
92
|
|
|
def compare_boards(board1, board2): |
|
|
|
|
93
|
|
|
for player in board1: |
94
|
|
|
assert player in board2 |
95
|
|
|
for piece in board1[player]: |
96
|
|
|
assert piece in board2[player] |
97
|
|
|
piece_locations1 = board1[player][piece] |
98
|
|
|
piece_locations2 = board2[player][piece] |
99
|
|
|
assert len(piece_locations1) == len(piece_locations2) |
100
|
|
|
for location in piece_locations1: |
101
|
|
|
assert location in piece_locations2 |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
class TestValidateKnightMoves(unittest.TestCase): |
|
|
|
|
105
|
|
|
|
106
|
|
|
def setUp(self): |
107
|
|
|
self.chess_board = ChessBoard() |
108
|
|
|
|
109
|
|
|
def test_move_knight(self): |
|
|
|
|
110
|
|
|
result = self.chess_board.move((0, 1), (2, 0)) |
111
|
|
|
|
112
|
|
|
assert result is True |
113
|
|
|
assert self.chess_board[(2, 0)].kind == 'knight' |
114
|
|
|
|
115
|
|
|
def test_move_knight_on_top_of_a_pawn(self): |
|
|
|
|
116
|
|
|
result = self.chess_board.move((1, 0), (3, 1)) |
117
|
|
|
|
118
|
|
|
assert result is False |
119
|
|
|
assert self.chess_board[(1, 3)].kind == 'pawn' |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
class TestValidatePawnMoves(unittest.TestCase): |
123
|
|
|
|
124
|
|
|
"""Chess movement unit tests.""" |
125
|
|
|
|
126
|
|
|
def setUp(self): |
127
|
|
|
self.chess_board = ChessBoard() |
128
|
|
|
|
129
|
|
|
def test_pawn_can_move_forward(self): |
|
|
|
|
130
|
|
|
assert self.chess_board[(2, 3)] is None |
131
|
|
|
ends = self.chess_board.end_locations_for_piece_at_location((1, 3)) |
132
|
|
|
assert ends == [(2, 3), (3, 3)] |
133
|
|
|
|
134
|
|
|
def test_pawn_cant_move_forward_twice_if_not_first_move(self): |
|
|
|
|
135
|
|
|
assert self.chess_board[(2, 3)] is None |
136
|
|
|
assert self.chess_board[(3, 3)] is None |
137
|
|
|
self.chess_board[(1, 3)].move_count = 1 |
138
|
|
|
ends = self.chess_board.end_locations_for_piece_at_location((1, 3)) |
139
|
|
|
assert ends == [(2, 3)] |
140
|
|
|
|
141
|
|
|
def test_move_white_pawn(self): |
|
|
|
|
142
|
|
|
pass |
143
|
|
|
|
144
|
|
|
def test_move_black_pawn(self): |
|
|
|
|
145
|
|
|
pass |
146
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.