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.Constraints;
26 using NUnit.Framework.Internal;
28 namespace NUnit.Framework
30 public partial class Assert
34 /// Verifies that a delegate throws a particular exception when called.
36 /// <param name="expression">A constraint to be satisfied by the exception</param>
37 /// <param name="code">A TestSnippet delegate</param>
38 /// <param name="message">The message that will be displayed on failure</param>
39 /// <param name="args">Arguments to be used in formatting the message</param>
40 public static Exception Throws(IResolveConstraint expression, TestDelegate code, string message, params object[] args)
42 Exception caughtException = null;
44 #if NET_4_0 || NET_4_5 || PORTABLE
45 if (AsyncInvocationRegion.IsAsyncOperation(code))
47 using (var region = AsyncInvocationRegion.Create(code))
53 region.WaitForPendingOperationsToComplete(null);
72 Assert.That(caughtException, expression, message, args);
74 return caughtException;
78 /// Verifies that a delegate throws a particular exception when called.
80 /// <param name="expression">A constraint to be satisfied by the exception</param>
81 /// <param name="code">A TestSnippet delegate</param>
82 public static Exception Throws(IResolveConstraint expression, TestDelegate code)
84 return Throws(expression, code, string.Empty, null);
88 /// Verifies that a delegate throws a particular exception when called.
90 /// <param name="expectedExceptionType">The exception Type expected</param>
91 /// <param name="code">A TestDelegate</param>
92 /// <param name="message">The message that will be displayed on failure</param>
93 /// <param name="args">Arguments to be used in formatting the message</param>
94 public static Exception Throws(Type expectedExceptionType, TestDelegate code, string message, params object[] args)
96 return Throws(new ExceptionTypeConstraint(expectedExceptionType), code, message, args);
100 /// Verifies that a delegate throws a particular exception when called.
102 /// <param name="expectedExceptionType">The exception Type expected</param>
103 /// <param name="code">A TestDelegate</param>
104 public static Exception Throws(Type expectedExceptionType, TestDelegate code)
106 return Throws(new ExceptionTypeConstraint(expectedExceptionType), code, string.Empty, null);
111 #region Throws<TActual>
114 /// Verifies that a delegate throws a particular exception when called.
116 /// <typeparam name="TActual">Type of the expected exception</typeparam>
117 /// <param name="code">A TestDelegate</param>
118 /// <param name="message">The message that will be displayed on failure</param>
119 /// <param name="args">Arguments to be used in formatting the message</param>
120 public static TActual Throws<TActual>(TestDelegate code, string message, params object[] args) where TActual : Exception
122 return (TActual)Throws(typeof(TActual), code, message, args);
126 /// Verifies that a delegate throws a particular exception when called.
128 /// <typeparam name="TActual">Type of the expected exception</typeparam>
129 /// <param name="code">A TestDelegate</param>
130 public static TActual Throws<TActual>(TestDelegate code) where TActual : Exception
132 return Throws<TActual>(code, string.Empty, null);
139 /// Verifies that a delegate throws an exception when called
142 /// <param name="code">A TestDelegate</param>
143 /// <param name="message">The message that will be displayed on failure</param>
144 /// <param name="args">Arguments to be used in formatting the message</param>
145 public static Exception Catch(TestDelegate code, string message, params object[] args)
147 return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code, message, args);
151 /// Verifies that a delegate throws an exception when called
154 /// <param name="code">A TestDelegate</param>
155 public static Exception Catch(TestDelegate code)
157 return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code);
161 /// Verifies that a delegate throws an exception of a certain Type
162 /// or one derived from it when called and returns it.
164 /// <param name="expectedExceptionType">The expected Exception Type</param>
165 /// <param name="code">A TestDelegate</param>
166 /// <param name="message">The message that will be displayed on failure</param>
167 /// <param name="args">Arguments to be used in formatting the message</param>
168 public static Exception Catch(Type expectedExceptionType, TestDelegate code, string message, params object[] args)
170 return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code, message, args);
174 /// Verifies that a delegate throws an exception of a certain Type
175 /// or one derived from it when called and returns it.
177 /// <param name="expectedExceptionType">The expected Exception Type</param>
178 /// <param name="code">A TestDelegate</param>
179 public static Exception Catch(Type expectedExceptionType, TestDelegate code)
181 return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code);
185 #region Catch<TActual>
188 /// Verifies that a delegate throws an exception of a certain Type
189 /// or one derived from it when called and returns it.
191 /// <param name="code">A TestDelegate</param>
192 /// <param name="message">The message that will be displayed on failure</param>
193 /// <param name="args">Arguments to be used in formatting the message</param>
194 public static TActual Catch<TActual>(TestDelegate code, string message, params object[] args) where TActual : System.Exception
196 return (TActual)Throws(new InstanceOfTypeConstraint(typeof(TActual)), code, message, args);
200 /// Verifies that a delegate throws an exception of a certain Type
201 /// or one derived from it when called and returns it.
203 /// <param name="code">A TestDelegate</param>
204 public static TActual Catch<TActual>(TestDelegate code) where TActual : System.Exception
206 return (TActual)Throws(new InstanceOfTypeConstraint(typeof(TActual)), code);
214 /// Verifies that a delegate does not throw an exception
216 /// <param name="code">A TestDelegate</param>
217 /// <param name="message">The message that will be displayed on failure</param>
218 /// <param name="args">Arguments to be used in formatting the message</param>
219 public static void DoesNotThrow(TestDelegate code, string message, params object[] args)
221 Assert.That(code, new ThrowsNothingConstraint(), message, args);
225 /// Verifies that a delegate does not throw an exception.
227 /// <param name="code">A TestDelegate</param>
228 public static void DoesNotThrow(TestDelegate code)
230 DoesNotThrow(code, string.Empty, null);