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 #define NUNIT_FRAMEWORK
30 using System.Collections;
31 using System.ComponentModel;
32 using NUnit.Framework.Constraints;
34 namespace NUnit.Framework
37 /// Delegate used by tests that execute code and
38 /// capture any thrown exception.
40 public delegate void TestDelegate();
42 #if NET_4_0 || NET_4_5 || PORTABLE
44 /// Delegate used by tests that execute async code and
45 /// capture any thrown exception.
47 public delegate System.Threading.Tasks.Task AsyncTestDelegate();
51 /// The Assert class contains a collection of static methods that
52 /// implement the most common assertions used in NUnit.
54 public partial class Assert
59 /// We don't actually want any instances of this object, but some people
60 /// like to inherit from it to add other static methods. Hence, the
61 /// protected constructor disallows any instances of this object.
63 protected Assert() { }
67 #region Equals and ReferenceEquals
70 /// The Equals method throws an InvalidOperationException. This is done
71 /// to make sure there is no mistake by calling this function.
73 /// <param name="a"></param>
74 /// <param name="b"></param>
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public static new bool Equals(object a, object b)
78 throw new InvalidOperationException("Assert.Equals should not be used for Assertions");
82 /// override the default ReferenceEquals to throw an InvalidOperationException. This
83 /// implementation makes sure there is no mistake in calling this function
84 /// as part of Assert.
86 /// <param name="a"></param>
87 /// <param name="b"></param>
88 public static new void ReferenceEquals(object a, object b)
90 throw new InvalidOperationException("Assert.ReferenceEquals should not be used for Assertions");
98 /// Throws a <see cref="SuccessException"/> with the message and arguments
99 /// that are passed in. This allows a test to be cut short, with a result
100 /// of success returned to NUnit.
102 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
103 /// <param name="args">Arguments to be used in formatting the message</param>
104 static public void Pass(string message, params object[] args)
106 if (message == null) message = string.Empty;
107 else if (args != null && args.Length > 0)
108 message = string.Format(message, args);
110 //throw new SuccessException(message);
114 /// Throws a <see cref="SuccessException"/> with the message and arguments
115 /// that are passed in. This allows a test to be cut short, with a result
116 /// of success returned to NUnit.
118 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
119 static public void Pass(string message)
121 Assert.Pass(message, null);
125 /// Throws a <see cref="SuccessException"/> with the message and arguments
126 /// that are passed in. This allows a test to be cut short, with a result
127 /// of success returned to NUnit.
129 static public void Pass()
131 Assert.Pass(string.Empty, null);
139 /// Throws an <see cref="AssertionException"/> with the message and arguments
140 /// that are passed in. This is used by the other Assert functions.
142 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
143 /// <param name="args">Arguments to be used in formatting the message</param>
144 static public void Fail(string message, params object[] args)
146 if (message == null) message = string.Empty;
147 else if (args != null && args.Length > 0)
148 message = string.Format(message, args);
150 throw new AssertionException(message);
154 /// Throws an <see cref="AssertionException"/> with the message that is
155 /// passed in. This is used by the other Assert functions.
157 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
158 static public void Fail(string message)
160 Assert.Fail(message, null);
164 /// Throws an <see cref="AssertionException"/>.
165 /// This is used by the other Assert functions.
167 static public void Fail()
169 Assert.Fail(string.Empty, null);
177 /// Throws an <see cref="IgnoreException"/> with the message and arguments
178 /// that are passed in. This causes the test to be reported as ignored.
180 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
181 /// <param name="args">Arguments to be used in formatting the message</param>
182 static public void Ignore(string message, params object[] args)
184 if (message == null) message = string.Empty;
185 else if (args != null && args.Length > 0)
186 message = string.Format(message, args);
188 throw new IgnoreException(message);
192 /// Throws an <see cref="IgnoreException"/> with the message that is
193 /// passed in. This causes the test to be reported as ignored.
195 /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
196 static public void Ignore(string message)
198 Assert.Ignore(message, null);
202 /// Throws an <see cref="IgnoreException"/>.
203 /// This causes the test to be reported as ignored.
205 static public void Ignore()
207 Assert.Ignore(string.Empty, null);
215 /// Throws an <see cref="InconclusiveException"/> with the message and arguments
216 /// that are passed in. This causes the test to be reported as inconclusive.
218 /// <param name="message">The message to initialize the <see cref="InconclusiveException"/> with.</param>
219 /// <param name="args">Arguments to be used in formatting the message</param>
220 static public void Inconclusive(string message, params object[] args)
222 if (message == null) message = string.Empty;
223 else if (args != null && args.Length > 0)
224 message = string.Format(message, args);
226 throw new InconclusiveException(message);
230 /// Throws an <see cref="InconclusiveException"/> with the message that is
231 /// passed in. This causes the test to be reported as inconclusive.
233 /// <param name="message">The message to initialize the <see cref="InconclusiveException"/> with.</param>
234 static public void Inconclusive(string message)
236 Assert.Inconclusive(message, null);
240 /// Throws an <see cref="InconclusiveException"/>.
241 /// This causes the test to be reported as Inconclusive.
243 static public void Inconclusive()
245 Assert.Inconclusive(string.Empty, null);
253 /// Asserts that an object is contained in a list.
255 /// <param name="expected">The expected object</param>
256 /// <param name="actual">The list to be examined</param>
257 /// <param name="message">The message to display in case of failure</param>
258 /// <param name="args">Array of objects to be used in formatting the message</param>
259 public static void Contains(object expected, ICollection actual, string message, params object[] args)
261 Assert.That(actual, new CollectionContainsConstraint(expected) ,message, args);
265 /// Asserts that an object is contained in a list.
267 /// <param name="expected">The expected object</param>
268 /// <param name="actual">The list to be examined</param>
269 public static void Contains(object expected, ICollection actual)
271 Assert.That(actual, new CollectionContainsConstraint(expected) ,null, null);
279 ///// If an assert fails within this block, execution will continue and
280 ///// the errors will be reported at the end of the block.
282 ///// <param name="del">The test delegate</param>
283 //public static void Multiple(TestDelegate del)