Browse Source

Added FindRootProcess tests

flabbet 4 years ago
parent
commit
50b7df8c53

+ 12 - 5
PixiEditor/Models/Controllers/UndoManager.cs

@@ -100,7 +100,7 @@ namespace PixiEditor.Models.Controllers
         private bool ChangeIsBlockedProperty(Change change)
         {
             return (change.Root != null || change.FindRootProcess != null)
-                && GetProperty(GetChangeRoot(change), change.Property) == newUndoChangeBlockedProperty;
+                && GetProperty(GetChangeRoot(change), change.Property).Item1 == newUndoChangeBlockedProperty;
         }
 
         private object GetChangeRoot(Change change)
@@ -110,12 +110,19 @@ namespace PixiEditor.Models.Controllers
 
         private void SetPropertyValue(object target, string propName, object value)
         {
-            PropertyInfo propertyToSet = GetProperty(target, propName);
+            var properties = GetProperty(target, propName);
+            PropertyInfo propertyToSet = properties.Item1;
             newUndoChangeBlockedProperty = propertyToSet;
-            propertyToSet.SetValue(target, value, null);
+            propertyToSet.SetValue(properties.Item2, value, null);
         }
 
-        private PropertyInfo GetProperty(object target, string propName)
+        /// <summary>
+        /// Gets property info for propName from target. Supports '.' format.
+        /// </summary>
+        /// <param name="target">A object where target can be found.</param>
+        /// <param name="propName">Name of property to get, supports nested property.</param>
+        /// <returns>PropertyInfo about property and target object where property can be found.</returns>
+        private Tuple<PropertyInfo, object> GetProperty(object target, string propName)
         {
             string[] bits = propName.Split('.');
             for (int i = 0; i < bits.Length - 1; i++)
@@ -124,7 +131,7 @@ namespace PixiEditor.Models.Controllers
                 target = propertyToGet.GetValue(target, null);
             }
 
-            return target.GetType().GetProperty(bits.Last());
+            return new Tuple<PropertyInfo, object>(target.GetType().GetProperty(bits.Last()), target);
         }
     }
 }

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

@@ -181,6 +181,58 @@ namespace PixiEditorTests.ModelsTests.ControllersTests
             Assert.Equal(newVal, TestPropClass.IntProperty);
         }
 
+        [Fact]
+        public void TestThatFindRootProcessWorks()
+        {
+            PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("IntProperty", 0, 5, FindRootProcess, null));
+
+            Change change = undoManager.UndoStack.Peek();
+
+            Assert.Equal(TestPropClass, change.FindRootProcess(change.FindRootProcessArgs));
+        }
+
+        [Fact]
+        public void TestThatUndoForFindRootProcessWorks()
+        {
+            PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("IntProperty", 0, 5, FindRootProcess, null));
+
+            TestPropClass.IntProperty = 5;
+
+            undoManager.Undo();
+
+            Assert.Equal(0, TestPropClass.IntProperty);
+        }
+
+        [Fact]
+        public void TestThatUndoAndRedoForFindRootProcessWorks()
+        {
+            PrepareUndoManagerForTest();
+            UndoManager undoManager = new UndoManager(this);
+
+            undoManager.AddUndoChange(new Change("IntProperty", 0, 5, FindRootProcess, null));
+
+            TestPropClass.IntProperty = 5;
+
+            undoManager.Undo();
+
+            Assert.Equal(0, TestPropClass.IntProperty);
+
+            undoManager.Redo();
+
+            Assert.Equal(5, TestPropClass.IntProperty);
+        }
+
+        private object FindRootProcess(object[] args)
+        {
+            return TestPropClass;
+        }
+
         private void ReverseProcess(object[] args)
         {
             ExampleProperty = (int)args[0];