Browse Source

Wrote tests for undomanager

Frytek 5 years ago
parent
commit
6738e98025

+ 3 - 12
PixiEditorDotNetCore3/Models/Controllers/UndoManager.cs

@@ -1,17 +1,8 @@
-using PixiEditor.Helpers;
-using PixiEditorDotNetCore3.Models.DataHolders;
-using System;
+using PixiEditorDotNetCore3.Models.DataHolders;
 using System.Collections.Generic;
-using System.ComponentModel;
 using System.Diagnostics;
-using System.Drawing.Printing;
-using System.IO;
 using System.Linq;
 using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows;
-using System.Windows.Media;
 
 namespace PixiEditorDotNetCore3.Models.Controllers
 {
@@ -101,14 +92,14 @@ namespace PixiEditorDotNetCore3.Models.Controllers
         /// <param name="oldValue">Old value of property.</param>
         /// <param name="newValue">New value of property.</param>
         /// <param name="undoDescription">Description of change.</param>
-        public static void AddUndoChange(string property, object oldValue, string undoDescription = "")
+        public static void AddUndoChange(string property, object oldValue, object newValue, string undoDescription = "")
         {
             if (_lastChangeWasUndo == false && RedoStack.Count > 0) //Cleares RedoStack if las move wasn't redo or undo and if redo stack is greater than 0
             {
                 RedoStack.Clear();
             }
             _lastChangeWasUndo = false;
-            UndoStack.Push(new Change(property, oldValue, undoDescription));
+            UndoStack.Push(new Change(property, oldValue, newValue, undoDescription));
             Debug.WriteLine("UndoStackCount: " + UndoStack.Count + " RedoStackCount: " + RedoStack.Count);
         }
 

+ 2 - 2
PixiEditorDotNetCore3/Models/DataHolders/Change.cs

@@ -18,12 +18,12 @@ namespace PixiEditorDotNetCore3.Models.DataHolders
 
         public string Property { get; set; }
 
-        public Change(string property, object oldValue, string description = "")
+        public Change(string property, object oldValue, object newValue, string description = "")
         {
             Property = property;
             OldValue = oldValue;
             Description = description;
-            NewValue = OldValue;
+            NewValue = newValue;
         }
 
         public Change()

+ 4 - 0
PixiEditorDotNetCore3/Models/DataHolders/StackEx.cs

@@ -25,6 +25,10 @@ namespace PixiEditorDotNetCore3.Models.DataHolders
             items.Clear();
         }
 
+        /// <summary>
+        /// Returns top object without deleting it.
+        /// </summary>
+        /// <returns>Returns n - 1 item from stack.</returns>
         public T Peek()
         {
             return items[items.Count - 1];

+ 81 - 0
PixiEditorTests/ModelsTests/ControllersTests/UndoManagerTests.cs

@@ -0,0 +1,81 @@
+using NUnit.Framework;
+using PixiEditorDotNetCore3.Models.Controllers;
+using PixiEditorDotNetCore3.Models.DataHolders;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace PixiEditorTests.ModelsTests.ControllersTests
+{
+    [TestFixture]
+    public class UndoManagerTests
+    {
+
+        public int ExampleProperty { get; set; } = 1;
+
+        [TestCase]
+        public void TestSetRoot()
+        {
+            UndoManager.SetMainRoot(this);
+            Assert.AreEqual(this, UndoManager.MainRoot);
+        }
+
+        [TestCase]
+        public void TestAddToUndoStack()
+        {
+            PrepareUnoManagerForTests();
+            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
+            Assert.IsTrue(UndoManager.UndoStack.Count == 1);
+            Assert.IsTrue((int)UndoManager.UndoStack.Peek().OldValue == ExampleProperty);
+        }
+
+        [TestCase]
+        public void TestThatUndoAddsToRedoStack()
+        {
+            PrepareUnoManagerForTests();
+            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
+            UndoManager.Undo();
+            Assert.IsTrue(UndoManager.RedoStack.Count == 1);
+        }
+
+        [TestCase]
+        public void TestUndo()
+        {
+            PrepareUnoManagerForTests();
+            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, 55));
+            ExampleProperty = 55;
+            UndoManager.Undo();
+            Assert.IsTrue((int)UndoManager.RedoStack.Peek().OldValue == ExampleProperty);
+        }
+
+
+        [TestCase]
+        public void TestThatRedoAddsToUndoStack()
+        {
+            PrepareUnoManagerForTests();
+            UndoManager.AddUndoChange(new Change("ExampleProperty", ExampleProperty, ExampleProperty));
+            UndoManager.Undo();
+            UndoManager.Redo();
+            Assert.IsTrue(UndoManager.UndoStack.Count == 1);
+        }
+
+        [TestCase]
+        public void TestRedo()
+        {
+            PrepareUnoManagerForTests();
+            ExampleProperty = 55;
+            UndoManager.AddUndoChange(new Change("ExampleProperty", 1, ExampleProperty));
+            UndoManager.Undo();
+            UndoManager.Redo();
+            Assert.IsTrue((int)UndoManager.UndoStack.Peek().NewValue == ExampleProperty);
+        }
+
+        private void PrepareUnoManagerForTests()
+        {
+            UndoManager.SetMainRoot(this);
+            UndoManager.UndoStack.Clear();
+            UndoManager.RedoStack.Clear();
+            ExampleProperty = 1;
+        }
+    }
+}