Example #1
0
    def test_check(self):
        """ Unit test for Match.check() method """
        test_match = Match()

        # craft scenario where black made a move that placed white king in check, but not checkmate
        pcs = {
            'black': {
                'initial': [(0, 0)],
                'final': [(4, 0)]
            },
            'white': {
                'initial': [(7, 4)],
                'final': [(4, 4)]
            }
        }
        test_match.chessboard = generate_scenario(pcs)
        test_match.turn = 'black'
        test_match.not_turn = 'white'

        # test Match can detect king is in check without checkmate
        test_match.check()
        self.assertTrue(test_match.incheck)
        self.assertFalse(test_match.checkmate)

        # craft complex checkmate scenario where black wins (involves 5 black pieces)
        pcs = {
            'black': {
                'initial': [(0, 0), (0, 2), (1, 3), (0, 5), (1, 6), (0, 6)],
                'final': [(4, 0), (2, 2), (2, 3), (2, 7), (4, 7), (1, 6)]
            },
            'white': {
                'initial': [(7, 4), (6, 3)],
                'final': [(4, 4), (5, 3)]
            }
        }
        test_match.chessboard = generate_scenario(pcs)
        test_match.black = 'black'
        test_match.check()

        # test Match can detect complex checkmate scenario
        self.assertTrue(test_match.checkmate)
Example #2
0
    def test_move(self):
        """ Unit test for Match.move() method """
        test_match = Match()

        # test success of moving pawn two spaces forward on first turn
        test_match.select_piece('d2')  # select white pawn
        self.assertTrue(test_match.move('d4'))  # move two spaces forward
        pawn = test_match.chessboard.get_piece(
            'd4')  # get occupant at new position
        self.assertIs(type(pawn), Pawn)  # ensure pawn is at the new position
        empty = test_match.chessboard.get_piece(
            'd2')  # get occupant at pawn's old position
        self.assertEqual(empty, 0)  # ensure pawn's old position is empty

        # test rejection of moving pawn two spaces forward after first turn
        self.assertFalse(test_match.move('d6'))

        # test success of moving pawn one space forward after first turn
        self.assertTrue(test_match.move('d5'))

        # craft scenario where moving a white pawn forward one space will result in white's king in check
        pcs = {
            'black': {
                'initial': [(0, 0)],
                'final': [(4, 0)]
            },
            'white': {
                'initial': [(7, 4), (6, 3)],
                'final': [(4, 4), (4, 3)]
            }
        }
        test_match.chessboard = generate_scenario(pcs)

        # test rejection of moving white pawn forward leaving white king in check
        test_match.select_piece('d4')  # select white pawn
        self.assertFalse(test_match.move('d5'))