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
37 /// A set of Assert methods
41 private static int counter = 0;
44 /// Gets the number of assertions executed so far and
45 /// resets the counter to zero.
47 public static int Counter
58 /// A private constructor disallows any instances of this object.
64 /// Asserts that a condition is true. If the condition is false the method throws
65 /// an <see cref="AssertionException"/>.
67 /// <param name="message">The message to display if the condition is false</param>
68 /// <param name="condition">The evaluated condition</param>
69 static public void IsTrue(bool condition, string message)
77 /// Asserts that a condition is true. If the condition is false the method throws
78 /// an <see cref="AssertionException"/>.
80 /// <param name="condition">The evaluated condition</param>
81 static public void IsTrue(bool condition)
83 Assert.IsTrue(condition, string.Empty);
87 /// Asserts that a condition is false. If the condition is true the method throws
88 /// an <see cref="AssertionException"/>.
90 /// <param name="message">The message to display if the condition is true</param>
91 /// <param name="condition">The evaluated condition</param>
92 static public void IsFalse(bool condition, string message)
100 /// Asserts that a condition is false. If the condition is true the method throws
101 /// an <see cref="AssertionException"/>.
103 /// <param name="condition">The evaluated condition</param>
104 static public void IsFalse(bool condition)
106 Assert.IsFalse(condition, string.Empty);
110 /// Verifies that two doubles are equal considering a delta. If the
111 /// expected value is infinity then the delta value is ignored. If
112 /// they are not equals then an <see cref="AssertionException"/> is
115 /// <param name="message">The message that will be printed on failure</param>
116 /// <param name="expected">The expected value</param>
117 /// <param name="actual">The actual value</param>
118 /// <param name="delta">The maximum acceptable difference between the
119 /// the expected and the actual</param>
120 static public void AreEqual(double expected,
121 double actual, double delta, string message)
124 // handle infinity specially since subtracting two infinite values gives
125 // NaN and the following test fails
126 if (double.IsInfinity(expected))
128 if (!(expected == actual))
129 Assert.FailNotEquals(expected, actual, message);
131 else if (!(Math.Abs(expected-actual) <= delta))
132 Assert.FailNotEquals(expected, actual, message);
136 /// Verifies that two doubles are equal considering a delta. If the
137 /// expected value is infinity then the delta value is ignored. If
138 /// they are not equals then an <see cref="AssertionException"/> is
141 /// <param name="expected">The expected value</param>
142 /// <param name="actual">The actual value</param>
143 /// <param name="delta">The maximum acceptable difference between the
144 /// the expected and the actual</param>
145 static public void AreEqual(double expected, double actual, double delta)
147 Assert.AreEqual(expected, actual, delta, string.Empty);
151 /// Verifies that two floats are equal considering a delta. If the
152 /// expected value is infinity then the delta value is ignored. If
153 /// they are not equals then an <see cref="AssertionException"/> is
156 /// <param name="message">The message printed out upon failure</param>
157 /// <param name="expected">The expected value</param>
158 /// <param name="actual">The actual value</param>
159 /// <param name="delta">The maximum acceptable difference between the
160 /// the expected and the actual</param>
161 static public void AreEqual(float expected,
162 float actual, float delta, string message)
165 // handle infinity specially since subtracting two infinite values gives
166 // NaN and the following test fails
167 if (float.IsInfinity(expected))
169 if (!(expected == actual))
170 Assert.FailNotEquals(expected, actual, message);
172 else if (!(Math.Abs(expected-actual) <= delta))
173 Assert.FailNotEquals(expected, actual, message);
177 /// Verifies that two floats are equal considering a delta. If the
178 /// expected value is infinity then the delta value is ignored. If
179 /// they are not equals then an <see cref="AssertionException"/> is
182 /// <param name="expected">The expected value</param>
183 /// <param name="actual">The actual value</param>
184 /// <param name="delta">The maximum acceptable difference between the
185 /// the expected and the actual</param>
186 static public void AreEqual(float expected, float actual, float delta)
188 Assert.AreEqual(expected, actual, delta, string.Empty);
192 /// Verifies that two decimals are equal. If
193 /// they are not equals then an <see cref="AssertionException"/> is
196 /// <param name="message">The message printed out upon failure</param>
197 /// <param name="expected">The expected value</param>
198 /// <param name="actual">The actual value</param>
199 static public void AreEqual(decimal expected, decimal actual, string message)
202 if(!(expected == actual))
203 Assert.FailNotEquals(expected, actual, message);
207 /// Verifies that two decimals are equal. If
208 /// they are not equals then an <see cref="AssertionException"/> is
211 /// <param name="expected">The expected value</param>
212 /// <param name="actual">The actual value</param>
213 static public void AreEqual(decimal expected, decimal actual)
215 Assert.AreEqual(expected, actual, string.Empty);
219 /// Verifies that two ints are equal. If
220 /// they are not equals then an <see cref="AssertionException"/> is
223 /// <param name="message">The message printed out upon failure</param>
224 /// <param name="expected">The expected value</param>
225 /// <param name="actual">The actual value</param>
226 static public void AreEqual(int expected, int actual, string message)
229 if(!(expected == actual))
230 Assert.FailNotEquals(expected, actual, message);
234 /// Verifies that two ints are equal. If
235 /// they are not equals then an <see cref="AssertionException"/> is
238 /// <param name="expected">The expected value</param>
239 /// <param name="actual">The actual value</param>
240 static public void AreEqual(int expected, int actual)
242 Assert.AreEqual(expected, actual, string.Empty);
248 /// Verifies that two objects are equal. Two objects are considered
249 /// equal if both are null, or if both have the same value. All
250 /// non-numeric types are compared by using the <c>Equals</c> method.
251 /// Arrays are compared by comparing each element using the same rules.
252 /// If they are not equal an <see cref="AssertionException"/> is thrown.
254 /// <param name="expected">The value that is expected</param>
255 /// <param name="actual">The actual value</param>
256 /// <param name="message">The message to display if objects are not equal</param>
257 static public void AreEqual(Object expected, Object actual, string message)
261 if (expected == null && actual == null) return;
263 if (expected != null && actual != null)
265 object[] aExpected = expected as object[];
266 object[] aActual = actual as object[];
268 if (aExpected != null && aActual != null )
270 int iLength = Math.Min( aExpected.Length, aActual.Length );
271 for( int i = 0; i < iLength; i++ )
272 if ( !ObjectsEqual( aExpected[i], aActual[i] ) )
274 Assert.FailArraysNotEqual(i, aExpected, aActual, message );
277 if ( aExpected.Length != aActual.Length )
278 Assert.FailArraysNotEqual( iLength, aExpected, aActual, message );
282 else if(ObjectsEqual( expected, actual ))
287 Assert.FailNotEquals(expected, actual, message);
291 /// Verifies that two objects are equal. Two objects are considered
292 /// equal if both are null, or if both have the same value. All
293 /// non-numeric types are compared by using the <c>Equals</c> method.
294 /// If they are not equal an <see cref="AssertionException"/> is thrown.
296 /// <param name="expected">The value that is expected</param>
297 /// <param name="actual">The actual value</param>
298 static public void AreEqual(Object expected, Object actual)
300 Assert.AreEqual(expected, actual, string.Empty);
304 /// The Equals method throws an AssertionException. This is done
305 /// to make sure there is no mistake by calling this function.
307 /// <param name="a"></param>
308 /// <param name="b"></param>
309 [EditorBrowsable(EditorBrowsableState.Never)]
310 public static new bool Equals(object a, object b)
312 throw new AssertionException("Assert.Equals should not be used for Assertions");
316 /// override the default ReferenceEquals to throw an AssertionException. This
317 /// implementation makes sure there is no mistake in calling this function
318 /// as part of Assert.
320 /// <param name="a"></param>
321 /// <param name="b"></param>
322 public static new void ReferenceEquals(object a, object b)
324 throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
328 /// Checks the type of the object, returning true if
329 /// the object is a numeric type.
331 /// <param name="obj">The object to check</param>
332 /// <returns>true if the object is a numeric type</returns>
333 static private bool IsNumericType( Object obj )
337 if( obj is byte ) return true;
338 if( obj is sbyte ) return true;
339 if( obj is decimal ) return true;
340 if( obj is double ) return true;
341 if( obj is float ) return true;
342 if( obj is int ) return true;
343 if( obj is uint ) return true;
344 if( obj is long ) return true;
345 if( obj is short ) return true;
346 if( obj is ushort ) return true;
348 if( obj is System.Byte ) return true;
349 if( obj is System.SByte ) return true;
350 if( obj is System.Decimal ) return true;
351 if( obj is System.Double ) return true;
352 if( obj is System.Single ) return true;
353 if( obj is System.Int32 ) return true;
354 if( obj is System.UInt32 ) return true;
355 if( obj is System.Int64 ) return true;
356 if( obj is System.UInt64 ) return true;
357 if( obj is System.Int16 ) return true;
358 if( obj is System.UInt16 ) return true;
364 /// Used to compare numeric types. Comparisons between
365 /// same types are fine (Int32 to Int32, or Int64 to Int64),
366 /// but the Equals method fails across different types.
367 /// This method was added to allow any numeric type to
368 /// be handled correctly, by using <c>ToString</c> and
369 /// comparing the result
371 /// <param name="expected"></param>
372 /// <param name="actual"></param>
373 /// <returns></returns>
374 static private bool ObjectsEqual( Object expected, Object actual )
376 if( IsNumericType( expected ) &&
377 IsNumericType( actual ) )
380 // Convert to strings and compare result to avoid
381 // issues with different types that have the same
384 string sExpected = expected.ToString();
385 string sActual = actual.ToString();
386 return sExpected.Equals( sActual );
388 return expected.Equals(actual);
392 /// Verifies that the object that is passed in is not equal to <code>null</code>
393 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
396 /// <param name="message">The message to be printed when the object is null</param>
397 /// <param name="anObject">The object that is to be tested</param>
398 static public void IsNotNull(Object anObject, string message)
400 Assert.IsTrue(anObject != null, message);
404 /// Verifies that the object that is passed in is not equal to <code>null</code>
405 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
408 /// <param name="anObject">The object that is to be tested</param>
409 static public void IsNotNull(Object anObject)
411 Assert.IsNotNull(anObject, string.Empty);
416 /// Verifies that the object that is passed in is equal to <code>null</code>
417 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
420 /// <param name="message">The message to be printed when the object is not null</param>
421 /// <param name="anObject">The object that is to be tested</param>
422 static public void IsNull(Object anObject, string message)
424 Assert.IsTrue(anObject == null, message);
428 /// Verifies that the object that is passed in is equal to <code>null</code>
429 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
432 /// <param name="anObject">The object that is to be tested</param>
433 static public void IsNull(Object anObject)
435 Assert.IsNull(anObject, string.Empty);
440 /// Asserts that two objects refer to the same object. If they
441 /// are not the same an <see cref="AssertionException"/> is thrown.
443 /// <param name="message">The message to be printed when the two objects are not the same object.</param>
444 /// <param name="expected">The expected object</param>
445 /// <param name="actual">The actual object</param>
446 static public void AreSame(Object expected, Object actual, string message)
449 if (object.ReferenceEquals(expected, actual)) return;
451 Assert.FailNotSame(expected, actual, message);
455 /// Asserts that two objects refer to the same object. If they
456 /// are not the same an <see cref="AssertionException"/> is thrown.
458 /// <param name="expected">The expected object</param>
459 /// <param name="actual">The actual object</param>
460 static public void AreSame(Object expected, Object actual)
462 Assert.AreSame(expected, actual, string.Empty);
466 /// Throws an <see cref="AssertionException"/> with the message that is
467 /// passed in. This is used by the other Assert functions.
469 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
470 static public void Fail(string message)
472 if (message == null) message = string.Empty;
474 throw new AssertionException(message);
478 /// Throws an <see cref="AssertionException"/> with the message that is
479 /// passed in. This is used by the other Assert functions.
481 static public void Fail()
483 Assert.Fail(string.Empty);
487 /// This method is called when two objects have been compared and found to be
488 /// different. This prints a nice message to the screen.
490 /// <param name="message">The message that is to be printed prior to the comparison failure</param>
491 /// <param name="expected">The expected object</param>
492 /// <param name="actual">The actual object</param>
493 static private void FailNotEquals(Object expected, Object actual, string message)
496 AssertionFailureMessage.FormatMessageForFailNotEquals(
503 /// This method is called when two arrays have been compared and found to be
504 /// different. This prints a nice message to the screen.
506 /// <param name="message">The message that is to be printed prior to the comparison failure</param>
507 /// <param name="expected">The expected array</param>
508 /// <param name="actual">The actual array</param>
509 static private void FailArraysNotEqual(int index, Object[] expected, Object[] actual, string message)
512 AssertionFailureMessage.FormatMessageForFailArraysNotEqual(
520 /// This method is called when the two objects are not the same.
522 /// <param name="message">The message to be printed on the screen</param>
523 /// <param name="expected">The expected object</param>
524 /// <param name="actual">The actual object</param>
525 static private void FailNotSame(Object expected, Object actual, string message)
527 string formatted=string.Empty;
529 formatted= message+" ";
530 Assert.Fail(formatted+"expected same");