public void TestNoChanges() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1, Dynamic = new DiffValueTypeA() { Text = "Test1" } }; var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // 6: BuildOrder+Name+Value+(Dynamic: Text+Text2+Text3) Assert.AreEqual(6, diffResultStripped.Count); // Check that everything is merging from asset2 Assert.True(diffResultStripped.All(node => node.ChangeType == Diff3ChangeType.MergeFromAsset2)); }
public void TestChangeOverrideToBaseSealed() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1 }; var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); newBaseAsset.Value = 3; var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); // Change base: Name to Base|Sealed // This should result into a reset of the value overriden in child // Make New on Name value on first element var objDesc = TypeDescriptorFactory.Default.Find(typeof(TestDiffAsset)); var memberDesc = objDesc.Members.First(t => t.Name == "Value"); newBaseAsset.SetOverride(memberDesc, OverrideType.Base | OverrideType.Sealed); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); // Check that merged result on Dynamic property is instance from asset2 var mergeResult = AssetMerge.Merge(diff, AssetMergePolicies.MergePolicyAsset2AsNewBaseOfAsset1); Assert.AreEqual(3, childAsset.Value); // Value is coming from base Assert.AreEqual(OverrideType.Base | OverrideType.Sealed, childAsset.GetOverride(memberDesc)); // Value is coming from base }
public void TestListWithIdsNoConflicts() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1 }; baseAsset.List.Add(new DiffComponent() { Name = "Test1", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test2", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test3", Position = new Vector4(1, 0, 0, 0) }); var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // Expecting only 2 diff from TestDiffAsset (3 properties + BuildOrder) Assert.AreEqual(4, diffResultStripped.Where(item => item.BaseNode.Parent?.Instance is TestDiffAsset).Count()); // Expecting 6 diffs for DiffComponent (3 elements, 5 properties (Name + Vector4)) Assert.AreEqual(3 * 5, diffResultStripped.Where(item => item.BaseNode.Parent?.Instance is DiffComponent || item.BaseNode.Parent?.Parent?.Instance is DiffComponent).Count()); // All changes must be from asset2 (considered as new base), as everything is setting base Assert.True(diffResultStripped.All(item => item.ChangeType == Diff3ChangeType.MergeFromAsset2)); foreach (var node in diffResultStripped.Where(item => item.BaseNode.Parent.Instance is DiffComponent)) { var base1 = (DiffComponent)node.BaseNode.Parent.Instance; var asset1 = (DiffComponent)node.Asset1Node.Parent.Instance; var asset2 = (DiffComponent)node.Asset2Node.Parent.Instance; var baseIndex = baseAsset.List.IndexOf(base1); var asset1Index = childAsset.List.IndexOf(asset1); var asset2Index = newBaseAsset.List.IndexOf(asset2); Assert.AreEqual(baseIndex, asset1Index); Assert.AreEqual(baseIndex, asset2Index); } }
public void TestListWithIdsChangeType() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1 }; baseAsset.List.Add(new DiffComponent() { Name = "Test1", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test2", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test3", Position = new Vector4(1, 0, 0, 0) }); // Change type of 2nd element in newBase list var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); newBaseAsset.List[1] = new DiffComponentSub() { Value = 1 }; var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffList = diffResult.Members.First(node => ((DataVisitMember)node.Asset1Node).MemberDescriptor.Name == "List"); // Check that we have only 3 items Assert.AreEqual(3, diffList.Items.Count); Assert.AreEqual(Diff3ChangeType.MergeFromAsset2, diffList.Items[1].ChangeType); var mergeResult = AssetMerge.Merge(diff, AssetMergePolicies.MergePolicyAsset2AsNewBaseOfAsset1); Assert.False(mergeResult.HasErrors); Assert.AreEqual(3, childAsset.List.Count); Assert.AreEqual("Test1", childAsset.List[0].Name); Assert.True(childAsset.List[1] is DiffComponentSub); Assert.AreEqual("Test3", childAsset.List[2].Name); }
public void TestWithNewTypeFromChild() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1, Dynamic = new DiffValueTypeA() { Text = "Test1" } }; var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); // Make New on Name value on first element var objDesc = TypeDescriptorFactory.Default.Find(typeof(TestDiffAsset)); var memberDesc = objDesc.Members.First(t => t.Name == "Dynamic"); childAsset.SetOverride(memberDesc, OverrideType.New); // Override Dynamic and change type childAsset.Dynamic = new DiffValueTypeB() { Value = 2 }; var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // Check at least one field has merge from asset 1 (Dynamic) Assert.True(diffResultStripped.Any(node => node.ChangeType == Diff3ChangeType.MergeFromAsset1)); // Check that merged result on Dynamic property is instance from asset2 var mergeResult = AssetMerge.Merge(diff, AssetMergePolicies.MergePolicyAsset2AsNewBaseOfAsset1); Assert.True(childAsset.Dynamic is DiffValueTypeB); Assert.AreEqual(2, ((DiffValueTypeB)childAsset.Dynamic).Value); }
public void TestWithNewTypeFromNewBase() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1, Dynamic = new DiffValueTypeA() { Text = "Test1" } }; var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); newBaseAsset.Dynamic = new DiffValueTypeB() { Value = 1 }; var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // Check that everything is merging from asset2 Assert.True(diffResultStripped.All(node => node.ChangeType == Diff3ChangeType.MergeFromAsset2)); // Check that merged result on Dynamic property is instance from asset2 var mergeResult = AssetMerge.Merge(diff, AssetMergePolicies.MergePolicyAsset2AsNewBaseOfAsset1); Assert.True(childAsset.Dynamic is DiffValueTypeB); Assert.AreEqual(1, ((DiffValueTypeB)childAsset.Dynamic).Value); }
public void TestListDiffWithIdsOrderChanged() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1 }; baseAsset.List.Add(new DiffComponent() { Name = "Test1", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test2", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test3", Position = new Vector4(1, 0, 0, 0) }); var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); // Swap elements in child asset var temp = childAsset.List[0]; childAsset.List[0] = childAsset.List[1]; childAsset.List[1] = temp; childAsset.List[0].Name = "Test21"; // Make New on Name value on first element var objDesc = TypeDescriptorFactory.Default.Find(typeof(DiffComponent)); var memberDesc = objDesc.Members.First(t => t.Name == "Name"); childAsset.List[0].SetOverride(memberDesc, OverrideType.New); // Perform the diff var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // Expecting only one field to be merged from asset1 (the new on Name property) var mergeFromAsset1 = diffResultStripped.Where(item => item.ChangeType == Diff3ChangeType.MergeFromAsset1).ToList(); Assert.AreEqual(1, mergeFromAsset1.Count); var nameMember = mergeFromAsset1[0].Asset1Node as DataVisitMember; Assert.NotNull(nameMember); Assert.AreEqual("Name", nameMember.MemberDescriptor.Name); // Check that DiffComponent are swapped for Asset1 but diff is able to recover this foreach (var node in diffResultStripped.Where(item => item.BaseNode.Parent.Instance is DiffComponent)) { var base1 = (DiffComponent)node.BaseNode.Parent.Instance; var asset1 = (DiffComponent)node.Asset1Node.Parent.Instance; var asset2 = (DiffComponent)node.Asset2Node.Parent.Instance; var baseIndex = baseAsset.List.IndexOf(base1); var asset1Index = childAsset.List.IndexOf(asset1); var asset2Index = newBaseAsset.List.IndexOf(asset2); Assert.AreEqual(baseIndex, asset2Index); switch (baseIndex) { // element 0 and 1 are swapped case 0: Assert.AreEqual(1, asset1Index); break; case 1: Assert.AreEqual(0, asset1Index); break; default: Assert.AreEqual(baseIndex, asset1Index); break; } } }
/// <summary> /// Merges the specified assets from <c>base</c> and <c>from2</c> into <c>from1</c>. /// </summary> /// <param name="assetDiff">A precomputed asset difference.</param> /// <param name="mergePolicy">The merge policy.</param> /// <param name="previewOnly">if set to <c>true</c> then the merge will not change the object.</param> /// <returns>MergePreviewResult.</returns> /// <exception cref="System.ArgumentNullException">assetDiff /// or /// mergePolicy</exception> public static MergeResult Merge(AssetDiff assetDiff, MergePolicyDelegate mergePolicy, bool previewOnly = false) { if (assetDiff == null) throw new ArgumentNullException("assetDiff"); if (mergePolicy == null) throw new ArgumentNullException("mergePolicy"); var allDiffs = assetDiff.Compute(); var diff3 = allDiffs.FindDifferences().ToList(); var result = new MergeResult(assetDiff.Asset1); // Try to merge foreach (var diff3Node in diff3) { Diff3ChangeType changeType; try { changeType = mergePolicy(diff3Node); if (changeType >= Diff3ChangeType.Conflict) { result.Error("Unresolved conflict [{0}] on node [{1}/{2}/{3}]", diff3Node.ChangeType, diff3Node.BaseNode, diff3Node.Asset1Node, diff3Node.Asset2Node); continue; } // If we are in preview only mode, just skip the update if (previewOnly) { continue; } object dataInstance; bool replaceValue; switch (changeType) { case Diff3ChangeType.MergeFromAsset2: // As we are merging into asset1, the only relevant changes can only come from asset2 dataInstance = diff3Node.Asset2Node != null ? diff3Node.Asset2Node.Instance : null; replaceValue = true; break; case Diff3ChangeType.Children: MergeContainer(diff3Node, out dataInstance); replaceValue = dataInstance != null; break; default: continue; } // Sets the value on the node if (replaceValue) diff3Node.ReplaceValue(dataInstance, node => node.Asset1Node); } catch (Exception ex) { result.Error("Unexpected error while merging [{0}] on node [{1}]", ex, diff3Node.ChangeType, diff3Node.InstanceType); break; } } if (!previewOnly) { foreach (var node in allDiffs.Asset1Node.Children(node => true)) { if (node.Instance is IDiffProxy) { ((IDiffProxy)node.Instance).ApplyChanges(); } } } return result; }
/// <summary> /// Merges the specified assets from <c>base</c> and <c>from2</c> into <c>from1</c>. /// </summary> /// <param name="assetDiff">A precomputed asset difference.</param> /// <param name="mergePolicy">The merge policy.</param> /// <param name="previewOnly">if set to <c>true</c> then the merge will not change the object.</param> /// <returns>MergePreviewResult.</returns> /// <exception cref="System.ArgumentNullException">assetDiff /// or /// mergePolicy</exception> public static MergeResult Merge(AssetDiff assetDiff, MergePolicyDelegate mergePolicy, bool previewOnly = false) { if (assetDiff == null) throw new ArgumentNullException("assetDiff"); if (mergePolicy == null) throw new ArgumentNullException("mergePolicy"); var allDiffs = assetDiff.Compute(); var diff3 = allDiffs.FindDifferences().ToList(); var result = new MergeResult(assetDiff.Asset1); // Try to merge foreach (var diff3Node in diff3) { Diff3ChangeType changeType; try { changeType = mergePolicy(diff3Node); if (changeType >= Diff3ChangeType.Conflict) { result.Error("Unresolved conflict [{0}] on node [{1}/{2}/{3}]", diff3Node.ChangeType, diff3Node.BaseNode, diff3Node.Asset1Node, diff3Node.Asset2Node); continue; } // If we are in preview only mode, just skip the update if (previewOnly) { continue; } object dataInstance = null; bool replaceValue = false; switch (changeType) { case Diff3ChangeType.MergeFromAsset2: // Because for collection, the merge is performed by the MergeContainer // Skip any merge for individual items, as they should have been merged by MergeContainer // TODO: This is a workaround as FindDifferences().ToList() is giving changes inside collection while we rebuild collection with MergeContainer if (diff3Node.Parent == null || diff3Node.Parent.Type != Diff3NodeType.Collection) { // As we are merging into asset1, the only relevant changes can only come from asset2 dataInstance = diff3Node.Asset2Node?.Instance; replaceValue = true; } break; case Diff3ChangeType.Children: MergeContainer(diff3Node, out dataInstance); replaceValue = dataInstance != null; break; default: continue; } // Sets the value on the node if (replaceValue) diff3Node.ReplaceValue(dataInstance, node => node.Asset1Node); // Applies the override for this node diff3Node.ApplyOverride(); } catch (Exception ex) { result.Error("Unexpected error while merging [{0}] on node [{1}]", ex, diff3Node.ChangeType, diff3Node.InstanceType); break; } } if (!previewOnly) { foreach (var node in allDiffs.Asset1Node.Children(node => true)) { if (node.Instance is IDiffProxy) { ((IDiffProxy)node.Instance).ApplyChanges(); } } } return result; }
public void TestListWithIdsNoConflicts() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1 }; baseAsset.List.Add(new DiffComponent() { Name= "Test1", Position = new Vector4(1, 0, 0, 0)}); baseAsset.List.Add(new DiffComponent() { Name = "Test2", Position = new Vector4(1, 0, 0, 0) }); baseAsset.List.Add(new DiffComponent() { Name = "Test3", Position = new Vector4(1, 0, 0, 0) }); var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // Expecting only 2 diff from TestDiffAsset (3 properties + BuildOrder) Assert.AreEqual(4, diffResultStripped.Where(item => item.BaseNode.Parent?.Instance is TestDiffAsset).Count()); // Expecting 6 diffs for DiffComponent (3 elements, 5 properties (Name + Vector4)) Assert.AreEqual(3 * 5, diffResultStripped.Where(item => item.BaseNode.Parent?.Instance is DiffComponent || item.BaseNode.Parent?.Parent?.Instance is DiffComponent).Count()); // All changes must be from asset2 (considered as new base), as everything is setting base Assert.True(diffResultStripped.All(item => item.ChangeType == Diff3ChangeType.MergeFromAsset2)); foreach (var node in diffResultStripped.Where(item => item.BaseNode.Parent.Instance is DiffComponent)) { var base1 = (DiffComponent)node.BaseNode.Parent.Instance; var asset1 = (DiffComponent)node.Asset1Node.Parent.Instance; var asset2 = (DiffComponent)node.Asset2Node.Parent.Instance; var baseIndex = baseAsset.List.IndexOf(base1); var asset1Index = childAsset.List.IndexOf(asset1); var asset2Index = newBaseAsset.List.IndexOf(asset2); Assert.AreEqual(baseIndex, asset1Index); Assert.AreEqual(baseIndex, asset2Index); } }
public void TestChangeOverrideToBaseSealed() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1 }; var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); newBaseAsset.Value = 3; var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); // Change base: Name to Base|Sealed // This should result into a reset of the value overriden in child // Make New on Name value on first element var objDesc = TypeDescriptorFactory.Default.Find(typeof(TestDiffAsset)); var memberDesc = objDesc.Members.First(t => t.Name == "Value"); newBaseAsset.SetOverride(memberDesc, OverrideType.Base|OverrideType.Sealed); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); // Check that merged result on Dynamic property is instance from asset2 var mergeResult = AssetMerge.Merge(diff, AssetMergePolicies.MergePolicyAsset2AsNewBaseOfAsset1); Assert.AreEqual(3, childAsset.Value); // Value is coming from base Assert.AreEqual(OverrideType.Base|OverrideType.Sealed, childAsset.GetOverride(memberDesc)); // Value is coming from base }
public void TestWithNewTypeFromNewBase() { var baseAsset = new TestDiffAsset() { Name = "Red", Value = 1, Dynamic = new DiffValueTypeA() { Text = "Test1"} }; var newBaseAsset = (TestDiffAsset)AssetCloner.Clone(baseAsset); newBaseAsset.Dynamic = new DiffValueTypeB() { Value = 1 }; var baseItem = new AssetItem("/base", baseAsset); var childAsset = (TestDiffAsset)baseItem.CreateChildAsset(); var diff = new AssetDiff(baseAsset, childAsset, newBaseAsset) { UseOverrideMode = true }; var diffResult = diff.Compute(); var diffResultStripped = diffResult.FindLeafDifferences().ToList(); // Check that everything is merging from asset2 Assert.True(diffResultStripped.All(node => node.ChangeType == Diff3ChangeType.MergeFromAsset2)); // Check that merged result on Dynamic property is instance from asset2 var mergeResult = AssetMerge.Merge(diff, AssetMergePolicies.MergePolicyAsset2AsNewBaseOfAsset1); Assert.True(childAsset.Dynamic is DiffValueTypeB); Assert.AreEqual(1, ((DiffValueTypeB)childAsset.Dynamic).Value); }