1 #region Copyright (c) 2002, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Philip A. Craig
2 /************************************************************************************
4 ' Copyright © 2002 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
5 ' Copyright © 2000-2002 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 © 2002 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
20 ' or Copyright © 2000-2002 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 '***********************************************************************************/
30 namespace NUnit.Framework
34 /// <summary>A set of Assert methods.</summary>
35 public class Assertion
38 /// Asserts that a condition is true. If it isn't it throws
39 /// an <see cref="AssertionFailedError"/>.
41 /// <param name="message">The message to display is the condition
43 /// <param name="condition">The evaluated condition</param>
44 static public void Assert(string message, bool condition)
47 Assertion.Fail(message);
51 /// Asserts that a condition is true. If it isn't it throws
52 /// an <see cref="AssertionFailedError"/>.
54 /// <param name="condition">The evaluated condition</param>
55 static public void Assert(bool condition)
57 Assertion.Assert(string.Empty, condition);
61 /// /// Asserts that two doubles are equal concerning a delta. If the
62 /// expected value is infinity then the delta value is ignored.
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)
70 Assertion.AssertEquals(string.Empty, expected, actual, delta);
73 /// /// Asserts that two singles are equal concerning a delta. If the
74 /// expected value is infinity then the delta value is ignored.
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)
82 Assertion.AssertEquals(string.Empty, expected, actual, delta);
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)
89 Assertion.AssertEquals(string.Empty, expected, actual);
92 static public void AssertEquals(int expected, int actual)
94 Assertion.AssertEquals(string.Empty, expected, actual);
97 static public void AssertEquals(string message, int expected, int actual)
99 if (expected != actual)
100 Assertion.FailNotEquals(message, expected, actual);
103 /// <summary>Asserts that two doubles are equal concerning a delta.
104 /// If the expected value is infinity then the delta value is ignored.
106 static public void AssertEquals(string message, double expected,
107 double actual, double delta)
109 // handle infinity specially since subtracting two infinite values gives
110 // NaN and the following test fails
111 if (double.IsInfinity(expected))
113 if (!(expected == actual))
114 Assertion.FailNotEquals(message, expected, actual);
116 else if (!(Math.Abs(expected-actual) <= delta))
117 Assertion.FailNotEquals(message, expected, actual);
120 /// <summary>Asserts that two floats are equal concerning a delta.
121 /// If the expected value is infinity then the delta value is ignored.
123 static public void AssertEquals(string message, float expected,
124 float actual, float delta)
126 // handle infinity specially since subtracting two infinite values gives
127 // NaN and the following test fails
128 if (float.IsInfinity(expected))
130 if (!(expected == actual))
131 Assertion.FailNotEquals(message, expected, actual);
133 else if (!(Math.Abs(expected-actual) <= delta))
134 Assertion.FailNotEquals(message, expected, actual);
138 /// Checks the type of the object, returning true if
139 /// the object is a numeric type.
141 /// <param name="obj"></param>
142 /// <returns></returns>
143 static private bool IsNumericType( Object obj )
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;
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;
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
181 /// <param name="expected"></param>
182 /// <param name="actual"></param>
183 /// <returns></returns>
184 static private bool ObjectsEqual( Object expected, Object actual )
186 if( IsNumericType( expected ) &&
187 IsNumericType( actual ) )
190 // Convert to strings and compare result to avoid
191 // issues with different types that have the same
194 string sExpected = expected.ToString();
195 string sActual = actual.ToString();
196 return sExpected.Equals( sActual );
198 return expected.Equals(actual);
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.
209 static public void AssertEquals(string message, Object expected, Object actual)
211 if (expected == null && actual == null)
215 if (expected != null && actual != null )
217 if( ObjectsEqual( expected, actual ) )
222 Assertion.FailNotEquals(message, expected, actual);
225 /// <summary>Asserts that an object isn't null.</summary>
226 static public void AssertNotNull(Object anObject)
228 Assertion.AssertNotNull(string.Empty, anObject);
231 /// <summary>Asserts that an object isn't null.</summary>
232 static public void AssertNotNull(string message, Object anObject)
234 Assertion.Assert(message, anObject != null);
237 /// <summary>Asserts that an object is null.</summary>
238 static public void AssertNull(Object anObject)
240 Assertion.AssertNull(string.Empty, anObject);
243 /// <summary>Asserts that an object is null.</summary>
244 static public void AssertNull(string message, Object anObject)
246 Assertion.Assert(message, anObject == null);
249 /// <summary>Asserts that two objects refer to the same object. If they
250 /// are not the same an <see cref="AssertionFailedError"/> is thrown.
252 static public void AssertSame(Object expected, Object actual)
254 Assertion.AssertSame(string.Empty, expected, actual);
257 /// <summary>Asserts that two objects refer to the same object.
258 /// If they are not an <see cref="AssertionFailedError"/> is thrown.
260 static public void AssertSame(string message, Object expected, Object actual)
262 if (expected == actual)
264 Assertion.FailNotSame(message, expected, actual);
267 /// <summary>Fails a test with no message.</summary>
268 static public void Fail()
270 Assertion.Fail(string.Empty);
273 /// <summary>Fails a test with the given message.</summary>
274 static public void Fail(string message)
277 message = string.Empty;
278 throw new AssertionException(message);
282 /// Called when two objects have been compared and found to be
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)
291 AssertionFailureMessage.FormatMessageForFailNotEquals(
297 static private void FailNotSame(string message, Object expected, Object actual)
299 string formatted=string.Empty;
301 formatted= message+" ";
302 Assertion.Fail(formatted+"expected same");