[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Assert / Assert.Exceptions.Async.cs
1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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 // ***********************************************************************
23
24 #define PORTABLE
25 #define TIZEN
26 #define NUNIT_FRAMEWORK
27 #define NUNITLITE
28 #define NET_4_5
29 #define PARALLEL
30 #if NET_4_0 || NET_4_5 || PORTABLE
31 using System;
32 using NUnit.Framework.Constraints;
33 using NUnit.Framework.Internal;
34
35
36 namespace NUnit.Framework
37 {
38     public partial class Assert
39     {
40         #region ThrowsAsync
41
42         /// <summary>
43         /// Verifies that an async delegate throws a particular exception when called.
44         /// </summary>
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)
50         {
51             Exception caughtException = null;
52             using (var region = AsyncInvocationRegion.Create(code))
53             {
54                 try
55                 {
56                     var task = code();
57                     region.WaitForPendingOperationsToComplete(task);
58                 }
59                 catch (Exception e)
60                 {
61                     caughtException = e;
62                 }
63             }
64
65             Assert.That(caughtException, expression, message, args);
66
67             return caughtException;
68         }
69
70         /// <summary>
71         /// Verifies that an async delegate throws a particular exception when called.
72         /// </summary>
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)
76         {
77             return ThrowsAsync(expression, code, string.Empty, null);
78         }
79
80         /// <summary>
81         /// Verifies that an async delegate throws a particular exception when called.
82         /// </summary>
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)
88         {
89             return ThrowsAsync(new ExceptionTypeConstraint(expectedExceptionType), code, message, args);
90         }
91
92         /// <summary>
93         /// Verifies that an async delegate throws a particular exception when called.
94         /// </summary>
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)
98         {
99             return ThrowsAsync(new ExceptionTypeConstraint(expectedExceptionType), code, string.Empty, null);
100         }
101
102         #endregion
103
104         #region ThrowsAsync<TActual>
105
106         /// <summary>
107         /// Verifies that an async delegate throws a particular exception when called.
108         /// </summary>
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
114         {
115             return (TActual)ThrowsAsync(typeof (TActual), code, message, args);
116         }
117
118         /// <summary>
119         /// Verifies that an async delegate throws a particular exception when called.
120         /// </summary>
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
124         {
125             return ThrowsAsync<TActual>(code, string.Empty, null);
126         }
127
128         #endregion
129
130         #region CatchAsync
131
132         /// <summary>
133         /// Verifies that an async delegate throws an exception when called
134         /// and returns it.
135         /// </summary>
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)
140         {
141             return ThrowsAsync(new InstanceOfTypeConstraint(typeof(Exception)), code, message, args);
142         }
143
144         /// <summary>
145         /// Verifies that an async delegate throws an exception when called
146         /// and returns it.
147         /// </summary>
148         /// <param name="code">A TestDelegate</param>
149         public static Exception CatchAsync(AsyncTestDelegate code)
150         {
151             return ThrowsAsync(new InstanceOfTypeConstraint(typeof(Exception)), code);
152         }
153
154         /// <summary>
155         /// Verifies that an async delegate throws an exception of a certain Type
156         /// or one derived from it when called and returns it.
157         /// </summary>
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)
163         {
164             return ThrowsAsync(new InstanceOfTypeConstraint(expectedExceptionType), code, message, args);
165         }
166
167         /// <summary>
168         /// Verifies that an async delegate throws an exception of a certain Type
169         /// or one derived from it when called and returns it.
170         /// </summary>
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)
174         {
175             return ThrowsAsync(new InstanceOfTypeConstraint(expectedExceptionType), code);
176         }
177
178         #endregion
179
180         #region CatchAsync<TActual>
181
182         /// <summary>
183         /// Verifies that an async delegate throws an exception of a certain Type
184         /// or one derived from it when called and returns it.
185         /// </summary>
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
190         {
191             return (TActual)ThrowsAsync(new InstanceOfTypeConstraint(typeof (TActual)), code, message, args);
192         }
193
194         /// <summary>
195         /// Verifies that an async delegate throws an exception of a certain Type
196         /// or one derived from it when called and returns it.
197         /// </summary>
198         /// <param name="code">A TestDelegate</param>
199         public static TActual CatchAsync<TActual>(AsyncTestDelegate code) where TActual : Exception
200         {
201             return (TActual)ThrowsAsync(new InstanceOfTypeConstraint(typeof (TActual)), code);
202         }
203
204         #endregion
205
206         #region DoesNotThrowAsync
207
208         /// <summary>
209         /// Verifies that an async delegate does not throw an exception
210         /// </summary>
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)
215         {
216             Assert.That(code, new ThrowsNothingConstraint(), message, args);
217         }
218         /// <summary>
219         /// Verifies that an async delegate does not throw an exception.
220         /// </summary>
221         /// <param name="code">A TestDelegate</param>
222         public static void DoesNotThrowAsync(AsyncTestDelegate code)
223         {
224             DoesNotThrowAsync(code, string.Empty, null);
225         }
226
227         #endregion
228     }
229 }
230 #endif