📄 assertionfailuremessage.cs
字号:
}
else
{
BuildStringLengthSameReport( sbOutput, sExpected, sActual );
}
}
/// <summary>
/// Reports the length of two arrays that are different lengths
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
static protected void BuildArrayLengthDifferentReport( StringBuilder sbOutput, Array expected, Array actual )
{
sbOutput.Append( "Array lengths differ. Expected length=" );
sbOutput.Append( expected.Length );
sbOutput.Append( ", but was length=" );
sbOutput.Append( actual.Length );
sbOutput.Append( "." );
sbOutput.Append( NewLine );
}
/// <summary>
/// Reports the length of two arrays that are the same length
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
static protected void BuildArrayLengthSameReport( StringBuilder sbOutput, Array expected, Array actual )
{
sbOutput.Append( "Array lengths are both " );
sbOutput.Append( expected.Length );
sbOutput.Append( "." );
sbOutput.Append( NewLine );
}
/// <summary>
/// Reports whether the array lengths are the same or different, and
/// what the array lengths are.
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
static protected void BuildArrayLengthReport( StringBuilder sbOutput, Array expected, Array actual )
{
if( expected.Length != actual.Length )
{
BuildArrayLengthDifferentReport( sbOutput, expected, actual );
}
else
{
BuildArrayLengthSameReport( sbOutput, expected, actual );
}
}
/// <summary>
///
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="sExpected"></param>
/// <param name="sActual"></param>
/// <param name="iPosition"></param>
static private void BuildContentDifferentAtPosition( StringBuilder sbOutput, string sExpected, string sActual, int iPosition )
{
BuildStringLengthReport( sbOutput, sExpected, sActual );
sbOutput.Append( "Strings differ at index " );
sbOutput.Append( iPosition );
sbOutput.Append( "." );
sbOutput.Append( NewLine );
//
// Clips the strings, then turns any hidden whitespace into visible
// characters
//
string sClippedExpected = ConvertWhitespace(ClipAroundPosition( sExpected, iPosition ));
string sClippedActual = ConvertWhitespace(ClipAroundPosition( sActual, iPosition ));
AppendExpectedAndActual(
sbOutput,
sClippedExpected,
sClippedActual );
sbOutput.Append( NewLine );
// Add a line showing where they differ. If the string lengths are
// different, they start differing just past the length of the
// shorter string
AppendPositionMarker(
sbOutput,
FindMismatchPosition( sClippedExpected, sClippedActual, 0 ) );
sbOutput.Append( NewLine );
}
/// <summary>
/// Turns CR, LF, or TAB into visual indicator to preserve visual marker
/// position. This is done by replacing the '\r' into '\\' and 'r'
/// characters, and the '\n' into '\\' and 'n' characters, and '\t' into
/// '\\' and 't' characters.
///
/// Thus the single character becomes two characters for display.
/// </summary>
/// <param name="sInput"></param>
/// <returns></returns>
static protected string ConvertWhitespace( string sInput )
{
if( null != sInput )
{
sInput = sInput.Replace( "\r", "\\r" );
sInput = sInput.Replace( "\n", "\\n" );
sInput = sInput.Replace( "\t", "\\t" );
}
return sInput;
}
/// <summary>
/// Shows the position two strings start to differ. Comparison
/// starts at the start index.
/// </summary>
/// <param name="sExpected"></param>
/// <param name="sActual"></param>
/// <param name="iStart"></param>
/// <returns>-1 if no mismatch found, or the index where mismatch found</returns>
static private int FindMismatchPosition( string sExpected, string sActual, int iStart )
{
int iLength = Math.Min( sExpected.Length, sActual.Length );
for( int i=iStart; i<iLength; i++ )
{
//
// If they mismatch at a specified position, report the
// difference.
//
if( sExpected[i] != sActual[i] )
{
return i;
}
}
//
// Strings have same content up to the length of the shorter string.
// Mismatch occurs because string lengths are different, so show
// that they start differing where the shortest string ends
//
if( sExpected.Length != sActual.Length )
{
return iLength;
}
//
// Same strings
//
Assert.IsTrue( sExpected.Equals( sActual ) );
return -1;
}
/// <summary>
/// Constructs a message that can be displayed when the content of two
/// strings are different, but the string lengths are the same. The
/// message will clip the strings to a reasonable length, centered
/// around the first position where they are mismatched, and draw
/// a line marking the position of the difference to make comparison
/// quicker.
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="sExpected"></param>
/// <param name="sActual"></param>
static protected void BuildContentDifferentMessage( StringBuilder sbOutput, string sExpected, string sActual )
{
//
// If they mismatch at a specified position, report the
// difference.
//
int iMismatch = FindMismatchPosition( sExpected, sActual, 0 );
if( -1 != iMismatch )
{
BuildContentDifferentAtPosition(
sbOutput,
sExpected,
sActual,
iMismatch );
return;
}
//
// If the lengths differ, but they match up to the length,
// show the difference just past the length of the shorter
// string
//
if( sExpected.Length != sActual.Length )
{
BuildContentDifferentAtPosition(
sbOutput,
sExpected,
sActual,
Math.Min(sExpected.Length, sActual.Length) );
}
}
/// <summary>
/// Called to append a message when the input strings are different.
/// A different message is rendered when the lengths are mismatched,
/// and when the lengths match but content is mismatched.
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
static private void BuildStringsDifferentMessage( StringBuilder sbOutput, string expected, string actual )
{
sbOutput.Append( NewLine );
if( LengthsDifferent( expected, actual ) )
{
BuildLengthsDifferentMessage( sbOutput, expected, actual );
}
else
{
BuildContentDifferentMessage( sbOutput, expected, actual );
}
}
/// <summary>
/// Called to append a message when the input arrays are different.
/// A different message is rendered when the lengths are mismatched,
/// and when the lengths match but content is mismatched.
/// </summary>
/// <param name="sbOutput"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
static private void BuildArraysDifferentMessage( StringBuilder sbOutput, int index, Array expected, Array actual )
{
sbOutput.Append( NewLine );
BuildArrayLengthReport( sbOutput, expected, actual );
sbOutput.Append( "Arrays differ at index " );
sbOutput.Append( index );
sbOutput.Append( "." );
sbOutput.Append( NewLine );
if ( index < expected.Length && index < actual.Length )
{
if( InputsAreStrings( expected.GetValue(index), actual.GetValue(index) ) )
{
BuildStringsDifferentMessage(
sbOutput,
(string)expected.GetValue(index),
(string)actual.GetValue(index) );
}
else
{
AppendExpectedAndActual( sbOutput, expected.GetValue(index), actual.GetValue(index) );
}
}
else if( expected.Length < actual.Length )
{
sbOutput.Append( NewLine );
sbOutput.Append( " extra:<" );
DisplayElements( sbOutput, actual, index, 3 );
sbOutput.Append( ">" );
}
else
{
sbOutput.Append( NewLine );
sbOutput.Append( " missing:<" );
DisplayElements( sbOutput, expected, index, 3 );
sbOutput.Append( ">" );
}
return;
}
static private void DisplayElements( StringBuilder sbOutput, Array array, int index, int max )
{
for( int i = 0; i < max; i++ )
{
sbOutput.Append( DisplayString( array.GetValue(index++) ) );
if ( index >= array.Length )
return;
sbOutput.Append( "," );
}
sbOutput.Append( "..." );
}
/// <summary>
/// Used to create a StringBuilder that is used for constructing
/// the output message when text is different. Handles initialization
/// when a message is provided. If message is null, an empty
/// StringBuilder is returned.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
static protected StringBuilder CreateStringBuilder( string message, params object[] args )
{
StringBuilder sbOutput;
if (message != null)
{
if ( args != null && args.Length > 0 )
sbOutput = new StringBuilder( string.Format( message, args ) );
else
sbOutput = new StringBuilder( message );
}
else
{
sbOutput = new StringBuilder();
}
return sbOutput;
}
/// <summary>
/// Called to create a message when two objects have been found to
/// be unequal. If the inputs are strings, a special message is
/// rendered that can help track down where the strings are different,
/// based on differences in length, or differences in content.
///
/// If the inputs are not strings, the ToString method of the objects
/// is used to show what is different about them.
/// </summary>
/// <param name="expected"></param>
/// <param name="actual"></param>
/// <param name="message"></param>
/// <param name="args"></param>
/// <returns></returns>
static public string FormatMessageForFailNotEquals(Object expected, Object actual,
string message, params object[] args)
{
StringBuilder sbOutput = CreateStringBuilder( message, args );
if( null != message )
{
if( message.Length > 0 )
{
sbOutput.Append( " " );
}
}
if( InputsAreStrings( expected, actual ) )
{
BuildStringsDifferentMessage(
sbOutput,
(string)expected,
(string)actual );
}
else
{
AppendExpectedAndActual( sbOutput, expected, actual );
}
return sbOutput.ToString();
}
/// <summary>
/// Called to create a message when two arrays are not equal.
/// </summary>
/// <param name="message"></param>
/// <param name="expected"></param>
/// <param name="actual"></param>
/// <returns></returns>
static public string FormatMessageForFailArraysNotEqual(int index, Array expected, Array actual,
string message, params object[] args)
{
StringBuilder sbOutput = CreateStringBuilder( message, args );
if( null != message )
{
if( message.Length > 0 )
{
sbOutput.Append( " " );
}
}
BuildArraysDifferentMessage(
sbOutput,
index,
expected,
actual );
return sbOutput.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -