[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / ThrowsConstraint.cs
1 // ***********************************************************************
2 // Copyright (c) 2008 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 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using NUnit.Framework.Internal;
31
32 namespace NUnit.Framework.Constraints
33 {
34     /// <summary>
35     /// ThrowsConstraint is used to test the exception thrown by 
36     /// a delegate by applying a constraint to it.
37     /// </summary>
38     public class ThrowsConstraint : PrefixConstraint
39     {
40         private Exception caughtException;
41
42         /// <summary>
43         /// Initializes a new instance of the <see cref="ThrowsConstraint"/> class,
44         /// using a constraint to be applied to the exception.
45         /// </summary>
46         /// <param name="baseConstraint">A constraint to apply to the caught exception.</param>
47         public ThrowsConstraint(IConstraint baseConstraint)
48             : base(baseConstraint) { }
49
50         /// <summary>
51         /// Get the actual exception thrown - used by Assert.Throws.
52         /// </summary>
53         public Exception ActualException
54         {
55             get { return caughtException; }
56         }
57
58         #region Constraint Overrides
59
60         /// <summary>
61         /// Gets text describing a constraint
62         /// </summary>
63         public override string Description
64         {
65             get { return BaseConstraint.Description; }
66         }
67
68         /// <summary>
69         /// Executes the code of the delegate and captures any exception.
70         /// If a non-null base constraint was provided, it applies that
71         /// constraint to the exception.
72         /// </summary>
73         /// <param name="actual">A delegate representing the code to be tested</param>
74         /// <returns>True if an exception is thrown and the constraint succeeds, otherwise false</returns>
75         public override ConstraintResult ApplyTo<TActual>(TActual actual)
76         {
77             //TestDelegate code = actual as TestDelegate;
78             //if (code == null)
79             //    throw new ArgumentException(
80             //        string.Format("The actual value must be a TestDelegate but was {0}", actual.GetType().Name), "actual");
81
82             //caughtException = null;
83
84             //try
85             //{
86             //    code();
87             //}
88             //catch (Exception ex)
89             //{
90             //    caughtException = ex;
91             //}
92
93             caughtException = ExceptionInterceptor.Intercept(actual);
94
95             return new ThrowsConstraintResult(
96                 this,
97                 caughtException,
98                 caughtException != null
99                     ? BaseConstraint.ApplyTo(caughtException)
100                     : null);
101         }
102
103         /// <summary>
104         /// Converts an ActualValueDelegate to a TestDelegate
105         /// before calling the primary overload.
106         /// </summary>
107         /// <param name="del"></param>
108         /// <returns></returns>
109         public override ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> del)
110         {
111             //TestDelegate testDelegate = new TestDelegate(delegate { del(); });
112             //return ApplyTo((object)testDelegate);
113             return ApplyTo(new GenericInvocationDescriptor<TActual>(del));
114         }
115
116         #endregion
117
118         #region Nested Result Class
119
120         private class ThrowsConstraintResult : ConstraintResult
121         {
122             private readonly ConstraintResult baseResult;
123
124             public ThrowsConstraintResult(ThrowsConstraint constraint,
125                 Exception caughtException,
126                 ConstraintResult baseResult)
127                 : base(constraint, caughtException)
128             {
129                 if (caughtException != null && baseResult.IsSuccess)
130                     Status = ConstraintStatus.Success;
131                 else
132                     Status = ConstraintStatus.Failure;
133
134                 this.baseResult = baseResult;
135             }
136
137             /// <summary>
138             /// Write the actual value for a failing constraint test to a
139             /// MessageWriter. This override only handles the special message
140             /// used when an exception is expected but none is thrown.
141             /// </summary>
142             /// <param name="writer">The writer on which the actual value is displayed</param>
143             public override void WriteActualValueTo(MessageWriter writer)
144             {
145                 if (ActualValue == null)
146                     writer.Write("no exception thrown");
147                 else
148                     baseResult.WriteActualValueTo(writer);
149             }
150         }
151
152         #endregion
153
154         #region ExceptionInterceptor
155
156         internal class ExceptionInterceptor
157         {
158             private ExceptionInterceptor() { }
159
160             internal static Exception Intercept(object invocation)
161             {
162                 var invocationDescriptor = GetInvocationDescriptor(invocation);
163
164 #if NET_4_0 || NET_4_5 || PORTABLE
165                 if (AsyncInvocationRegion.IsAsyncOperation(invocationDescriptor.Delegate))
166                 {
167                     using (var region = AsyncInvocationRegion.Create(invocationDescriptor.Delegate))
168                     {
169                         try
170                         {
171                             object result = invocationDescriptor.Invoke();
172                             region.WaitForPendingOperationsToComplete(result);
173                             return null;
174                         }
175                         catch (Exception ex)
176                         {
177                             return ex;
178                         }
179                     }
180                 }
181                 else
182 #endif
183                 {
184                     try
185                     {
186                         invocationDescriptor.Invoke();
187                         return null;
188                     }
189                     catch (Exception ex)
190                     {
191                         return ex;
192                     }
193                 }
194             }
195
196             private static IInvocationDescriptor GetInvocationDescriptor(object actual)
197             {
198                 var invocationDescriptor = actual as IInvocationDescriptor;
199
200                 if (invocationDescriptor == null)
201                 {
202                     var testDelegate = actual as TestDelegate;
203
204                     if (testDelegate != null)
205                     {
206                         invocationDescriptor = new VoidInvocationDescriptor(testDelegate);
207                     }
208
209 #if NET_4_0 || NET_4_5 || PORTABLE
210                     else
211                     {
212                         var asyncTestDelegate = actual as AsyncTestDelegate;
213                         if (asyncTestDelegate != null)
214                         {
215                             invocationDescriptor = new GenericInvocationDescriptor<System.Threading.Tasks.Task>(() => asyncTestDelegate());
216                         }
217                     }
218 #endif
219                 }
220                 if (invocationDescriptor == null)
221                     throw new ArgumentException(
222                         String.Format(
223                             "The actual value must be a TestDelegate or AsyncTestDelegate but was {0}",
224                             actual.GetType().Name),
225                         "actual");
226
227                 return invocationDescriptor;
228             }
229         }
230
231 #endregion
232
233 #region InvocationDescriptor
234
235         internal class GenericInvocationDescriptor<T> : IInvocationDescriptor
236         {
237             private readonly ActualValueDelegate<T> _del;
238
239             public GenericInvocationDescriptor(ActualValueDelegate<T> del)
240             {
241                 _del = del;
242             }
243
244             public object Invoke()
245             {
246                 return _del();
247             }
248
249             public Delegate Delegate
250             {
251                 get { return _del; }
252             }
253         }
254
255         private interface IInvocationDescriptor
256         {
257             Delegate Delegate { get; }
258             object Invoke();
259         }
260
261         private class VoidInvocationDescriptor : IInvocationDescriptor
262         {
263             private readonly TestDelegate _del;
264
265             public VoidInvocationDescriptor(TestDelegate del)
266             {
267                 _del = del;
268             }
269
270             public object Invoke()
271             {
272                 _del();
273                 return null;
274             }
275
276             public Delegate Delegate
277             {
278                 get { return _del; }
279             }
280         }
281
282 #endregion
283     }
284 }