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 // ***********************************************************************
26 #define NUNIT_FRAMEWORK
30 #if NET_4_0 || NET_4_5 || PORTABLE
32 using NUnit.Framework.Constraints;
33 using NUnit.Framework.Internal;
36 namespace NUnit.Framework
38 public partial class Assert
43 /// Verifies that an async delegate throws a particular exception when called.
45 /// <param name="expression">A constraint to be satisfied by the exception</param>
46 /// <param name="code">A TestSnippet delegate</param>
47 /// <param name="message">The message that will be displayed on failure</param>
48 /// <param name="args">Arguments to be used in formatting the message</param>
49 public static Exception ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, string message, params object[] args)
51 Exception caughtException = null;
52 using (var region = AsyncInvocationRegion.Create(code))
57 region.WaitForPendingOperationsToComplete(task);
65 Assert.That(caughtException, expression, message, args);
67 return caughtException;
71 /// Verifies that an async delegate throws a particular exception when called.
73 /// <param name="expression">A constraint to be satisfied by the exception</param>
74 /// <param name="code">A TestSnippet delegate</param>
75 public static Exception ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code)
77 return ThrowsAsync(expression, code, string.Empty, null);
81 /// Verifies that an async delegate throws a particular exception when called.
83 /// <param name="expectedExceptionType">The exception Type expected</param>
84 /// <param name="code">A TestDelegate</param>
85 /// <param name="message">The message that will be displayed on failure</param>
86 /// <param name="args">Arguments to be used in formatting the message</param>
87 public static Exception ThrowsAsync(Type expectedExceptionType, AsyncTestDelegate code, string message, params object[] args)
89 return ThrowsAsync(new ExceptionTypeConstraint(expectedExceptionType), code, message, args);
93 /// Verifies that an async delegate throws a particular exception when called.
95 /// <param name="expectedExceptionType">The exception Type expected</param>
96 /// <param name="code">A TestDelegate</param>
97 public static Exception ThrowsAsync(Type expectedExceptionType, AsyncTestDelegate code)
99 return ThrowsAsync(new ExceptionTypeConstraint(expectedExceptionType), code, string.Empty, null);
104 #region ThrowsAsync<TActual>
107 /// Verifies that an async delegate throws a particular exception when called.
109 /// <typeparam name="TActual">Type of the expected exception</typeparam>
110 /// <param name="code">A TestDelegate</param>
111 /// <param name="message">The message that will be displayed on failure</param>
112 /// <param name="args">Arguments to be used in formatting the message</param>
113 public static TActual ThrowsAsync<TActual>(AsyncTestDelegate code, string message, params object[] args) where TActual : Exception
115 return (TActual)ThrowsAsync(typeof (TActual), code, message, args);
119 /// Verifies that an async delegate throws a particular exception when called.
121 /// <typeparam name="TActual">Type of the expected exception</typeparam>
122 /// <param name="code">A TestDelegate</param>
123 public static TActual ThrowsAsync<TActual>(AsyncTestDelegate code) where TActual : Exception
125 return ThrowsAsync<TActual>(code, string.Empty, null);
133 /// Verifies that an async delegate throws an exception when called
136 /// <param name="code">A TestDelegate</param>
137 /// <param name="message">The message that will be displayed on failure</param>
138 /// <param name="args">Arguments to be used in formatting the message</param>
139 public static Exception CatchAsync(AsyncTestDelegate code, string message, params object[] args)
141 return ThrowsAsync(new InstanceOfTypeConstraint(typeof(Exception)), code, message, args);
145 /// Verifies that an async delegate throws an exception when called
148 /// <param name="code">A TestDelegate</param>
149 public static Exception CatchAsync(AsyncTestDelegate code)
151 return ThrowsAsync(new InstanceOfTypeConstraint(typeof(Exception)), code);
155 /// Verifies that an async delegate throws an exception of a certain Type
156 /// or one derived from it when called and returns it.
158 /// <param name="expectedExceptionType">The expected Exception Type</param>
159 /// <param name="code">A TestDelegate</param>
160 /// <param name="message">The message that will be displayed on failure</param>
161 /// <param name="args">Arguments to be used in formatting the message</param>
162 public static Exception CatchAsync(Type expectedExceptionType, AsyncTestDelegate code, string message, params object[] args)
164 return ThrowsAsync(new InstanceOfTypeConstraint(expectedExceptionType), code, message, args);
168 /// Verifies that an async delegate throws an exception of a certain Type
169 /// or one derived from it when called and returns it.
171 /// <param name="expectedExceptionType">The expected Exception Type</param>
172 /// <param name="code">A TestDelegate</param>
173 public static Exception CatchAsync(Type expectedExceptionType, AsyncTestDelegate code)
175 return ThrowsAsync(new InstanceOfTypeConstraint(expectedExceptionType), code);
180 #region CatchAsync<TActual>
183 /// Verifies that an async delegate throws an exception of a certain Type
184 /// or one derived from it when called and returns it.
186 /// <param name="code">A TestDelegate</param>
187 /// <param name="message">The message that will be displayed on failure</param>
188 /// <param name="args">Arguments to be used in formatting the message</param>
189 public static TActual CatchAsync<TActual>(AsyncTestDelegate code, string message, params object[] args) where TActual : Exception
191 return (TActual)ThrowsAsync(new InstanceOfTypeConstraint(typeof (TActual)), code, message, args);
195 /// Verifies that an async delegate throws an exception of a certain Type
196 /// or one derived from it when called and returns it.
198 /// <param name="code">A TestDelegate</param>
199 public static TActual CatchAsync<TActual>(AsyncTestDelegate code) where TActual : Exception
201 return (TActual)ThrowsAsync(new InstanceOfTypeConstraint(typeof (TActual)), code);
206 #region DoesNotThrowAsync
209 /// Verifies that an async delegate does not throw an exception
211 /// <param name="code">A TestDelegate</param>
212 /// <param name="message">The message that will be displayed on failure</param>
213 /// <param name="args">Arguments to be used in formatting the message</param>
214 public static void DoesNotThrowAsync(AsyncTestDelegate code, string message, params object[] args)
216 Assert.That(code, new ThrowsNothingConstraint(), message, args);
219 /// Verifies that an async delegate does not throw an exception.
221 /// <param name="code">A TestDelegate</param>
222 public static void DoesNotThrowAsync(AsyncTestDelegate code)
224 DoesNotThrowAsync(code, string.Empty, null);