def test_player_move_castle_success(self): """Test player move castling is successful when it is a valid move""" white_rook_position = Position(0, 0) white_rook = Rook(None, constants.WHITE, white_rook_position) test_board = {'white_pieces': [white_rook]} board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board) game.player_move(Position(4, 0), Position(2, 0)) self.assertEqual(white_rook.position, Position(3, 0)) self.assertTrue(isinstance(board.get_piece(Position(2, 0)), King))
def test_player_move_fails_into_check(self): """Test trying to make a move into check fails""" white_rook_position = Position(4, 1) black_rook = Rook(None, constants.BLACK, Position(4, 5)) white_rook = Rook(None, constants.WHITE, white_rook_position) test_board = { 'white_pieces': [white_rook], 'black_pieces': [black_rook] } board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board) with pytest.raises(InvalidMoveError): game.player_move(white_rook_position, Position(2, 1))
def test_player_move_castle_fails(self): """Test castling fails when a position in between king and rook is attacked""" white_rook_position = Position(0, 0) black_rook = Rook(None, constants.BLACK, Position(2, 5)) white_rook = Rook(None, constants.WHITE, white_rook_position) test_board = { 'white_pieces': [white_rook], 'black_pieces': [black_rook] } board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board) with pytest.raises(InvalidMoveError): game.player_move(Position(4, 0), Position(2, 0)) self.assertEqual(white_rook.position, white_rook_position) self.assertTrue(isinstance(board.get_piece(Position(4, 0)), King))
def test_pawn_promotion(self): """Test when a pawn reaches the end of the board that it is promoted to a queen""" white_pawn_position = Position(0, 6) end_position = Position(0, 7) white_pawn = Pawn(None, constants.WHITE, white_pawn_position) test_board = {'white_pieces': [white_pawn]} board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board) game.player_move(white_pawn_position, end_position) self.assertIsNone(white_pawn.position) self.assertNotIn(white_pawn, board.white_pieces) queen = board.get_piece(end_position) self.assertTrue(isinstance(queen, Queen))
def test_player_move_already_in_check_fails(self): """Test trying to move when you're in check and still in check after the move fails""" white_bishop_position = Position(3, 3) black_rook = Rook(None, constants.BLACK, Position(4, 5)) white_bishop = Rook(None, constants.WHITE, white_bishop_position) test_board = { 'white_pieces': [white_bishop], 'black_pieces': [black_rook] } board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board) with pytest.raises(InvalidMoveError): game.player_move(white_bishop_position, Position(6, 6))
def test_player_move_capture_success(self): """Test a basic capture of a piece works as expected""" white_rook_position = Position(2, 0) black_rook_position = Position(2, 5) black_rook = Rook(None, constants.BLACK, black_rook_position) white_rook = Rook(None, constants.WHITE, white_rook_position) test_board = { 'white_pieces': [white_rook], 'black_pieces': [black_rook] } board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board) game.player_move(white_rook_position, black_rook_position) self.assertEqual(white_rook.position, black_rook_position) self.assertIsNone(black_rook.position) self.assertNotIn(black_rook, board.black_pieces)
def test_player_move_en_passant_fails(self): """Test calling making an en passant move is fails when not valid""" white_position = Position(0, 3) black_position = Position(1, 3) white_rook = Rook(None, constants.WHITE, white_position) black_pawn = Pawn(None, constants.BLACK, black_position) test_board = { 'white_pieces': [white_rook], 'black_pieces': [black_pawn] } board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board, turn=constants.BLACK, history=get_pawn_move_record_passant( white_position, Position(0, 1))) en_passant_position = Position(0, 2) with pytest.raises(InvalidMoveError): game.player_move(black_position, en_passant_position)
def test_player_move_en_passant(self): """Test calling making an en passant move is successful if valid""" white_position = Position(0, 3) black_position = Position(1, 3) white_pawn = Pawn(None, constants.WHITE, white_position) black_pawn = Pawn(None, constants.BLACK, black_position) test_board = { 'white_pieces': [white_pawn], 'black_pieces': [black_pawn] } board = set_up_test_board(new_game=False, test_board=test_board) game = set_up_test_game(board, turn=constants.BLACK, history=get_pawn_move_record_passant( white_position, Position(0, 1))) en_passant_position = Position(0, 2) game.player_move(black_position, en_passant_position) self.assertEqual(black_pawn.position, en_passant_position) self.assertIsNone(white_pawn.position) self.assertNotIn(white_pawn, board.white_pieces)