adding the new files is important
[mono.git] / mcs / nunit20 / framework / Assert.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
6 '
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 
9 ' software.
10
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:
14 '
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.
18 '
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 using System;
31 using System.Collections;
32 using System.ComponentModel;
33
34 namespace NUnit.Framework
35 {
36         public class Assert
37         {
38                 /// <summary>
39                 /// A private constructor disallows any instances of this object. 
40                 /// </summary>
41                 private Assert()
42                 {}
43
44                 /// <summary>
45                 /// Asserts that a condition is true. If the condition is false the method throws
46                 /// an <see cref="AssertionException"/>.
47                 /// </summary> 
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) 
51                 {
52                         if (!condition)
53                                 Assert.Fail(message);
54                 }
55     
56                 /// <summary>
57                 /// Asserts that a condition is true. If the condition is false the method throws
58                 /// an <see cref="AssertionException"/>.
59                 /// </summary>
60                 /// <param name="condition">The evaluated condition</param>
61                 static public void IsTrue(bool condition) 
62                 {
63                         Assert.IsTrue(condition, string.Empty);
64                 }
65
66                 /// <summary>
67                 /// Asserts that a condition is false. If the condition is true the method throws
68                 /// an <see cref="AssertionException"/>.
69                 /// </summary>
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) 
73                 {
74                         if (condition)
75                                 Assert.Fail(message);
76                 }
77                 
78                 /// <summary>
79                 /// Asserts that a condition is false. If the condition is true the method throws
80                 /// an <see cref="AssertionException"/>.
81                 /// </summary>
82                 /// <param name="condition">The evaluated condition</param>
83                 static public void IsFalse(bool condition) 
84                 {
85                         Assert.IsFalse(condition, string.Empty);
86                 }
87
88                 /// <summary>
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
92                 /// thrown.
93                 /// </summary>
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) 
101                 {
102                         // handle infinity specially since subtracting two infinite values gives 
103                         // NaN and the following test fails
104                         if (double.IsInfinity(expected)) 
105                         {
106                                 if (!(expected == actual))
107                                         Assert.FailNotEquals(expected, actual, message);
108                         } 
109                         else if (!(Math.Abs(expected-actual) <= delta))
110                                 Assert.FailNotEquals(expected, actual, message);
111                 }
112
113                 /// <summary>
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
117                 /// thrown.
118                 /// </summary>
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) 
124                 {
125                         Assert.AreEqual(expected, actual, delta, string.Empty);
126                 }
127
128                 /// <summary>
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
132                 /// thrown.
133                 /// </summary>
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) 
141                 {
142                         // handle infinity specially since subtracting two infinite values gives 
143                         // NaN and the following test fails
144                         if (float.IsInfinity(expected)) 
145                         {
146                                 if (!(expected == actual))
147                                         Assert.FailNotEquals(expected, actual, message);
148                         } 
149                         else if (!(Math.Abs(expected-actual) <= delta))
150                                 Assert.FailNotEquals(expected, actual, message);
151                 }
152
153                 /// <summary>
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
157                 /// thrown.
158                 /// </summary>
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) 
164                 {
165                         Assert.AreEqual(expected, actual, delta, string.Empty);
166                 }
167
168                 /// <summary>
169                 /// Verifies that two decimals are equal. If 
170                 /// they are not equals then an <see cref="AssertionException"/> is
171                 /// thrown.
172                 /// </summary>
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) 
177                 {
178                         if(!(expected == actual))
179                                 Assert.FailNotEquals(expected, actual, message);
180                 }
181
182                 /// <summary>
183                 /// Verifies that two decimals are equal. If 
184                 /// they are not equals then an <see cref="AssertionException"/> is
185                 /// thrown.
186                 /// </summary>
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) 
190                 {
191                         Assert.AreEqual(expected, actual, string.Empty);
192                 }
193                 
194                 /// <summary>
195                 /// Verifies that two ints are equal. If 
196                 /// they are not equals then an <see cref="AssertionException"/> is
197                 /// thrown.
198                 /// </summary>
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) 
203                 {
204                         if(!(expected == actual))
205                                 Assert.FailNotEquals(expected, actual, message);
206                 }
207
208                 /// <summary>
209                 /// Verifies that two ints are equal. If 
210                 /// they are not equals then an <see cref="AssertionException"/> is
211                 /// thrown.
212                 /// </summary>
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) 
216                 {
217                         Assert.AreEqual(expected, actual, string.Empty);
218                 }
219
220                 
221                 
222                 /// <summary>
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.
227                 /// </summary>
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)
232                 {
233                         if (expected == null && actual == null) return;
234
235                         if (expected != null && actual != null)
236                         {
237                                 if(ObjectsEqual( expected, actual ))
238                                 {
239                                         return;
240                                 }
241                         }
242                         Assert.FailNotEquals(expected, actual, message);
243                 }
244
245                 /// <summary>
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.
250                 /// </summary>
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) 
254                 {
255                         Assert.AreEqual(expected, actual, string.Empty);
256                 }
257
258                 /// <summary>
259                 /// The Equals method throws an AssertionException. This is done 
260                 /// to make sure there is no mistake by calling this function.
261                 /// </summary>
262                 /// <param name="a"></param>
263                 /// <param name="b"></param>
264                 [EditorBrowsable(EditorBrowsableState.Never)]
265                 public static new bool Equals(object a, object b)
266                 {
267                         throw new AssertionException("Assert.Equals should not be used for Assertions");
268                 }
269
270                 /// <summary>
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. 
274                 /// </summary>
275                 /// <param name="a"></param>
276                 /// <param name="b"></param>
277                 public static new void ReferenceEquals(object a, object b)
278                 {
279                         throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
280                 }
281                                 
282                 /// <summary>
283                 /// Checks the type of the object, returning true if
284                 /// the object is a numeric type.
285                 /// </summary>
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 )
289                 {
290                         if( null != obj )
291                         {
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;
302
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;
314                         }
315                         return false;
316                 }
317
318                 /// <summary>
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
325                 /// </summary>
326                 /// <param name="expected"></param>
327                 /// <param name="actual"></param>
328                 /// <returns></returns>
329                 static private bool ObjectsEqual( Object expected, Object actual )
330                 {
331                         if( IsNumericType( expected )  &&
332                                 IsNumericType( actual ) )
333                         {
334                                 //
335                                 // Convert to strings and compare result to avoid
336                                 // issues with different types that have the same
337                                 // value
338                                 //
339                                 string sExpected = expected.ToString();
340                                 string sActual   = actual.ToString();
341                                 return sExpected.Equals( sActual );
342                         }
343                         return expected.Equals(actual);
344                 }
345     
346                 /// <summary>
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"/>
349                 /// is thrown.
350                 /// </summary>
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) 
354                 {
355                         Assert.IsTrue(anObject != null, message); 
356                 }
357
358                 /// <summary>
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"/>
361                 /// is thrown.
362                 /// </summary>
363                 /// <param name="anObject">The object that is to be tested</param>
364                 static public void IsNotNull(Object anObject) 
365                 {
366                         Assert.IsNotNull(anObject, string.Empty);
367                 }
368     
369                     
370                 /// <summary>
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"/>
373                 /// is thrown.
374                 /// </summary>
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) 
378                 {
379                         Assert.IsTrue(anObject == null, message); 
380                 }
381
382                 /// <summary>
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"/>
385                 /// is thrown.
386                 /// </summary>
387                 /// <param name="anObject">The object that is to be tested</param>
388                 static public void IsNull(Object anObject) 
389                 {
390                         Assert.IsNull(anObject, string.Empty);
391                 }
392     
393     
394                 /// <summary>
395                 /// Asserts that two objects refer to the same object. If they
396                 /// are not the same an <see cref="AssertionException"/> is thrown.
397                 /// </summary>
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)
402                 {
403                         if (object.ReferenceEquals(expected, actual)) return;
404
405                         Assert.FailNotSame(expected, actual, message);
406                 }
407
408                 /// <summary>
409                 /// Asserts that two objects refer to the same object. If they
410                 /// are not the same an <see cref="AssertionException"/> is thrown.
411                 /// </summary>
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) 
415                 {
416                         Assert.AreSame(expected, actual, string.Empty);
417                 }
418    
419                 /// <summary>
420                 /// Throws an <see cref="AssertionException"/> with the message that is 
421                 /// passed in. This is used by the other Assert functions. 
422                 /// </summary>
423                 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
424                 static public void Fail(string message) 
425                 {
426                         if (message == null) message = string.Empty;
427
428                         throw new AssertionException(message);
429                 }
430
431                 /// <summary>
432                 /// Throws an <see cref="AssertionException"/> with the message that is 
433                 /// passed in. This is used by the other Assert functions. 
434                 /// </summary>
435                 static public void Fail() 
436                 {
437                         Assert.Fail(string.Empty);
438                 }
439     
440                 /// <summary>
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. 
443                 /// </summary>
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) 
448                 {
449                         Assert.Fail( 
450                                 AssertionFailureMessage.FormatMessageForFailNotEquals( 
451                                 expected, 
452                                 actual, 
453                                 message));
454                 }
455     
456                 /// <summary>
457                 ///  This method is called when the two objects are not the same. 
458                 /// </summary>
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) 
463                 {
464                         string formatted=string.Empty;
465                         if (message != null)
466                                 formatted= message+" ";
467                         Assert.Fail(formatted+"expected same");
468                 }
469         }
470 }