public static string Diff(string actualValue, string expectedValue, DiffStyle diffStyle, StringBuilder output) { if (actualValue == null || expectedValue == null) { return(string.Empty); } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return(string.Empty); } output.AppendLine(" Idx Expected Actual"); output.AppendLine("-------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { output.AppendLine($"{(i < minLen && actualValue[i] == expectedValue[i] ? " " : "*")} {i,-3} {(i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : ""),-4} {(i < expectedValue.Length ? expectedValue[i].ToSafeString() : ""),-3} {(i < actualValue.Length ? ((int)actualValue[i]).ToString() : ""),-4} {(i < actualValue.Length ? actualValue[i].ToSafeString() : ""),-3}" // character safe string ); } } output.AppendLine(); return(output.ToString()); }
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle, TextWriter output) { if (actualValue == null || expectedValue == null) { //Assert.AreEqual(expectedValue, actualValue); Assert.Equal(expectedValue, actualValue); return; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) return; output.WriteLine(" Idx Expected Actual"); output.WriteLine("-------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { output.WriteLine("{0} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}", i < minLen && actualValue[i] == expectedValue[i] ? " " : "*", // put a mark beside a differing row i, // the index i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : "", // character decimal value i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? ((int)actualValue[i]).ToString() : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "" // character safe string ); } } output.WriteLine(); //Assert.AreEqual(expectedValue, actualValue); Assert.Equal(expectedValue, actualValue); }
public static void AreEqual(string expectedValue, string actualValue, DiffStyle diffStyle, TextWriter output) { if (actualValue == null || expectedValue == null) { Assert.AreEqual(expectedValue, actualValue); return; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return; } Console.WriteLine(); output.WriteLine("Index Expected Actual"); output.WriteLine("----------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); if (diffStyle != DiffStyle.Minimal) { int startDifferAt = 0; for (int i = 0; i < maxLen; i++) { if (i >= minLen || actualValue[i] != expectedValue[i]) { startDifferAt = i; break; } } var endDifferAt = Math.Min(startDifferAt + 10, maxLen); startDifferAt = Math.Max(startDifferAt - 10, 0); bool isFirstDiff = true; for (int i = startDifferAt; i < endDifferAt; i++) { if (i >= minLen || actualValue[i] != expectedValue[i]) { output.WriteLine("{0,-3} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}", i < minLen && actualValue[i] == expectedValue[i] ? " " : isFirstDiff ? ">>>": "***", // put a mark beside a differing row i, // the index i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : "", // character decimal value i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? ((int)actualValue[i]).ToString() : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "" // character safe string ); isFirstDiff = false; } } //output.WriteLine(); } Assert.True(string.CompareOrdinal(expectedValue, actualValue) == 0, "strings are differing"); }
public static void AreEqual(string expectedValue, string actualValue, DiffStyle diffStyle, TextWriter output) { if (actualValue == null || expectedValue == null) { Assert.AreEqual(expectedValue, actualValue); return; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return; } Console.WriteLine(); output.WriteLine("Index Expected Actual"); output.WriteLine("----------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); if (diffStyle != DiffStyle.Minimal) { int startDifferAt = 0; for (int i = 0; i < maxLen; i++) { if (i >= minLen || actualValue[i] != expectedValue[i]) { startDifferAt = i; break; } } var endDifferAt = Math.Min(startDifferAt + 10, maxLen); startDifferAt = Math.Max(startDifferAt - 10, 0); bool isFirstDiff = true; for (int i = startDifferAt; i < endDifferAt; i++) { if (i >= minLen || actualValue[i] != expectedValue[i]) { output.WriteLine("{0,-3} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}", i < minLen && actualValue[i] == expectedValue[i] ? " " : isFirstDiff ? ">>>": "***", // put a mark beside a differing row i, // the index i < expectedValue.Length ? ((int) expectedValue[i]).ToString() : "", // character decimal value i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? ((int) actualValue[i]).ToString() : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "" // character safe string ); isFirstDiff = false; } } //output.WriteLine(); } Assert.True(string.CompareOrdinal(expectedValue, actualValue) == 0, "strings are differing"); }
public bool HasStyle(DiffStyle diffStyle) { // Special case the zero-flag. if (diffStyle == DiffStyle.None) { return(Style == DiffStyle.None); } return((Style & diffStyle) == diffStyle); }
public static bool HasStyle(this DiffToken token, DiffStyle diffStyle) { // Special case the zero-flag. if (diffStyle == DiffStyle.None) { return(token.Style == DiffStyle.None); } return((token.Style & diffStyle) == diffStyle); }
/// <summary> /// Writes the diffs using the specified presentation style and max context length. /// </summary> /// <remarks> /// <para> /// Changes are annotated by markers: <see cref="Marker.DiffAddition" />, <see cref="Marker.DiffDeletion" /> /// and <see cref="Marker.DiffChange" />. /// </para> /// <para> /// If the style is <see cref="DiffStyle.Interleaved" /> then the left document /// is considered the original and the right document is the considered to be the /// one that was modified so deletions appear within the left and additions within the right. /// </para> /// <para> /// If the style is <see cref="DiffStyle.LeftOnly" /> or <see cref="DiffStyle.RightOnly" /> /// then only the deletion and changed markers are used. /// </para> /// </remarks> /// <param name="writer">The test log stream writer to receive the highlighted document.</param> /// <param name="style">The presentation style.</param> /// <param name="maxContextLength">The maximum number of characters of unchanged regions /// to display for context, or <see cref="int.MaxValue" /> for no limit. Extraneous context /// is split in two with an ellipsis inserted in between both halves.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref nameref="writer" /> if null.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="maxContextLength"/> /// is negative.</exception> public void WriteTo(MarkupStreamWriter writer, DiffStyle style, int maxContextLength) { if (writer == null) { throw new ArgumentNullException("writer"); } if (maxContextLength < 0) { throw new ArgumentOutOfRangeException("maxContextLength"); } foreach (Diff diff in diffs) { if (diff.Kind == DiffKind.NoChange) { WriteContext(writer, new Substring(leftDocument, diff.LeftRange), maxContextLength); } else { if (diff.LeftRange.Length != 0) { switch (style) { case DiffStyle.Interleaved: using (writer.BeginMarker(Marker.DiffDeletion)) writer.Write(diff.LeftRange.SubstringOf(leftDocument)); break; case DiffStyle.LeftOnly: using (writer.BeginMarker(diff.RightRange.Length == 0 ? Marker.DiffDeletion : Marker.DiffChange)) writer.Write(diff.LeftRange.SubstringOf(leftDocument)); break; } } if (diff.RightRange.Length != 0) { switch (style) { case DiffStyle.Interleaved: using (writer.BeginMarker(Marker.DiffAddition)) writer.Write(diff.RightRange.SubstringOf(rightDocument)); break; case DiffStyle.RightOnly: using (writer.BeginMarker(diff.LeftRange.Length == 0 ? Marker.DiffDeletion : Marker.DiffChange)) writer.Write(diff.RightRange.SubstringOf(rightDocument)); break; } } } } }
private static void DetailDiff(string expectedValue, string actualValue, DiffStyle diffStyle, StringBuilder output) { output.AppendLine(); output.AppendLine(" Idx Expected Actual"); output.AppendLine("-------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { output.AppendFormat("{0} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}\n", i < minLen && actualValue[i] == expectedValue[i] ? " " : "*", // put a mark beside a differing row i, // the index i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : "", // character decimal value i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? ((int)actualValue[i]).ToString() : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "" // character safe string ); } } }
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle = DiffStyle.Full, TextWriter output = null) { if (actualValue == null || expectedValue == null) { Assert.Equal(expectedValue, actualValue); return; } if (output == null) output = Console.Out; if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) return; output.WriteLine("Position\t Expected\t\t Actual"); output.WriteLine("---------------------------------------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { output.WriteLine("{0,-3}{1}\t\t {2,-4} ({3,-3})\t\t {4,-4} ({5,-3} {6})", i, // the index i < minLen && actualValue[i] == expectedValue[i] ? " " : "*", // put a mark beside a differing row i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < expectedValue.Length ? HexChar(expectedValue[i]) : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? HexChar(actualValue[i]) : "", // character decimal value i < actualValue.Length ? CharUnicodeInfo.GetUnicodeCategory(actualValue[i]).ToString() : "" // char Unicode category ); } } output.WriteLine(); output.Close(); Assert.Equal(expectedValue, actualValue); }
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle) { ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out); }
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle = DiffStyle.Full, TextWriter output = null) { if (actualValue == null || expectedValue == null) { Assert.Equal(expectedValue, actualValue); return; } if (output == null) { output = Console.Out; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return; } output.WriteLine("Position\t Expected\t\t Actual"); output.WriteLine("---------------------------------------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { output.WriteLine("{0,-3}{1}\t\t {2,-4} ({3,-3})\t\t {4,-4} ({5,-3} {6})", i, // the index i < minLen && actualValue[i] == expectedValue[i] ? " " : "*", // put a mark beside a differing row i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < expectedValue.Length ? HexChar(expectedValue[i]) : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? HexChar(actualValue[i]) : "", // character decimal value i < actualValue.Length ? CharUnicodeInfo.GetUnicodeCategory(actualValue[i]).ToString() : "" // char Unicode category ); } } output.WriteLine(); output.Close(); Assert.Equal(expectedValue, actualValue); }
public static void AreEqual(string expectedValue, string actualValue, DiffStyle diffStyle) { AreEqual(actualValue, expectedValue, diffStyle, Console.Out); }
/// <summary> /// Writes the diffs using the specified /// presentation style and no limits on the context length. /// </summary> /// <remarks> /// <para> /// Changes are annotated by markers: <see cref="Marker.DiffAddition" />, <see cref="Marker.DiffDeletion" /> /// and <see cref="Marker.DiffChange" />. /// </para> /// <para> /// If the style is <see cref="DiffStyle.Interleaved" /> then the left document /// is considered the original and the right document is the considered to be the /// one that was modified so deletions appear within the left and additions within the right. /// </para> /// <para> /// If the style is <see cref="DiffStyle.LeftOnly" /> or <see cref="DiffStyle.RightOnly" /> /// then only the deletion and changed markers are used. /// </para> /// </remarks> /// <param name="writer">The test log stream writer to receive the highlighted document.</param> /// <param name="style">The presentation style.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref nameref="writer" /> if null.</exception> public void WriteTo(MarkupStreamWriter writer, DiffStyle style) { WriteTo(writer, style, int.MaxValue); }
/// <summary> /// Writes the diffs using the specified presentation style and max context length. /// </summary> /// <remarks> /// <para> /// Changes are annotated by markers: <see cref="Marker.DiffAddition" />, <see cref="Marker.DiffDeletion" /> /// and <see cref="Marker.DiffChange" />. /// </para> /// <para> /// If the style is <see cref="DiffStyle.Interleaved" /> then the left document /// is considered the original and the right document is the considered to be the /// one that was modified so deletions appear within the left and additions within the right. /// </para> /// <para> /// If the style is <see cref="DiffStyle.LeftOnly" /> or <see cref="DiffStyle.RightOnly" /> /// then only the deletion and changed markers are used. /// </para> /// </remarks> /// <param name="writer">The test log stream writer to receive the highlighted document.</param> /// <param name="style">The presentation style.</param> /// <param name="maxContextLength">The maximum number of characters of unchanged regions /// to display for context, or <see cref="int.MaxValue" /> for no limit. Extraneous context /// is split in two with an ellipsis inserted in between both halves.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref nameref="writer" /> if null.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="maxContextLength"/> /// is negative.</exception> public void WriteTo(MarkupStreamWriter writer, DiffStyle style, int maxContextLength) { if (writer == null) throw new ArgumentNullException("writer"); if (maxContextLength < 0) throw new ArgumentOutOfRangeException("maxContextLength"); foreach (Diff diff in diffs) { if (diff.Kind == DiffKind.NoChange) { WriteContext(writer, new Substring(leftDocument, diff.LeftRange), maxContextLength); } else { if (diff.LeftRange.Length != 0) { switch (style) { case DiffStyle.Interleaved: using (writer.BeginMarker(Marker.DiffDeletion)) writer.Write(diff.LeftRange.SubstringOf(leftDocument)); break; case DiffStyle.LeftOnly: using (writer.BeginMarker(diff.RightRange.Length == 0 ? Marker.DiffDeletion : Marker.DiffChange)) writer.Write(diff.LeftRange.SubstringOf(leftDocument)); break; } } if (diff.RightRange.Length != 0) { switch (style) { case DiffStyle.Interleaved: using (writer.BeginMarker(Marker.DiffAddition)) writer.Write(diff.RightRange.SubstringOf(rightDocument)); break; case DiffStyle.RightOnly: using (writer.BeginMarker(diff.LeftRange.Length == 0 ? Marker.DiffDeletion : Marker.DiffChange)) writer.Write(diff.RightRange.SubstringOf(rightDocument)); break; } } } } }
public static EqualityCompareResult Equal(string expectedValue, string actualValue, DiffStyle diffStyle) { if (actualValue == null || expectedValue == null) { return(new EqualityCompareResult() { IsEqual = (expectedValue == actualValue) }); } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return(new EqualityCompareResult() { IsEqual = true }); } List <string> verboseMessage = new List <string>(); verboseMessage.Add("Index Expected Actual"); verboseMessage.Add("----------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); if (diffStyle != DiffStyle.Minimal) { int startDifferAt = 0; for (int i = 0; i < maxLen; i++) { if (i >= minLen || actualValue[i] != expectedValue[i]) { startDifferAt = i; break; } } var endDifferAt = Math.Min(startDifferAt + 10, maxLen); startDifferAt = Math.Max(startDifferAt - 10, 0); bool isFirstDiff = true; for (int i = startDifferAt; i < endDifferAt; i++) { if (i >= minLen || actualValue[i] != expectedValue[i]) { verboseMessage.Add(string.Format("{0,-3} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}", i < minLen && actualValue[i] == expectedValue[i] ? " " : isFirstDiff ? ">>>" : "***", // put a mark beside a differing row i, // the index i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : "", // character decimal value i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string i < actualValue.Length ? ((int)actualValue[i]).ToString() : "", // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : "" // character safe string )); isFirstDiff = false; } } } return(new EqualityCompareResult() { IsEqual = (expectedValue == actualValue), VerboseMessage = verboseMessage }); }
/// <summary> /// Tests whether the specified values are equal and throws an exception if the two values are not equal. /// Outputs detailed differences to the console. /// </summary> /// <param name="expectedValue">The first value to compare. This is the value the tests expects.</param> /// <param name="actualValue">The second value to compare. This is the value produced by the code under test.</param> /// <param name="diffStyle">The difference style.</param> public static void AreEqual(string expectedValue, string actualValue, DiffStyle diffStyle = DiffStyle.Full) { ShouldEqualWithDiff(expectedValue, actualValue, diffStyle, Console.Out); }
public static void ShouldEqualWithDiff(string actualValue, string expectedValue, DiffStyle diffStyle = DiffStyle.Full) => ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);
public static void AreEqual(string expected, string actual, DiffStyle diffStyle) { AreEqual(expected, actual, diffStyle, Console.Out); }
public DiffToken(DiffStyle style, DiffTokenKind kind, string text) { Style = style; Text = text; Kind = kind; }
public static void ShouldEqualWithDiff(string expectedValue, string actualValue, DiffStyle diffStyle) { if (actualValue == null || expectedValue == null) { //Assert.AreEqual(expectedValue, actualValue); Assert.AreEqual(expectedValue, actualValue); return; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return; } StringBuilder output = new StringBuilder(); if (diffStyle == DiffStyle.Compact) { CompactDiff(actualValue, expectedValue, output); } else { DetailDiff(actualValue, expectedValue, diffStyle, output); } throw new Exception(string.Format("\nExpected: {0}\nActual : {1}\nDetails : {2}", expectedValue, actualValue, output.ToString())); }
public static void ShouldEqualWithDiff(string actualValue, string expectedValue, DiffStyle diffStyle, TextWriter output) { if (actualValue is null || expectedValue is null) { Assert.AreEqual(expectedValue, actualValue); return; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return; } output.WriteLine(" Idx Expected Actual"); output.WriteLine("-------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { // put a mark beside a differing row index character decimal value character safe string character decimal value character safe string output.WriteLine($"{(i < minLen && actualValue[i] == expectedValue[i] ? " " : " * ")} {i,-3} {(i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : ""),-4} {(i < expectedValue.Length ? expectedValue[i].ToSafeString() : ""),-3} {(i < actualValue.Length ? ((int)actualValue[i]).ToString() : ""),-4} {(i < actualValue.Length ? actualValue[i].ToSafeString() : ""),-3}"); } } output.WriteLine(); Assert.AreEqual(expectedValue, actualValue); }
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle, TextWriter output) { if (actualValue == null || expectedValue == null) { Assert.AreEqual(expectedValue, actualValue); return; } if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) { return; } output.WriteLine(" Idx Expected Actual"); output.WriteLine("-------------------------"); int maxLen = Math.Max(actualValue.Length, expectedValue.Length); int minLen = Math.Min(actualValue.Length, expectedValue.Length); for (int i = 0; i < maxLen; i++) { if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i]) { output.WriteLine( "{0} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}", i < minLen && actualValue[i] == expectedValue[i] ? " " : "*", // put a mark beside a differing row i, // the index i < expectedValue.Length ? ((int)expectedValue[i]).ToString() : string.Empty, // character decimal value i < expectedValue.Length ? expectedValue[i].ToSafeString() : string.Empty, // character safe string i < actualValue.Length ? ((int)actualValue[i]).ToString() : string.Empty, // character decimal value i < actualValue.Length ? actualValue[i].ToSafeString() : string.Empty); // character safe string } } output.WriteLine(); Assert.AreEqual(expectedValue, actualValue); }
private static bool HasStyle(IEnumerable <DiffToken> tokens, DiffStyle diffStyle) { return(tokens.Where(t => t.HasStyle(diffStyle)).Any()); }