[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Assert / Assert.Exceptions.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 using System;
25 using NUnit.Framework.Constraints;
26 using NUnit.Framework.Internal;
27
28 namespace NUnit.Framework
29 {
30     public partial class Assert
31     {
32         #region Throws
33         /// <summary>
34         /// Verifies that a delegate throws a particular exception when called.
35         /// </summary>
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)
41         {
42             Exception caughtException = null;
43
44 #if NET_4_0 || NET_4_5 || PORTABLE
45             if (AsyncInvocationRegion.IsAsyncOperation(code))
46             {
47                 using (var region = AsyncInvocationRegion.Create(code))
48                 {
49                     code();
50
51                     try
52                     {
53                         region.WaitForPendingOperationsToComplete(null);
54                     }
55                     catch (Exception e)
56                     {
57                         caughtException = e;
58                     }
59                 }
60             }
61             else
62 #endif
63             try
64             {
65                 code();
66             }
67             catch (Exception ex)
68             {
69                 caughtException = ex;
70             }
71
72             Assert.That(caughtException, expression, message, args);
73
74             return caughtException;
75         }
76
77         /// <summary>
78         /// Verifies that a delegate throws a particular exception when called.
79         /// </summary>
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)
83         {
84             return Throws(expression, code, string.Empty, null);
85         }
86
87         /// <summary>
88         /// Verifies that a delegate throws a particular exception when called.
89         /// </summary>
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)
95         {
96             return Throws(new ExceptionTypeConstraint(expectedExceptionType), code, message, args);
97         }
98
99         /// <summary>
100         /// Verifies that a delegate throws a particular exception when called.
101         /// </summary>
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)
105         {
106             return Throws(new ExceptionTypeConstraint(expectedExceptionType), code, string.Empty, null);
107         }
108
109         #endregion
110
111         #region Throws<TActual>
112
113         /// <summary>
114         /// Verifies that a delegate throws a particular exception when called.
115         /// </summary>
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
121         {
122             return (TActual)Throws(typeof(TActual), code, message, args);
123         }
124
125         /// <summary>
126         /// Verifies that a delegate throws a particular exception when called.
127         /// </summary>
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
131         {
132             return Throws<TActual>(code, string.Empty, null);
133         }
134
135         #endregion
136
137         #region Catch
138         /// <summary>
139         /// Verifies that a delegate throws an exception when called
140         /// and returns it.
141         /// </summary>
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)
146         {
147             return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code, message, args);
148         }
149
150         /// <summary>
151         /// Verifies that a delegate throws an exception when called
152         /// and returns it.
153         /// </summary>
154         /// <param name="code">A TestDelegate</param>
155         public static Exception Catch(TestDelegate code)
156         {
157             return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code);
158         }
159
160         /// <summary>
161         /// Verifies that a delegate throws an exception of a certain Type
162         /// or one derived from it when called and returns it.
163         /// </summary>
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)
169         {
170             return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code, message, args);
171         }
172
173         /// <summary>
174         /// Verifies that a delegate throws an exception of a certain Type
175         /// or one derived from it when called and returns it.
176         /// </summary>
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)
180         {
181             return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code);
182         }
183         #endregion
184
185         #region Catch<TActual>
186
187         /// <summary>
188         /// Verifies that a delegate throws an exception of a certain Type
189         /// or one derived from it when called and returns it.
190         /// </summary>
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
195         {
196             return (TActual)Throws(new InstanceOfTypeConstraint(typeof(TActual)), code, message, args);
197         }
198
199         /// <summary>
200         /// Verifies that a delegate throws an exception of a certain Type
201         /// or one derived from it when called and returns it.
202         /// </summary>
203         /// <param name="code">A TestDelegate</param>
204         public static TActual Catch<TActual>(TestDelegate code) where TActual : System.Exception
205         {
206             return (TActual)Throws(new InstanceOfTypeConstraint(typeof(TActual)), code);
207         }
208
209         #endregion
210
211         #region DoesNotThrow
212
213         /// <summary>
214         /// Verifies that a delegate does not throw an exception
215         /// </summary>
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)
220         {
221             Assert.That(code, new ThrowsNothingConstraint(), message, args);
222         }
223
224         /// <summary>
225         /// Verifies that a delegate does not throw an exception.
226         /// </summary>
227         /// <param name="code">A TestDelegate</param>
228         public static void DoesNotThrow(TestDelegate code)
229         {
230             DoesNotThrow(code, string.Empty, null);
231         }
232
233         #endregion
234     }
235 }