Total Complexity | 48 |
Total Lines | 204 |
Duplicated Lines | 20.1 % |
Changes | 12 | ||
Bugs | 1 | Features | 5 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like 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_own_pieces(self): |
||
65 | start = (7, 2) |
||
66 | middle = (4, 2) |
||
67 | |||
68 | self.chess_board[start] = Mock(color=1) |
||
69 | self.chess_board[middle] = Mock(color=1) |
||
70 | self.chess_board[(6, 2)] = None |
||
71 | self.chess_board[(5, 2)] = None |
||
72 | |||
73 | starting_end_locations = [(3, 2), (4, 2), (6, 2)] |
||
74 | expected_positions = [(4, 2), (6, 2)] |
||
75 | new_end_locations = cant_jump_pieces(self.chess_board, start, None, starting_end_locations, Mock()) |
||
76 | |||
77 | assert new_end_locations == expected_positions |
||
78 | |||
79 | View Code Duplication | def test_cant_jump_own_pieces_diagonally(self): |
|
80 | start = (0, 2) |
||
81 | middle = (1, 1) |
||
82 | |||
83 | self.chess_board[start] = Mock(color=1) |
||
84 | self.chess_board[middle] = Mock(color=1) |
||
85 | self.chess_board[(2, 0)] = None |
||
86 | |||
87 | starting_end_locations = [(1, 1), (2, 0)] |
||
88 | expected_positions = [(1, 1)] |
||
89 | new_end_locations = cant_jump_pieces(self.chess_board, start, None, starting_end_locations, Mock()) |
||
90 | |||
91 | assert new_end_locations == expected_positions |
||
92 | |||
93 | def test_cant_jump_pieces_divide_by_one_error(self): |
||
94 | start = (2, 2) |
||
95 | |||
96 | self.chess_board[start] = Mock() |
||
97 | |||
98 | starting_end_locations = [(3, 2), (1, 2)] |
||
99 | expected_positions = [(3, 2), (1, 2)] |
||
100 | new_end_locations = cant_jump_pieces(self.chess_board, start, None, starting_end_locations, Mock()) |
||
101 | |||
102 | assert new_end_locations == expected_positions |
||
103 | |||
104 | def test_cant_jump_pieces_doesnt_limit_if_no_pieces_are_in_the_way(self): |
||
105 | start = (1, 1) |
||
106 | |||
107 | self.chess_board[start] = Mock() |
||
108 | |||
109 | starting_end_locations = [(2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1)] |
||
110 | |||
111 | new_end_locations = cant_jump_pieces(self.chess_board, start, None, starting_end_locations, Mock()) |
||
112 | |||
113 | View Code Duplication | assert new_end_locations == starting_end_locations |
|
114 | |||
115 | def test_get_potential_end_squares_vertical(self): |
||
116 | start = (0, 0) |
||
117 | directions = [(0, 1)] |
||
118 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
119 | |||
120 | expected_ends = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7)] |
||
121 | assert ends == expected_ends |
||
122 | View Code Duplication | ||
123 | def test_get_potential_end_squares_horizontal(self): |
||
124 | start = (0, 0) |
||
125 | directions = [(1, 0)] |
||
126 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
127 | |||
128 | expected_ends = [(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0)] |
||
129 | assert ends == expected_ends |
||
130 | |||
131 | def test_get_potential_end_squares_diagonal(self): |
||
132 | start = (0, 0) |
||
133 | directions = [(1, 1)] |
||
134 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
135 | |||
136 | expected_ends = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)] |
||
137 | assert ends == expected_ends |
||
138 | |||
139 | def test_get_potential_end_squares_rook(self): |
||
140 | start = (0, 0) |
||
141 | directions = [(2, 1)] |
||
142 | ends = get_all_potential_end_locations(start, directions, self.chess_board) |
||
143 | |||
144 | expected_ends = [(2, 1), (4, 2), (6, 3)] |
||
145 | assert ends == expected_ends |
||
146 | |||
147 | def test_ends_on_enemy_no_enemy(self): |
||
148 | start = (0, 0) |
||
149 | potential_end_locations = [(1, 0), (2, 0), (2, 2)] |
||
150 | ends = ends_on_enemy(self.chess_board, start, None, potential_end_locations, Mock()) |
||
151 | assert ends == [] |
||
152 | |||
153 | def test_ends_on_same_players_piece(self): |
||
154 | start = (0, 0) |
||
155 | self.chess_board[start] = Mock(color=1) |
||
156 | potential_end_locations = [(1, 0), (2, 0), (2, 2)] |
||
157 | for end in potential_end_locations: |
||
158 | self.chess_board[end] = Mock(color=1) |
||
159 | ends = ends_on_enemy(self.chess_board, start, None, potential_end_locations, Mock()) |
||
160 | assert ends == [] |
||
161 | |||
162 | View Code Duplication | def test_ends_on_enemy_players_piece(self): |
|
163 | start = (0, 0) |
||
164 | self.chess_board[start] = Mock(color=1) |
||
165 | potential_end_locations = [(1, 0), (2, 0), (2, 2)] |
||
166 | end = None |
||
167 | for end in potential_end_locations: |
||
168 | self.chess_board[end] = Mock(color=1) |
||
169 | self.chess_board[end] = Mock(color=2) |
||
170 | ends = ends_on_enemy(self.chess_board, start, None, potential_end_locations, Mock()) |
||
171 | assert ends == [(2, 2)] |
||
172 | |||
173 | def test_doesnt_land_on_piece(self): |
||
174 | start = (1, 1) |
||
175 | potential_end_locations = [(1, 2), (1, 3), (2, 4), (1, 4), (0, 2)] |
||
176 | for end in potential_end_locations: |
||
177 | self.chess_board[end] = Mock(color=2) |
||
178 | potential_end_locations.append((2, 3)) |
||
179 | ends = doesnt_land_on_piece(self.chess_board, start, None, potential_end_locations, Mock()) |
||
180 | assert ends == [(2, 3)] |
||
181 | |||
182 | def test_directional_white(self): |
||
183 | start = (3, 1) |
||
184 | potential_end_locations = [(4, 1), (2, 1)] |
||
185 | |||
186 | ends = directional(self.chess_board, start, None, potential_end_locations, (1, 0)) |
||
187 | assert ends == [(4, 1)] |
||
188 | |||
189 | def test_directional_black(self): |
||
190 | start = (3, 1) |
||
191 | potential_end_locations = [(4, 1), (2, 1)] |
||
192 | |||
193 | ends = directional(self.chess_board, start, None, potential_end_locations, (-1, 0)) |
||
194 | assert ends == [(2, 1)] |
||
195 | |||
196 | def test_directional_diagonal(self): |
||
197 | start = (3, 1) |
||
198 | potential_end_locations = [(4, 2), (2, 2)] |
||
199 | |||
200 | ends = directional(self.chess_board, start, None, potential_end_locations, (1, 1)) |
||
201 | assert ends == [(4, 2)] |
||
202 | |||
203 | def test_first_move(self): |
||
204 | start = (3, 1) |
||
205 | self.chess_board[start] = Mock(move_count=0) |
||
206 | potential_end_locations = [(4, 2), (4, 3), (2, 2), (2, 1), (2, 2)] |
||
207 | |||
208 | ends = first_move(self.chess_board, start, None, potential_end_locations, Mock()) |
||
209 | |||
210 | assert ends == potential_end_locations |
||
211 | |||
212 | def test_not_first_move(self): |
||
213 | start = (3, 1) |
||
214 | self.chess_board[start] = Mock(move_count=1) |
||
215 | potential_end_locations = [(4, 2), (4, 3), (2, 2), (2, 1), (2, 2)] |
||
216 | |||
217 | ends = first_move(self.chess_board, start, None, potential_end_locations, Mock()) |
||
218 | |||
219 | assert ends == [] |
||
220 |