public void BothChangedSame() { var left = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Changed("a", inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); var right = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Changed("a", inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Changed("a", inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsFalse(conflicts.HasConflicts); }
public void OneConflictUseLeft() { var diffConflicted = DiffFactory.Create <Sample>().Class() .Changed(x => x.ValueInner, inner => inner.Class() .Conflicted( c => c.Replaced(x => x.Value, "a", "b"), c => c.Replaced(x => x.Value, "a", "c") ) ) .MakeDiff(); var diffResolved = DiffFactory.Create <Sample>().Class() .Changed(x => x.ValueInner, inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); var conflictResolver = Merger.Instance.Partial.GetConflictResolver(diffConflicted); Assert.IsTrue(conflictResolver.HasConflicts); IDiffItemConflicted[] conflicts = conflictResolver.ToArray(); Assert.AreEqual(1, conflicts.Length); conflictResolver.ResolveConflict(conflicts[0], ResolveAction.UseLeft); Assert.IsFalse(conflictResolver.HasConflicts); Assert.AreEqual(diffResolved, conflictResolver.Finish()); }
public void MergeObjectConflictDescendants() { var left = DiffFactory.Create <SampleBase>().Value() .ChangedType <SampleDescendant1>(inner => inner.Class() .Replaced(x => x.ValueBase, "a", "b") ) .MakeDiff(); var right = DiffFactory.Create <SampleBase>().Value() .ChangedType <SampleDescendant1>(inner => inner.Class() .Replaced(x => x.ValueBase, "a", "c") ) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <SampleBase>().Value() .ChangedType <SampleDescendant1>(inner => inner.Class() .Conflicted( c => c.Replaced(x => x.ValueBase, "a", "b"), c => c.Replaced(x => x.ValueBase, "a", "c") ) ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsTrue(conflicts.HasConflicts); }
public void TestArrayReplaced() { var @base = new[] { new SampleInner { Id = 1, Value = "a" }, new SampleInner { Id = 2, Value = "b" } }; var changed = new[] { new SampleInner { Id = 1, Value = "a" }, new SampleInner { Id = 2, Value = "a" } }; var expected = DiffFactory.Create <SampleInner[]>().Ordered() .Replaced(1, new SampleInner { Id = 2, Value = "b" }, new SampleInner { Id = 2, Value = "a" }) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void BothSidesAddedTwo() { var left = DiffFactory.Create <int[]>().Ordered() .Added(3, 3) .Added(3, 4) .MakeDiff(); var right = DiffFactory.Create <int[]>().Ordered() .Added(3, 5) .Added(3, 6) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <int[]>().Ordered() .Conflicted( c => c .Added(3, 3) .Added(3, 4), c => c .Added(3, 5) .Added(3, 6) ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsTrue(conflicts.HasConflicts); }
public void OneRemoved() { var @base = new[] { new Sample { Id = 0, Value = "a" }, new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" } }; var changed = new[] { new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" } }; var expected = DiffFactory.Create <Sample[]>().Ordered() .Removed(0, new Sample { Id = 0, Value = "a" }) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void TestDictionaryReplaced() { var @base = new Dictionary <string, SampleInner> { { "a", new SampleInner { Id = 1, Value = "a" } }, { "b", new SampleInner { Id = 2, Value = "b" } } }; var changed = new Dictionary <string, SampleInner> { { "a", new SampleInner { Id = 1, Value = "a" } }, { "b", new SampleInner { Id = 2, Value = "a" } } }; var expected = DiffFactory.Create <Dictionary <string, SampleInner> >().KeyValue() .Replaced("b", new SampleInner { Id = 2, Value = "b" }, new SampleInner { Id = 2, Value = "a" }) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void Set() { var diff = DiffFactory.Create <HashSet <SampleInner> >().Unordered() .Changed(1, inner => inner.Class() .Replaced(x => x.Value, "Hello", "World") ) .Replaced(new SampleInner(), new SampleInner()) .Removed(new SampleInner()) .Added(new SampleInner()) .Unchanged(new SampleInner()) .MakeDiff(); var expected = new StringBuilder(); expected.AppendLine("=1:"); expected.AppendLine("\t-Value:Hello"); expected.AppendLine("\t+Value:World"); expected.AppendLine("-<SampleInner>"); expected.AppendLine("+<SampleInner>"); expected.AppendLine("-<SampleInner>"); expected.AppendLine("+<SampleInner>"); expected.Append(" <SampleInner>"); Assert.AreEqual(expected.ToString(), diff.ToString()); }
public void OneRemoved() { var diff = DiffFactory.Create <HashSet <Sample> >().Unordered() .Removed(new Sample { Id = 3, Value = "c" }) .MakeDiff(); var obj = new HashSet <Sample> { new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" }, new Sample { Id = 3, Value = "c" } }; var changed = new HashSet <Sample> { new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" } }; var ret = Merger.Instance.Partial.ApplyPatch(obj, diff); CollectionAssert.AreEqual(changed.OrderBy(x => x.Id).ToArray(), ret.ToArray()); }
public void Array() { var diff = DiffFactory.Create <SampleInner[]>().Ordered() .Changed(0, inner => inner.Class() .Replaced(x => x.Value, "Hello", "World") ) .Replaced(1, new SampleInner(), new SampleInner()) .Removed(2, new SampleInner()) .Added(2, new SampleInner()) .Unchanged(3, new SampleInner()) .MakeDiff(); var expected = new StringBuilder(); expected.AppendLine("=0:"); expected.AppendLine("\t-Value:Hello"); expected.AppendLine("\t+Value:World"); expected.AppendLine("-1:<SampleInner>"); expected.AppendLine("+1:<SampleInner>"); expected.AppendLine("-2:<SampleInner>"); expected.AppendLine("+2:<SampleInner>"); expected.Append(" 3:<SampleInner>"); Assert.AreEqual(expected.ToString(), diff.ToString()); }
public void Dictionary() { var diff = DiffFactory.Create <Dictionary <string, SampleInner> >().KeyValue() .Changed("a", inner => inner.Class() .Replaced(x => x.Value, "Hello", "World") ) .Replaced("b", new SampleInner(), new SampleInner()) .Removed("c", new SampleInner()) .Added("d", new SampleInner()) .Unchanged("e", new SampleInner()) .MakeDiff(); var expected = new StringBuilder(); expected.AppendLine("=a:"); expected.AppendLine("\t-Value:Hello"); expected.AppendLine("\t+Value:World"); expected.AppendLine("-b:<SampleInner>"); expected.AppendLine("+b:<SampleInner>"); expected.AppendLine("-c:<SampleInner>"); expected.AppendLine("+d:<SampleInner>"); expected.Append(" e:<SampleInner>"); Assert.AreEqual(expected.ToString(), diff.ToString()); }
public void MergeNonConflictingRightItemLast() { var left = DiffFactory.Create <Sample>().Class() .Replaced(x => x.Value, "a", "b") .MakeDiff(); var right = DiffFactory.Create <Sample>().Class() .Replaced(x => x.Value2, "a", "b") .Changed(x => x.ValueInner, inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <Sample>().Class() .Replaced(x => x.Value, "a", "b") .Replaced(x => x.Value2, "a", "b") .Changed(x => x.ValueInner, inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsFalse(conflicts.HasConflicts); }
public static async Task <List <Diff> > GenerateDiffCache(string fileA, string fileB) { if (string.IsNullOrEmpty(fileA) || string.IsNullOrEmpty(fileB)) { return(new List <Diff>()); } fileA = fileA.Replace("/", "\\"); fileB = fileB.Replace("/", "\\"); var fileAFolder = await StorageFolder.GetFolderFromPathAsync( fileA.Substring(0, fileA.LastIndexOf('\\'))); var fileBFolder = await StorageFolder.GetFolderFromPathAsync( fileB.Substring(0, fileB.LastIndexOf('\\'))); var fileAFile = await fileAFolder.GetFileAsync(FileHelper.GetFileName(fileA)); var fileBFile = await fileBFolder.GetFileAsync(FileHelper.GetFileName(fileB)); var fileAText = await Windows.Storage.FileIO.ReadTextAsync(fileAFile); var fileBText = await Windows.Storage.FileIO.ReadTextAsync(fileBFile); fileAText = fileAText.Replace("\r\n", "\n"); fileBText = fileBText.Replace("\r\n", "\n"); return(DiffFactory.GenerateDiffCache(fileAText, fileBText)); }
public void BothAddedDifferent() { var left = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Added("a", new Sample { Id = 1 }) .MakeDiff(); var right = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Added("a", new Sample { Id = 2 }) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Conflicted( c => c.Added("a", new Sample { Id = 1 }), c => c.Added("a", new Sample { Id = 2 }) ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsTrue(conflicts.HasConflicts); }
public void BothChanged() { var left = DiffFactory.Create <Sample[]>().Ordered() .Changed(5, inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); var right = DiffFactory.Create <Sample[]>().Ordered() .Changed(5, inner => inner.Class() .Replaced(x => x.Value, "a", "c") ) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <Sample[]>().Ordered() .Changed(5, inner => inner.Class() .Conflicted( c => c.Replaced(x => x.Value, "a", "b"), c => c.Replaced(x => x.Value, "a", "c") ) ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsTrue(conflicts.HasConflicts); }
public void ConflictRemovedRemovedThenReplacedUseLeft() { var diffConflicted = DiffFactory.Create <int[]>().Ordered() .Conflicted( c => c.Removed(0, 0), c => c.Replaced(0, 0, 5) ) .Removed(1, 1) .Replaced(2, 3, 5) .MakeDiff(); var diffResolved = DiffFactory.Create <int[]>().Ordered() .Removed(0, 0) .Removed(1, 1) .Replaced(2, 3, 5) .MakeDiff(); var conflictResolver = Merger.Instance.Partial.GetConflictResolver(diffConflicted); Assert.IsTrue(conflictResolver.HasConflicts); IDiffItemConflicted[] conflicts = conflictResolver.ToArray(); Assert.AreEqual(1, conflicts.Length); conflictResolver.ResolveConflict(conflicts[0], ResolveAction.UseLeft); Assert.IsFalse(conflictResolver.HasConflicts); Assert.AreEqual(diffResolved, conflictResolver.Finish()); }
public void OneChanged() { var @base = new[] { new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" } }; var changed = new[] { new Sample { Id = 1, Value = "c" }, new Sample { Id = 2, Value = "b" } }; var expected = DiffFactory.Create <Sample[]>().Ordered() .Changed(0, inner => inner.Class() .Replaced(x => x.Value, "a", "c") ) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void OneReplacedWithOtherDescendant() { var @base = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "b" } }; var changed = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant2 { Id = 2, Value = "b" } }; var expected = DiffFactory.Create <SampleBase[]>().Ordered() .Changed(1, inner => inner.Value() .Replaced(new SampleDescendant1 { Id = 2, Value = "b" }, new SampleDescendant2 { Id = 2, Value = "b" }) ) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void TestSetReplaced() { var @base = new HashSet <SampleInner> { new SampleInner { Id = 1, Value = "a" }, new SampleInner { Id = 2, Value = "b" } }; var changed = new HashSet <SampleInner> { new SampleInner { Id = 1, Value = "a" }, new SampleInner { Id = 2, Value = "a" } }; var expected = DiffFactory.Create <HashSet <SampleInner> >().Unordered() .Replaced(new SampleInner { Id = 2, Value = "b" }, new SampleInner { Id = 2, Value = "a" }) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void OneChangedProperty() { var @base = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "b" } }; var changed = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "c" } }; var expected = DiffFactory.Create <SampleBase[]>().Ordered() .Changed(1, inner => inner.Value() .ChangedType <SampleDescendant1>(inner2 => inner2.Class() .Replaced(x => x.Value, "b", "c") ) ) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void TestClassReplaced() { var @base = new Sample { ValueInner = new SampleInner { Id = 1, Value = "one" } }; var changed = new Sample { ValueInner = new SampleInner { Id = 1, Value = "two" } }; var expected = DiffFactory.Create <Sample>().Class() .Replaced(x => x.ValueInner, new SampleInner { Id = 1, Value = "one" }, new SampleInner { Id = 1, Value = "two" }) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void OneAdded() { var diff = DiffFactory.Create <SampleBase[]>().Ordered() .Added(1, new SampleDescendant1 { Id = 2, Value = "b" }) .MakeDiff(); var obj = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" } }; var changed = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "b" } }; var ret = Merger.Instance.Partial.ApplyPatch(obj, diff); CollectionAssert.AreEqual(changed, ret); }
public void UnchangedNonEmpty() { var @base = new Dictionary <string, Sample> { { "a", new Sample { Id = 1, Value = "a" } }, { "b", new Sample { Id = 2, Value = "b" } } }; var changed = new Dictionary <string, Sample> { { "a", new Sample { Id = 1, Value = "a" } }, { "b", new Sample { Id = 2, Value = "b" } } }; var expected = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void OneChangedProperty() { var diff = DiffFactory.Create <SampleBase[]>().Ordered() .Changed(1, inner => inner.Value() .ChangedType <SampleDescendant1>(inner2 => inner2.Class() .Replaced(x => x.Value, "b", "c") ) ) .MakeDiff(); var obj = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "b" } }; var changed = new SampleBase[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "c" } }; var ret = Merger.Instance.Partial.ApplyPatch(obj, diff); CollectionAssert.AreEqual(changed, ret); }
public void RemovedAndReplacedPlusNonConflicted() { var left = DiffFactory.Create <int[]>().Ordered() .Removed(3, 3) .Removed(4, 4) .Added(5, 4) .MakeDiff(); var right = DiffFactory.Create <int[]>().Ordered() .Replaced(3, 3, 4) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <int[]>().Ordered() .Conflicted( c => c.Removed(3, 3), c => c.Replaced(3, 3, 4) ) .Removed(4, 4) .Added(5, 4) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsTrue(conflicts.HasConflicts); }
public void OneRemoved() { var diff = DiffFactory.Create <Dictionary <string, Sample> >().KeyValue() .Removed("c", new Sample { Id = 3, Value = "c" }) .MakeDiff(); var obj = new Dictionary <string, Sample> { { "a", new Sample { Id = 1, Value = "a" } }, { "b", new Sample { Id = 2, Value = "b" } }, { "c", new Sample { Id = 3, Value = "c" } } }; var changed = new Dictionary <string, Sample> { { "a", new Sample { Id = 1, Value = "a" } }, { "b", new Sample { Id = 2, Value = "b" } } }; var ret = Merger.Instance.Partial.ApplyPatch(obj, diff); CollectionAssert.AreEqual(changed, ret); }
public void OneAdded() { var @base = new[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 3, Value = "c" } }; var changed = new[] { new SampleDescendant1 { Id = 1, Value = "a" }, new SampleDescendant1 { Id = 2, Value = "b" }, new SampleDescendant1 { Id = 3, Value = "c" } }; var expected = DiffFactory.Create <SampleDescendant1[]>().Ordered() .Added(1, new SampleDescendant1 { Id = 2, Value = "b" }) .MakeDiff(); var ret = Merger.Instance.Partial.Diff(@base, changed); Assert.AreEqual(expected, ret); }
public void OneRemoved() { var diff = DiffFactory.Create <Sample[]>().Ordered() .Removed(2, new Sample { Id = 3, Value = "c" }) .MakeDiff(); var obj = new[] { new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" }, new Sample { Id = 3, Value = "c" } }; var changed = new[] { new Sample { Id = 1, Value = "a" }, new Sample { Id = 2, Value = "b" } }; var ret = Merger.Instance.Partial.ApplyPatch(obj, diff); CollectionAssert.AreEqual(changed, ret); }
public void ChangedRemoved() { var left = DiffFactory.Create <HashSet <Sample> >().Unordered() .Changed(1, inner => inner.Class() .Replaced(x => x.Value, "a", "b") ) .MakeDiff(); var right = DiffFactory.Create <HashSet <Sample> >().Unordered() .Removed(new Sample { Id = 1, Value = "a" }) .MakeDiff(); IConflictContainer conflicts; var result = Merger.Instance.Partial.MergeDiffs(left, right, out conflicts); var merged = DiffFactory.Create <HashSet <Sample> >().Unordered() .Conflicted( c => c.Changed(1, inner => inner.Class() .Replaced(x => x.Value, "a", "b") ), c => c.Removed(new Sample { Id = 1, Value = "a" }) ) .MakeDiff(); Assert.AreEqual(merged, result); Assert.IsTrue(conflicts.HasConflicts); }
public void TestUnifiedDiffParsing_AddedRemoved() { string inputA = "This is a test file!\n" + "Added another line to test file\n" + "Adding a third line to test file"; string inputB = "This is a test file!\n" + "Adding a third line to test file\n" + "Adding a fourth line to test file"; List <Diff.Diff> diff = DiffFactory.GenerateDiffCache(inputA, inputB); DiffTextModel textModel = DiffFactory.GenerateUnifiedDiff(diff); Assert.IsTrue(textModel.LineCount == 5); DiffTextLine removedLine = textModel.GetLine(1) as DiffTextLine; Assert.IsTrue(removedLine.ToString() == "Added another line to test file"); Assert.IsTrue(removedLine.BeforeLineNo == 2); Assert.IsTrue(removedLine.ChangeType == DiffLineType.Remove); DiffTextLine addedLine = textModel.GetLine(3) as DiffTextLine; Assert.IsTrue(addedLine.ToString() == "Adding a third line to test file"); Assert.IsTrue(addedLine.BeforeLineNo == -1); Assert.IsTrue(addedLine.LineNo == 2); Assert.IsTrue(addedLine.ChangeType == DiffLineType.Insert); }