1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
31 using System.Collections;
32 using System.ComponentModel;
34 namespace NUnit.Framework
39 /// A private constructor disallows any instances of this object.
45 /// Asserts that a condition is true. If the condition is false the method throws
46 /// an <see cref="AssertionException"/>.
48 /// <param name="message">The message to display if the condition is false</param>
49 /// <param name="condition">The evaluated condition</param>
50 static public void IsTrue(bool condition, string message)
57 /// Asserts that a condition is true. If the condition is false the method throws
58 /// an <see cref="AssertionException"/>.
60 /// <param name="condition">The evaluated condition</param>
61 static public void IsTrue(bool condition)
63 Assert.IsTrue(condition, string.Empty);
67 /// Asserts that a condition is false. If the condition is true the method throws
68 /// an <see cref="AssertionException"/>.
70 /// <param name="message">The message to display if the condition is true</param>
71 /// <param name="condition">The evaluated condition</param>
72 static public void IsFalse(bool condition, string message)
79 /// Asserts that a condition is false. If the condition is true the method throws
80 /// an <see cref="AssertionException"/>.
82 /// <param name="condition">The evaluated condition</param>
83 static public void IsFalse(bool condition)
85 Assert.IsFalse(condition, string.Empty);
89 /// Verifies that two doubles are equal considering a delta. If the
90 /// expected value is infinity then the delta value is ignored. If
91 /// they are not equals then an <see cref="AssertionException"/> is
94 /// <param name="message">The message that will be printed on failure</param>
95 /// <param name="expected">The expected value</param>
96 /// <param name="actual">The actual value</param>
97 /// <param name="delta">The maximum acceptable difference between the
98 /// the expected and the actual</param>
99 static public void AreEqual(double expected,
100 double actual, double delta, string message)
102 // handle infinity specially since subtracting two infinite values gives
103 // NaN and the following test fails
104 if (double.IsInfinity(expected))
106 if (!(expected == actual))
107 Assert.FailNotEquals(expected, actual, message);
109 else if (!(Math.Abs(expected-actual) <= delta))
110 Assert.FailNotEquals(expected, actual, message);
114 /// Verifies that two doubles are equal considering a delta. If the
115 /// expected value is infinity then the delta value is ignored. If
116 /// they are not equals then an <see cref="AssertionException"/> is
119 /// <param name="expected">The expected value</param>
120 /// <param name="actual">The actual value</param>
121 /// <param name="delta">The maximum acceptable difference between the
122 /// the expected and the actual</param>
123 static public void AreEqual(double expected, double actual, double delta)
125 Assert.AreEqual(expected, actual, delta, string.Empty);
129 /// Verifies that two floats are equal considering a delta. If the
130 /// expected value is infinity then the delta value is ignored. If
131 /// they are not equals then an <see cref="AssertionException"/> is
134 /// <param name="message">The message printed out upon failure</param>
135 /// <param name="expected">The expected value</param>
136 /// <param name="actual">The actual value</param>
137 /// <param name="delta">The maximum acceptable difference between the
138 /// the expected and the actual</param>
139 static public void AreEqual(float expected,
140 float actual, float delta, string message)
142 // handle infinity specially since subtracting two infinite values gives
143 // NaN and the following test fails
144 if (float.IsInfinity(expected))
146 if (!(expected == actual))
147 Assert.FailNotEquals(expected, actual, message);
149 else if (!(Math.Abs(expected-actual) <= delta))
150 Assert.FailNotEquals(expected, actual, message);
154 /// Verifies that two floats are equal considering a delta. If the
155 /// expected value is infinity then the delta value is ignored. If
156 /// they are not equals then an <see cref="AssertionException"/> is
159 /// <param name="expected">The expected value</param>
160 /// <param name="actual">The actual value</param>
161 /// <param name="delta">The maximum acceptable difference between the
162 /// the expected and the actual</param>
163 static public void AreEqual(float expected, float actual, float delta)
165 Assert.AreEqual(expected, actual, delta, string.Empty);
169 /// Verifies that two decimals are equal. If
170 /// they are not equals then an <see cref="AssertionException"/> is
173 /// <param name="message">The message printed out upon failure</param>
174 /// <param name="expected">The expected value</param>
175 /// <param name="actual">The actual value</param>
176 static public void AreEqual(decimal expected, decimal actual, string message)
178 if(!(expected == actual))
179 Assert.FailNotEquals(expected, actual, message);
183 /// Verifies that two decimals are equal. If
184 /// they are not equals then an <see cref="AssertionException"/> is
187 /// <param name="expected">The expected value</param>
188 /// <param name="actual">The actual value</param>
189 static public void AreEqual(decimal expected, decimal actual)
191 Assert.AreEqual(expected, actual, string.Empty);
195 /// Verifies that two ints are equal. If
196 /// they are not equals then an <see cref="AssertionException"/> is
199 /// <param name="message">The message printed out upon failure</param>
200 /// <param name="expected">The expected value</param>
201 /// <param name="actual">The actual value</param>
202 static public void AreEqual(int expected, int actual, string message)
204 if(!(expected == actual))
205 Assert.FailNotEquals(expected, actual, message);
209 /// Verifies that two ints are equal. If
210 /// they are not equals then an <see cref="AssertionException"/> is
213 /// <param name="expected">The expected value</param>
214 /// <param name="actual">The actual value</param>
215 static public void AreEqual(int expected, int actual)
217 Assert.AreEqual(expected, actual, string.Empty);
223 /// Verifies that two objects are equal. Two objects are considered
224 /// equal if both are null, or if both have the same value. All
225 /// non-numeric types are compared by using the <c>Equals</c> method.
226 /// If they are not equal an <see cref="AssertionFailedError"/> is thrown.
228 /// <param name="expected">The value that is expected</param>
229 /// <param name="actual">The actual value</param>
230 /// <param name="message">The message to display if objects are not equal</param>
231 static public void AreEqual(Object expected, Object actual, string message)
233 if (expected == null && actual == null) return;
235 if (expected != null && actual != null)
237 if(ObjectsEqual( expected, actual ))
242 Assert.FailNotEquals(expected, actual, message);
246 /// Verifies that two objects are equal. Two objects are considered
247 /// equal if both are null, or if both have the same value. All
248 /// non-numeric types are compared by using the <c>Equals</c> method.
249 /// If they are not equal an <see cref="AssertionFailedError"/> is thrown.
251 /// <param name="expected">The value that is expected</param>
252 /// <param name="actual">The actual value</param>
253 static public void AreEqual(Object expected, Object actual)
255 Assert.AreEqual(expected, actual, string.Empty);
259 /// The Equals method throws an AssertionException. This is done
260 /// to make sure there is no mistake by calling this function.
262 /// <param name="a"></param>
263 /// <param name="b"></param>
264 [EditorBrowsable(EditorBrowsableState.Never)]
265 public static new bool Equals(object a, object b)
267 throw new AssertionException("Assert.Equals should not be used for Assertions");
271 /// override the default ReferenceEquals to throw an AssertionException. This
272 /// implementation makes sure there is no mistake in calling this function
273 /// as part of Assert.
275 /// <param name="a"></param>
276 /// <param name="b"></param>
277 public static new void ReferenceEquals(object a, object b)
279 throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
283 /// Checks the type of the object, returning true if
284 /// the object is a numeric type.
286 /// <param name="obj">The object to check</param>
287 /// <returns>true if the object is a numeric type</returns>
288 static private bool IsNumericType( Object obj )
292 if( obj is byte ) return true;
293 if( obj is sbyte ) return true;
294 if( obj is decimal ) return true;
295 if( obj is double ) return true;
296 if( obj is float ) return true;
297 if( obj is int ) return true;
298 if( obj is uint ) return true;
299 if( obj is long ) return true;
300 if( obj is short ) return true;
301 if( obj is ushort ) return true;
303 if( obj is System.Byte ) return true;
304 if( obj is System.SByte ) return true;
305 if( obj is System.Decimal ) return true;
306 if( obj is System.Double ) return true;
307 if( obj is System.Single ) return true;
308 if( obj is System.Int32 ) return true;
309 if( obj is System.UInt32 ) return true;
310 if( obj is System.Int64 ) return true;
311 if( obj is System.UInt64 ) return true;
312 if( obj is System.Int16 ) return true;
313 if( obj is System.UInt16 ) return true;
319 /// Used to compare numeric types. Comparisons between
320 /// same types are fine (Int32 to Int32, or Int64 to Int64),
321 /// but the Equals method fails across different types.
322 /// This method was added to allow any numeric type to
323 /// be handled correctly, by using <c>ToString</c> and
324 /// comparing the result
326 /// <param name="expected"></param>
327 /// <param name="actual"></param>
328 /// <returns></returns>
329 static private bool ObjectsEqual( Object expected, Object actual )
331 if( IsNumericType( expected ) &&
332 IsNumericType( actual ) )
335 // Convert to strings and compare result to avoid
336 // issues with different types that have the same
339 string sExpected = expected.ToString();
340 string sActual = actual.ToString();
341 return sExpected.Equals( sActual );
343 return expected.Equals(actual);
347 /// Verifies that the object that is passed in is not equal to <code>null</code>
348 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
351 /// <param name="message">The message to be printed when the object is null</param>
352 /// <param name="anObject">The object that is to be tested</param>
353 static public void IsNotNull(Object anObject, string message)
355 Assert.IsTrue(anObject != null, message);
359 /// Verifies that the object that is passed in is not equal to <code>null</code>
360 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
363 /// <param name="anObject">The object that is to be tested</param>
364 static public void IsNotNull(Object anObject)
366 Assert.IsNotNull(anObject, string.Empty);
371 /// Verifies that the object that is passed in is equal to <code>null</code>
372 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
375 /// <param name="message">The message to be printed when the object is not null</param>
376 /// <param name="anObject">The object that is to be tested</param>
377 static public void IsNull(Object anObject, string message)
379 Assert.IsTrue(anObject == null, message);
383 /// Verifies that the object that is passed in is equal to <code>null</code>
384 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
387 /// <param name="anObject">The object that is to be tested</param>
388 static public void IsNull(Object anObject)
390 Assert.IsNull(anObject, string.Empty);
395 /// Asserts that two objects refer to the same object. If they
396 /// are not the same an <see cref="AssertionException"/> is thrown.
398 /// <param name="message">The message to be printed when the two objects are not the same object.</param>
399 /// <param name="expected">The expected object</param>
400 /// <param name="actual">The actual object</param>
401 static public void AreSame(Object expected, Object actual, string message)
403 if (object.ReferenceEquals(expected, actual)) return;
405 Assert.FailNotSame(expected, actual, message);
409 /// Asserts that two objects refer to the same object. If they
410 /// are not the same an <see cref="AssertionException"/> is thrown.
412 /// <param name="expected">The expected object</param>
413 /// <param name="actual">The actual object</param>
414 static public void AreSame(Object expected, Object actual)
416 Assert.AreSame(expected, actual, string.Empty);
420 /// Throws an <see cref="AssertionException"/> with the message that is
421 /// passed in. This is used by the other Assert functions.
423 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
424 static public void Fail(string message)
426 if (message == null) message = string.Empty;
428 throw new AssertionException(message);
432 /// Throws an <see cref="AssertionException"/> with the message that is
433 /// passed in. This is used by the other Assert functions.
435 static public void Fail()
437 Assert.Fail(string.Empty);
441 /// This method is called when two objects have been compared and found to be
442 /// different. This prints a nice message to the screen.
444 /// <param name="message">The message that is to be printed prior to the comparison failure</param>
445 /// <param name="expected">The expected object</param>
446 /// <param name="actual">The actual object</param>
447 static private void FailNotEquals(Object expected, Object actual, string message)
450 AssertionFailureMessage.FormatMessageForFailNotEquals(
457 /// This method is called when the two objects are not the same.
459 /// <param name="message">The message to be printed on the screen</param>
460 /// <param name="expected">The expected object</param>
461 /// <param name="actual">The actual object</param>
462 static private void FailNotSame(Object expected, Object actual, string message)
464 string formatted=string.Empty;
466 formatted= message+" ";
467 Assert.Fail(formatted+"expected same");