-
-
Save jarshwah/1610860 to your computer and use it in GitHub Desktop.
String Comparison Unit Test Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TestHelpers | |
{ | |
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue) | |
{ | |
if (actualValue == expectedValue) return; | |
Console.WriteLine("Idx Expected Actual"); | |
Console.WriteLine("---------------------"); | |
var maxLength = Math.Max(actualValue.Length, expectedValue.Length); | |
for (int i = 0; i < maxLength; i++) | |
{ | |
Console.Write(i); | |
Console.Out.WritePadding(i); | |
if (i < actualValue.Length) | |
{ | |
Console.Out.WriteCharInfo(actualValue[i]); | |
} | |
else | |
Console.Write(" "); | |
Console.Write("\t"); | |
if (i < expectedValue.Length) | |
{ | |
Console.Out.WriteCharInfo(expectedValue[i]); | |
} | |
else | |
Console.Write(" "); | |
Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
Assert.Equal(actualValue, expectedValue); | |
} | |
private static void WriteCharInfo(this TextWriter output, char c) | |
{ | |
output.Write((int)c); | |
output.WritePadding(c); | |
output.Write(c.ToSafeString()); | |
} | |
// Assumes strings smaller than 1000 chars. Typical with unit tests. | |
private static void WritePadding(this TextWriter output, int i) | |
{ | |
output.Write(" "); | |
if (i < 100) | |
{ | |
output.Write(" "); | |
} | |
if (i < 10) | |
{ | |
output.Write(" "); | |
} | |
} | |
private static string ToSafeString(this char c) | |
{ | |
switch (c) | |
{ | |
case '\r': | |
return @"\r"; | |
case '\n': | |
return @"\n"; | |
case '\t': | |
return @"\t"; | |
} | |
return c.ToString(CultureInfo.InvariantCulture) + " "; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment