| Total Complexity | 42 |
| Total Lines | 164 |
| Duplicated Lines | 0 % |
Complex classes like chess.test.TestMovements 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.""" |
||
| 16 | class TestMovements(unittest.TestCase): |
||
| 17 | |||
| 18 | """Chess unit test class.""" |
||
| 19 | |||
| 20 | def setUp(self): |
||
| 21 | self.chess_board = ChessBoard() |
||
| 22 | |||
| 23 | def test_distance_of_one_filtering_given_positions(self): |
||
|
|
|||
| 24 | directions = [(1, 0), (0, 1), (0, -1)] |
||
| 25 | start_square = (1, 1) |
||
| 26 | already_acceptable_possitions = [(1, 2), (1, 0), (2, 1), (3, 3), (5, 5), (2, 3), (9, 1)] |
||
| 27 | positions = distance_of_one(self.chess_board, start_square, directions, potential_end_locations=already_acceptable_possitions, player_direction=Mock()) |
||
| 28 | expected_positions = [(1, 2), (1, 0), (2, 1)] |
||
| 29 | assert len(positions) == len(expected_positions) |
||
| 30 | for pos in positions: |
||
| 31 | assert pos in positions |
||
| 32 | |||
| 33 | def test_does_end_on_own_piece(self): |
||
| 34 | start = (1, 1) |
||
| 35 | end = (1, 0) |
||
| 36 | self.chess_board[start] = Mock(color=1) |
||
| 37 | self.chess_board[end] = Mock(color=1) |
||
| 38 | potential_end_locations = [end] |
||
| 39 | ret_val = doesnt_land_on_own_piece(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 40 | assert ret_val == [] |
||
| 41 | |||
| 42 | def test_doesnt_end_on_own_piece(self): |
||
| 43 | start = (1, 1) |
||
| 44 | end = (1, 0) |
||
| 45 | self.chess_board[start] = Mock(color=1) |
||
| 46 | self.chess_board[end] = Mock(color=2) |
||
| 47 | potential_end_locations = [end] |
||
| 48 | ret_val = doesnt_land_on_own_piece(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 49 | assert ret_val == [(1, 0)] |
||
| 50 | |||
| 51 | def test_cant_jump_pieces(self): |
||
| 52 | start = (2, 2) |
||
| 53 | middle = (4, 2) |
||
| 54 | |||
| 55 | self.chess_board[start] = Mock() |
||
| 56 | self.chess_board[middle] = Mock() |
||
| 57 | |||
| 58 | starting_end_locations = [(3, 2), (4, 2), (5, 2), (6, 2)] |
||
| 59 | expected_positions = [(3, 2), (4, 2)] |
||
| 60 | new_end_locations = cant_jump_pieces(self.chess_board, start, None, starting_end_locations, Mock()) |
||
| 61 | |||
| 62 | assert new_end_locations == expected_positions |
||
| 63 | |||
| 64 | def test_cant_jump_pieces_doesnt_limit_if_no_pieces_are_in_the_way(self): |
||
| 65 | start = (1, 1) |
||
| 66 | |||
| 67 | self.chess_board[start] = Mock() |
||
| 68 | |||
| 69 | starting_end_locations = [(2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1)] |
||
| 70 | |||
| 71 | new_end_locations = cant_jump_pieces(self.chess_board, start, None, starting_end_locations, Mock()) |
||
| 72 | |||
| 73 | assert new_end_locations == starting_end_locations |
||
| 74 | |||
| 75 | def test_get_potential_end_squares_vertical(self): |
||
| 76 | start = (0, 0) |
||
| 77 | directions = [(0, 1)] |
||
| 78 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
| 79 | |||
| 80 | expected_ends = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7)] |
||
| 81 | assert ends == expected_ends |
||
| 82 | |||
| 83 | def test_get_potential_end_squares_horizontal(self): |
||
| 84 | start = (0, 0) |
||
| 85 | directions = [(1, 0)] |
||
| 86 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
| 87 | |||
| 88 | expected_ends = [(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0)] |
||
| 89 | assert ends == expected_ends |
||
| 90 | |||
| 91 | def test_get_potential_end_squares_diagonal(self): |
||
| 92 | start = (0, 0) |
||
| 93 | directions = [(1, 1)] |
||
| 94 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
| 95 | |||
| 96 | expected_ends = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)] |
||
| 97 | assert ends == expected_ends |
||
| 98 | |||
| 99 | def test_get_potential_end_squares_rook(self): |
||
| 100 | start = (0, 0) |
||
| 101 | directions = [(2, 1)] |
||
| 102 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
| 103 | |||
| 104 | expected_ends = [(2, 1), (4, 2), (6, 3)] |
||
| 105 | assert ends == expected_ends |
||
| 106 | |||
| 107 | def test_ends_on_enemy_no_enemy(self): |
||
| 108 | start = (0, 0) |
||
| 109 | potential_end_locations = [(1, 0), (2, 0), (2, 2)] |
||
| 110 | ends = ends_on_enemy(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 111 | assert ends == [] |
||
| 112 | |||
| 113 | View Code Duplication | def test_ends_on_same_players_piece(self): |
|
| 114 | start = (0, 0) |
||
| 115 | self.chess_board[start] = Mock(color=1) |
||
| 116 | potential_end_locations = [(1, 0), (2, 0), (2, 2)] |
||
| 117 | for end in potential_end_locations: |
||
| 118 | self.chess_board[end] = Mock(color=1) |
||
| 119 | ends = ends_on_enemy(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 120 | assert ends == [] |
||
| 121 | |||
| 122 | View Code Duplication | def test_ends_on_enemy_players_piece(self): |
|
| 123 | start = (0, 0) |
||
| 124 | self.chess_board[start] = Mock(color=1) |
||
| 125 | potential_end_locations = [(1, 0), (2, 0), (2, 2)] |
||
| 126 | end = None |
||
| 127 | for end in potential_end_locations: |
||
| 128 | self.chess_board[end] = Mock(color=1) |
||
| 129 | self.chess_board[end] = Mock(color=2) |
||
| 130 | ends = ends_on_enemy(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 131 | assert ends == [(2, 2)] |
||
| 132 | |||
| 133 | def test_doesnt_land_on_piece(self): |
||
| 134 | start = (1, 1) |
||
| 135 | potential_end_locations = [(1, 2), (1, 3), (2, 4), (1, 4), (0, 2)] |
||
| 136 | for end in potential_end_locations: |
||
| 137 | self.chess_board[end] = Mock(color=2) |
||
| 138 | potential_end_locations.append((2, 3)) |
||
| 139 | ends = doesnt_land_on_piece(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 140 | assert ends == [(2, 3)] |
||
| 141 | |||
| 142 | def test_directional_white(self): |
||
| 143 | start = (3, 1) |
||
| 144 | potential_end_locations = [(4, 1), (2, 1)] |
||
| 145 | |||
| 146 | ends = directional(self.chess_board, start, None, potential_end_locations, (1, 0)) |
||
| 147 | assert ends == [(4, 1)] |
||
| 148 | |||
| 149 | def test_directional_black(self): |
||
| 150 | start = (3, 1) |
||
| 151 | potential_end_locations = [(4, 1), (2, 1)] |
||
| 152 | |||
| 153 | ends = directional(self.chess_board, start, None, potential_end_locations, (-1, 0)) |
||
| 154 | assert ends == [(2, 1)] |
||
| 155 | |||
| 156 | def test_directional_diagonal(self): |
||
| 157 | start = (3, 1) |
||
| 158 | potential_end_locations = [(4, 2), (2, 2)] |
||
| 159 | |||
| 160 | ends = directional(self.chess_board, start, None, potential_end_locations, (1, 1)) |
||
| 161 | assert ends == [(4, 2)] |
||
| 162 | |||
| 163 | def test_first_move(self): |
||
| 164 | start = (3, 1) |
||
| 165 | self.chess_board[start] = Mock(move_count=0) |
||
| 166 | potential_end_locations = [(4, 2), (4, 3), (2, 2), (2, 1), (2, 2)] |
||
| 167 | |||
| 168 | ends = first_move(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 169 | |||
| 170 | assert ends == potential_end_locations |
||
| 171 | |||
| 172 | def test_not_first_move(self): |
||
| 173 | start = (3, 1) |
||
| 174 | self.chess_board[start] = Mock(move_count=1) |
||
| 175 | potential_end_locations = [(4, 2), (4, 3), (2, 2), (2, 1), (2, 2)] |
||
| 176 | |||
| 177 | ends = first_move(self.chess_board, start, None, potential_end_locations, Mock()) |
||
| 178 | |||
| 179 | assert ends == [] |
||
| 180 |
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.