def test_player_move_fails_wrong_turn(self, board): rook_position = Position(1, 1) board.get_piece.return_value = Rook(board, constants.BLACK, rook_position) game = Game() game._board = board game._turn = constants.WHITE with pytest.raises(InvalidMoveError): game.player_move(rook_position, Position)
def test_get_moves_stalemate(self, board, try_move): try_move.return_value = True game = Game() game._board = board mocked_board_moves = {} board.in_check.return_value = False board.get_moves.return_value = mocked_board_moves all_game_moves = game.get_moves(constants.WHITE) self.assertEqual(game.status, constants.STALEMATE)
def set_up_test_game(board, turn=WHITE, history=None): """Helper method sets up a game for testing""" game = Game() game._board = board game._turn = turn if history: game._history.append(history) game.valid_moves = game.get_moves(game.turn) return game
def test_try_move_without_capture(self, board): rook_position = Position(4, 4) rook = Rook(board, constants.WHITE, rook_position) board.in_check.return_value = False game = Game() game._board = board self.assertTrue(game.try_move(rook, Position(5, 4))) self.assertEqual(rook.position, rook_position) board.in_check.return_value = True self.assertFalse(game.try_move(rook, Position(5, 4))) self.assertEqual(rook.position, rook_position)
def test_get_moves_no_special(self, board, try_move): invalid_position = Position(3, 3) def my_side_effect(*args): if args[1] == invalid_position: return False return True try_move.side_effect = my_side_effect game = Game() game._board = board rook = Rook(board, constants.WHITE, Position(4, 4)) bishop = Bishop(board, constants.WHITE, Position(1, 1)) knight = Knight(board, constants.WHITE, Position(7, 0)) mocked_board_moves = { rook: { constants.MOVES: [Position(5, 4), Position(3, 4)], constants.SPECIAL_MOVES: {} }, bishop: { constants.MOVES: [Position(2, 2), invalid_position], constants.SPECIAL_MOVES: {} }, knight: { constants.MOVES: [], constants.SPECIAL_MOVES: {} } } valid_moves = { rook: { constants.MOVES: [Position(5, 4), Position(3, 4)], constants.SPECIAL_MOVES: {} }, bishop: { constants.MOVES: [Position(2, 2)], constants.SPECIAL_MOVES: {} }, knight: { constants.MOVES: [], constants.SPECIAL_MOVES: {} } } board.get_moves.return_value = mocked_board_moves all_game_moves = game.get_moves(constants.WHITE) self.assertEqual(len(mocked_board_moves), len(all_game_moves)) for key in valid_moves.keys(): self.assertIn(key, all_game_moves) self.assertEqual(len(valid_moves[key]), len(all_game_moves[key])) for move in mocked_board_moves[key]: self.assertIn(move, all_game_moves[key]) self.assertEqual(game.status, constants.IN_PROGRESS)
def test_try_move_with_capture(self, board): white_rook_position = Position(4, 4) white_rook = Rook(board, constants.WHITE, white_rook_position) to_position = Position(5, 4) black_rook = Rook(board, constants.BLACK, to_position) board.in_check.return_value = False game = Game() game._board = board self.assertTrue(game.try_move(white_rook, to_position)) self.assertEqual(white_rook.position, white_rook_position) self.assertEqual(black_rook.position, to_position) board.in_check.return_value = True self.assertFalse(game.try_move(white_rook, to_position)) self.assertEqual(white_rook.position, white_rook_position) self.assertEqual(black_rook.position, to_position)
def test_player_move_fails_invalid_move(self, board, mock_get_moves): rook_position = Position(1, 1) rook = Rook(board, constants.WHITE, rook_position) board.get_piece.return_value = rook mock_get_moves.return_value = { rook: { constants.MOVES: [Position(0, 1), Position(1, 2)], constants.SPECIAL_MOVES: {} } } game = Game() game._board = board with pytest.raises(InvalidMoveError): game.player_move(rook.position, Position(2, 2))
def test_player_move_success(self, mock_get_moves): rook_position = Position(1, 1) rook = Rook(None, constants.WHITE, rook_position) test_board = {'black_pieces': [rook]} mock_get_moves.return_value = { rook: { constants.MOVES: [Position(0, 1), Position(1, 2), Position(2, 2)], constants.SPECIAL_MOVES: {} } } board = set_up_test_board(new_game=False, test_board=test_board) game = Game() game._board = board game.player_move(rook.position, Position(2, 2)) self.assertEqual(rook.position, Position(2, 2))
def test_is_valid_passant(self, board): white_pawn_position = Position(3, 3) black_pawn_position = Position(4, 3) black_pawn = Pawn(board, constants.BLACK, black_pawn_position) white_pawn = Pawn(board, constants.WHITE, white_pawn_position) game = Game() game._board = board en_passant_move = Position(3, 2) game._history.append({ constants.COLOUR: white_pawn.colour, constants.FROM: Position(3, 1), constants.TO: white_pawn.position, constants.PIECE_TYPE: type(white_pawn), constants.CAPTURED: None, constants.TYPE: constants.EN_PASSANT, constants.FIRST_MOVE: True }) board.get_piece.return_value = white_pawn board.in_check.return_value = True self.assertFalse( game.is_valid_passant(black_pawn, en_passant_move, white_pawn)) self.assertEqual(white_pawn.position, white_pawn_position) self.assertEqual(black_pawn.position, black_pawn_position) board.in_check.return_value = False self.assertTrue( game.is_valid_passant(black_pawn, en_passant_move, white_pawn)) self.assertEqual(white_pawn.position, white_pawn_position) self.assertEqual(black_pawn.position, black_pawn_position) game._history[0][constants.FIRST_MOVE] = False self.assertFalse( game.is_valid_passant(black_pawn, en_passant_move, white_pawn)) game._history[0][constants.FIRST_MOVE] = True board.get_piece.return_value = Rook(board, constants.BLACK, Position(1, 1)) self.assertFalse( game.is_valid_passant(black_pawn, en_passant_move, white_pawn))
def test_player_move_fails_no_piece(self, board): board.get_piece.return_value = None game = Game() game._board = board with pytest.raises(InvalidMoveError): game.player_move(Position(1, 1), Position(2, 2))