def test_en_passant_moves(self): """Test if conditions are met en passant moves are given in proper format""" black_pawn_position = Position(1, 3) white_pawn_position = Position(0, 3) black_pawn = Pawn(None, BLACK, black_pawn_position) white_pawn = Pawn(None, WHITE, white_pawn_position) test_board = { 'white_pieces': [white_pawn], 'black_pieces': [black_pawn] } board = set_up_test_board(test_board=test_board) self.assertEqual(len(black_pawn.en_passant_moves), 1) test_en_passant_move = { TYPE: EN_PASSANT, CAPTURE_PIECE: white_pawn } en_passant_move = black_pawn.en_passant_moves position_key = (0, 2) self.assertIn(position_key, en_passant_move.keys()) for key in test_en_passant_move.keys(): self.assertEqual(en_passant_move[position_key][key], test_en_passant_move[key] )
def test_in_check(self): """Test in_check returns true when king is in_check""" black_rook = Rook(None, BLACK, Position(4, 4)) test_board = { 'black_pieces': [black_rook] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertTrue(board.in_check(WHITE))
def test_castling_moves_both_sides(self): """Test king castling moves returns correct moves when castling is open on both sides""" king_side_rook = Rook(None, WHITE, Position(0, 0)) queen_side_rook = Rook(None, WHITE, Position(7, 0)) test_board = {'white_pieces': [king_side_rook, queen_side_rook]} board = set_up_test_board(new_game=False, test_board=test_board) white_king = board.get_piece(Position(4, 0)) self.assertEqual(len(white_king.castling_moves), 2)
def test_is_open(self): """Test is open returns true if square is open and false otherwise""" pawn = Pawn(None, WHITE, Position(1, 4)) test_board = { 'white_pieces': [pawn] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertTrue(board.is_open(Position(1, 1))) self.assertFalse(board.is_open(Position(1, 4)))
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_knight_moves_new_game(self): """Test valid moves for a black and a white knight in starting position""" board = set_up_test_board(new_game=True) white_knight = board.get_piece(Position(1, 0)) self.assertEqual(len(white_knight.moves), 2) self.assertIn(Position(0, 2), white_knight.moves) self.assertIn(Position(2, 2), white_knight.moves) black_knight = board.get_piece(Position(1, 7)) self.assertEqual(len(black_knight.moves), 2) self.assertIn(Position(0, 5), black_knight.moves) self.assertIn(Position(2, 5), black_knight.moves)
def test_white_pawn_moves_from_start(self): """Test pawn moves returns the correct moves when a pawn is in the starting position""" pawn = Pawn(None, WHITE, Position(1, 1)) test_board = { 'white_pieces': [pawn] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(pawn.moves), 2) self.assertIn(Position(1, 2), pawn.moves) self.assertIn(Position(1, 3), pawn.moves)
def test_white_pawn_moves_one_enemy(self): """Test pawn moves returns correct moves with one enemy on a diagonal square""" pawn = Pawn(None, WHITE, Position(1, 1)) enemy = Bishop(None, BLACK, Position(2, 2)) test_board = { 'white_pieces': [pawn], 'black_pieces': [enemy] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(pawn.moves), 3) self.assertIn(Position(2, 2), pawn.moves)
def test_en_passant_moves_empty(self): """Test en passant moves empty if piece in attacked position is not a pawn""" black_pawn_position = Position(1, 3) white_knight_position = Position(0, 3) black_pawn = Pawn(None, BLACK, black_pawn_position) white_knight = Knight(None, WHITE, white_knight_position) test_board = { 'white_pieces': [white_knight], 'black_pieces': [black_pawn] } board = set_up_test_board(test_board=test_board) self.assertEqual(0, len(black_pawn.en_passant_moves))
def test_castling_moves_empty_when_attacked(self): """Test castling moves is empty when an enemy is attacking a square in between king and rook""" black_knight = Knight(None, BLACK, Position(2, 2)) white_rook = Rook(None, WHITE, Position(0, 0)) white_rook2 = Rook(None, WHITE, Position(7, 0)) test_board = { 'white_pieces': [white_rook, white_rook2], 'black_pieces': [black_knight] } board = set_up_test_board(new_game=False, test_board=test_board) white_king = board.get_piece(Position(4, 0)) self.assertEqual(len(white_king.castling_moves), 1)
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_is_enemy_pawn(self): """Test is_enemy_pawn returns True if enemy pawn and False otherwise""" black_pawn_position = Position(1, 4) black_rook_position = Position(2, 4) black_pawn = Pawn(None, BLACK, black_pawn_position) black_rook = Rook(None, BLACK, black_rook_position) test_board = { 'black_pieces': [black_rook, black_pawn] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertTrue(board.is_enemy_pawn(black_pawn_position, WHITE)) self.assertFalse(board.is_enemy_pawn(black_rook_position, WHITE))
def test_is_enemy(self): """Test is enemy returns True for enemy and false otherwise""" pawn = Pawn(None, WHITE, Position(1, 4)) pawn_black = Pawn(None, BLACK, Position(6, 4)) test_board = { 'white_pieces': [pawn], 'black_pieces': [pawn_black] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertTrue(board.is_enemy(Position(1, 4), BLACK)) self.assertFalse(board.is_enemy(Position(1, 4), WHITE)) self.assertTrue(board.is_enemy(Position(6, 4), WHITE)) self.assertFalse(board.is_enemy(Position(6, 4), BLACK))
def test_knight_moves_open(self): """Test valid moves for a knight on an open board with no pieces""" white_knight = Knight(None, WHITE, Position(3, 3)) test_board = { 'white_pieces': [white_knight] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(white_knight.moves), 8) moves = [ Position(5, 2), Position(5, 4), Position(1, 2), Position(1, 4), Position(4, 1), Position(4, 5), Position(2, 5), Position(2, 1) ] for m in moves: self.assertIn(m, white_knight.moves)
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_white_bishop_moves_random(self): """Test bishop moves returns correct moves when, friendlies and enemies are on it's lines""" white_bishop = Bishop(None, WHITE, Position(4, 4)) board = set_up_test_board(new_game=True) board.white_pieces.append(white_bishop) white_bishop.board = board self.assertEqual(len(white_bishop.moves), 8) moves = [ Position(3, 5), Position(2, 6), Position(5, 5), Position(6, 6), Position(5, 3), Position(6, 2), Position(3, 3), Position(2, 2) ] for m in moves: self.assertIn(m, white_bishop.moves)
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_white_bishop_moves_empty_board(self): """Test bishop moves returns correct moves when only dealing with empty squares""" white_bishop = Bishop(None, WHITE, Position(4, 4)) test_board = { 'white_pieces': [white_bishop] } board = set_up_test_board(test_board=test_board) self.assertEqual(len(white_bishop.moves), 13) moves = [ Position(3, 5), Position(2, 6), Position(1, 7), Position(5, 5), Position(6, 6), Position(7, 7), Position(5, 3), Position(6, 2), Position(7, 1), Position(3, 3), Position(2, 2), Position(1, 1), Position(0, 0) ] for m in moves: self.assertIn(m, white_bishop.moves)
def test_king_moves_open_board(self): """Test valid moves for king on an open board with no pieces""" white_king = King(None, WHITE, Position(4, 4)) test_board = {'white_pieces': [white_king]} board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(white_king.moves), 8) moves = [ Position(3, 4), Position(3, 5), Position(4, 5), Position(5, 5), Position(5, 4), Position(5, 3), Position(4, 3), Position(3, 3) ] for m in moves: self.assertIn(m, white_king.moves)
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_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_knight_moves_enemy_and_friendly(self): """Test valid moves for a knight with enemy and friendly pieces in attacked squares""" white_knight = Knight(None, WHITE, Position(3, 3)) black_knight = Knight(None, BLACK, Position(2, 1)) white_knight2 = Knight(None, WHITE, Position(5, 4)) white_knight3 = Knight(None, WHITE, Position(5, 2)) test_board = { 'white_pieces': [white_knight, white_knight2, white_knight3], 'black_pieces': [black_knight] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(white_knight.moves), 6) moves = [ Position(1, 2), Position(1, 4), Position(4, 1), Position(4, 5), Position(2, 5), Position(2, 1) ] for m in moves: self.assertIn(m, white_knight.moves)
def test_get_moves(self): """Test the get moves method returns the correct moves""" black_pawn = Pawn(None, BLACK, Position(2, 3)) black_rook = Rook(None, BLACK, Position(4, 4)) test_board = { 'black_pieces': [black_rook, black_pawn] } board = set_up_test_board(new_game=False, test_board=test_board) moves = board.get_moves(BLACK) for piece in test_board['black_pieces']: self.assertIn(piece, moves.keys()) for move in black_pawn.moves: self.assertIn(move, moves[black_pawn]['moves']) for move in black_pawn.en_passant_moves: self.assertIn(move, moves[black_pawn]['special_moves']) for move in black_rook.moves: self.assertIn(move, moves[black_rook]['moves'])
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)
def test_castling_kingside_only(self): """Test castling moves only contains kingside castle, when pieces are in the way of a queenside castle""" queen_side_rook = Rook(None, WHITE, Position(0, 0)) king_side_rook = Rook(None, WHITE, Position(7, 0)) white_bishop = Bishop(None, WHITE, Position(2, 0)) test_board = { 'white_pieces': [king_side_rook, queen_side_rook, white_bishop] } board = set_up_test_board(new_game=False, test_board=test_board) white_king = board.get_piece(Position(4, 0)) self.assertEqual(len(white_king.castling_moves), 1) king_move_key = (6, 0) self.assertIn(king_move_key, white_king.castling_moves.keys()) test_castle_move = { TYPE: CASTLE, ROOK: king_side_rook, ROOK_TO: Position(5, 0) } for key in test_castle_move.keys(): self.assertEqual(test_castle_move[key], white_king.castling_moves[king_move_key][key])
def test_white_rook_moves_random(self): """Test bishop moves returns correct moves when, friendlies and enemies are on it's lines""" white_rook = Rook(None, WHITE, Position(4, 4)) board = set_up_test_board(new_game=True) board.white_pieces.append(white_rook) white_rook.board = board self.assertEqual(len(white_rook.moves), 11) moves = [ Position(4, 5), Position(4, 6), Position(4, 3), Position(4, 2), Position(5, 4), Position(6, 4), Position(7, 4), Position(3, 4), Position(2, 4), Position(1, 4), Position(0, 4) ] for m in moves: self.assertIn(m, white_rook.moves)
def test_white_are_bishop_and_rook_moves(self): """Test that queen moves are equal to the sum of bishop and rook moves from the same position""" white_bishop = Bishop(None, WHITE, Position(4, 4)) white_queen = Queen(None, WHITE, Position(4, 4)) white_rook = Rook(None, WHITE, Position(4, 4)) test_board1 = {'white_pieces': [white_bishop]} test_board2 = {'white_pieces': [white_rook]} test_board3 = {'white_pieces': [white_queen]} set_up_test_board(test_board=test_board1) set_up_test_board(test_board=test_board2) set_up_test_board(test_board=test_board3) bishop_rook_moves = white_bishop.moves + white_rook.moves self.assertEqual(len(bishop_rook_moves), len(white_queen.moves)) for m in bishop_rook_moves: self.assertIn(m, white_queen.moves)
def test_white_rook_moves_empty_board(self): """Test rook moves returns correct moves when only dealing with empty squares""" white_rook = Rook(None, WHITE, Position(4, 4)) test_board = {'white_pieces': [white_rook]} board = set_up_test_board(test_board=test_board) self.assertEqual(len(white_rook.moves), 13) moves = [ Position(5, 4), Position(6, 4), Position(7, 4), Position(3, 4), Position(2, 4), Position(1, 4), Position(0, 4), Position(4, 5), Position(4, 6), Position(4, 7), Position(4, 3), Position(4, 2), Position(4, 1) ] for m in moves: self.assertIn(m, white_rook.moves)