using System; namespace NUnit.Framework { #region StringAsserter /// /// Abstract class used as a base for asserters that compare /// expected and an actual string values in some way or another. /// public abstract class StringAsserter : AbstractAsserter { /// /// The expected value, used as the basis for comparison. /// protected string expected; /// /// The actual value to be compared. /// protected string actual; /// /// Constructs a StringAsserter for two strings /// /// The expected value /// The actual value /// The message to issue on failure /// Arguments to apply in formatting the message public StringAsserter( string expected, string actual, string message, params object[] args ) : base( message, args ) { this.expected = expected; this.actual = actual; } /// /// Message related to a failure. If no failure has /// occured, the result is unspecified. /// public override string Message { get { FailureMessage.AddExpectedLine( Expectation ); FailureMessage.DisplayActualValue( actual ); return FailureMessage.ToString(); } } /// /// String value that represents what the asserter expected /// to find. Defaults to the expected value itself. /// protected virtual string Expectation { get { return string.Format( "<\"{0}\">", expected ); } } } #endregion #region ContainsAsserter /// /// Summary description for ContainsAsserter. /// public class ContainsAsserter : StringAsserter { /// /// Constructs a ContainsAsserter for two strings /// /// The expected substring /// The actual string to be examined /// The message to issue on failure /// Arguments to apply in formatting the message public ContainsAsserter( string expected, string actual, string message, params object[] args ) : base( expected, actual, message, args ) { } /// /// Test the assertion. /// /// True if the test succeeds public override bool Test() { return actual.IndexOf( expected ) >= 0; } /// /// String value that represents what the asserter expected /// protected override string Expectation { get { return string.Format( "String containing \"{0}\"", expected ); } } } #endregion #region StartsWithAsserter /// /// Summary description for StartsWithAsserter. /// public class StartsWithAsserter : StringAsserter { /// /// Constructs a StartsWithAsserter for two strings /// /// The expected substring /// The actual string to be examined /// The message to issue on failure /// Arguments to apply in formatting the message public StartsWithAsserter( string expected, string actual, string message, params object[] args ) : base( expected, actual, message, args ) { } /// /// Test the assertion. /// /// True if the test succeeds public override bool Test() { return actual.StartsWith( expected ); } /// /// String value that represents what the asserter expected /// protected override string Expectation { get { return string.Format( "String starting with \"{0}\"", expected ); } } } #endregion #region EndsWithAsserter /// /// Summary description for EndsWithAsserter. /// public class EndsWithAsserter : StringAsserter { /// /// Constructs a EndsWithAsserter for two strings /// /// The expected substring /// The actual string to be examined /// The message to issue on failure /// Arguments to apply in formatting the message public EndsWithAsserter( string expected, string actual, string message, params object[] args ) : base( expected, actual, message, args ) { } /// /// Test the assertion. /// /// True if the test succeeds public override bool Test() { return actual.EndsWith( expected ); } /// /// String value that represents what the asserter expected /// protected override string Expectation { get { return string.Format( "String ending with \"{0}\"", expected ); } } } #endregion #region EqualIgnoringCaseAsserter /// /// Asserter that implements AreEqualIgnoringCase /// public class EqualIgnoringCaseAsserter : StringAsserter { /// /// Constructs an EqualIgnoringCaseAsserter for two strings /// /// The expected string /// The actual string /// The message to issue on failure /// Arguments to apply in formatting the message public EqualIgnoringCaseAsserter( string expected, string actual, string message, params object[] args ) : base( expected, actual, message, args ) { } /// /// Test the assertion. /// /// True if the test succeeds public override bool Test() { return string.Compare( expected, actual, true ) == 0; } /// /// String value that represents what the asserter expected /// public override string Message { get { FailureMessage.DisplayDifferences( expected, actual, true ); return FailureMessage.ToString(); } } } #endregion }