1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 using NUnit.Framework.Internal;
27 namespace NUnit.Framework
29 public partial class Assert
36 /// Verifies that two doubles are equal considering a delta. If the
37 /// expected value is infinity then the delta value is ignored. If
38 /// they are not equal then an <see cref="AssertionException"/> is
41 /// <param name="expected">The expected value</param>
42 /// <param name="actual">The actual value</param>
43 /// <param name="delta">The maximum acceptable difference between the
44 /// the expected and the actual</param>
45 /// <param name="message">The message to display in case of failure</param>
46 /// <param name="args">Array of objects to be used in formatting the message</param>
47 public static void AreEqual(double expected, double actual, double delta, string message, params object[] args)
49 AssertDoublesAreEqual(expected, actual, delta, message, args);
53 /// Verifies that two doubles are equal considering a delta. If the
54 /// expected value is infinity then the delta value is ignored. If
55 /// they are not equal then an <see cref="AssertionException"/> is
58 /// <param name="expected">The expected value</param>
59 /// <param name="actual">The actual value</param>
60 /// <param name="delta">The maximum acceptable difference between the
61 /// the expected and the actual</param>
62 public static void AreEqual(double expected, double actual, double delta)
64 AssertDoublesAreEqual(expected, actual, delta, null, null);
68 /// Verifies that two doubles are equal considering a delta. If the
69 /// expected value is infinity then the delta value is ignored. If
70 /// they are not equal then an <see cref="AssertionException"/> is
73 /// <param name="expected">The expected value</param>
74 /// <param name="actual">The actual value</param>
75 /// <param name="delta">The maximum acceptable difference between the
76 /// the expected and the actual</param>
77 /// <param name="message">The message to display in case of failure</param>
78 /// <param name="args">Array of objects to be used in formatting the message</param>
79 public static void AreEqual(double expected, double? actual, double delta, string message, params object[] args)
81 AssertDoublesAreEqual(expected, (double)actual, delta, message, args);
85 /// Verifies that two doubles are equal considering a delta. If the
86 /// expected value is infinity then the delta value is ignored. If
87 /// they are not equal then an <see cref="AssertionException"/> is
90 /// <param name="expected">The expected value</param>
91 /// <param name="actual">The actual value</param>
92 /// <param name="delta">The maximum acceptable difference between the
93 /// the expected and the actual</param>
94 public static void AreEqual(double expected, double? actual, double delta)
96 AssertDoublesAreEqual(expected, (double)actual, delta, null, null);
104 /// Verifies that two objects are equal. Two objects are considered
105 /// equal if both are null, or if both have the same value. NUnit
106 /// has special semantics for some object types.
107 /// If they are not equal an <see cref="AssertionException"/> is thrown.
109 /// <param name="expected">The value that is expected</param>
110 /// <param name="actual">The actual value</param>
111 /// <param name="message">The message to display in case of failure</param>
112 /// <param name="args">Array of objects to be used in formatting the message</param>
113 public static void AreEqual(object expected, object actual, string message, params object[] args)
115 Assert.That(actual, Is.EqualTo(expected), message, args);
119 /// Verifies that two objects are equal. Two objects are considered
120 /// equal if both are null, or if both have the same value. NUnit
121 /// has special semantics for some object types.
122 /// If they are not equal an <see cref="AssertionException"/> is thrown.
124 /// <param name="expected">The value that is expected</param>
125 /// <param name="actual">The actual value</param>
126 public static void AreEqual(object expected, object actual)
128 Assert.That(actual, Is.EqualTo(expected), null, null);
140 /// Verifies that two objects are not equal. Two objects are considered
141 /// equal if both are null, or if both have the same value. NUnit
142 /// has special semantics for some object types.
143 /// If they are equal an <see cref="AssertionException"/> is thrown.
145 /// <param name="expected">The value that is expected</param>
146 /// <param name="actual">The actual value</param>
147 /// <param name="message">The message to display in case of failure</param>
148 /// <param name="args">Array of objects to be used in formatting the message</param>
149 public static void AreNotEqual(object expected, object actual, string message, params object[] args)
151 Assert.That(actual, Is.Not.EqualTo(expected), message, args);
155 /// Verifies that two objects are not equal. Two objects are considered
156 /// equal if both are null, or if both have the same value. NUnit
157 /// has special semantics for some object types.
158 /// If they are equal an <see cref="AssertionException"/> is thrown.
160 /// <param name="expected">The value that is expected</param>
161 /// <param name="actual">The actual value</param>
162 public static void AreNotEqual(object expected, object actual)
164 Assert.That(actual, Is.Not.EqualTo(expected), null, null);
174 /// Asserts that two objects refer to the same object. If they
175 /// are not the same an <see cref="AssertionException"/> is thrown.
177 /// <param name="expected">The expected object</param>
178 /// <param name="actual">The actual object</param>
179 /// <param name="message">The message to display in case of failure</param>
180 /// <param name="args">Array of objects to be used in formatting the message</param>
181 public static void AreSame(object expected, object actual, string message, params object[] args)
183 Assert.That(actual, Is.SameAs(expected), message, args);
187 /// Asserts that two objects refer to the same object. If they
188 /// are not the same an <see cref="AssertionException"/> is thrown.
190 /// <param name="expected">The expected object</param>
191 /// <param name="actual">The actual object</param>
192 public static void AreSame(object expected, object actual)
194 Assert.That(actual, Is.SameAs(expected), null, null);
202 /// Asserts that two objects do not refer to the same object. If they
203 /// are the same an <see cref="AssertionException"/> is thrown.
205 /// <param name="expected">The expected object</param>
206 /// <param name="actual">The actual object</param>
207 /// <param name="message">The message to display in case of failure</param>
208 /// <param name="args">Array of objects to be used in formatting the message</param>
209 public static void AreNotSame(object expected, object actual, string message, params object[] args)
211 Assert.That(actual, Is.Not.SameAs(expected), message, args);
215 /// Asserts that two objects do not refer to the same object. If they
216 /// are the same an <see cref="AssertionException"/> is thrown.
218 /// <param name="expected">The expected object</param>
219 /// <param name="actual">The actual object</param>
220 public static void AreNotSame(object expected, object actual)
222 Assert.That(actual, Is.Not.SameAs(expected), null, null);
227 #region Helper Methods
230 /// Helper for Assert.AreEqual(double expected, double actual, ...)
231 /// allowing code generation to work consistently.
233 /// <param name="expected">The expected value</param>
234 /// <param name="actual">The actual value</param>
235 /// <param name="delta">The maximum acceptable difference between the
236 /// the expected and the actual</param>
237 /// <param name="message">The message to display in case of failure</param>
238 /// <param name="args">Array of objects to be used in formatting the message</param>
239 protected static void AssertDoublesAreEqual(double expected, double actual, double delta, string message, object[] args)
241 if (double.IsNaN(expected) || double.IsInfinity(expected))
242 Assert.That(actual, Is.EqualTo(expected), message, args);
244 Assert.That(actual, Is.EqualTo(expected).Within(delta), message, args);
247 private static void IncrementAssertCount()
249 TestExecutionContext.CurrentContext.IncrementAssertCount();