def test_in_bounds(self): self.assertTrue( all(position.in_bounds for position in self.flat_positions)) self.assertFalse( any(Position(-1, col).in_bounds for col in range(-1, 9))) self.assertFalse( any(Position(8, col).in_bounds for col in range(-1, 9))) self.assertFalse( any(Position(row, -1).in_bounds for row in range(-1, 9))) self.assertFalse( any(Position(row, 8).in_bounds for row in range(-1, 9)))
def test_from_notation(self): for row, notation_row in enumerate(self.notation): for col, square in enumerate(notation_row): board_piece = self.board[row][col] if square == "": self.assertEqual(board_piece, None) else: self.assertEqual(Position(row, col), board_piece.position) self.assertEqual(Color.from_notation(square[0]), board_piece.color) self.assertEqual(Piece.from_notation(square[1]), type(board_piece))
def test_eq_hash(self): for row, position_row in enumerate(self.positions): for col, position in enumerate(position_row): others = self.flat_positions - {position} # test equality self.assertEqual(position, Position(row, col)) self.assertFalse(any(position == other for other in others)) # test hash self.assertIn(position, self.flat_positions) self.assertNotIn(position, others)
def test_add(self): board = Board() self.assertTrue(all(square == None for square in board.squares())) board.add(Color.white, Pawn, Position(3, 4)) board.add(Color.black, Pawn, Position(6, 6)) self.assertEqual(Color.white, board.at(Position(3, 4)).color) self.assertEqual(Pawn, type(board.at(Position(3, 4)))) self.assertEqual(Color.black, board.at(Position(6, 6)).color) self.assertEqual(Pawn, type(board.at(Position(6, 6)))) self.assertEqual(2, len(list(board.pieces()))) for position in board.positions(): if position not in (Position(3, 4), Position(6, 6)): self.assertIsNone(board.at(position))
def setUp(self): self.positions = [[Position(row, col) for col in range(8)] for row in range(8)] self.flat_positions = set(itertools.chain(*self.positions)) self.notations = [["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1"], ["A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2"], ["A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3"], ["A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4"], ["A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5"], ["A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6"], ["A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7"], ["A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8"]]
def test_possible_moves(self): upper_moves = self.upper_rook.possible_moves self.assertEqual(10, len(upper_moves)) self.assertIn(Position(0, 1), upper_moves) # move up self.assertIn(Position(1, 0), upper_moves) # move left self.assertContainsAll(upper_moves, [Position(2, 1), Position(3, 1)]) # moves down self.assertContainsAll(upper_moves, [Position(1, col) for col in range(2, 8)]) # moves right left_moves = self.left_rook.possible_moves self.assertEqual(7, len(left_moves)) self.assertContainsAll(left_moves, [Position(3, 1), Position(2, 1)]) # moves up self.assertIn(Position(4, 0), left_moves) # moves left self.assertContainsAll(left_moves, [Position(row, 1) for row in range(5, 8)]) # moves down self.assertIn(Position(4, 2), left_moves) # moves right right_moves = self.right_rook.possible_moves self.assertEqual(10, len(right_moves)) self.assertContainsAll(right_moves, [Position(row, 5) for row in range(4)]) # moves up self.assertIn(Position(4, 4), right_moves) # moves left self.assertContainsAll(right_moves, [Position(row, 5) for row in range(5, 8)]) # moves down self.assertContainsAll(right_moves, [Position(4, col) for col in range(6, 8)]) # moves right middle_moves = self.middle_rook.possible_moves self.assertEqual(9, len(middle_moves)) self.assertContainsAll(middle_moves, [Position(row, 3) for row in range(4)]) # moves up self.assertIn(Position(4, 2), middle_moves) # moves left self.assertIn(Position(4, 4), middle_moves) # moves right self.assertContainsAll(middle_moves, [Position(row, 3) for row in range(5, 8)]) # moves down lower_moves = self.lower_rook.possible_moves self.assertEqual(14, len(lower_moves)) self.assertContainsAll(lower_moves, [Position(row, 7) for row in range(6)]) # moves up self.assertContainsAll(lower_moves, [Position(6, col) for col in range(7)]) # moves left self.assertIn(Position(7, 7), lower_moves) # moves down
def position(self): return Position(self.row, self.col)
def setUp(self): self.board = Board() self.wpawn = self.board.add(Color.white, Pawn, Position(1, 4)) self.bpawn = self.board.add(Color.black, Pawn, Position(2, 3))
def test_possible_moves(self): self.assertListEqual(self.wpawn.possible_moves, [Position(2, 4), Position(3, 4)]) self.assertListEqual(self.bpawn.possible_moves, [Position(1, 3)])