def testSetPieceAt(self): self.boardWidget.setPieceAt(chess.A4, chess.Piece(chess.PAWN, chess.WHITE)) w = self.boardWidget.cellWidgetAtSquare(chess.A4) self.assertTrue(w.isPiece()) self.assertFalse(w.isHighlighted()) self.assertFalse(w.isMarked()) self.assertTrue(w.isCheckable()) self.assertEqual(w.objectName(), "cell_white_pawn") self.boardWidget = hichess.BoardWidget(sides=hichess.ONLY_WHITE_SIDE) self.boardWidget.setPieceAt(chess.A4, chess.Piece(chess.PAWN, chess.WHITE)) w = self.boardWidget.cellWidgetAtSquare(chess.A4) self.assertTrue(w.isPiece()) self.assertFalse(w.isHighlighted()) self.assertFalse(w.isMarked()) self.assertTrue(w.isCheckable()) self.assertEqual(w.objectName(), "cell_white_pawn") self.boardWidget = hichess.BoardWidget(fen=chess.STARTING_FEN, sides=hichess.ONLY_BLACK_SIDE) self.boardWidget.setPieceAt(chess.A4, chess.Piece(chess.PAWN, chess.WHITE)) w = self.boardWidget.cellWidgetAtSquare(chess.A4) self.assertTrue(w.isPiece()) self.assertFalse(w.isHighlighted()) self.assertFalse(w.isMarked()) self.assertEqual(w.objectName(), "cell_white_pawn") self.boardWidget = hichess.BoardWidget(sides=hichess.ONLY_BLACK_SIDE) self.boardWidget.setPieceAt(chess.A4, chess.Piece(chess.PAWN, chess.BLACK)) w = self.boardWidget.cellWidgetAtSquare(chess.A4) self.assertTrue(w.isPiece()) self.assertFalse(w.isHighlighted()) self.assertFalse(w.isMarked()) self.assertTrue(w.isCheckable()) self.assertEqual(w.objectName(), "cell_black_pawn") self.boardWidget = hichess.BoardWidget(sides=hichess.BOTH_SIDES) self.boardWidget.setPieceAt(chess.A4, chess.Piece(chess.PAWN, chess.BLACK)) w = self.boardWidget.cellWidgetAtSquare(chess.A4) self.assertTrue(w.isPiece()) self.assertFalse(w.isHighlighted()) self.assertFalse(w.isMarked()) self.assertEqual(w.objectName(), "cell_black_pawn")
def testInit(self): boardWidgetCopy = self.boardWidget self.assertEqual(self.boardWidget.board.fen(), chess.STARTING_FEN) self.assertFalse(self.boardWidget.flipped) self.assertEqual(self.boardWidget.accessibleSides, hichess.NO_SIDE) self.assertDictEqual(self.boardWidget.board.piece_map(), chess.Board().piece_map()) self.assertEqual( len( list( filter(hichess.CellWidget.isPiece, self.boardWidget.cellWidgets()))), 32) self.assertEqual( len( list( filter(hichess.CellWidget.isPlain, self.boardWidget.cellWidgets()))), 32) self.boardWidget = hichess.BoardWidget(fen=None, flipped=False, sides=hichess.NO_SIDE) self.assertEqual(self.boardWidget.board.fen(), chess.Board(None).fen()) self.assertFalse(self.boardWidget.flipped) self.assertEqual(self.boardWidget.accessibleSides, hichess.NO_SIDE) self.assertFalse( list( filter(hichess.CellWidget.isPiece, self.boardWidget.cellWidgets()))) self.assertEqual( len( list( filter(hichess.CellWidget.isPlain, self.boardWidget.cellWidgets()))), 64) self.boardWidget = hichess.BoardWidget(fen=chess.STARTING_FEN, flipped=True, sides=hichess.BOTH_SIDES) self.assertEqual(self.boardWidget.board.fen(), chess.STARTING_FEN) self.assertTrue(self.boardWidget.flipped) self.assertEqual(self.boardWidget.accessibleSides, hichess.BOTH_SIDES) for i in range(64): w1 = self.boardWidget.cellWidgetAtSquare(i) w2 = boardWidgetCopy.cellWidgetAtSquare(i) self.assertEqual(w1.isPlain(), w2.isPlain()) self.assertEqual(w1.isPiece(), w2.isPiece()) self.assertEqual(w1.isHighlighted(), w2.isHighlighted()) self.assertEqual(w1.isMarked(), w2.isMarked()) self.assertEqual(w1.objectName(), w2.objectName())
def testFlip(self): boardWidgetCopy = self.boardWidget self.boardWidget = hichess.BoardWidget(fen=chess.STARTING_FEN, flipped=False, sides=hichess.BOTH_SIDES) for i in range(64): w1 = self.boardWidget.cellWidgetAtSquare(i) w2 = boardWidgetCopy.cellWidgetAtSquare(i) self.assertEqual(w1.isPlain(), w2.isPlain()) self.assertEqual(w1.isPiece(), w2.isPiece()) self.assertEqual(w1.isHighlighted(), w2.isHighlighted()) self.assertEqual(w1.isMarked(), w2.isMarked()) self.assertEqual(w1.objectName(), w2.objectName()) self.boardWidget.flip() for i in range(64): w1 = self.boardWidget.cellWidgetAtSquare(i) w2 = boardWidgetCopy.cellWidgetAtSquare(i) self.assertEqual(w1.isPlain(), w2.isPlain()) self.assertEqual(w1.isPiece(), w2.isPiece()) self.assertEqual(w1.isHighlighted(), w2.isHighlighted()) self.assertEqual(w1.isMarked(), w2.isMarked()) self.assertEqual(w1.objectName(), w2.objectName())
def testPushForRaises(self): boardWidget = hichess.BoardWidget() illegalMoves = ["a1d6", "b2b6", "c2d5", "a7a6", "e1g1"] for uci in illegalMoves: with self.assertRaises(hichess.IllegalMove): boardWidget.push(chess.Move.from_uci(uci))
def testMakeMove(self): for gameName in os.listdir("games"): with open(f"games/{gameName}") as pgn: game = chess.pgn.read_game(pgn) self.boardWidget = hichess.BoardWidget(fen=game.board().fen(), flipped=False, sides=hichess.NO_SIDE) mockMoveMade = Mock() self.boardWidget.moveMade.connect(mockMoveMade) for move in game.mainline_moves(): san = self.boardWidget.board.san(move) self.boardWidget.makeMove(move) mockMoveMade.assert_called_with(san) w = self.boardWidget.cellWidgetAtSquare(move.to_square) self.assertTrue(w.isPiece()) colorName, pieceName = w.objectName().split('_')[1:] pieceType = chess.PIECE_TYPES[ chess.PIECE_NAMES.index(pieceName) - 1] self.assertEqual( self.boardWidget.board.piece_at( self.boardWidget.squareOf(w)), chess.Piece(pieceType, colorName == 'white')) self.assertEqual(mockMoveMade.call_count, len(self.boardWidget.board.move_stack))
def testSetPieceMap(self, mockSynchronize): self.boardWidget = hichess.BoardWidget(fen=None) mockSynchronize.assert_called_once() newPieceMap = chess.Board().piece_map() self.boardWidget.setPieceMap(newPieceMap) self.assertEqual(mockSynchronize.call_count, 2) self.assertDictEqual(self.boardWidget.board.piece_map(), newPieceMap)
def testSynchronize(self): self.boardWidget = hichess.BoardWidget(fen=None) self.assertFalse( list( filter(hichess.CellWidget.isPiece, self.boardWidget.cellWidgets()))) self.boardWidget.board.set_fen(chess.STARTING_FEN) self.boardWidget.synchronize() self.assertEqual(self.boardWidget.board.fen(), chess.Board.starting_fen) self.assertDictEqual(self.boardWidget.board.piece_map(), chess.Board().piece_map())
def setUp(self): self.boardWidget = hichess.BoardWidget(fen=chess.STARTING_FEN, flipped=False, sides=hichess.NO_SIDE)