Exemplo n.º 1
0
    def test_populate(self, clear_mock):
        """test the initial placing of pieces on the board."""
        num_rows = constants.STD_BOARD_WIDTH
        num_cols = constants.STD_BOARD_HEIGHT
        test_board = Board({'num_rows': num_rows, 'num_cols': num_cols})

        # raise if piece's indexes are out of bounds
        test_pieces_with_oob = [(Piece(ChessColor.WHITE), (num_rows, 0)),
                                (Piece(ChessColor.WHITE), (0, num_cols))]
        for pair in test_pieces_with_oob:
            with self.assertRaises(PiecePlacementException):
                test_board.populate([pair])
        clear_mock.assert_called()

        clear_mock.reset_mock()
        # raise if two pieces are populated to same square
        dup_piece_strings = ['w Kb2', 'w Qb2']
        dup_piece_list = [psns(s) for s in dup_piece_strings]
        with self.assertRaises(PiecePlacementException):
            test_board.populate(dup_piece_list)
        clear_mock.assert_called()

        good_test_strings = ['w Kd2', 'w Qb4', 'b h8', 'b h5']
        good_test_list = [psns(s) for s in good_test_strings]
        test_board.populate(good_test_list)
        for test_piece, coordinates in good_test_list:
            row_idx = coordinates[0]
            col_idx = coordinates[1]
            self.assertEqual(test_board.squares[row_idx][col_idx].piece,
                             test_piece)
Exemplo n.º 2
0
    def test_get_active_pieces(self):
        """Tests the get_active_pieces method."""
        num_rows = constants.STD_BOARD_WIDTH
        num_cols = constants.STD_BOARD_HEIGHT
        test_board = Board({'num_rows': num_rows, 'num_cols': num_cols})

        # test with empty piece list
        res = test_board.get_active_pieces()
        self.assertTupleEqual(res, ([], []))

        white_strings = ['w Kd2', 'w Qb4']
        black_strings = ['b h8', 'b h5']
        white_coord_map = [psns(s) for s in white_strings]
        black_coord_map = [psns(s) for s in black_strings]
        test_board.populate(white_coord_map + black_coord_map)

        white_mapping = [(p, test_board.squares[row][col])
                         for p, (row, col) in white_coord_map]
        black_mapping = [(p, test_board.squares[row][col])
                         for p, (row, col) in black_coord_map]

        res = test_board.get_active_pieces()
        self.assertTupleEqual(res, (white_mapping, black_mapping))