| Total Complexity | 40 |
| Total Lines | 92 |
| Duplicated Lines | 0 % |
Complex classes like chess.test.TestBoard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Chess Board unit test module.""" |
||
| 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 | |||
| 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.