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="condition">The evaluated condition</param>
68 /// <param name="message">The message to display if the condition is false</param>
69 /// <param name="args">Arguments to be used in formatting the message</param>
70 static public void IsTrue(bool condition
, string message
, params object[] args
)
74 Assert
.Fail(message
, args
);
78 /// Asserts that a condition is true. If the condition is false the method throws
79 /// an <see cref="AssertionException"/>.
81 /// <param name="condition">The evaluated condition</param>
82 /// <param name="message">The message to display if the condition is false</param>
83 static public void IsTrue(bool condition
, string message
)
85 Assert
.IsTrue(condition
, message
, null);
89 /// Asserts that a condition is true. If the condition is false the method throws
90 /// an <see cref="AssertionException"/>.
92 /// <param name="condition">The evaluated condition</param>
93 static public void IsTrue(bool condition
)
95 Assert
.IsTrue(condition
, string.Empty
, null);
99 /// Asserts that a condition is false. If the condition is true the method throws
100 /// an <see cref="AssertionException"/>.
102 /// <param name="condition">The evaluated condition</param>
103 /// <param name="message">The message to display if the condition is true</param>
104 /// <param name="args">Arguments to be used in formatting the message</param>
105 static public void IsFalse(bool condition
, string message
, params object[] args
)
109 Assert
.Fail(message
, args
);
113 /// Asserts that a condition is false. If the condition is true the method throws
114 /// an <see cref="AssertionException"/>.
116 /// <param name="condition">The evaluated condition</param>
117 /// <param name="message">The message to display if the condition is true</param>
118 static public void IsFalse(bool condition
, string message
)
120 Assert
.IsFalse( condition
, message
, null );
124 /// Asserts that a condition is false. If the condition is true the method throws
125 /// an <see cref="AssertionException"/>.
127 /// <param name="condition">The evaluated condition</param>
128 static public void IsFalse(bool condition
)
130 Assert
.IsFalse(condition
, string.Empty
, null);
134 /// Verifies that two doubles are equal considering a delta. If the
135 /// expected value is infinity then the delta value is ignored. If
136 /// they are not equals then an <see cref="AssertionException"/> is
139 /// <param name="expected">The expected value</param>
140 /// <param name="actual">The actual value</param>
141 /// <param name="delta">The maximum acceptable difference between the
142 /// the expected and the actual</param>
143 /// <param name="message">The message that will be printed on failure</param>
144 /// <param name="args">Arguments to be used in formatting the message</param>
145 static public void AreEqual(double expected
,
146 double actual
, double delta
, string message
, params object[] args
)
149 // handle infinity specially since subtracting two infinite values gives
150 // NaN and the following test fails. mono also needs NaN to be handled
151 // specially although ms.net could use either method.
152 if (double.IsInfinity(expected
) || double.IsNaN(expected
) || double.IsNaN(actual
))
154 if (!(expected
== actual
))
155 Assert
.FailNotEquals(expected
, actual
, message
, args
);
157 else if (!(Math
.Abs(expected
-actual
) <= delta
))
158 Assert
.FailNotEquals(expected
, actual
, message
, args
);
162 /// Verifies that two doubles are equal considering a delta. If the
163 /// expected value is infinity then the delta value is ignored. If
164 /// they are not equals then an <see cref="AssertionException"/> is
167 /// <param name="expected">The expected value</param>
168 /// <param name="actual">The actual value</param>
169 /// <param name="delta">The maximum acceptable difference between the
170 /// the expected and the actual</param>
171 /// <param name="message">The message that will be printed on failure</param>
172 static public void AreEqual(double expected
,
173 double actual
, double delta
, string message
)
175 Assert
.AreEqual( expected
, actual
, delta
, message
, null );
179 /// Verifies that two doubles are equal considering a delta. If the
180 /// expected value is infinity then the delta value is ignored. If
181 /// they are not equals then an <see cref="AssertionException"/> is
184 /// <param name="expected">The expected value</param>
185 /// <param name="actual">The actual value</param>
186 /// <param name="delta">The maximum acceptable difference between the
187 /// the expected and the actual</param>
188 static public void AreEqual(double expected
, double actual
, double delta
)
190 Assert
.AreEqual(expected
, actual
, delta
, string.Empty
, null);
194 /// Verifies that two floats are equal considering a delta. If the
195 /// expected value is infinity then the delta value is ignored. If
196 /// they are not equals then an <see cref="AssertionException"/> is
199 /// <param name="expected">The expected value</param>
200 /// <param name="actual">The actual value</param>
201 /// <param name="delta">The maximum acceptable difference between the
202 /// the expected and the actual</param>
203 /// <param name="message">The message printed out upon failure</param>
204 /// <param name="args">Arguments to be used in formatting the message</param>
205 static public void AreEqual(float expected
,
206 float actual
, float delta
, string message
, params object[] args
)
209 // handle infinity specially since subtracting two infinite values gives
210 // NaN and the following test fails. mono also needs NaN to be handled
211 // specially although ms.net could use either method.
212 if (float.IsInfinity(expected
) || float.IsNaN(expected
) || float.IsNaN(actual
))
214 if (!(expected
== actual
))
215 Assert
.FailNotEquals(expected
, actual
, message
, args
);
217 else if (!(Math
.Abs(expected
-actual
) <= delta
))
218 Assert
.FailNotEquals(expected
, actual
, message
, args
);
222 /// Verifies that two floats are equal considering a delta. If the
223 /// expected value is infinity then the delta value is ignored. If
224 /// they are not equals then an <see cref="AssertionException"/> is
227 /// <param name="expected">The expected value</param>
228 /// <param name="actual">The actual value</param>
229 /// <param name="delta">The maximum acceptable difference between the
230 /// the expected and the actual</param>
231 /// <param name="message">The message printed out upon failure</param>
232 static public void AreEqual(float expected
, float actual
, float delta
, string message
)
234 Assert
.AreEqual(expected
, actual
, delta
, message
, null);
238 /// Verifies that two floats are equal considering a delta. If the
239 /// expected value is infinity then the delta value is ignored. If
240 /// they are not equals then an <see cref="AssertionException"/> is
243 /// <param name="expected">The expected value</param>
244 /// <param name="actual">The actual value</param>
245 /// <param name="delta">The maximum acceptable difference between the
246 /// the expected and the actual</param>
247 static public void AreEqual(float expected
, float actual
, float delta
)
249 Assert
.AreEqual(expected
, actual
, delta
, string.Empty
, null);
253 /// Verifies that two decimals are equal. If
254 /// they are not equals then an <see cref="AssertionException"/> is
257 /// <param name="expected">The expected value</param>
258 /// <param name="actual">The actual value</param>
259 /// <param name="message">The message printed out upon failure</param>
260 /// <param name="args">Arguments to be used in formatting the message</param>
261 static public void AreEqual(decimal expected
, decimal actual
, string message
, params object[] args
)
264 if(!(expected
== actual
))
265 Assert
.FailNotEquals(expected
, actual
, message
, args
);
269 /// Verifies that two decimals are equal. If
270 /// they are not equals then an <see cref="AssertionException"/> is
273 /// <param name="expected">The expected value</param>
274 /// <param name="actual">The actual value</param>
275 /// <param name="message">The message printed out upon failure</param>
276 static public void AreEqual(decimal expected
, decimal actual
, string message
)
278 Assert
.AreEqual(expected
, actual
, message
, null);
282 /// Verifies that two decimals are equal. If
283 /// they are not equals then an <see cref="AssertionException"/> is
286 /// <param name="expected">The expected value</param>
287 /// <param name="actual">The actual value</param>
288 static public void AreEqual(decimal expected
, decimal actual
)
290 Assert
.AreEqual(expected
, actual
, string.Empty
, null);
294 /// Verifies that two ints are equal. If
295 /// they are not equals then an <see cref="AssertionException"/> is
298 /// <param name="expected">The expected value</param>
299 /// <param name="actual">The actual value</param>
300 /// <param name="message">The message printed out upon failure</param>
301 /// <param name="args">Arguments to be used in formatting the message</param>
302 static public void AreEqual(int expected
, int actual
, string message
, params object[] args
)
305 if(!(expected
== actual
))
306 Assert
.FailNotEquals(expected
, actual
, message
, args
);
310 /// Verifies that two ints are equal. If
311 /// they are not equals then an <see cref="AssertionException"/> is
314 /// <param name="expected">The expected value</param>
315 /// <param name="actual">The actual value</param>
316 /// <param name="message">The message printed out upon failure</param>
317 static public void AreEqual(int expected
, int actual
, string message
)
319 Assert
.AreEqual(expected
, actual
, message
, null);
323 /// Verifies that two ints are equal. If
324 /// they are not equals then an <see cref="AssertionException"/> is
327 /// <param name="expected">The expected value</param>
328 /// <param name="actual">The actual value</param>
329 static public void AreEqual(int expected
, int actual
)
331 Assert
.AreEqual(expected
, actual
, string.Empty
, null);
335 /// Verifies that two arrays are equal. If they are not,
336 /// then an <see cref="AssertionException"/> is thrown.
338 /// <param name="expected">The expected value</param>
339 /// <param name="actual">The actual value</param>
340 /// <param name="message">The message printed out upon failure</param>
341 /// <param name="args">Arguments to be used in formatting the message</param>
342 static public void AreEqual( System
.Array expected
, System
.Array actual
, string message
, params object[] args
)
346 if ( expected
== null && actual
== null ) return;
348 if ( expected
== null || actual
== null )
349 Assert
.FailNotEquals( expected
, actual
, message
, args
);
351 if ( expected
.Rank
!= actual
.Rank
)
352 Assert
.FailNotEquals( expected
, actual
, message
, args
);
354 if ( expected
.Rank
!= 1 )
355 Assert
.Fail( "Multi-dimension array comparison is not supported" );
357 int iLength
= Math
.Min( expected
.Length
, actual
.Length
);
358 for( int i
= 0; i
< iLength
; i
++ )
359 if ( !ObjectsEqual( expected
.GetValue( i
), actual
.GetValue( i
) ) )
361 Assert
.FailArraysNotEqual(i
, expected
, actual
, message
, args
);
364 if ( expected
.Length
!= actual
.Length
)
365 Assert
.FailArraysNotEqual( iLength
, expected
, actual
, message
, args
);
371 /// Verifies that two arrays are equal. If they are not,
372 /// then an <see cref="AssertionException"/> is thrown.
374 /// <param name="expected">The expected value</param>
375 /// <param name="actual">The actual value</param>
376 /// <param name="message">The message printed out upon failure</param>
377 static public void AreEqual( System
.Array expected
, System
.Array actual
, string message
)
379 Assert
.AreEqual( expected
, actual
, message
, null );
383 /// Verifies that two arrays are equal. If they are not,
384 /// then an <see cref="AssertionException"/> is thrown.
386 /// <param name="expected">The expected value</param>
387 /// <param name="actual">The actual value</param>
388 static public void AreEqual( System
.Array expected
, System
.Array actual
)
390 Assert
.AreEqual( expected
, actual
, string.Empty
, null );
394 /// Verifies that two objects are equal. Two objects are considered
395 /// equal if both are null, or if both have the same value. All
396 /// non-numeric types are compared by using the <c>Equals</c> method.
397 /// Arrays are compared by comparing each element using the same rules.
398 /// If they are not equal an <see cref="AssertionException"/> is thrown.
400 /// <param name="expected">The value that is expected</param>
401 /// <param name="actual">The actual value</param>
402 /// <param name="message">The message to display if objects are not equal</param>
403 /// <param name="args">Arguments to be used in formatting the message</param>
404 static public void AreEqual(Object expected
, Object actual
, string message
, params object[] args
)
406 if ( expected
== null && actual
== null ) return;
407 if ( expected
== null || actual
== null )
408 Assert
.FailNotEquals( expected
, actual
, message
, args
);
410 // FOr now, dynamically call array assertion if necessary. Try to move
411 // this into the ObjectsEqual method later on.
412 if ( expected
.GetType().IsArray
&& actual
.GetType().IsArray
)
413 Assert
.AreEqual( (System
.Array
)expected
, (System
.Array
)actual
, message
, args
);
418 if ( !ObjectsEqual( expected
, actual
) )
419 Assert
.FailNotEquals( expected
, actual
, message
, args
);
424 /// Verifies that two objects are equal. Two objects are considered
425 /// equal if both are null, or if both have the same value. All
426 /// non-numeric types are compared by using the <c>Equals</c> method.
427 /// If they are not equal an <see cref="AssertionException"/> is thrown.
429 /// <param name="expected">The value that is expected</param>
430 /// <param name="actual">The actual value</param>
431 /// <param name="message">The message to display if objects are not equal</param>
432 static public void AreEqual(Object expected
, Object actual
, string message
)
434 Assert
.AreEqual(expected
, actual
, message
, null);
438 /// Verifies that two objects are equal. Two objects are considered
439 /// equal if both are null, or if both have the same value. All
440 /// non-numeric types are compared by using the <c>Equals</c> method.
441 /// If they are not equal an <see cref="AssertionException"/> is thrown.
443 /// <param name="expected">The value that is expected</param>
444 /// <param name="actual">The actual value</param>
445 static public void AreEqual(Object expected
, Object actual
)
447 Assert
.AreEqual(expected
, actual
, string.Empty
, null);
451 /// The Equals method throws an AssertionException. This is done
452 /// to make sure there is no mistake by calling this function.
454 /// <param name="a"></param>
455 /// <param name="b"></param>
456 [EditorBrowsable(EditorBrowsableState
.Never
)]
457 public static new bool Equals(object a
, object b
)
459 throw new AssertionException("Assert.Equals should not be used for Assertions");
463 /// override the default ReferenceEquals to throw an AssertionException. This
464 /// implementation makes sure there is no mistake in calling this function
465 /// as part of Assert.
467 /// <param name="a"></param>
468 /// <param name="b"></param>
469 public static new void ReferenceEquals(object a
, object b
)
471 throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
475 /// Checks the type of the object, returning true if
476 /// the object is a numeric type.
478 /// <param name="obj">The object to check</param>
479 /// <returns>true if the object is a numeric type</returns>
480 static protected bool IsNumericType( Object obj
)
484 if( obj
is byte ) return true;
485 if( obj
is sbyte ) return true;
486 if( obj
is decimal ) return true;
487 if( obj
is double ) return true;
488 if( obj
is float ) return true;
489 if( obj
is int ) return true;
490 if( obj
is uint ) return true;
491 if( obj
is long ) return true;
492 if( obj
is short ) return true;
493 if( obj
is ushort ) return true;
495 if( obj
is System
.Byte
) return true;
496 if( obj
is System
.SByte
) return true;
497 if( obj
is System
.Decimal
) return true;
498 if( obj
is System
.Double
) return true;
499 if( obj
is System
.Single
) return true;
500 if( obj
is System
.Int32
) return true;
501 if( obj
is System
.UInt32
) return true;
502 if( obj
is System
.Int64
) return true;
503 if( obj
is System
.UInt64
) return true;
504 if( obj
is System
.Int16
) return true;
505 if( obj
is System
.UInt16
) return true;
511 /// Used to compare two objects. Two nulls are equal and null
512 /// is not equal to non-null. Comparisons between the same
513 /// numeric types are fine (Int32 to Int32, or Int64 to Int64),
514 /// but the Equals method fails across different types so we
515 /// use <c>ToString</c> and compare the results.
517 /// <param name="expected"></param>
518 /// <param name="actual"></param>
519 /// <returns></returns>
520 static protected bool ObjectsEqual( Object expected
, Object actual
)
522 if ( expected
== null && actual
== null ) return true;
523 if ( expected
== null || actual
== null ) return false;
525 if( IsNumericType( expected
) &&
526 IsNumericType( actual
) )
529 // Convert to strings and compare result to avoid
530 // issues with different types that have the same
533 string sExpected
= expected
.ToString();
534 string sActual
= actual
.ToString();
535 return sExpected
.Equals( sActual
);
537 return expected
.Equals(actual
);
541 /// Verifies that the object that is passed in is not equal to <code>null</code>
542 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
545 /// <param name="anObject">The object that is to be tested</param>
546 /// <param name="message">The message to be printed when the object is null</param>
547 /// <param name="args">Arguments to be used in formatting the message</param>
548 static public void IsNotNull(Object anObject
, string message
, params object[] args
)
550 Assert
.IsTrue(anObject
!= null, message
, args
);
554 /// Verifies that the object that is passed in is not equal to <code>null</code>
555 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
558 /// <param name="anObject">The object that is to be tested</param>
559 static public void IsNotNull(Object anObject
, string message
)
561 Assert
.IsNotNull(anObject
, message
, null);
565 /// Verifies that the object that is passed in is not equal to <code>null</code>
566 /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
569 /// <param name="anObject">The object that is to be tested</param>
570 static public void IsNotNull(Object anObject
)
572 Assert
.IsNotNull(anObject
, string.Empty
, null);
577 /// Verifies that the object that is passed in is equal to <code>null</code>
578 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
581 /// <param name="anObject">The object that is to be tested</param>
582 /// <param name="message">The message to be printed when the object is not null</param>
583 /// <param name="args">Arguments to be used in formatting the message</param>
584 static public void IsNull(Object anObject
, string message
, params object[] args
)
586 Assert
.IsTrue(anObject
== null, message
, args
);
590 /// Verifies that the object that is passed in is equal to <code>null</code>
591 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
594 /// <param name="anObject">The object that is to be tested</param>
595 static public void IsNull(Object anObject
, string message
)
597 Assert
.IsNull(anObject
, message
, null);
601 /// Verifies that the object that is passed in is equal to <code>null</code>
602 /// If the object is <code>null</code> then an <see cref="AssertionException"/>
605 /// <param name="anObject">The object that is to be tested</param>
606 static public void IsNull(Object anObject
)
608 Assert
.IsNull(anObject
, string.Empty
, null);
613 /// Asserts that two objects refer to the same object. If they
614 /// are not the same an <see cref="AssertionException"/> is thrown.
616 /// <param name="expected">The expected object</param>
617 /// <param name="actual">The actual object</param>
618 /// <param name="message">The message to be printed when the two objects are not the same object.</param>
619 /// <param name="args">Arguments to be used in formatting the message</param>
620 static public void AreSame(Object expected
, Object actual
, string message
, params object[] args
)
623 if (object.ReferenceEquals(expected
, actual
)) return;
625 Assert
.FailNotSame(expected
, actual
, message
, args
);
629 /// Asserts that two objects refer to the same object. If they
630 /// are not the same an <see cref="AssertionException"/> is thrown.
632 /// <param name="expected">The expected object</param>
633 /// <param name="actual">The actual object</param>
634 /// <param name="message">The message to be printed when the object is null</param>
635 static public void AreSame(Object expected
, Object actual
, string message
)
637 Assert
.AreSame(expected
, actual
, message
, null);
641 /// Asserts that two objects refer to the same object. If they
642 /// are not the same an <see cref="AssertionException"/> is thrown.
644 /// <param name="expected">The expected object</param>
645 /// <param name="actual">The actual object</param>
646 static public void AreSame(Object expected
, Object actual
)
648 Assert
.AreSame(expected
, actual
, string.Empty
, null);
652 /// Throws an <see cref="AssertionException"/> with the message and arguments
653 /// that are passed in. This is used by the other Assert functions.
655 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
656 /// <param name="args">Arguments to be used in formatting the message</param>
657 static public void Fail(string message
, params object[] args
)
659 if (message
== null) message
= string.Empty
;
660 else if ( args
!= null && args
.Length
> 0 )
661 message
= string.Format( message
, args
);
663 throw new AssertionException(message
);
667 /// Throws an <see cref="AssertionException"/> with the message that is
668 /// passed in. This is used by the other Assert functions.
670 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
671 static public void Fail(string message
)
673 Assert
.Fail(message
, null);
677 /// Throws an <see cref="AssertionException"/>.
678 /// This is used by the other Assert functions.
680 static public void Fail()
682 Assert
.Fail(string.Empty
, null);
686 /// Throws an <see cref="IgnoreException"/> with the message and arguments
687 /// that are passed in. This causes the test to be reported as ignored.
689 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
690 /// <param name="args">Arguments to be used in formatting the message</param>
691 static public void Ignore( string message
, object[] args
)
693 if (message
== null) message
= string.Empty
;
694 else if ( args
!= null && args
.Length
> 0 )
695 message
= string.Format( message
, args
);
697 throw new IgnoreException(message
);
701 /// Throws an <see cref="IgnoreException"/> with the message that is
702 /// passed in. This causes the test to be reported as ignored.
704 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
705 static public void Ignore( string message
)
707 Assert
.Ignore( message
, null );
711 /// Throws an <see cref="IgnoreException"/>.
712 /// This causes the test to be reported as ignored.
714 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
715 static public void Ignore()
717 Assert
.Ignore( string.Empty
, null );
721 /// This method is called when two objects have been compared and found to be
722 /// different. This prints a nice message to the screen.
724 /// <param name="message">The message that is to be printed prior to the comparison failure</param>
725 /// <param name="expected">The expected object</param>
726 /// <param name="actual">The actual object</param>
727 /// <param name="args">Arguments to be used in formatting the message</param>
728 static protected void FailNotEquals(Object expected
, Object actual
, string message
,
729 params object[] args
)
732 AssertionFailureMessage
.FormatMessageForFailNotEquals(
740 /// This method is called when two arrays have been compared and found to be
741 /// different. This prints a nice message to the screen.
743 /// <param name="index">The index at which the failure occured</param>
744 /// <param name="expected">The expected array</param>
745 /// <param name="actual">The actual array</param>
746 /// <param name="message">The message that is to be printed prior to the comparison failure</param>
747 /// <param name="args">Arguments to be used in formatting the message</param>
748 static protected void FailArraysNotEqual(int index
, Array expected
, Array actual
,
749 string message
, params object[] args
)
752 AssertionFailureMessage
.FormatMessageForFailArraysNotEqual(
761 /// This method is called when the two objects are not the same.
763 /// <param name="message">The message to be printed on the screen</param>
764 /// <param name="expected">The expected object</param>
765 /// <param name="actual">The actual object</param>
766 /// <param name="args">Arguments to be used in formatting the message</param>
767 static protected void FailNotSame(Object expected
, Object actual
, string message
, params object[] args
)
769 string formatted
= string.Empty
;
770 if ( message
!= null )
772 if (args
!= null && args
.Length
> 0 )
773 formatted
= string.Format( message
+" ", args
);
775 formatted
= message
+" ";
778 Assert
.Fail(formatted
+"expected same");