Example #1
0
        public void ForceFIFO()
        {
            int            maxSize = 3;
            CommandHistory history = new CommandHistory(maxSize);

            // Add 3 commands
            TestCommand1 cmd1 = new TestCommand1();
            TestCommand2 cmd2 = new TestCommand2();
            TestCommand3 cmd3 = new TestCommand3();

            history.AddCommand(cmd1);
            history.AddCommand(cmd2);
            history.AddCommand(cmd3);
            Assert.AreEqual(3, history.NumCommands);

            // Force history to drop oldest item (FIFO)
            TestCommand4 cmd4 = new TestCommand4();

            history.AddCommand(cmd4);
            Assert.AreEqual(3, history.NumCommands);

            // Undo all commands
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand4", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand3", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand2", _message);

            // We've undone all the commands; test the boundary
            // by adding a new one
            TestCommand5 cmd5 = new TestCommand5();

            history.AddCommand(cmd5);
            Assert.AreEqual(1, history.NumCommands);
        }
		public void AddUndoRedo()
		{
			int maxSize = 10;
			CommandHistory history = new CommandHistory(maxSize);

			// Add 4 commands
			TestCommand1 cmd1 = new TestCommand1();
			TestCommand2 cmd2 = new TestCommand2();
			TestCommand3 cmd3 = new TestCommand3();
			TestCommand4 cmd4 = new TestCommand4();

			history.AddCommand(cmd1);
			history.AddCommand(cmd2);
			history.AddCommand(cmd3);
			history.AddCommand(cmd4);

			Assert.AreEqual(4, history.NumCommands);

			// Undo all commands we've added 
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand4", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand3", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand2", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand1", _message);

			// Try to undo two extra times, even though we should be at the beginning
			_message = String.Empty;
			history.Undo();
			Assert.AreEqual(String.Empty, _message);
			history.Undo();
			Assert.AreEqual(String.Empty, _message);

			// Redo all commands we've added
			history.Redo();
			Assert.AreEqual("Executed TestCommand1", _message);
			history.Redo();
			Assert.AreEqual("Executed TestCommand2", _message);
			history.Redo();
			Assert.AreEqual("Executed TestCommand3", _message);
			history.Redo();
			Assert.AreEqual("Executed TestCommand4", _message);

			// Try to redo two extra times, even though we should be at the end
			_message = String.Empty;
			history.Redo();
			Assert.AreEqual(String.Empty, _message);
			history.Redo();
			Assert.AreEqual(String.Empty, _message);

			// Add another command
			TestCommand5 cmd5 = new TestCommand5();
			history.AddCommand(cmd5);

			Assert.AreEqual(5, history.NumCommands);

			// Undo a couple times
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand5", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand4", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand3", _message);

			// Now add a new command
			TestCommand6 cmd6 = new TestCommand6();
			history.AddCommand(cmd6);

			Assert.AreEqual(3, history.NumCommands);
			
			// Try to redo again
			_message = String.Empty;
			history.Redo();
			Assert.AreEqual(String.Empty, _message);

			// Undo again
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand6", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand2", _message);
		}
		public void ForceFIFO()
		{
			int maxSize = 3;
			CommandHistory history = new CommandHistory(maxSize);

			// Add 3 commands
			TestCommand1 cmd1 = new TestCommand1();
			TestCommand2 cmd2 = new TestCommand2();
			TestCommand3 cmd3 = new TestCommand3();

			history.AddCommand(cmd1);
			history.AddCommand(cmd2);
			history.AddCommand(cmd3);
			Assert.AreEqual(3, history.NumCommands);

			// Force history to drop oldest item (FIFO)
			TestCommand4 cmd4 = new TestCommand4();
			history.AddCommand(cmd4);
			Assert.AreEqual(3, history.NumCommands);

			// Undo all commands
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand4", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand3", _message);
			history.Undo();
			Assert.AreEqual("Unexecuted TestCommand2", _message);

			// We've undone all the commands; test the boundary
			// by adding a new one
			TestCommand5 cmd5 = new TestCommand5();
			history.AddCommand(cmd5);
			Assert.AreEqual(1, history.NumCommands);
		}
Example #4
0
        public void AddUndoRedo()
        {
            int            maxSize = 10;
            CommandHistory history = new CommandHistory(maxSize);

            // Add 4 commands
            TestCommand1 cmd1 = new TestCommand1();
            TestCommand2 cmd2 = new TestCommand2();
            TestCommand3 cmd3 = new TestCommand3();
            TestCommand4 cmd4 = new TestCommand4();

            history.AddCommand(cmd1);
            history.AddCommand(cmd2);
            history.AddCommand(cmd3);
            history.AddCommand(cmd4);

            Assert.AreEqual(4, history.NumCommands);

            // Undo all commands we've added
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand4", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand3", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand2", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand1", _message);

            // Try to undo two extra times, even though we should be at the beginning
            _message = String.Empty;
            history.Undo();
            Assert.AreEqual(String.Empty, _message);
            history.Undo();
            Assert.AreEqual(String.Empty, _message);

            // Redo all commands we've added
            history.Redo();
            Assert.AreEqual("Executed TestCommand1", _message);
            history.Redo();
            Assert.AreEqual("Executed TestCommand2", _message);
            history.Redo();
            Assert.AreEqual("Executed TestCommand3", _message);
            history.Redo();
            Assert.AreEqual("Executed TestCommand4", _message);

            // Try to redo two extra times, even though we should be at the end
            _message = String.Empty;
            history.Redo();
            Assert.AreEqual(String.Empty, _message);
            history.Redo();
            Assert.AreEqual(String.Empty, _message);

            // Add another command
            TestCommand5 cmd5 = new TestCommand5();

            history.AddCommand(cmd5);

            Assert.AreEqual(5, history.NumCommands);

            // Undo a couple times
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand5", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand4", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand3", _message);

            // Now add a new command
            TestCommand6 cmd6 = new TestCommand6();

            history.AddCommand(cmd6);

            Assert.AreEqual(3, history.NumCommands);

            // Try to redo again
            _message = String.Empty;
            history.Redo();
            Assert.AreEqual(String.Empty, _message);

            // Undo again
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand6", _message);
            history.Undo();
            Assert.AreEqual("Unexecuted TestCommand2", _message);
        }