Пример #1
0
    def test_board(self):
        repo = Repository()
        board = BoardService(repo)

        self.assertEqual(len(board.get_board()), 6)
        self.assertEqual(len(board.get_board()[randint(0, 5)]), 6)

        board.make_move(0, 0, 1)
        self.assertEqual(board.get_cell(0, 0), Cell(board.cell_id(0, 0), 2))
        for i in [0, 1]:
            for j in [0, 1]:
                if i + j:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        board.make_move(2, 2, 2)
        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3))
        for i in [1, 3]:
            for j in [1, 3]:
                if i != 2 or j != 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        self.assertRaises(AttributeError, lambda: board.make_move(1, 1, 1))
        self.assertRaises(AttributeError, lambda: board.make_move(6, 1, 1))
        self.assertRaises(AttributeError, lambda: board.make_move(4, 4, 3))

        self.assertFalse(board.ctrl_win())
        board.make_move(3, 0, 2)
        board.make_move(5, 1, 2)
        board.make_move(4, 4, 2)
        board.make_move(1, 4, 2)
        self.assertFalse(board.ctrl_win())
        board.make_move(0, 2, 2)
        self.assertTrue(board.ctrl_win())
Пример #2
0
    def test_board_undo(self):
        repo = Repository()
        history_repo = Repository()
        history_service = HistoryService(history_repo)
        board = BoardService(repo, history_service)

        self.assertRaises(AttributeError, lambda: history_service.undo())
        self.assertRaises(AttributeError, lambda: history_service.redo())
        board.make_move(2, 2, 2, assignUndo=True)

        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3))
        for i in [1, 3]:
            for j in [1, 3]:
                if i != 2 or j != 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        history_service.undo()

        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 0))
        for i in [1, 3]:
            for j in [1, 3]:
                self.assertEqual(board.get_cell(i, j),
                                 Cell(board.cell_id(i, j), 0))

        history_service.redo()
        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3))
        for i in [1, 3]:
            for j in [1, 3]:
                if i != 2 or j != 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        board.make_move(0, 0, 2, assignUndo=True)
        history_service.undo()
        for i in [0, 1, 2]:
            for j in [0, 1, 2]:
                if not i or not j:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 0))
                elif i == 2 and j == 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 3))
                else:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))