// **************************************************************** // Copyright 2007, Charlie Poole // This is free software licensed under the NUnit license. You may // obtain a copy of the license at http://nunit.org/?p=license&r=2.4 // **************************************************************** using System; using System.Text.RegularExpressions; namespace NUnit.Framework.Constraints { /// /// EmptyStringConstraint tests whether a string is empty. /// public class EmptyStringConstraint : EmptyConstraint { /// /// Test whether the constraint is satisfied by a given value /// /// The value to be tested /// True for success, false for failure public override bool Matches(object actual) { this.actual = actual; if ( !(actual is string) ) return false; return (string)actual == string.Empty; } /// /// Write the constraint description to a MessageWriter /// /// The writer on which the description is displayed public override void WriteDescriptionTo(MessageWriter writer) { writer.Write( "" ); } } /// /// SubstringConstraint can test whether a string contains /// the expected substring. /// public class SubstringConstraint : Constraint { string expected; /// /// Initializes a new instance of the class. /// /// The expected. public SubstringConstraint(string expected) { this.expected = expected; } /// /// Test whether the constraint is satisfied by a given value /// /// The value to be tested /// True for success, false for failure public override bool Matches(object actual) { this.actual = actual; if ( !(actual is string) ) return false; if (this.caseInsensitive) return ((string)actual).ToLower().IndexOf(expected.ToLower()) >= 0; else return ((string)actual).IndexOf(expected) >= 0; } /// /// Write the constraint description to a MessageWriter /// /// The writer on which the description is displayed public override void WriteDescriptionTo(MessageWriter writer) { writer.WritePredicate("String containing"); writer.WriteExpectedValue(expected); if ( this.caseInsensitive ) writer.WriteModifier( "ignoring case" ); } } /// /// StartsWithConstraint can test whether a string starts /// with an expected substring. /// public class StartsWithConstraint : Constraint { private string expected; /// /// Initializes a new instance of the class. /// /// The expected string public StartsWithConstraint(string expected) { this.expected = expected; } /// /// Test whether the constraint is matched by the actual value. /// This is a template method, which calls the IsMatch method /// of the derived class. /// /// /// public override bool Matches(object actual) { this.actual = actual; if (!(actual is string)) return false; if ( this.caseInsensitive ) return ((string)actual).ToLower().StartsWith(expected.ToLower()); else return ((string)actual).StartsWith(expected); } /// /// Write the constraint description to a MessageWriter /// /// The writer on which the description is displayed public override void WriteDescriptionTo(MessageWriter writer) { writer.WritePredicate("String starting with"); writer.WriteExpectedValue( MsgUtils.ClipString(expected, writer.MaxLineLength - 40, 0) ); if ( this.caseInsensitive ) writer.WriteModifier( "ignoring case" ); } } /// /// EndsWithConstraint can test whether a string ends /// with an expected substring. /// public class EndsWithConstraint : Constraint { private string expected; /// /// Initializes a new instance of the class. /// /// The expected string public EndsWithConstraint(string expected) { this.expected = expected; } /// /// Test whether the constraint is matched by the actual value. /// This is a template method, which calls the IsMatch method /// of the derived class. /// /// /// public override bool Matches(object actual) { this.actual = actual; if (!(actual is string)) return false; if ( this.caseInsensitive ) return ((string)actual).ToLower().EndsWith(expected.ToLower()); else return ((string)actual).EndsWith(expected); } /// /// Write the constraint description to a MessageWriter /// /// The writer on which the description is displayed public override void WriteDescriptionTo(MessageWriter writer) { writer.WritePredicate("String ending with"); writer.WriteExpectedValue(expected); if ( this.caseInsensitive ) writer.WriteModifier( "ignoring case" ); } } /// /// RegexConstraint can test whether a string matches /// the pattern provided. /// public class RegexConstraint : Constraint { string pattern; /// /// Initializes a new instance of the class. /// /// The pattern. public RegexConstraint(string pattern) { this.pattern = pattern; } /// /// Test whether the constraint is satisfied by a given value /// /// The value to be tested /// True for success, false for failure public override bool Matches(object actual) { this.actual = actual; return actual is string && Regex.IsMatch( (string)actual, this.pattern, this.caseInsensitive ? RegexOptions.IgnoreCase : RegexOptions.None ); } /// /// Write the constraint description to a MessageWriter /// /// The writer on which the description is displayed public override void WriteDescriptionTo(MessageWriter writer) { writer.WritePredicate("String matching"); writer.WriteExpectedValue(this.pattern); if ( this.caseInsensitive ) writer.WriteModifier( "ignoring case" ); } } }