Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Forked from haacked/TestHelper.cs
Created January 14, 2012 09:49
Show Gist options
  • Save jarshwah/1610860 to your computer and use it in GitHub Desktop.
Save jarshwah/1610860 to your computer and use it in GitHub Desktop.
String Comparison Unit Test Helper
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