Exemplo n.º 1
0
class TicTacToeTest(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.controller = Controller(TicTacToe, False)

    def setUp(self):
        self.controller.reset()

    def test_initial_state(self):
        """ . . .
            . . .
            . . . """
        grid = self.controller.grid
        self.assertEqual(3, len(grid))
        for row in grid:
            self.assertEqual(3, len(row))
            self.assertNotIn(None, row)
            for cell in row:
                self.assertNotIsInstance(cell, TicTacToe.Mark)
                self.assertIsInstance(cell[0], int)
                self.assertIsInstance(cell[1], int)

    def test_internals(self):
        self.assertEqual(9, len(self.controller.nexts))
        self.assertIsNone(self.controller.prev)

    def test_illegal_transition(self):
        self.assertRaises(IndexError, self.controller.make_move, 42)
        self.assertRaises(IndexError, self.controller.make_move, 9)

    def grid_repr_compare(self):
        current_state = self.controller.grid
        str_repr = repr(self.controller)
        cell = lambda i: current_state[i//3][i%3]
        for i in range(9):
            char = str_repr[i*2]
            if char in ('x', 'o'):
                self.assertEqual(char, cell(i).value)
            elif char == '.' and cell(i) is not None:
                self.assertIsInstance(cell(i), self.controller.GridItem)
        self.assertEqual(6, str_repr.count(' '))
        self.assertEqual(2, str_repr.count('\n'))

    def test_initial_undo_move(self):
        self.controller.undo_move()

    def test_gameplay_01(self):
        """ . . X
            o X o
            X o x """
        self.make_moves([8, 7, 6, 5, 4, 3, 2])
        self.grid_repr_compare()
        self.assertEqual(0, len(self.controller.nexts))

    def test_gameplay_01_undo_move_works(self):
        self.test_gameplay_01()
        self.controller.undo_move()
        self.assertEqual(3, len(self.controller.nexts))

    def test_gameplay_01_not_offering_transitions_beyond_terminal_state(self):
        self.test_gameplay_01()
        self.assertIsNone(self.controller.grid[0][0])
        self.assertIsNone(self.controller.grid[0][1])

    def test_gameplay_02(self):
        """ x x o
            o x x
            x o o """
        self.make_moves([4, 2, 3, 2, 1, 2, 0, 1, 0])
        self.grid_repr_compare()
        self.assertEqual(0, len(self.controller.nexts))

    def test_gameplay_02_undo_move_works(self):
        self.test_gameplay_02()
        self.controller.undo_move()
        self.assertEqual(1, len(self.controller.nexts))

    def test_gameplay_02_make_move_on_a_full_grid_raises(self):
        self.test_gameplay_02()
        position_before_make_move = self.controller._position
        with self.assertRaises(IndexError):
            self.controller.make_move(0)
        self.assertEqual(position_before_make_move, self.controller._position)

    def test_gameplay_02_AI_move_on_a_full_grid_does_nothing(self):
        self.test_gameplay_02()
        position_before_ai_move = self.controller._position
        self.controller.ai_move()
        self.assertEqual(position_before_ai_move, self.controller._position)

    def test_gameplay_03(self):
        """ x . .
            o x o
            x o x """
        self.make_moves([4, 3, 0, 4, 3, 2, 2])
        self.grid_repr_compare()
        self.assertEqual(0, len(self.controller.nexts))

    def test_gameplay_03_undo_move_works(self):
        self.test_gameplay_03()
        self.controller.undo_move()
        self.assertEqual(3, len(self.controller.nexts))

    def test_gameplay_03_make_move_after_terminal_state_raises(self):
        self.test_gameplay_03()
        position_before_make_move = self.controller._position
        with self.assertRaises(IndexError):
            self.controller.make_move(0)
        self.assertEqual(position_before_make_move, self.controller._position)

    def test_gameplay_03_AI_move_after_terminal_state_does_nothing(self):
        self.test_gameplay_03()
        position_before_ai_move = self.controller._position
        self.controller.ai_move()
        self.assertEqual(position_before_ai_move, self.controller._position)

    def test_gameplay_04(self):
        """ x . x
            o . o
            . o x """
        self.make_moves([0, 2, 1, 2, 4, 3])
        self.grid_repr_compare()

    def test_gameplay_04_transitions(self):
        self.test_gameplay_04()
        self.assertNotIn(None, self.controller.grid)
        self.assertIsNotNone(self.controller.grid[1][1])

    def test_gameplay_04_AI_picks_the_winning_move(self):
        self.test_gameplay_04()
        self.controller.ai_move()
        self.grid_repr_compare()
        self.assertIsNone(self.controller.grid[1][1])

    def make_moves(self, state_transitions):
        """ Carries out a sequence of state transitions (game moves). """
        while len(state_transitions) > 0:
            self.controller.make_move(state_transitions.pop(0))