1 // ****************************************************************
2 // Copyright 2007, Charlie Poole
3 // This is free software licensed under the NUnit license. You may
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
5 // ****************************************************************
6 
7 using System.ComponentModel;
8 using NUnit.Framework.Constraints;
9 
10 namespace NUnit.Framework
11 {
12 	/// <summary>
13 	/// Basic Asserts on strings.
14 	/// </summary>
15 	public class StringAssert
16 	{
17 		#region Equals and ReferenceEquals
18 
19 		/// <summary>
20 		/// The Equals method throws an AssertionException. This is done
21 		/// to make sure there is no mistake by calling this function.
22 		/// </summary>
23 		/// <param name="a"></param>
24 		/// <param name="b"></param>
25 		[EditorBrowsable(EditorBrowsableState.Never)]
Equals(object a, object b)26 		public static new bool Equals(object a, object b)
27 		{
28 			throw new AssertionException("Assert.Equals should not be used for Assertions");
29 		}
30 
31 		/// <summary>
32 		/// override the default ReferenceEquals to throw an AssertionException. This
33 		/// implementation makes sure there is no mistake in calling this function
34 		/// as part of Assert.
35 		/// </summary>
36 		/// <param name="a"></param>
37 		/// <param name="b"></param>
ReferenceEquals(object a, object b)38 		public static new void ReferenceEquals(object a, object b)
39 		{
40 			throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
41 		}
42 
43 		#endregion
44 
45 		#region Contains
46 
47 		/// <summary>
48 		/// Asserts that a string is found within another string.
49 		/// </summary>
50 		/// <param name="expected">The expected string</param>
51 		/// <param name="actual">The string to be examined</param>
52 		/// <param name="message">The message to display in case of failure</param>
53 		/// <param name="args">Arguments used in formatting the message</param>
Contains( string expected, string actual, string message, params object[] args )54 		static public void Contains( string expected, string actual, string message, params object[] args )
55 		{
56             Assert.That(actual, new SubstringConstraint(expected), message, args);
57 		}
58 
59 		/// <summary>
60 		/// Asserts that a string is found within another string.
61 		/// </summary>
62 		/// <param name="expected">The expected string</param>
63 		/// <param name="actual">The string to be examined</param>
64 		/// <param name="message">The message to display in case of failure</param>
Contains( string expected, string actual, string message )65 		static public void Contains( string expected, string actual, string message )
66 		{
67 			Contains( expected, actual, message, null );
68 		}
69 
70 		/// <summary>
71 		/// Asserts that a string is found within another string.
72 		/// </summary>
73 		/// <param name="expected">The expected string</param>
74 		/// <param name="actual">The string to be examined</param>
Contains( string expected, string actual )75 		static public void Contains( string expected, string actual )
76 		{
77 			Contains( expected, actual, string.Empty, null );
78 		}
79 
80 		#endregion
81 
82 		#region StartsWith
83 
84 		/// <summary>
85 		/// Asserts that a string starts with another string.
86 		/// </summary>
87 		/// <param name="expected">The expected string</param>
88 		/// <param name="actual">The string to be examined</param>
89 		/// <param name="message">The message to display in case of failure</param>
90 		/// <param name="args">Arguments used in formatting the message</param>
StartsWith( string expected, string actual, string message, params object[] args )91 		static public void StartsWith( string expected, string actual, string message, params object[] args )
92 		{
93             Assert.That(actual, new StartsWithConstraint(expected), message, args);
94 		}
95 
96 		/// <summary>
97 		/// Asserts that a string starts with another string.
98 		/// </summary>
99 		/// <param name="expected">The expected string</param>
100 		/// <param name="actual">The string to be examined</param>
101 		/// <param name="message">The message to display in case of failure</param>
StartsWith( string expected, string actual, string message )102 		static public void StartsWith( string expected, string actual, string message )
103 		{
104 			StartsWith( expected, actual, message, null );
105 		}
106 
107 		/// <summary>
108 		/// Asserts that a string starts with another string.
109 		/// </summary>
110 		/// <param name="expected">The expected string</param>
111 		/// <param name="actual">The string to be examined</param>
StartsWith( string expected, string actual )112 		static public void StartsWith( string expected, string actual )
113 		{
114 			StartsWith( expected, actual, string.Empty, null );
115 		}
116 
117 		#endregion
118 
119 		#region EndsWith
120 
121 		/// <summary>
122 		/// Asserts that a string ends with another string.
123 		/// </summary>
124 		/// <param name="expected">The expected string</param>
125 		/// <param name="actual">The string to be examined</param>
126 		/// <param name="message">The message to display in case of failure</param>
127 		/// <param name="args">Arguments used in formatting the message</param>
EndsWith( string expected, string actual, string message, params object[] args )128 		static public void EndsWith( string expected, string actual, string message, params object[] args )
129 		{
130             Assert.That(actual, new EndsWithConstraint(expected), message, args);
131 		}
132 
133 		/// <summary>
134 		/// Asserts that a string ends with another string.
135 		/// </summary>
136 		/// <param name="expected">The expected string</param>
137 		/// <param name="actual">The string to be examined</param>
138 		/// <param name="message">The message to display in case of failure</param>
EndsWith( string expected, string actual, string message )139 		static public void EndsWith( string expected, string actual, string message )
140 		{
141 			EndsWith( expected, actual, message, null );
142 		}
143 
144 		/// <summary>
145 		/// Asserts that a string ends with another string.
146 		/// </summary>
147 		/// <param name="expected">The expected string</param>
148 		/// <param name="actual">The string to be examined</param>
EndsWith( string expected, string actual )149 		static public void EndsWith( string expected, string actual )
150 		{
151 			EndsWith( expected, actual, string.Empty, null );
152 		}
153 
154 		#endregion
155 
156 		#region AreEqualIgnoringCase
157 		/// <summary>
158 		/// Asserts that two strings are equal, without regard to case.
159 		/// </summary>
160 		/// <param name="expected">The expected string</param>
161 		/// <param name="actual">The actual string</param>
162 		/// <param name="message">The message to display in case of failure</param>
163 		/// <param name="args">Arguments used in formatting the message</param>
AreEqualIgnoringCase( string expected, string actual, string message, params object[] args )164 		static public void AreEqualIgnoringCase( string expected, string actual, string message, params object[] args )
165 		{
166             Assert.That(actual, new EqualConstraint(expected).IgnoreCase, message, args);
167 		}
168 
169 		/// <summary>
170 		/// Asserts that two strings are equal, without regard to case.
171 		/// </summary>
172 		/// <param name="expected">The expected string</param>
173 		/// <param name="actual">The actual string</param>
174 		/// <param name="message">The message to display in case of failure</param>
AreEqualIgnoringCase( string expected, string actual, string message )175 		static public void AreEqualIgnoringCase( string expected, string actual, string message )
176 		{
177 			AreEqualIgnoringCase( expected, actual, message, null );
178 		}
179 
180 		/// <summary>
181 		/// Asserts that two strings are equal, without regard to case.
182 		/// </summary>
183 		/// <param name="expected">The expected string</param>
184 		/// <param name="actual">The actual string</param>
AreEqualIgnoringCase( string expected, string actual )185 		static public void AreEqualIgnoringCase( string expected, string actual )
186 		{
187 			AreEqualIgnoringCase( expected, actual, string.Empty, null );
188 		}
189 
190 		#endregion
191 
192 		#region IsMatch
193 		/// <summary>
194 		/// Asserts that a string matches an expected regular expression pattern.
195 		/// </summary>
196 		/// <param name="expected">The expected expression</param>
197 		/// <param name="actual">The actual string</param>
198 		/// <param name="message">The message to display in case of failure</param>
199 		/// <param name="args">Arguments used in formatting the message</param>
IsMatch( string expected, string actual, string message, params object[] args )200 		static public void IsMatch( string expected, string actual, string message, params object[] args )
201 		{
202             Assert.That(actual, new RegexConstraint(expected), message, args);
203 		}
204 
205 		/// <summary>
206 		/// Asserts that a string matches an expected regular expression pattern.
207 		/// </summary>
208 		/// <param name="expected">The expected expression</param>
209 		/// <param name="actual">The actual string</param>
210 		/// <param name="message">The message to display in case of failure</param>
IsMatch( string expected, string actual, string message )211 		static public void IsMatch( string expected, string actual, string message )
212 		{
213 			IsMatch( expected, actual, message, null );
214 		}
215 
216 		/// <summary>
217 		/// Asserts that a string matches an expected regular expression pattern.
218 		/// </summary>
219 		/// <param name="expected">The expected expression</param>
220 		/// <param name="actual">The actual string</param>
IsMatch( string expected, string actual )221 		static public void IsMatch( string expected, string actual )
222 		{
223 			IsMatch( expected, actual, string.Empty, null );
224 		}
225 		#endregion
226 	}
227 }
228