public virtual void TestComparatorReduceCommonStartEnd() { RawTextComparator c = RawTextComparator.DEFAULT; Edit e; e = c.ReduceCommonStartEnd(T(string.Empty), T(string.Empty), new Edit(0, 0, 0, 0) ); NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 0), e); e = c.ReduceCommonStartEnd(T("a"), T("b"), new Edit(0, 1, 0, 1)); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); e = c.ReduceCommonStartEnd(T("a"), T("a"), new Edit(0, 1, 0, 1)); NUnit.Framework.Assert.AreEqual(new Edit(1, 1, 1, 1), e); e = c.ReduceCommonStartEnd(T("axB"), T("axC"), new Edit(0, 3, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); e = c.ReduceCommonStartEnd(T("Bxy"), T("Cxy"), new Edit(0, 3, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); e = c.ReduceCommonStartEnd(T("bc"), T("Abc"), new Edit(0, 2, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 1), e); e = new Edit(0, 5, 0, 5); e = c.ReduceCommonStartEnd(T("abQxy"), T("abRxy"), e); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); RawText a = new RawText(Sharpen.Runtime.GetBytesForString("p\na b\nQ\nc d\n", "UTF-8" )); RawText b = new RawText(Sharpen.Runtime.GetBytesForString("p\na b \nR\n c d \n" , "UTF-8")); e = new Edit(0, 4, 0, 4); e = RawTextComparator.WS_IGNORE_ALL.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); }
private HunkRangeInfo(ITextSnapshot snapshot, Edit edit, List<string> originalText) { _snapshot = snapshot; _edit = edit; _originalText = originalText; _canRollback = false; }
public virtual void TestCreate() { Edit e = new Edit(1, 2, 3, 4); NUnit.Framework.Assert.AreEqual(1, e.GetBeginA()); NUnit.Framework.Assert.AreEqual(2, e.GetEndA()); NUnit.Framework.Assert.AreEqual(3, e.GetBeginB()); NUnit.Framework.Assert.AreEqual(4, e.GetEndB()); }
public override bool Equals(object o) { if (o is NGit.Diff.Edit) { NGit.Diff.Edit e = (NGit.Diff.Edit)o; return(this.beginA == e.beginA && this.endA == e.endA && this.beginB == e.beginB && this.endB == e.endB); } return(false); }
public virtual void TestCreateEmpty() { Edit e = new Edit(1, 3); NUnit.Framework.Assert.AreEqual(1, e.GetBeginA()); NUnit.Framework.Assert.AreEqual(1, e.GetEndA()); NUnit.Framework.Assert.AreEqual(3, e.GetBeginB()); NUnit.Framework.Assert.AreEqual(3, e.GetEndB()); NUnit.Framework.Assert.IsTrue(e.IsEmpty(), "is empty"); NUnit.Framework.Assert.AreEqual(Edit.Type.EMPTY, e.GetType()); }
public virtual void TestComparatorReduceCommonStartButLastLineNoEol() { RawText a; RawText b; Edit e; a = new RawText(Sharpen.Runtime.GetBytesForString("start", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("start of line", "UTF-8")); e = new Edit(0, 1, 0, 1); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); }
public HunkRangeInfo(ITextSnapshot snapshot, Edit edit, RawText originalText, RawText workingText) { if (snapshot == null) throw new ArgumentNullException("snapshot"); if (edit == null) throw new ArgumentNullException("edit"); if (originalText == null) throw new ArgumentNullException("originalText"); if (workingText == null) throw new ArgumentNullException("workingText"); _snapshot = snapshot; _edit = edit; _originalText = originalText.GetString(edit.GetBeginA(), edit.GetEndA(), true).Split('\n').Select(i => i.TrimEnd('\r')).ToList(); _canRollback = true; }
public virtual void TestAddOne() { Edit e = new Edit(1, 2, 1, 1); EditList l = new EditList(); Extensions.AddItem(l, e); NUnit.Framework.Assert.AreEqual(1, l.Count); NUnit.Framework.Assert.IsFalse(Extensions.IsEmpty(l)); NUnit.Framework.Assert.AreSame(e, l[0]); NUnit.Framework.Assert.AreSame(e, Extensions.Iterator(l).Next()); NUnit.Framework.Assert.AreEqual(l, l); NUnit.Framework.Assert.IsFalse(l.Equals(new EditList())); EditList l2 = new EditList(); Extensions.AddItem(l2, e); NUnit.Framework.Assert.AreEqual(l2, l); NUnit.Framework.Assert.AreEqual(l, l2); NUnit.Framework.Assert.AreEqual(AList<Edit>.GetHashCode(l), AList<Edit>.GetHashCode(l2)); }
public virtual void TestAddOne() { Edit e = new Edit(1, 2, 1, 1); EditList l = new EditList(); l.AddItem(e); NUnit.Framework.Assert.AreEqual(1, l.Count); NUnit.Framework.Assert.IsFalse(l.IsEmpty()); NUnit.Framework.Assert.AreSame(e, l[0]); NUnit.Framework.Assert.AreSame(e, l.Iterator().Next()); NUnit.Framework.Assert.AreEqual(l, l); NUnit.Framework.Assert.IsFalse(l.Equals(new EditList())); EditList l2 = new EditList(); l2.AddItem(e); NUnit.Framework.Assert.AreEqual(l2, l); NUnit.Framework.Assert.AreEqual(l, l2); NUnit.Framework.Assert.AreEqual(l.GetHashCode(), l2.GetHashCode()); }
public virtual void TestAddTwo() { Edit e1 = new Edit(1, 2, 1, 1); Edit e2 = new Edit(8, 8, 8, 12); EditList l = new EditList(); Extensions.AddItem(l, e1); Extensions.AddItem(l, e2); NUnit.Framework.Assert.AreEqual(2, l.Count); NUnit.Framework.Assert.AreSame(e1, l[0]); NUnit.Framework.Assert.AreSame(e2, l[1]); Iterator<Edit> i = Extensions.Iterator(l); NUnit.Framework.Assert.AreSame(e1, i.Next()); NUnit.Framework.Assert.AreSame(e2, i.Next()); NUnit.Framework.Assert.AreEqual(l, l); NUnit.Framework.Assert.IsFalse(l.Equals(new EditList())); EditList l2 = new EditList(); Extensions.AddItem(l2, e1); Extensions.AddItem(l2, e2); NUnit.Framework.Assert.AreEqual(l2, l); NUnit.Framework.Assert.AreEqual(l, l2); NUnit.Framework.Assert.AreEqual(AList<Edit>.GetHashCode(l), AList<Edit>.GetHashCode(l2)); }
private void Diff(Edit r) { switch (r.GetType()) { case Edit.Type.INSERT: case Edit.Type.DELETE: { this.edits.AddItem(r); break; } case Edit.Type.REPLACE: { this.DiffReplace(r); break; } case Edit.Type.EMPTY: default: { throw new InvalidOperationException(); } } }
public virtual void TestAddTwo() { Edit e1 = new Edit(1, 2, 1, 1); Edit e2 = new Edit(8, 8, 8, 12); EditList l = new EditList(); l.AddItem(e1); l.AddItem(e2); NUnit.Framework.Assert.AreEqual(2, l.Count); NUnit.Framework.Assert.AreSame(e1, l[0]); NUnit.Framework.Assert.AreSame(e2, l[1]); Iterator<Edit> i = l.Iterator(); NUnit.Framework.Assert.AreSame(e1, i.Next()); NUnit.Framework.Assert.AreSame(e2, i.Next()); NUnit.Framework.Assert.IsTrue(l.Equals(l)); NUnit.Framework.Assert.IsFalse(l.Equals(new EditList())); EditList l2 = new EditList(); l2.AddItem(e1); l2.AddItem(e2); NUnit.Framework.Assert.IsTrue(l.Equals(l2)); NUnit.Framework.Assert.IsTrue(l2.Equals(l)); NUnit.Framework.Assert.AreEqual(l.GetHashCode(), l2.GetHashCode()); }
public virtual void TestType_Delete() { Edit e = new Edit(1, 2, 1, 1); NUnit.Framework.Assert.AreEqual(Edit.Type.DELETE, e.GetType()); NUnit.Framework.Assert.IsFalse(e.IsEmpty(), "not empty"); NUnit.Framework.Assert.AreEqual(1, e.GetLengthA()); NUnit.Framework.Assert.AreEqual(0, e.GetLengthB()); }
public virtual void TestComparatorReduceCommonStartEnd_EmptyLine() { RawText a; RawText b; Edit e; a = new RawText(Sharpen.Runtime.GetBytesForString("R\n y\n", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("S\n\n y\n", "UTF-8")); e = new Edit(0, 2, 0, 3); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 2), e); a = new RawText(Sharpen.Runtime.GetBytesForString("S\n\n y\n", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("R\n y\n", "UTF-8")); e = new Edit(0, 3, 0, 2); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 2, 0, 1), e); }
private static bool End(Edit edit, int a, int b) { return edit.GetEndA() <= a && edit.GetEndB() <= b; }
public virtual void TestType_Insert() { Edit e = new Edit(1, 1, 1, 2); NUnit.Framework.Assert.AreEqual(Edit.Type.INSERT, e.GetType()); NUnit.Framework.Assert.IsFalse(e.IsEmpty(), "not empty"); NUnit.Framework.Assert.AreEqual(0, e.GetLengthA()); NUnit.Framework.Assert.AreEqual(1, e.GetLengthB()); }
public virtual void TestSet() { Edit e1 = new Edit(1, 2, 1, 1); Edit e2 = new Edit(3, 4, 3, 3); EditList l = new EditList(); l.AddItem(e1); NUnit.Framework.Assert.AreSame(e1, l[0]); NUnit.Framework.Assert.AreSame(e1, l.Set(0, e2)); NUnit.Framework.Assert.AreSame(e2, l[0]); }
public override void DiffNonCommon <S>(EditList edits, HashedSequenceComparator <S> cmp, HashedSequence <S> a, HashedSequence <S> b, Edit region) { new NGit.Diff.MyersDiff <S>(edits, cmp, a, b, region); }
public HunkRangeInfo TranslateTo(ITextSnapshot snapshot) { if (snapshot == null) throw new ArgumentNullException("snapshot"); if (snapshot == _snapshot) return this; if (IsDeletion) { // track a point ITextSnapshotLine line = _snapshot.GetLineFromLineNumber(_edit.GetBeginB()); ITrackingPoint trackingPoint = _snapshot.CreateTrackingPoint(line.Start, PointTrackingMode.Negative); SnapshotPoint updated = trackingPoint.GetPoint(snapshot); int updatedLineNumber = updated.GetContainingLine().LineNumber; Edit updatedEdit = new Edit(_edit.GetBeginA(), _edit.GetEndA(), updatedLineNumber, updatedLineNumber); return new HunkRangeInfo(snapshot, updatedEdit, _originalText); } else { // track a span ITextSnapshotLine startLine = _snapshot.GetLineFromLineNumber(_edit.GetBeginB()); ITextSnapshotLine endLine = _snapshot.GetLineFromLineNumber(_edit.GetEndB() - 1); ITrackingSpan trackingSpan = _snapshot.CreateTrackingSpan(new SnapshotSpan(startLine.Start, endLine.EndIncludingLineBreak), SpanTrackingMode.EdgeInclusive); SnapshotSpan updated = trackingSpan.GetSpan(snapshot); int updatedStartLineNumber = updated.Start.GetContainingLine().LineNumber; int updatedEndLineNumber = updated.End.GetContainingLine().LineNumber; Edit updatedEdit = new Edit(_edit.GetBeginA(), _edit.GetEndA(), updatedStartLineNumber, updatedEndLineNumber); return new HunkRangeInfo(snapshot, updatedEdit, _originalText); } }
public virtual void TestExtendB() { Edit e = new Edit(1, 2, 1, 1); e.ExtendB(); NUnit.Framework.Assert.AreEqual(new Edit(1, 2, 1, 2), e); e.ExtendB(); NUnit.Framework.Assert.AreEqual(new Edit(1, 2, 1, 3), e); }
/// <returns>a list describing the content edits performed within the hunk.</returns> public virtual EditList ToEditList() { if (editList == null) { editList = new EditList(); byte[] buf = file.buf; int c = RawParseUtils.NextLF(buf, startOffset); int oLine = old.startLine; int nLine = newStartLine; Edit @in = null; for (; c < endOffset; c = RawParseUtils.NextLF(buf, c)) { switch (buf[c]) { case (byte)(' '): case (byte)('\n'): { @in = null; oLine++; nLine++; continue; goto case (byte)('-'); } case (byte)('-'): { if (@in == null) { @in = new Edit(oLine - 1, nLine - 1); editList.AddItem(@in); } oLine++; @in.ExtendA(); continue; goto case (byte)('+'); } case (byte)('+'): { if (@in == null) { @in = new Edit(oLine - 1, nLine - 1); editList.AddItem(@in); } nLine++; @in.ExtendB(); continue; goto case (byte)('\\'); } case (byte)('\\'): { // Matches "\ No newline at end of file" continue; goto default; } default: { goto SCAN_break; break; } } SCAN_continue: ; } SCAN_break: ; } return editList; }
private DiffMarkerType GetMarkerType(Edit.Type type) { switch (type) { case Edit.Type.INSERT: return DiffMarkerType.Added; case Edit.Type.DELETE: return DiffMarkerType.Removed; case Edit.Type.REPLACE: return DiffMarkerType.Changed; default: throw new ArgumentOutOfRangeException("type"); } }
public virtual void TestToString() { Edit e = new Edit(1, 2, 1, 4); NUnit.Framework.Assert.AreEqual("REPLACE(1-2,1-4)", e.ToString()); }
/// <summary>Construct a subsequence around the B region/base sequence.</summary> /// <remarks>Construct a subsequence around the B region/base sequence.</remarks> /// <?></?> /// <param name="b">the B sequence.</param> /// <param name="region"> /// the region of /// <code>b</code> /// to create a subsequence around. /// </param> /// <returns> /// subsequence of /// <code>base</code> /// as described by B in /// <code>region</code> /// . /// </returns> public static NGit.Diff.Subsequence <S> B <S>(S b, Edit region) where S : Sequence { return(new NGit.Diff.Subsequence <S>(b, region.beginB, region.endB)); }
public virtual void TestRemove() { Edit e1 = new Edit(1, 2, 1, 1); Edit e2 = new Edit(8, 8, 8, 12); EditList l = new EditList(); l.AddItem(e1); l.AddItem(e2); l.Remove(e1); NUnit.Framework.Assert.AreEqual(1, l.Count); NUnit.Framework.Assert.AreSame(e2, l[0]); }
/// <summary>Construct a new edit representing the region before cut.</summary> /// <remarks>Construct a new edit representing the region before cut.</remarks> /// <param name="cut"> /// the cut point. The beginning A and B points are used as the /// end points of the returned edit. /// </param> /// <returns> /// an edit representing the slice of /// <code>this</code> /// edit that occurs /// before /// <code>cut</code> /// starts. /// </returns> public NGit.Diff.Edit Before(NGit.Diff.Edit cut) { return(new NGit.Diff.Edit(beginA, cut.beginA, beginB, cut.beginB)); }
public virtual void TestBeforeAfterCuts() { Edit whole = new Edit(1, 8, 2, 9); Edit mid = new Edit(4, 5, 3, 6); NUnit.Framework.Assert.AreEqual(new Edit(1, 4, 2, 3), whole.Before(mid)); NUnit.Framework.Assert.AreEqual(new Edit(5, 8, 6, 9), whole.After(mid)); }
/// <summary>Construct a new edit representing the region after cut.</summary> /// <remarks>Construct a new edit representing the region after cut.</remarks> /// <param name="cut"> /// the cut point. The ending A and B points are used as the /// starting points of the returned edit. /// </param> /// <returns> /// an edit representing the slice of /// <code>this</code> /// edit that occurs /// after /// <code>cut</code> /// ends. /// </returns> public NGit.Diff.Edit After(NGit.Diff.Edit cut) { return(new NGit.Diff.Edit(cut.endA, endA, cut.endB, endB)); }
/// <summary>Construct a subsequence around the A region/base sequence.</summary> /// <remarks>Construct a subsequence around the A region/base sequence.</remarks> /// <?></?> /// <param name="a">the A sequence.</param> /// <param name="region"> /// the region of /// <code>a</code> /// to create a subsequence around. /// </param> /// <returns> /// subsequence of /// <code>base</code> /// as described by A in /// <code>region</code> /// . /// </returns> public static NGit.Diff.Subsequence <S> A <S>(S a, Edit region) where S : Sequence { return(new NGit.Diff.Subsequence <S>(a, region.beginA, region.endA)); }
/// <summary>Compare two sequences and identify a list of edits between them.</summary> /// <remarks> /// Compare two sequences and identify a list of edits between them. /// This method should be invoked only after the two sequences have been /// proven to have no common starting or ending elements. The expected /// elimination of common starting and ending elements is automatically /// performed by the /// <see cref="DiffAlgorithm.Diff{S}(SequenceComparator{S}, Sequence, Sequence)">DiffAlgorithm.Diff<S>(SequenceComparator<S>, Sequence, Sequence) /// </see> /// method, which invokes this method using /// <see cref="Subsequence{S}">Subsequence<S></see> /// s. /// </remarks> /// <?></?> /// <param name="edits">result list to append the region's edits onto.</param> /// <param name="cmp">the comparator supplying the element equivalence function.</param> /// <param name="a"> /// the first (also known as old or pre-image) sequence. Edits /// returned by this algorithm will reference indexes using the /// 'A' side: /// <see cref="Edit.GetBeginA()">Edit.GetBeginA()</see> /// , /// <see cref="Edit.GetEndA()">Edit.GetEndA()</see> /// . /// </param> /// <param name="b"> /// the second (also known as new or post-image) sequence. Edits /// returned by this algorithm will reference indexes using the /// 'B' side: /// <see cref="Edit.GetBeginB()">Edit.GetBeginB()</see> /// , /// <see cref="Edit.GetEndB()">Edit.GetEndB()</see> /// . /// </param> /// <param name="region">the region being compared within the two sequences.</param> public abstract void DiffNonCommon <S>(EditList edits, HashedSequenceComparator <S> cmp, HashedSequence <S> a, HashedSequence <S> b, Edit region) where S : Sequence;
public virtual void TestType_Replace() { Edit e = new Edit(1, 2, 1, 4); NUnit.Framework.Assert.AreEqual(Edit.Type.REPLACE, e.GetType()); NUnit.Framework.Assert.IsFalse(e.IsEmpty(), "not empty"); NUnit.Framework.Assert.AreEqual(1, e.GetLengthA()); NUnit.Framework.Assert.AreEqual(3, e.GetLengthB()); }
private static bool End(Edit edit, int a, int b) { return(edit.GetEndA() <= a && edit.GetEndB() <= b); }
public virtual void TestType_Empty() { Edit e = new Edit(1, 1, 2, 2); NUnit.Framework.Assert.AreEqual(Edit.Type.EMPTY, e.GetType()); NUnit.Framework.Assert.AreEqual(Edit.Type.EMPTY, new Edit(1, 2).GetType()); NUnit.Framework.Assert.IsTrue(e.IsEmpty(), "is empty"); NUnit.Framework.Assert.AreEqual(0, e.GetLengthA()); NUnit.Framework.Assert.AreEqual(0, e.GetLengthB()); }
public override void DiffNonCommon <S>(EditList edits, HashedSequenceComparator <S> cmp, HashedSequence <S> a, HashedSequence <S> b, Edit region) { new HistogramDiff.State <S>(this, edits, cmp, a, b).DiffReplace(region); }
private void WriteLine(StringBuilder @out, List<TextMarker> markers, Text text, int line, Edit.Type editType) { int start = @out.Length; WriteLine(@out, text, line); int end = @out.Length; Color color; switch (editType) { case Edit.Type.INSERT: color = DiffColor.Added.LightColor; break; case Edit.Type.DELETE: color = DiffColor.Removed.LightColor; break; case Edit.Type.REPLACE: color = DiffColor.Changed.LightColor; break; default: throw new ArgumentOutOfRangeException("editType"); } markers.Add(new TextMarker( start, end - start, TextMarkerType.SolidBlock | TextMarkerType.ExtendToBorder, color )); }
public virtual void TestEquals1() { Edit e1 = new Edit(1, 2, 3, 4); Edit e2 = new Edit(1, 2, 3, 4); NUnit.Framework.Assert.IsTrue(e1.Equals(e1)); NUnit.Framework.Assert.IsTrue(e1.Equals(e2)); NUnit.Framework.Assert.IsTrue(e2.Equals(e1)); NUnit.Framework.Assert.AreEqual(e1.GetHashCode(), e2.GetHashCode()); NUnit.Framework.Assert.IsFalse(e1.Equals(string.Empty)); }