1 // *********************************************************************** 2 // Copyright (c) 2009 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 System.Collections; 26 using NUnit.Framework.Constraints; 27 28 namespace NUnit.Framework 29 { 30 /// <summary> 31 /// Helper class with properties and methods that supply 32 /// constraints that operate on exceptions. 33 /// </summary> 34 public class Throws 35 { 36 #region Exception 37 38 /// <summary> 39 /// Creates a constraint specifying an expected exception 40 /// </summary> 41 public static ResolvableConstraintExpression Exception 42 { 43 get { return new ConstraintExpression().Append(new ThrowsOperator()); } 44 } 45 46 #endregion 47 48 #region InnerException 49 50 /// <summary> 51 /// Creates a constraint specifying an exception with a given InnerException 52 /// </summary> 53 public static ResolvableConstraintExpression InnerException 54 { 55 get { return Exception.InnerException; } 56 } 57 58 #endregion 59 60 #region TargetInvocationException 61 62 /// <summary> 63 /// Creates a constraint specifying an expected TargetInvocationException 64 /// </summary> 65 public static ExactTypeConstraint TargetInvocationException 66 { 67 get { return TypeOf(typeof(System.Reflection.TargetInvocationException)); } 68 } 69 70 #endregion 71 72 #region ArgumentException 73 74 /// <summary> 75 /// Creates a constraint specifying an expected TargetInvocationException 76 /// </summary> 77 public static ExactTypeConstraint ArgumentException 78 { 79 get { return TypeOf(typeof(System.ArgumentException)); } 80 } 81 82 #endregion 83 84 #region InvalidOperationException 85 86 /// <summary> 87 /// Creates a constraint specifying an expected TargetInvocationException 88 /// </summary> 89 public static ExactTypeConstraint InvalidOperationException 90 { 91 get { return TypeOf(typeof(System.InvalidOperationException)); } 92 } 93 94 #endregion 95 96 #region Nothing 97 98 /// <summary> 99 /// Creates a constraint specifying that no exception is thrown 100 /// </summary> 101 public static ThrowsNothingConstraint Nothing 102 { 103 get { return new ThrowsNothingConstraint(); } 104 } 105 106 #endregion 107 108 #region TypeOf 109 110 /// <summary> 111 /// Creates a constraint specifying the exact type of exception expected 112 /// </summary> TypeOf(Type expectedType)113 public static ExactTypeConstraint TypeOf(Type expectedType) 114 { 115 return Exception.TypeOf(expectedType); 116 } 117 118 #if CLR_2_0 || CLR_4_0 119 /// <summary> 120 /// Creates a constraint specifying the exact type of exception expected 121 /// </summary> TypeOf()122 public static ExactTypeConstraint TypeOf<T>() 123 { 124 return TypeOf(typeof(T)); 125 } 126 #endif 127 128 #endregion 129 130 #region InstanceOf 131 132 /// <summary> 133 /// Creates a constraint specifying the type of exception expected 134 /// </summary> InstanceOf(Type expectedType)135 public static InstanceOfTypeConstraint InstanceOf(Type expectedType) 136 { 137 return Exception.InstanceOf(expectedType); 138 } 139 140 #if CLR_2_0 || CLR_4_0 141 /// <summary> 142 /// Creates a constraint specifying the type of exception expected 143 /// </summary> InstanceOf()144 public static InstanceOfTypeConstraint InstanceOf<T>() 145 { 146 return InstanceOf(typeof(T)); 147 } 148 #endif 149 150 #endregion 151 152 } 153 } 154