String Constraints (NUnit 2.4)
String constraints perform tests that are specific to strings. The following string constraints are provided.
Syntax Helpers | Constructor | Operation |
---|---|---|
Text.Contains( string ) Text.DoesNotContain( string ) | SubstringConstraint( string ) | tests for a substring |
Text.StartsWith( string ) Text.DoesNotStartWith( string ) | StartsWithConstraint( string ) | tests for an initial string |
Text.EndsWith( string ) Text.DoesNotEndWith( string ) | EndsWithConstraint( string ) | tests for an ending string |
Text.Matches( string ) Text.DoesNotMatch( string ) | RegexConstraint( string ) | tests that a pattern is matched |
Examples
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Text.Contains( "tests fail" ) ); Assert.That( phrase, Text.Contains( "make" ).IgnoreCase ); Assert.That( phrase, Text.StartsWith( "Make" ) ); Assert.That( phrase, Text.Not.StartsWith( "Break" ) ); Assert.That( phrase, Text.DoesNotStartWith( "Break" ) ); Assert.That( phrase, Text.EndsWith( "!" ) ); Assert.That( phrase, Text.EndsWith( "PASSING!" ).IgnoreCase ); Assert.That( phrase, Text.Matches( "Make.*tests.*pass" ) ); Assert.That( phrase, Text.Not.Matches( "your.*passing.*tests" ) ); Assert.That( phrase, Text.DoesNotMatch( "your.*passing.*tests" ) ); // Using Inheritance Expect( phrase, Contains( "make" ).IgnoreCase ); Expect( phrase, EndsWith( "!" ) ); Expect( phrase, Matches( "Make.*pass" ) );