📄 failuremessagefixture.cs
字号:
FormatMessageForFailNotEquals(null, null, "message" ) );
Assert.AreEqual( "message \r\n\texpected:<1>\r\n\t but was:<2>",
FormatMessageForFailNotEquals( 1, 2, "message" ) );
Assert.AreEqual( "message \r\n\texpected:<\"\">\r\n\t but was:<(null)>",
FormatMessageForFailNotEquals( "", null, "message" ) );
Assert.AreEqual( "message \r\n\texpected:<1>\r\n\t but was:<\"1\">",
FormatMessageForFailNotEquals( 1, "1", "message" ) );
Assert.AreEqual( "message 5 \r\n\texpected:<1>\r\n\t but was:<\"1\">",
FormatMessageForFailNotEquals( 1, "1", "message {0}", 5 ) );
AnalyzeMessageForStrings( "a", "aa", "message" );
AnalyzeMessageForStrings( "aa", "ab", "message" );
AnalyzeMessageForStrings( "a", "abc", "message" );
AnalyzeMessageForStrings( "123", "1x3", "message" );
AnalyzeMessageForStrings( "12345", "123", "message" );
}
private void AnalyzeMessageForStrings( string expected, string actual, string message )
{
string[] lines = SplitMessage(
FormatMessageForFailNotEquals( expected, actual, message ) );
string msg = string.Format( "Testing expected={0}, actual={1}", expected, actual );
// First line should contain the user message
Assert.AreEqual( message, lines[0], msg );
// Second line compares the lengths
Assert.AreEqual(
expected.Length == actual.Length ? string.Format( "\tString lengths are both {0}.", expected.Length )
: string.Format( "\tString lengths differ. Expected length={0}, but was length={1}.", expected.Length, actual.Length ),
lines[1], msg );
// Third line indicates the point of difference
int index = 0;
while ( index < expected.Length && index < actual.Length
&& actual[index] == expected[index] )
index++;
Assert.AreEqual( string.Format( "\tStrings differ at index {0}.", index ), lines[2], msg );
// Fourth line is empty
Assert.AreEqual( string.Empty, lines[3], msg );
// Fifth line gives the expected value
Assert.AreEqual( string.Format( "\texpected:<\"{0}\">", expected, msg ), lines[4], msg );
// Sixth line gives the actual value
Assert.AreEqual( string.Format( "\t but was:<\"{0}\">", actual, msg ), lines[5], msg );
// Seventh line contains dashes and a caret. The caret should point
// at the first nomatching character in the strings. This works
// even though the lines may contain ellipses. The line should
// contain a tab, dashes and the caret. We add 11 to match the
// initial string "expected:" plus the opening quote.
string caretLine = "\t" + new String( '-', index + 11 ) + "^";
Assert.AreEqual( caretLine, lines[6] );
}
private string[] SplitMessage( string msg )
{
int index = 0;
ArrayList lines = new ArrayList();
while ( index < msg.Length )
{
int next = msg.IndexOf( "\r\n", index );
if ( next < 0 )
next = msg.Length;
else
next = next + 2;
lines.Add( msg.Substring( index, next - index ).TrimEnd() );
index = next;
}
return (string[])lines.ToArray( typeof( string ) );
}
/// <summary>
/// Checks several common failure conditions to ensure the output
/// strings match the expected output when newlines are in the
/// string. Newlines (CR or LF) characters are rendered in the
/// output string as "\"+"r" or "\"+"n", so that they can be
/// seen, and also to preserve the alignment of the output
/// position differences. Without this, the lines will wrap
/// when displayed, and the differences marker will not align
/// where the differences actually are.
/// </summary>
[Test]
public void TestFormatMessageForFailNotEqualsNewlines()
{
Assert.AreEqual( "message \r\n\tString lengths differ. Expected length=2, but was length=3.\r\n\tStrings differ at index 1.\r\n\t\r\n\texpected:<\"a\\r\">\r\n\t but was:<\"aa\\r\">\r\n\t------------^\r\n\t",
FormatMessageForFailNotEquals( "a\r", "aa\r", "message" ) );
Assert.AreEqual( "message \r\n\tString lengths differ. Expected length=2, but was length=3.\r\n\tStrings differ at index 1.\r\n\t\r\n\texpected:<\"a\\n\">\r\n\t but was:<\"aa\\n\">\r\n\t------------^\r\n\t",
FormatMessageForFailNotEquals( "a\n", "aa\n", "message" ) );
Assert.AreEqual( "message \r\n\tString lengths are both 6.\r\n\tStrings differ at index 5.\r\n\t\r\n\texpected:<\"aa\\r\\naa\">\r\n\t but was:<\"aa\\r\\nab\">\r\n\t------------------^\r\n\t",
FormatMessageForFailNotEquals( "aa\r\naa", "aa\r\nab", "message" ) );
}
/// <summary>
/// Test to track down a bug with string length differences
/// One string has length 164, the other length 166. Content is the
/// same for both strings, but the longer string has 2 extra chars.
/// </summary>
[Test]
public void TestStringLengthsDiffer()
{
// 2 strings, one length 166, the other length 164. Indexes for each:
// length 166: index=0...165
// length 164: index=0...163
// Assume content is same for all 164 common characters, and only the length
// is different, so we want to show the length marker starting at position
// 164
// 111111111111111111111111111111
// 444444444455555555556666666666
// 012345678901234567890123456789
// ...same content to here!++ length 166 has 2 extra chars
// ...same content to here! length 164
// ------------------------^
// On entry, the iPosition will be 164 since we start mismatching after
// the first string ends (since all content is the same)
// Need to clip appropriately when iPosition=164, and build characters
// for the shorter string appropriately. Longer string will be able
// to be clipped up to position 164, but 164 will be past the end of
// the short string.
string sFirst = "00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666";
string sSecond = "00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666++";
Assert.AreEqual(
"\r\n\tString lengths differ. Expected length=164, but was length=166.\r\n\tStrings differ at index 164.\r\n\t\r\n\texpected:<\"...23333333333444444444455555555556666\">\r\n\t but was:<\"...23333333333444444444455555555556666++\">\r\n\t" + (new string('-',ButWasText().Length+"...".Length+PreClipLength+1)) + "^\r\n\t",
FormatMessageForFailNotEquals( sFirst, sSecond, null ) );
}
/// <summary>
/// Test to verify conditions found by a bug, where strings have same
/// content bug lengths are different. Checking lengths shorter than
/// clipping range, with 2 extra chars on longer string.
/// </summary>
[Test]
public void TestStringLengthsDiffer2()
{
string sFirst = "0000000000111111111122";
string sSecond = "0000000000111111111122++";
Assert.AreEqual( "\r\n\tString lengths differ. Expected length=22, but was length=24.\r\n\tStrings differ at index 22.\r\n\t\r\n\texpected:<\"0000000000111111111122\">\r\n\t but was:<\"0000000000111111111122++\">\r\n\t" + (new string('-',ButWasText().Length+23)) + "^\r\n\t",
FormatMessageForFailNotEquals( sFirst, sSecond, null ) );
}
/// <summary>
/// Exhaustive test, all strings up to 200 characters in length,
/// with one string 1, 2, 3, 4, or 5 characters longer than the other.
///
/// Verifies conditions found by a bug, that appears when strings have same
/// content bug lengths are different.
/// </summary>
[Test]
public void TestStringLengthsDiffer3()
{
VerifySameContentDifferentLengths( "+" );
VerifySameContentDifferentLengths( "++" );
VerifySameContentDifferentLengths( "+++" );
VerifySameContentDifferentLengths( "++++" );
VerifySameContentDifferentLengths( "+++++" );
}
private void VerifySameContentDifferentLengths( string sExtra )
{
//
// Makes sure N-longer is ok just up to the point where
// clipping would occur
//
for( int i=1; i<=PreClipLength; i++ )
{
string sFirst = new string( '=', i );
string sSecond = new string( '=', i ) + sExtra;
Assert.AreEqual( "\r\n\tString lengths differ. Expected length=" + i + ", but was length=" + (i+sExtra.Length) + ".\r\n\tStrings differ at index "+ i +".\r\n\t\r\n\texpected:<\""+ sFirst +"\">\r\n\t but was:<\""+ sSecond +"\">\r\n\t" + (new string('-',ButWasText().Length+i+1)) + "^\r\n\t",
FormatMessageForFailNotEquals( sFirst, sSecond, null ),
"Failed at index " + i);
}
//
// Makes sure N-longer is ok from for strings equal in length
// to the start of clipping and longer
//
string sExpected = "..." + new string( '=', PreClipLength );
string sActual = "..." + new string( '=', PreClipLength ) + sExtra;
for( int i=PreClipLength+1; i<200; i++ )
{
string sFirst = new string( '=', i );
string sSecond = new string( '=', i ) + sExtra;
Assert.AreEqual( "\r\n\tString lengths differ. Expected length=" + i + ", but was length=" + (i+sExtra.Length) + ".\r\n\tStrings differ at index "+ i +".\r\n\t\r\n\texpected:<\""+ sExpected +"\">\r\n\t but was:<\""+ sActual +"\">\r\n\t" + (new string('-',ButWasText().Length+"...".Length+PreClipLength+1)) + "^\r\n\t",
FormatMessageForFailNotEquals( sFirst, sSecond, null ),
"Failed at index " + i );
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -