f563c0bb7af2c320dd0a9144db012badb76978cf
[mono.git] / mcs / nunit20 / framework / Assertion.cs
1 #region Copyright (c) 2002, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright © 2002 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
5 ' Copyright © 2000-2002 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 © 2002 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov 
20 ' or Copyright © 2000-2002 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 namespace NUnit.Framework 
31 {
32         using System;
33
34         /// <summary>A set of Assert methods.</summary>
35         public class Assertion
36         {
37                 /// <summary>
38                 /// Asserts that a condition is true. If it isn't it throws
39                 /// an <see cref="AssertionFailedError"/>.
40                 /// </summary>
41                 /// <param name="message">The message to display is the condition
42                 /// is false</param>
43                 /// <param name="condition">The evaluated condition</param>
44                 static public void Assert(string message, bool condition) 
45                 {
46                         if (!condition)
47                                 Assertion.Fail(message);
48                 }
49     
50                 /// <summary>
51                 /// Asserts that a condition is true. If it isn't it throws
52                 /// an <see cref="AssertionFailedError"/>.
53                 /// </summary>
54                 /// <param name="condition">The evaluated condition</param>
55                 static public void Assert(bool condition) 
56                 {
57                         Assertion.Assert(string.Empty, condition);
58                 }
59
60                 /// <summary>
61                 /// /// Asserts that two doubles are equal concerning a delta. If the
62                 /// expected value is infinity then the delta value is ignored.
63                 /// </summary>
64                 /// <param name="expected">The expected value</param>
65                 /// <param name="actual">The actual value</param>
66                 /// <param name="delta">The maximum acceptable difference between the
67                 /// the expected and the actual</param>
68                 static public void AssertEquals(double expected, double actual, double delta) 
69                 {
70                         Assertion.AssertEquals(string.Empty, expected, actual, delta);
71                 }
72                 /// <summary>
73                 /// /// Asserts that two singles are equal concerning a delta. If the
74                 /// expected value is infinity then the delta value is ignored.
75                 /// </summary>
76                 /// <param name="expected">The expected value</param>
77                 /// <param name="actual">The actual value</param>
78                 /// <param name="delta">The maximum acceptable difference between the
79                 /// the expected and the actual</param>
80                 static public void AssertEquals(float expected, float actual, float delta) 
81                 {
82                         Assertion.AssertEquals(string.Empty, expected, actual, delta);
83                 }
84
85                 /// <summary>Asserts that two objects are equal. If they are not
86                 /// an <see cref="AssertionFailedError"/> is thrown.</summary>
87                 static public void AssertEquals(Object expected, Object actual) 
88                 {
89                         Assertion.AssertEquals(string.Empty, expected, actual);
90                 }
91
92                 static public void AssertEquals(int expected, int actual) 
93                 {
94                         Assertion.AssertEquals(string.Empty, expected, actual);
95                 }
96
97                 static public void AssertEquals(string message, int expected, int actual) 
98                 {
99                         if (expected != actual)
100                                 Assertion.FailNotEquals(message, expected, actual);
101                 }
102                 
103                 /// <summary>Asserts that two doubles are equal concerning a delta.
104                 /// If the expected value is infinity then the delta value is ignored.
105                 /// </summary>
106                 static public void AssertEquals(string message, double expected, 
107                         double actual, double delta) 
108                 {
109                         // handle infinity specially since subtracting two infinite values gives 
110                         // NaN and the following test fails
111                         if (double.IsInfinity(expected)) 
112                         {
113                                 if (!(expected == actual))
114                                         Assertion.FailNotEquals(message, expected, actual);
115                         } 
116                         else if (!(Math.Abs(expected-actual) <= delta))
117                                 Assertion.FailNotEquals(message, expected, actual);
118                 }
119                 
120                 /// <summary>Asserts that two floats are equal concerning a delta.
121                 /// If the expected value is infinity then the delta value is ignored.
122                 /// </summary>
123                 static public void AssertEquals(string message, float expected, 
124                         float actual, float delta) 
125                 {
126                         // handle infinity specially since subtracting two infinite values gives 
127                         // NaN and the following test fails
128                         if (float.IsInfinity(expected)) 
129                         {
130                                 if (!(expected == actual))
131                                         Assertion.FailNotEquals(message, expected, actual);
132                         } 
133                         else if (!(Math.Abs(expected-actual) <= delta))
134                                 Assertion.FailNotEquals(message, expected, actual);
135                 }
136
137         /// <summary>
138         /// Checks the type of the object, returning true if
139         /// the object is a numeric type.
140         /// </summary>
141         /// <param name="obj"></param>
142         /// <returns></returns>
143         static private bool IsNumericType( Object obj )
144         {
145             if( null != obj )
146             {
147                 if( obj is byte    ) return true;
148                 if( obj is sbyte   ) return true;
149                 if( obj is decimal ) return true;
150                 if( obj is double  ) return true;
151                 if( obj is float   ) return true;
152                 if( obj is int     ) return true;
153                 if( obj is uint    ) return true;
154                 if( obj is long    ) return true;
155                 if( obj is short   ) return true;
156                 if( obj is ushort  ) return true;
157
158                 if( obj is System.Byte    ) return true;
159                 if( obj is System.SByte   ) return true;
160                 if( obj is System.Decimal ) return true;
161                 if( obj is System.Double  ) return true;
162                 if( obj is System.Single  ) return true;
163                 if( obj is System.Int32   ) return true;
164                 if( obj is System.UInt32  ) return true;
165                 if( obj is System.Int64   ) return true;
166                 if( obj is System.UInt64  ) return true;
167                 if( obj is System.Int16   ) return true;
168                 if( obj is System.UInt16  ) return true;
169             }
170             return false;
171         }
172
173         /// <summary>
174         /// Used to compare numeric types.  Comparisons between
175         /// same types are fine (Int32 to Int32, or Int64 to Int64),
176         /// but the Equals method fails across different types.
177         /// This method was added to allow any numeric type to
178         /// be handled correctly, by using <c>ToString</c> and
179         /// comparing the result
180         /// </summary>
181         /// <param name="expected"></param>
182         /// <param name="actual"></param>
183         /// <returns></returns>
184         static private bool ObjectsEqual( Object expected, Object actual )
185         {
186             if( IsNumericType( expected )  &&
187                 IsNumericType( actual ) )
188             {
189                 //
190                 // Convert to strings and compare result to avoid
191                 // issues with different types that have the same
192                 // value
193                 //
194                 string sExpected = expected.ToString();
195                 string sActual   = actual.ToString();
196                 return sExpected.Equals( sActual );
197             }
198             return expected.Equals(actual);
199         }
200
201                 /// <summary>
202                 /// Asserts that two objects are equal.  Two objects are considered
203                 /// equal if both are null, or if both have the same value.  Numeric
204                 /// types are compared via string comparision on their contents to
205                 /// avoid problems comparing values between different types.  All
206                 /// non-numeric types are compared by using the <c>Equals</c> method.
207                 /// If they are not equal an <see cref="AssertionFailedError"/> is thrown.
208                 /// </summary>
209                 static public void AssertEquals(string message, Object expected, Object actual)
210                 {
211             if (expected == null && actual == null)
212             {
213                 return;
214             }
215             if (expected != null && actual != null )
216             {
217                 if( ObjectsEqual( expected, actual ) )
218                 {
219                     return;
220                 }
221             }
222                         Assertion.FailNotEquals(message, expected, actual);
223                 }
224     
225                 /// <summary>Asserts that an object isn't null.</summary>
226                 static public void AssertNotNull(Object anObject) 
227                 {
228                         Assertion.AssertNotNull(string.Empty, anObject);
229                 }
230     
231                 /// <summary>Asserts that an object isn't null.</summary>
232                 static public void AssertNotNull(string message, Object anObject) 
233                 {
234                         Assertion.Assert(message, anObject != null); 
235                 }
236     
237                 /// <summary>Asserts that an object is null.</summary>
238                 static public void AssertNull(Object anObject) 
239                 {
240                         Assertion.AssertNull(string.Empty, anObject);
241                 }
242     
243                 /// <summary>Asserts that an object is null.</summary>
244                 static public void AssertNull(string message, Object anObject) 
245                 {
246                         Assertion.Assert(message, anObject == null); 
247                 }
248     
249                 /// <summary>Asserts that two objects refer to the same object. If they
250                 /// are not the same an <see cref="AssertionFailedError"/> is thrown.
251                 /// </summary>
252                 static public void AssertSame(Object expected, Object actual) 
253                 {
254                         Assertion.AssertSame(string.Empty, expected, actual);
255                 }
256     
257                 /// <summary>Asserts that two objects refer to the same object. 
258                 /// If they are not an <see cref="AssertionFailedError"/> is thrown.
259                 /// </summary>
260                 static public void AssertSame(string message, Object expected, Object actual)
261                 {
262                         if (expected == actual)
263                                 return;
264                         Assertion.FailNotSame(message, expected, actual);
265                 }
266     
267                 /// <summary>Fails a test with no message.</summary>
268                 static public void Fail() 
269                 {
270                         Assertion.Fail(string.Empty);
271                 }
272     
273                 /// <summary>Fails a test with the given message.</summary>
274                 static public void Fail(string message) 
275                 {
276                         if (message == null)
277                                 message = string.Empty;
278                         throw new AssertionException(message);
279                 }
280
281         /// <summary>
282         /// Called when two objects have been compared and found to be
283         /// different.
284         /// </summary>
285         /// <param name="message"></param>
286         /// <param name="expected"></param>
287         /// <param name="actual"></param>
288                 static private void FailNotEquals(string message, Object expected, Object actual) 
289                 {
290             Assertion.Fail( 
291                 AssertionFailureMessage.FormatMessageForFailNotEquals( 
292                     message, 
293                     expected, 
294                     actual) );
295                 }
296     
297                 static private void FailNotSame(string message, Object expected, Object actual) 
298                 {
299                         string formatted=string.Empty;
300                         if (message != null)
301                                 formatted= message+" ";
302                         Assertion.Fail(formatted+"expected same");
303                 }
304         }
305 }