[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / ThrowsExceptionConstraint.cs
1 // ***********************************************************************
2 // Copyright (c) 2012 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     /// ThrowsExceptionConstraint tests that an exception has
36     /// been thrown, without any further tests.
37     /// </summary>
38     public class ThrowsExceptionConstraint : Constraint
39     {
40         /// <summary>
41         /// The Description of what this constraint tests, for
42         /// use in messages and in the ConstraintResult.
43         /// </summary>
44         public override string Description
45         {
46             get { return "an exception to be thrown"; }
47         }
48
49         /// <summary>
50         /// Executes the code and returns success if an exception is thrown.
51         /// </summary>
52         /// <param name="actual">A delegate representing the code to be tested</param>
53         /// <returns>True if an exception is thrown, otherwise false</returns>
54         public override ConstraintResult ApplyTo<TActual>(TActual actual)
55         {
56             TestDelegate code = actual as TestDelegate;
57             Exception caughtException = null;
58
59             if (code != null)
60             {
61                 try
62                 {
63                     code();
64                 }
65                 catch (Exception ex)
66                 {
67                     caughtException = ex;
68                 }
69             }
70 #if NET_4_0 || NET_4_5 || PORTABLE
71             AsyncTestDelegate asyncCode = actual as AsyncTestDelegate;
72             if (asyncCode != null)
73             {
74                 using (var region = AsyncInvocationRegion.Create(asyncCode))
75                 {
76                     try
77                     {
78                         var task = asyncCode();
79                         region.WaitForPendingOperationsToComplete(task);
80                     }
81                     catch (Exception ex)
82                     {
83                         caughtException = ex;
84                     }
85                 }
86             }
87             if (code == null && asyncCode == null)
88 #else
89             else
90 #endif
91             {
92                 throw new ArgumentException(string.Format("The actual value must be a TestDelegate or AsyncTestDelegate but was {0}", actual.GetType().Name), "actual");
93             }
94             return new ThrowsExceptionConstraintResult(this, caughtException);
95         }
96
97         /// <summary>
98         /// Returns the ActualValueDelegate itself as the value to be tested.
99         /// </summary>
100         /// <param name="del">A delegate representing the code to be tested</param>
101         /// <returns>The delegate itself</returns>
102         protected override object GetTestObject<TActual>(ActualValueDelegate<TActual> del)
103         {
104             return new TestDelegate(() => del());
105         }
106
107         #region Nested Result Class
108
109         class ThrowsExceptionConstraintResult : ConstraintResult
110         {
111             public ThrowsExceptionConstraintResult(ThrowsExceptionConstraint constraint, Exception caughtException)
112                 : base(constraint, caughtException, caughtException != null) { }
113
114             public override void WriteActualValueTo(MessageWriter writer)
115             {
116                 if (this.Status == ConstraintStatus.Failure)
117                     writer.Write("no exception thrown");
118                 else
119                     base.WriteActualValueTo(writer);
120             }
121         }
122
123         #endregion
124     }
125 }