Пример #1
0
    def testUndoDo(self):
        """
        Test an undo() do() sequence.
        """
        state = {"actions": 2}

        class Action(UndoableAction):

            def do(self):
                state["actions"] += 1

            def undo(self):
                state["actions"] -= 1

        stack = UndoableActionStack("meh")
        action1 = Action()
        action2 = Action()
        stack.push(action1)
        stack.push(action2)

        stack.undo()
        self.assertEqual(state["actions"], 0)

        stack.do()
        self.assertEqual(state["actions"], 2)
Пример #2
0
    def testUndoError(self):
        """
        Undo a stack containing a failing action.
        """
        state = {"done": True}

        def doneCb(action, value):
            state["done"] = value

        state["actions"] = 2

        class Action(UndoableAction):
            def undo(self):
                state["actions"] -= 1
                if state["actions"] == 1:
                    self.__class__.undo = self.__class__.undo_fail

                self._undone()

            def undo_fail(self):
                raise UndoError("meh")

        stack = UndoableActionStack("meh")
        stack.connect("done", doneCb)
        action1 = Action()
        action2 = Action()
        stack.push(action1)
        stack.push(action2)

        self.assertRaises(UndoError, stack.undo)
        self.assertEqual(state["actions"], 1)
        self.assertTrue(state["done"])
Пример #3
0
    def testUndoDo(self):
        """
        Test an undo() do() sequence.
        """
        state = {"done": True, "actions": 2}

        def doneCb(action, value):
            state["done"] = value

        state["done"] = 2

        class Action(UndoableAction):
            def do(self):
                state["actions"] += 1
                self._done()

            def undo(self):
                state["actions"] -= 1
                self._undone()

        stack = UndoableActionStack("meh")
        stack.connect("done", doneCb, True)
        stack.connect("undone", doneCb, False)
        action1 = Action()
        action2 = Action()
        stack.push(action1)
        stack.push(action2)

        stack.undo()
        self.assertEqual(state["actions"], 0)
        self.assertFalse(state["done"])

        stack.do()
        self.assertEqual(state["actions"], 2)
        self.assertTrue(state["done"])
Пример #4
0
    def testUndoDo(self):
        """
        Test an undo() do() sequence.
        """
        state = {"done": True, "actions": 2}

        def doneCb(action, value):
            state["done"] = value

        state["done"] = 2

        class Action(UndoableAction):
            def do(self):
                state["actions"] += 1
                self._done()

            def undo(self):
                state["actions"] -= 1
                self._undone()

        stack = UndoableActionStack("meh")
        stack.connect("done", doneCb, True)
        stack.connect("undone", doneCb, False)
        action1 = Action()
        action2 = Action()
        stack.push(action1)
        stack.push(action2)

        stack.undo()
        self.assertEqual(state["actions"], 0)
        self.assertFalse(state["done"])

        stack.do()
        self.assertEqual(state["actions"], 2)
        self.assertTrue(state["done"])
Пример #5
0
    def testUndoError(self):
        """
        Undo a stack containing a failing action.
        """
        state = {"done": True}

        def doneCb(action, value):
            state["done"] = value

        state["actions"] = 2

        class Action(UndoableAction):
            def undo(self):
                state["actions"] -= 1
                if state["actions"] == 1:
                    self.__class__.undo = self.__class__.undo_fail

                self._undone()

            def undo_fail(self):
                raise UndoError("meh")

        stack = UndoableActionStack("meh")
        stack.connect("done", doneCb)
        action1 = Action()
        action2 = Action()
        stack.push(action1)
        stack.push(action2)

        self.assertRaises(UndoError, stack.undo)
        self.assertEqual(state["actions"], 1)
        self.assertTrue(state["done"])
Пример #6
0
    def test_undo_error(self):
        """Checks undo a stack containing a failing action."""
        stack = UndoableActionStack("meh", mergeable=False)
        action1 = mock.Mock(spec=UndoableAction)
        action1.expand.return_value = False
        action2 = mock.Mock(spec=UndoableAction)
        action2.expand.return_value = False
        action2.undo.side_effect = UndoError("meh")
        action3 = mock.Mock(spec=UndoableAction)
        stack.push(action1)
        stack.push(action2)
        stack.push(action3)

        self.assertRaises(UndoError, stack.undo)
        self.assertEqual(action1.undo.call_count, 0)
        self.assertEqual(action2.undo.call_count, 1)
        self.assertEqual(action3.undo.call_count, 1)
Пример #7
0
    def testUndoError(self):
        """
        Undo a stack containing a failing action.
        """
        stack = UndoableActionStack("meh")
        action1 = mock.Mock(spec=UndoableAction)
        action2 = mock.Mock(spec=UndoableAction)
        action2.undo.side_effect = UndoError("meh")
        action3 = mock.Mock(spec=UndoableAction)
        stack.push(action1)
        stack.push(action2)
        stack.push(action3)

        self.assertRaises(UndoError, stack.undo)
        self.assertEqual(action1.undo.call_count, 0)
        self.assertEqual(action2.undo.call_count, 1)
        self.assertEqual(action3.undo.call_count, 1)
Пример #8
0
    def testUndoError(self):
        """
        Undo a stack containing a failing action.
        """
        stack = UndoableActionStack("meh")
        action1 = mock.Mock(spec=UndoableAction)
        action2 = mock.Mock(spec=UndoableAction)
        action2.undo.side_effect = UndoError("meh")
        action3 = mock.Mock(spec=UndoableAction)
        stack.push(action1)
        stack.push(action2)
        stack.push(action3)

        self.assertRaises(UndoError, stack.undo)
        self.assertEqual(action1.undo.call_count, 0)
        self.assertEqual(action2.undo.call_count, 1)
        self.assertEqual(action3.undo.call_count, 1)
Пример #9
0
    def test_expand(self):
        stack = UndoableActionStack("good one!")
        gobject = mock.Mock()
        stack.push(PropertyChangedAction(gobject, "field", 5, 7))
        stack.push(PropertyChangedAction(gobject, "field", 11, 13))
        self.assertEqual(len(stack.done_actions), 1, stack.done_actions)
        self.assertEqual(stack.done_actions[0].old_value, 5)
        self.assertEqual(stack.done_actions[0].new_value, 13)

        stack.push(PropertyChangedAction(gobject, "field2", 0, 1))
        self.assertEqual(len(stack.done_actions), 2, stack.done_actions)

        stack.push(PropertyChangedAction(mock.Mock(), "field", 0, 1))
        self.assertEqual(len(stack.done_actions), 3, stack.done_actions)
Пример #10
0
    def test_expand(self):
        stack = UndoableActionStack("good one!", mergeable=False)
        gobject = mock.Mock()
        stack.push(PropertyChangedAction(gobject, "field", 5, 7))
        stack.push(PropertyChangedAction(gobject, "field", 11, 13))
        self.assertEqual(len(stack.done_actions), 1, stack.done_actions)
        self.assertEqual(stack.done_actions[0].old_value, 5)
        self.assertEqual(stack.done_actions[0].new_value, 13)

        stack.push(PropertyChangedAction(gobject, "field2", 0, 1))
        self.assertEqual(len(stack.done_actions), 2, stack.done_actions)

        stack.push(PropertyChangedAction(mock.Mock(), "field", 0, 1))
        self.assertEqual(len(stack.done_actions), 3, stack.done_actions)
Пример #11
0
    def test_undo_do(self):
        """Checks an undo() and do() sequence."""
        state = {"actions": 2}

        class Action(UndoableAction):
            def do(self):
                state["actions"] += 1

            def undo(self):
                state["actions"] -= 1

        stack = UndoableActionStack("meh")
        action1 = Action()
        action2 = Action()
        stack.push(action1)
        stack.push(action2)

        stack.undo()
        self.assertEqual(state["actions"], 0)

        stack.do()
        self.assertEqual(state["actions"], 2)