def test_cell_says_if_can_move(position, direction, can_actually_move): b = Board( size=(10, 24), deactivated_cells=[Cell(position=(0, 23), color=(0, 0, 0))], ) c = Cell(position=position, color=(0, 0, 0)) assert c.can_move(b, direction) is can_actually_move
def test_cell_can_be_printed(draw_rect_mocked): color = (0, 0, 0) c = Cell(position=(5, 10), color=color) screen = None c.draw(screen=screen) draw_rect_mocked.assert_called_once_with( screen, color, (5 * RECT_SIZE, 10 * RECT_SIZE, RECT_SIZE, RECT_SIZE) )
def test_cell_can_move(direction): b = Board(size=(10, 24)) c = Cell(position=(3, 4), color=(0, 0, 0)) old_position = c.position c.move(b, direction) assert c.position == ( old_position[0] + direction[0], old_position[1] + direction[1], )
def test_board_is_init_correctly(): deactivated_cells = [ Cell((0, 24), color=(0, 0, 0)), Cell((1, 24), color=(0, 0, 0)), Cell((2, 24), color=(0, 0, 0)), ] b = Board(size=(10, 24), deactivated_cells=deactivated_cells) assert b.size == (10, 24) assert b.active_piece is None assert b.deactivated_cells == deactivated_cells
def test_piece_can_not_move_if_one_cell_has_obstacle(piece_blueprint, direction): b = Board( size=(10, 24), deactivated_cells=[ Cell((2, 1), (0, 0, 0)), Cell((4, 2), (0, 0, 0)), Cell((1, 3), (0, 0, 0)), Cell((3, 4), (0, 0, 0)), ], ) p = Piece(piece_blueprint) # Overwrite piece's cells p.cells = [ Cell((2, 2), (0, 0, 0)), Cell((3, 2), (0, 0, 0)), Cell((2, 3), (0, 0, 0)), Cell((3, 3), (0, 0, 0)), ] with pytest.raises(CanNotMove): p.move(b, direction) # Assert piece hasn't moved assert [c.position for c in p.cells] == [(2, 2), (3, 2), (2, 3), (3, 3)]
def test_board_gives_positions_of_deactivated_cells(): b = Board( size=(10, 24), deactivated_cells=[ Cell((0, 24), color=(0, 0, 0)), Cell((1, 24), color=(0, 0, 0)), Cell((2, 24), color=(0, 0, 0)), ], ) assert b.positions_of_deactivated_cells == [ (0, 24), (1, 24), (2, 24), ]
def test_cell_can_not_move_if_exceed_limit(direction): b = Board(size=(10, 24)) direction_x, direction_y = direction if direction_x: potential_values_of_x = {1: 9, -1: 0} initial_position = (potential_values_of_x[direction_x], 0) else: potential_values_of_y = {1: 23, -1: 0} initial_position = (0, potential_values_of_y[direction_y]) c = Cell(position=initial_position, color=(0, 0, 0)) with pytest.raises(CanNotMove): c.move(b, direction) assert c.position == initial_position
def test_piece_can_be_drawn(draw_rect_mocked, piece_draw_mocked, cell_draw_mocked): b = Board( size=(10, 24), deactivated_cells=[ Cell((0, 24), color=(0, 0, 0)), Cell((1, 24), color=(0, 0, 0)), Cell((2, 24), color=(0, 0, 0)), ], ) b.active_piece = Piece(PieceBlueprints.get_random()) b.draw(None) piece_draw_mocked.assert_called_once() assert cell_draw_mocked.call_count == 3 assert draw_rect_mocked.call_count == b.size[0] * b.size[1]
def test_cell_can_not_move_if_there_already_is_a_cell(direction): b = Board( size=(10, 24), deactivated_cells=[Cell(position=(4, 4), color=(0, 0, 0))], ) direction_x, direction_y = direction potential_values = {1: 3, -1: 5, 0: 4} initial_position = ( potential_values[direction_x], potential_values[direction_y], ) c = Cell(position=initial_position, color=(0, 0, 0)) with pytest.raises(CanNotMove): c.move(b, direction) assert c.position == initial_position
def test_piece_can_not_move_if_one_cell_is_on_the_border( piece_blueprint, direction): b = Board(size=(2, 2)) p = Piece(piece_blueprint) # Overwrite piece's cells p.cells = [ Cell((0, 0), (0, 0, 0)), Cell((1, 0), (0, 0, 0)), Cell((0, 1), (0, 0, 0)), Cell((1, 1), (0, 0, 0)), ] with pytest.raises(CanNotMove): p.move(b, direction) # Assert piece hasn't moved assert [c.position for c in p.cells] == [(0, 0), (1, 0), (0, 1), (1, 1)]
def test_piece_can_move(piece_blueprint, direction): b = Board( size=(10, 24), deactivated_cells=[Cell((0, 24), color=(0, 0, 0))], ) p = Piece(piece_blueprint) # Overwrite piece's cells p.cells = [Cell((1, 1), (0, 0, 0)), Cell((1, 2), (0, 0, 0))] p.move(b, direction) if direction == (1, 0): new_positions = [(2, 1), (2, 2)] elif direction == (-1, 0): new_positions = [(0, 1), (0, 2)] elif direction == (0, 1): new_positions = [(1, 2), (1, 3)] else: new_positions = [(1, 0), (1, 1)] assert [c.position for c in p.cells] == new_positions
def test_piece_can_go_at_the_bottom(): b = Board(size=(10, 24), deactivated_cells=[Cell((4, 23), color=(0, 0, 0))]) p = Piece(PieceBlueprints.I.value) p.go_at_the_bottom(b) assert [c.position for c in p.cells] == [ (3, 22), (4, 22), (5, 22), (6, 22), ]
def test_cell_is_init_correctly(): c = Cell(position=(3, 4), color=(0, 0, 0)) assert c.position == (3, 4) assert c.color == (0, 0, 0)