Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
String Comparison Unit Test Helper
public static class TestHelpers
{
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
{
ShouldEqualWithDiff(actualValue, expectedValue, Console.Out);
}
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, TextWriter output)
{
if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) return;
output.WriteLine("Idx Expected Actual");
output.WriteLine("---------------------");
int maxLen = Math.Max(actualValue.Length, expectedValue.Length);
for (int i = 0; i < maxLen; i++)
{
output.Write(i);
output.WritePadding(i);
if (i < actualValue.Length)
{
output.WriteCharInfo(actualValue[i]);
}
else
output.Write(" ");
output.Write("\t");
if (i < expectedValue.Length)
{
output.WriteCharInfo(expectedValue[i]);
}
else
output.Write(" ");
output.WriteLine();
}
output.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)
{
if (Char.IsControl(c) || Char.IsWhiteSpace(c))
{
switch (c)
{
case '\r':
return @"\r";
case '\n':
return @"\n";
case '\t':
return @"\t";
case '\a':
return @"\a";
case '\v':
return @"\v";
case '\f':
return @"\f";
case ' ':
return @"\s";
default:
return String.Format("\\u{0:X};", (int)c);
}
}
return c.ToString(CultureInfo.InvariantCulture) + " ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.