1 // ****************************************************************
\r
2 // Copyright 2007, Charlie Poole
\r
3 // This is free software licensed under the NUnit license. You may
\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
\r
5 // ****************************************************************
\r
7 using System.ComponentModel;
\r
8 using NUnit.Framework.Constraints;
\r
10 namespace NUnit.Framework
\r
13 /// Basic Asserts on strings.
\r
15 public class StringAssert
\r
17 #region Equals and ReferenceEquals
\r
20 /// The Equals method throws an AssertionException. This is done
\r
21 /// to make sure there is no mistake by calling this function.
\r
23 /// <param name="a"></param>
\r
24 /// <param name="b"></param>
\r
25 [EditorBrowsable(EditorBrowsableState.Never)]
\r
26 public static new bool Equals(object a, object b)
\r
28 throw new AssertionException("Assert.Equals should not be used for Assertions");
\r
32 /// override the default ReferenceEquals to throw an AssertionException. This
\r
33 /// implementation makes sure there is no mistake in calling this function
\r
34 /// as part of Assert.
\r
36 /// <param name="a"></param>
\r
37 /// <param name="b"></param>
\r
38 public static new void ReferenceEquals(object a, object b)
\r
40 throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
\r
48 /// Asserts that a string is found within another string.
\r
50 /// <param name="expected">The expected string</param>
\r
51 /// <param name="actual">The string to be examined</param>
\r
52 /// <param name="message">The message to display in case of failure</param>
\r
53 /// <param name="args">Arguments used in formatting the message</param>
\r
54 static public void Contains( string expected, string actual, string message, params object[] args )
\r
56 Assert.That(actual, new SubstringConstraint(expected), message, args);
\r
60 /// Asserts that a string is found within another string.
\r
62 /// <param name="expected">The expected string</param>
\r
63 /// <param name="actual">The string to be examined</param>
\r
64 /// <param name="message">The message to display in case of failure</param>
\r
65 static public void Contains( string expected, string actual, string message )
\r
67 Contains( expected, actual, message, null );
\r
71 /// Asserts that a string is found within another string.
\r
73 /// <param name="expected">The expected string</param>
\r
74 /// <param name="actual">The string to be examined</param>
\r
75 static public void Contains( string expected, string actual )
\r
77 Contains( expected, actual, string.Empty, null );
\r
85 /// Asserts that a string starts with another string.
\r
87 /// <param name="expected">The expected string</param>
\r
88 /// <param name="actual">The string to be examined</param>
\r
89 /// <param name="message">The message to display in case of failure</param>
\r
90 /// <param name="args">Arguments used in formatting the message</param>
\r
91 static public void StartsWith( string expected, string actual, string message, params object[] args )
\r
93 Assert.That(actual, new StartsWithConstraint(expected), message, args);
\r
97 /// Asserts that a string starts with another string.
\r
99 /// <param name="expected">The expected string</param>
\r
100 /// <param name="actual">The string to be examined</param>
\r
101 /// <param name="message">The message to display in case of failure</param>
\r
102 static public void StartsWith( string expected, string actual, string message )
\r
104 StartsWith( expected, actual, message, null );
\r
108 /// Asserts that a string starts with another string.
\r
110 /// <param name="expected">The expected string</param>
\r
111 /// <param name="actual">The string to be examined</param>
\r
112 static public void StartsWith( string expected, string actual )
\r
114 StartsWith( expected, actual, string.Empty, null );
\r
122 /// Asserts that a string ends with another string.
\r
124 /// <param name="expected">The expected string</param>
\r
125 /// <param name="actual">The string to be examined</param>
\r
126 /// <param name="message">The message to display in case of failure</param>
\r
127 /// <param name="args">Arguments used in formatting the message</param>
\r
128 static public void EndsWith( string expected, string actual, string message, params object[] args )
\r
130 Assert.That(actual, new EndsWithConstraint(expected), message, args);
\r
134 /// Asserts that a string ends with another string.
\r
136 /// <param name="expected">The expected string</param>
\r
137 /// <param name="actual">The string to be examined</param>
\r
138 /// <param name="message">The message to display in case of failure</param>
\r
139 static public void EndsWith( string expected, string actual, string message )
\r
141 EndsWith( expected, actual, message, null );
\r
145 /// Asserts that a string ends with another string.
\r
147 /// <param name="expected">The expected string</param>
\r
148 /// <param name="actual">The string to be examined</param>
\r
149 static public void EndsWith( string expected, string actual )
\r
151 EndsWith( expected, actual, string.Empty, null );
\r
156 #region AreEqualIgnoringCase
\r
158 /// Asserts that two strings are equal, without regard to case.
\r
160 /// <param name="expected">The expected string</param>
\r
161 /// <param name="actual">The actual string</param>
\r
162 /// <param name="message">The message to display in case of failure</param>
\r
163 /// <param name="args">Arguments used in formatting the message</param>
\r
164 static public void AreEqualIgnoringCase( string expected, string actual, string message, params object[] args )
\r
166 Assert.That(actual, new EqualConstraint(expected).IgnoreCase, message, args);
\r
170 /// Asserts that two strings are equal, without regard to case.
\r
172 /// <param name="expected">The expected string</param>
\r
173 /// <param name="actual">The actual string</param>
\r
174 /// <param name="message">The message to display in case of failure</param>
\r
175 static public void AreEqualIgnoringCase( string expected, string actual, string message )
\r
177 AreEqualIgnoringCase( expected, actual, message, null );
\r
181 /// Asserts that two strings are equal, without regard to case.
\r
183 /// <param name="expected">The expected string</param>
\r
184 /// <param name="actual">The actual string</param>
\r
185 static public void AreEqualIgnoringCase( string expected, string actual )
\r
187 AreEqualIgnoringCase( expected, actual, string.Empty, null );
\r
194 /// Asserts that a string matches an expected regular expression pattern.
\r
196 /// <param name="expected">The expected expression</param>
\r
197 /// <param name="actual">The actual string</param>
\r
198 /// <param name="message">The message to display in case of failure</param>
\r
199 /// <param name="args">Arguments used in formatting the message</param>
\r
200 static public void IsMatch( string expected, string actual, string message, params object[] args )
\r
202 Assert.That(actual, new RegexConstraint(expected), message, args);
\r
206 /// Asserts that a string matches an expected regular expression pattern.
\r
208 /// <param name="expected">The expected expression</param>
\r
209 /// <param name="actual">The actual string</param>
\r
210 /// <param name="message">The message to display in case of failure</param>
\r
211 static public void IsMatch( string expected, string actual, string message )
\r
213 IsMatch( expected, actual, message, null );
\r
217 /// Asserts that a string matches an expected regular expression pattern.
\r
219 /// <param name="expected">The expected expression</param>
\r
220 /// <param name="actual">The actual string</param>
\r
221 static public void IsMatch( string expected, string actual )
\r
223 IsMatch( expected, actual, string.Empty, null );
\r