1 // ***********************************************************************
2 // Copyright (c) 2012 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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using NUnit.Framework.Internal;
32 namespace NUnit.Framework.Constraints
35 /// ThrowsExceptionConstraint tests that an exception has
36 /// been thrown, without any further tests.
38 public class ThrowsExceptionConstraint : Constraint
41 /// The Description of what this constraint tests, for
42 /// use in messages and in the ConstraintResult.
44 public override string Description
46 get { return "an exception to be thrown"; }
50 /// Executes the code and returns success if an exception is thrown.
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)
56 TestDelegate code = actual as TestDelegate;
57 Exception caughtException = null;
70 #if NET_4_0 || NET_4_5 || PORTABLE
71 AsyncTestDelegate asyncCode = actual as AsyncTestDelegate;
72 if (asyncCode != null)
74 using (var region = AsyncInvocationRegion.Create(asyncCode))
78 var task = asyncCode();
79 region.WaitForPendingOperationsToComplete(task);
87 if (code == null && asyncCode == null)
92 throw new ArgumentException(string.Format("The actual value must be a TestDelegate or AsyncTestDelegate but was {0}", actual.GetType().Name), "actual");
94 return new ThrowsExceptionConstraintResult(this, caughtException);
98 /// Returns the ActualValueDelegate itself as the value to be tested.
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)
104 return new TestDelegate(() => del());
107 #region Nested Result Class
109 class ThrowsExceptionConstraintResult : ConstraintResult
111 public ThrowsExceptionConstraintResult(ThrowsExceptionConstraint constraint, Exception caughtException)
112 : base(constraint, caughtException, caughtException != null) { }
114 public override void WriteActualValueTo(MessageWriter writer)
116 if (this.Status == ConstraintStatus.Failure)
117 writer.Write("no exception thrown");
119 base.WriteActualValueTo(writer);