Exemple #1
0
 def _survey_changed(self):
     from apptools.undo.api import CommandStack
     self.current_survey_line = None
     self.current_survey_line_group = None
     self.selected_survey_lines = []
     # reset undo stack
     self.command_stack = CommandStack(undo_manager=self.undo_manager)
     self.undo_manager.active_stack = self.command_stack
 def setUp(self):
     self.command_stack = CommandStack()
     self.command_stack.push = MagicMock()
     self.component = Component(position=[50, 50], bounds=[100, 100])
     self.container = Container()
     self.container.add(self.component)
     self.tool = ResizeCommandTool(component=self.component,
                                   command_stack=self.command_stack)
     self.component.tools.append(self.tool)
Exemple #3
0
    def _command_stack_default(self):
        """ Trait initializer. """

        # We make sure the undo package is entirely optional.
        try:
            from apptools.undo.api import CommandStack
        except ImportError:
            return None

        return CommandStack(undo_manager=self.window.workbench.undo_manager)
Exemple #4
0
 def _command_stack_default(self):
     stack = CommandStack(undo_manager=self.undo_manager)
     self.undo_manager.active_stack = stack
     return stack
Exemple #5
0
    def setUp(self):
        self.stack = CommandStack()
        undo_manager = UndoManager()
        self.stack.undo_manager = undo_manager

        self.command = SimpleCommand()
Exemple #6
0
class TestCommandStack(unittest.TestCase):
    def setUp(self):
        self.stack = CommandStack()
        undo_manager = UndoManager()
        self.stack.undo_manager = undo_manager

        self.command = SimpleCommand()

    # Command pushing tests ---------------------------------------------------

    def test_empty_command_stack(self):
        with assert_n_commands_pushed(self.stack, 0):
            pass

    def test_1_command_pushed(self):
        with assert_n_commands_pushed(self.stack, 1):
            self.stack.push(self.command)

    def test_n_command_pushed(self):
        n = 4
        with assert_n_commands_pushed(self.stack, n):
            for i in range(n):
                self.stack.push(self.command)

    # Undo/Redo tests ---------------------------------------------------------

    def test_undo_1_command(self):
        with assert_n_commands_pushed_and_undone(self.stack, 1):
            self.stack.push(self.command)
            self.assertEqual(self.stack.undo_name, self.command.name)
            self.stack.undo()

    def test_undo_n_command(self):
        n = 4
        with assert_n_commands_pushed_and_undone(self.stack, n):
            for i in range(n):
                self.stack.push(self.command)

            for i in range(n):
                self.stack.undo()

    def test_undo_unnamed_command(self):
        unnamed_command = UnnamedCommand()
        with assert_n_commands_pushed(self.stack, 1):
            self.stack.push(unnamed_command)

            # But the command cannot be undone because it has no name
            self.assertEqual(self.stack.undo_name, "")
            # This is a no-op
            self.stack.undo()

    def test_undo_redo_1_command(self):
        with assert_n_commands_pushed(self.stack, 1):
            self.stack.push(self.command)
            self.stack.undo()
            self.stack.redo()

    # Macro tests -------------------------------------------------------------

    def test_define_macro(self):
        with assert_n_commands_pushed(self.stack, 1):
            add_macro(self.stack, num_commands=2)

    def test_undo_macro(self):
        with assert_n_commands_pushed_and_undone(self.stack, 1):
            # The 2 pushes are viewed as 1 command
            add_macro(self.stack, num_commands=2)
            self.stack.undo()

    # Cleanliness tests -------------------------------------------------------

    def test_empty_stack_is_clean(self):
        self.assertTrue(self.stack.clean)

    def test_non_empty_stack_is_dirty(self):
        self.stack.push(self.command)
        self.assertFalse(self.stack.clean)

    def test_make_clean(self):
        # This makes it dirty by default
        self.stack.push(self.command)
        # Make the current tip of the stack clean
        self.stack.clean = True
        self.assertTrue(self.stack.clean)

    def test_make_dirty(self):
        # Start from a clean state:
        self.stack.push(self.command)
        self.stack.clean = True

        self.stack.clean = False
        self.assertFalse(self.stack.clean)

    def test_save_push_undo_is_clean(self):
        self.stack.push(self.command)

        self.stack.clean = True
        self.stack.push(self.command)
        self.stack.undo()
        self.assertTrue(self.stack.clean)

    def test_save_push_save_undo_is_clean(self):
        self.stack.push(self.command)

        self.stack.clean = True
        self.stack.push(self.command)
        self.stack.clean = True
        self.stack.undo()
        self.assertTrue(self.stack.clean)

    def test_push_undo_save_redo_is_dirty(self):
        self.stack.push(self.command)
        self.stack.undo()
        self.stack.clean = True
        self.stack.redo()
        self.assertFalse(self.stack.clean)
Exemple #7
0
class TestCommandStack(unittest.TestCase):
    def setUp(self):
        self.stack = CommandStack()
        undo_manager = UndoManager()
        self.stack.undo_manager = undo_manager

        self.command = SimpleCommand()

    # Command pushing tests ---------------------------------------------------

    def test_empty_command_stack(self):
        with self.assert_n_commands_pushed(self.stack, 0):
            pass

    def test_1_command_pushed(self):
        with self.assert_n_commands_pushed(self.stack, 1):
            self.stack.push(self.command)

    def test_n_command_pushed(self):
        n = 4
        with self.assert_n_commands_pushed(self.stack, n):
            for i in range(n):
                self.stack.push(self.command)

    # Undo/Redo tests ---------------------------------------------------------

    def test_undo_1_command(self):
        with self.assert_n_commands_pushed_and_undone(self.stack, 1):
            self.stack.push(self.command)
            self.assertEqual(self.stack.undo_name, self.command.name)
            self.stack.undo()

    def test_undo_n_command(self):
        n = 4
        with self.assert_n_commands_pushed_and_undone(self.stack, n):
            for i in range(n):
                self.stack.push(self.command)

            for i in range(n):
                self.stack.undo()

    def test_undo_unnamed_command(self):
        unnamed_command = UnnamedCommand()
        with self.assert_n_commands_pushed(self.stack, 1):
            self.stack.push(unnamed_command)

            # But the command cannot be undone because it has no name
            self.assertEqual(self.stack.undo_name, "")
            # This is a no-op
            self.stack.undo()

    def test_undo_redo_1_command(self):
        with self.assert_n_commands_pushed(self.stack, 1):
            self.stack.push(self.command)
            self.stack.undo()
            self.stack.redo()

    # Macro tests -------------------------------------------------------------

    def test_define_macro(self):
        with self.assert_n_commands_pushed(self.stack, 1):
            add_macro(self.stack, num_commands=2)

    def test_undo_macro(self):
        with self.assert_n_commands_pushed_and_undone(self.stack, 1):
            # The 2 pushes are viewed as 1 command
            add_macro(self.stack, num_commands=2)
            self.stack.undo()

    # Cleanliness tests -------------------------------------------------------

    def test_empty_stack_is_clean(self):
        self.assertTrue(self.stack.clean)

    def test_non_empty_stack_is_dirty(self):
        self.stack.push(self.command)
        self.assertFalse(self.stack.clean)

    def test_make_clean(self):
        # This makes it dirty by default
        self.stack.push(self.command)
        # Make the current tip of the stack clean
        self.stack.clean = True
        self.assertTrue(self.stack.clean)

    def test_make_dirty(self):
        # Start from a clean state:
        self.stack.push(self.command)
        self.stack.clean = True

        self.stack.clean = False
        self.assertFalse(self.stack.clean)

    def test_save_push_undo_is_clean(self):
        self.stack.push(self.command)

        self.stack.clean = True
        self.stack.push(self.command)
        self.stack.undo()
        self.assertTrue(self.stack.clean)

    def test_save_push_save_undo_is_clean(self):
        self.stack.push(self.command)

        self.stack.clean = True
        self.stack.push(self.command)
        self.stack.clean = True
        self.stack.undo()
        self.assertTrue(self.stack.clean)

    def test_push_undo_save_redo_is_dirty(self):
        self.stack.push(self.command)
        self.stack.undo()
        self.stack.clean = True
        self.stack.redo()
        self.assertFalse(self.stack.clean)

    # Assertion helpers -------------------------------------------------------

    @contextmanager
    def assert_n_commands_pushed(self, stack, n):
        current_length = len(stack._stack)
        yield
        # N commands have been pushed...
        self.assertEqual(len(stack._stack), current_length + n)
        # ... and the state is at the tip of the stack...
        self.assertEqual(stack._index, current_length + n - 1)

    @contextmanager
    def assert_n_commands_pushed_and_undone(self, stack, n):
        current_length = len(stack._stack)
        yield
        # N commands have been pushed and then reverted. The stack still
        # contains the commands...
        self.assertEqual(len(stack._stack), n)
        # ... but we are back to the initial (clean) state
        self.assertEqual(stack._index, current_length - 1)
Exemple #8
0
 def _command_stack_default(self):
     """ Return the default undo manager """
     from apptools.undo.api import CommandStack
     command_stack = CommandStack()
     return command_stack
    def setUp(self):
        self.stack = CommandStack()
        undo_manager = UndoManager()
        self.stack.undo_manager = undo_manager

        self.command = SimpleCommand()
class TestCommandStack(unittest.TestCase):
    def setUp(self):
        self.stack = CommandStack()
        undo_manager = UndoManager()
        self.stack.undo_manager = undo_manager

        self.command = SimpleCommand()

    # Command pushing tests ---------------------------------------------------

    def test_empty_command_stack(self):
        with assert_n_commands_pushed(self.stack, 0):
            pass

    def test_1_command_pushed(self):
        with assert_n_commands_pushed(self.stack, 1):
            self.stack.push(self.command)

    def test_n_command_pushed(self):
        n = 4
        with assert_n_commands_pushed(self.stack, n):
            for i in range(n):
                self.stack.push(self.command)

    # Undo/Redo tests ---------------------------------------------------------

    def test_undo_1_command(self):
        with assert_n_commands_pushed_and_undone(self.stack, 1):
            self.stack.push(self.command)
            self.assertEqual(self.stack.undo_name, self.command.name)
            self.stack.undo()

    def test_undo_n_command(self):
        n = 4
        with assert_n_commands_pushed_and_undone(self.stack, n):
            for i in range(n):
                self.stack.push(self.command)

            for i in range(n):
                self.stack.undo()

    def test_undo_unnamed_command(self):
        unnamed_command = UnnamedCommand()
        with assert_n_commands_pushed(self.stack, 1):
            self.stack.push(unnamed_command)

            # But the command cannot be undone because it has no name
            self.assertEqual(self.stack.undo_name, "")
            # This is a no-op
            self.stack.undo()

    def test_undo_redo_1_command(self):
        with assert_n_commands_pushed(self.stack, 1):
            self.stack.push(self.command)
            self.stack.undo()
            self.stack.redo()

    # Macro tests -------------------------------------------------------------

    def test_define_macro(self):
        with assert_n_commands_pushed(self.stack, 1):
            add_macro(self.stack, num_commands=2)

    def test_undo_macro(self):
        with assert_n_commands_pushed_and_undone(self.stack, 1):
            # The 2 pushes are viewed as 1 command
            add_macro(self.stack, num_commands=2)
            self.stack.undo()

    # Cleanliness tests -------------------------------------------------------

    def test_empty_stack_is_clean(self):
        self.assertTrue(self.stack.clean)

    def test_non_empty_stack_is_dirty(self):
        self.stack.push(self.command)
        self.assertFalse(self.stack.clean)

    def test_make_clean(self):
        # This makes it dirty by default
        self.stack.push(self.command)
        # Make the current tip of the stack clean
        self.stack.clean = True
        self.assertTrue(self.stack.clean)

    def test_make_dirty(self):
        # Start from a clean state:
        self.stack.push(self.command)
        self.stack.clean = True

        self.stack.clean = False
        self.assertFalse(self.stack.clean)

    def test_save_push_undo_is_clean(self):
        self.stack.push(self.command)

        self.stack.clean = True
        self.stack.push(self.command)
        self.stack.undo()
        self.assertTrue(self.stack.clean)

    def test_save_push_save_undo_is_clean(self):
        self.stack.push(self.command)

        self.stack.clean = True
        self.stack.push(self.command)
        self.stack.clean = True
        self.stack.undo()
        self.assertTrue(self.stack.clean)

    def test_push_undo_save_redo_is_dirty(self):
        self.stack.push(self.command)
        self.stack.undo()
        self.stack.clean = True
        self.stack.redo()
        self.assertFalse(self.stack.clean)