1 // *********************************************************************** 2 // Copyright (c) 2007 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.Collections; 25 using NUnit.Framework.Constraints; 26 27 namespace NUnit.Framework 28 { 29 /// <summary> 30 /// AssertionHelper is an optional base class for user tests, 31 /// allowing the use of shorter names for constraints and 32 /// asserts and avoiding conflict with the definition of 33 /// <see cref="Is"/>, from which it inherits much of its 34 /// behavior, in certain mock object frameworks. 35 /// </summary> 36 public class AssertionHelper : ConstraintFactory 37 { 38 #region Assert 39 //private Assertions assert = new Assertions(); 40 //public virtual Assertions Assert 41 //{ 42 // get { return assert; } 43 //} 44 #endregion 45 46 #region Expect 47 48 #region Object 49 50 #if !CLR_2_0 && !CLR_4_0 51 /// <summary> 52 /// Apply a constraint to an actual value, succeeding if the constraint 53 /// is satisfied and throwing an assertion exception on failure. Works 54 /// identically to Assert.That. 55 /// </summary> 56 /// <param name="actual">The actual value to test</param> 57 /// <param name="expression">A Constraint to be applied</param> Expect(object actual, IResolveConstraint expression)58 public void Expect(object actual, IResolveConstraint expression) 59 { 60 Assert.That(actual, expression, null, null); 61 } 62 63 /// <summary> 64 /// Apply a constraint to an actual value, succeeding if the constraint 65 /// is satisfied and throwing an assertion exception on failure. Works 66 /// identically to Assert.That. 67 /// </summary> 68 /// <param name="actual">The actual value to test</param> 69 /// <param name="expression">A Constraint to be applied</param> 70 /// <param name="message">The message to be displayed in case of failure</param> Expect(object actual, IResolveConstraint expression, string message)71 public void Expect(object actual, IResolveConstraint expression, string message) 72 { 73 Assert.That(actual, expression, message, null); 74 } 75 76 /// <summary> 77 /// Apply a constraint to an actual value, succeeding if the constraint 78 /// is satisfied and throwing an assertion exception on failure. Works 79 /// identically to Assert.That. 80 /// </summary> 81 /// <param name="actual">The actual value to test</param> 82 /// <param name="expression">A Constraint to be applied</param> 83 /// <param name="message">The message to be displayed in case of failure</param> 84 /// <param name="args">Arguments to use in formatting the message</param> Expect(object actual, IResolveConstraint expression, string message, params object[] args)85 public void Expect(object actual, IResolveConstraint expression, string message, params object[] args) 86 { 87 Assert.That(actual, expression, message, args); 88 } 89 #endif 90 91 #endregion 92 93 #region Boolean 94 95 /// <summary> 96 /// Asserts that a condition is true. If the condition is false the method throws 97 /// an <see cref="AssertionException"/>. Works Identically to 98 /// <see cref="Assert.That(bool, string, object[])"/>. 99 /// </summary> 100 /// <param name="condition">The evaluated condition</param> 101 /// <param name="message">The message to display if the condition is false</param> 102 /// <param name="args">Arguments to be used in formatting the message</param> Expect(bool condition, string message, params object[] args)103 public void Expect(bool condition, string message, params object[] args) 104 { 105 Assert.That(condition, Is.True, message, args); 106 } 107 108 /// <summary> 109 /// Asserts that a condition is true. If the condition is false the method throws 110 /// an <see cref="AssertionException"/>. Works Identically to 111 /// <see cref="Assert.That(bool, string)"/>. 112 /// </summary> 113 /// <param name="condition">The evaluated condition</param> 114 /// <param name="message">The message to display if the condition is false</param> Expect(bool condition, string message)115 public void Expect(bool condition, string message) 116 { 117 Assert.That(condition, Is.True, message, null); 118 } 119 120 /// <summary> 121 /// Asserts that a condition is true. If the condition is false the method throws 122 /// an <see cref="AssertionException"/>. Works Identically to <see cref="Assert.That(bool)"/>. 123 /// </summary> 124 /// <param name="condition">The evaluated condition</param> Expect(bool condition)125 public void Expect(bool condition) 126 { 127 Assert.That(condition, Is.True, null, null); 128 } 129 130 #endregion 131 132 #region ref Boolean 133 134 #if !CLR_2_0 && !CLR_4_0 135 /// <summary> 136 /// Apply a constraint to a referenced boolean, succeeding if the constraint 137 /// is satisfied and throwing an assertion exception on failure. 138 /// </summary> 139 /// <param name="constraint">A Constraint to be applied</param> 140 /// <param name="actual">The actual value to test</param> Expect(ref bool actual, IResolveConstraint constraint)141 public void Expect(ref bool actual, IResolveConstraint constraint) 142 { 143 Assert.That(ref actual, constraint.Resolve(), null, null); 144 } 145 146 /// <summary> 147 /// Apply a constraint to a referenced value, succeeding if the constraint 148 /// is satisfied and throwing an assertion exception on failure. 149 /// </summary> 150 /// <param name="constraint">A Constraint to be applied</param> 151 /// <param name="actual">The actual value to test</param> 152 /// <param name="message">The message that will be displayed on failure</param> Expect(ref bool actual, IResolveConstraint constraint, string message)153 public void Expect(ref bool actual, IResolveConstraint constraint, string message) 154 { 155 Assert.That(ref actual, constraint.Resolve(), message, null); 156 } 157 158 /// <summary> 159 /// Apply a constraint to a referenced value, succeeding if the constraint 160 /// is satisfied and throwing an assertion exception on failure. 161 /// </summary> 162 /// <param name="actual">The actual value to test</param> 163 /// <param name="expression">A Constraint expression to be applied</param> 164 /// <param name="message">The message that will be displayed on failure</param> 165 /// <param name="args">Arguments to be used in formatting the message</param> Expect(ref bool actual, IResolveConstraint expression, string message, params object[] args)166 public void Expect(ref bool actual, IResolveConstraint expression, string message, params object[] args) 167 { 168 Assert.That(ref actual, expression, message, args); 169 } 170 #endif 171 172 #endregion 173 174 #region ActualValueDelegate 175 176 #if CLR_2_0 || CLR_4_0 177 /// <summary> 178 /// Apply a constraint to an actual value, succeeding if the constraint 179 /// is satisfied and throwing an assertion exception on failure. 180 /// </summary> 181 /// <param name="expr">A Constraint expression to be applied</param> 182 /// <param name="del">An ActualValueDelegate returning the value to be tested</param> Expect(ActualValueDelegate<T> del, IResolveConstraint expr)183 public void Expect<T>(ActualValueDelegate<T> del, IResolveConstraint expr) 184 { 185 Assert.That(del, expr.Resolve(), null, null); 186 } 187 188 /// <summary> 189 /// Apply a constraint to an actual value, succeeding if the constraint 190 /// is satisfied and throwing an assertion exception on failure. 191 /// </summary> 192 /// <param name="expr">A Constraint expression to be applied</param> 193 /// <param name="del">An ActualValueDelegate returning the value to be tested</param> 194 /// <param name="message">The message that will be displayed on failure</param> Expect(ActualValueDelegate<T> del, IResolveConstraint expr, string message)195 public void Expect<T>(ActualValueDelegate<T> del, IResolveConstraint expr, string message) 196 { 197 Assert.That(del, expr.Resolve(), message, null); 198 } 199 200 /// <summary> 201 /// Apply a constraint to an actual value, succeeding if the constraint 202 /// is satisfied and throwing an assertion exception on failure. 203 /// </summary> 204 /// <param name="del">An ActualValueDelegate returning the value to be tested</param> 205 /// <param name="expr">A Constraint expression to be applied</param> 206 /// <param name="message">The message that will be displayed on failure</param> 207 /// <param name="args">Arguments to be used in formatting the message</param> Expect(ActualValueDelegate<T> del, IResolveConstraint expr, string message, params object[] args)208 public void Expect<T>(ActualValueDelegate<T> del, IResolveConstraint expr, string message, params object[] args) 209 { 210 Assert.That(del, expr, message, args); 211 } 212 #else 213 /// <summary> 214 /// Apply a constraint to an actual value, succeeding if the constraint 215 /// is satisfied and throwing an assertion exception on failure. 216 /// </summary> 217 /// <param name="expr">A Constraint expression to be applied</param> 218 /// <param name="del">An ActualValueDelegate returning the value to be tested</param> Expect(ActualValueDelegate del, IResolveConstraint expr)219 public void Expect(ActualValueDelegate del, IResolveConstraint expr) 220 { 221 Assert.That(del, expr.Resolve(), null, null); 222 } 223 224 /// <summary> 225 /// Apply a constraint to an actual value, succeeding if the constraint 226 /// is satisfied and throwing an assertion exception on failure. 227 /// </summary> 228 /// <param name="expr">A Constraint expression to be applied</param> 229 /// <param name="del">An ActualValueDelegate returning the value to be tested</param> 230 /// <param name="message">The message that will be displayed on failure</param> Expect(ActualValueDelegate del, IResolveConstraint expr, string message)231 public void Expect(ActualValueDelegate del, IResolveConstraint expr, string message) 232 { 233 Assert.That(del, expr.Resolve(), message, null); 234 } 235 236 /// <summary> 237 /// Apply a constraint to an actual value, succeeding if the constraint 238 /// is satisfied and throwing an assertion exception on failure. 239 /// </summary> 240 /// <param name="del">An ActualValueDelegate returning the value to be tested</param> 241 /// <param name="expr">A Constraint expression to be applied</param> 242 /// <param name="message">The message that will be displayed on failure</param> 243 /// <param name="args">Arguments to be used in formatting the message</param> Expect(ActualValueDelegate del, IResolveConstraint expr, string message, params object[] args)244 public void Expect(ActualValueDelegate del, IResolveConstraint expr, string message, params object[] args) 245 { 246 Assert.That(del, expr, message, args); 247 } 248 #endif 249 250 #endregion 251 252 #region ref Object 253 254 #if CLR_2_0 || CLR_4_0 255 /// <summary> 256 /// Apply a constraint to a referenced value, succeeding if the constraint 257 /// is satisfied and throwing an assertion exception on failure. 258 /// </summary> 259 /// <param name="actual">The actual value to test</param> 260 /// <param name="expression">A Constraint to be applied</param> Expect(ref T actual, IResolveConstraint expression)261 public void Expect<T>(ref T actual, IResolveConstraint expression) 262 { 263 Assert.That(ref actual, expression, null, null); 264 } 265 266 /// <summary> 267 /// Apply a constraint to a referenced value, succeeding if the constraint 268 /// is satisfied and throwing an assertion exception on failure. 269 /// </summary> 270 /// <param name="actual">The actual value to test</param> 271 /// <param name="expression">A Constraint to be applied</param> 272 /// <param name="message">The message that will be displayed on failure</param> Expect(ref T actual, IResolveConstraint expression, string message)273 public void Expect<T>(ref T actual, IResolveConstraint expression, string message) 274 { 275 Assert.That(ref actual, expression, message, null); 276 } 277 278 /// <summary> 279 /// Apply a constraint to a referenced value, succeeding if the constraint 280 /// is satisfied and throwing an assertion exception on failure. 281 /// </summary> 282 /// <param name="actual">The actual value to test</param> 283 /// <param name="expression">A Constraint to be applied</param> 284 /// <param name="message">The message that will be displayed on failure</param> 285 /// <param name="args">Arguments to be used in formatting the message</param> Expect(ref T actual, IResolveConstraint expression, string message, params object[] args)286 public void Expect<T>(ref T actual, IResolveConstraint expression, string message, params object[] args) 287 { 288 Assert.That(ref actual, expression, message, args); 289 } 290 #endif 291 292 #endregion 293 294 #region TestDelegate 295 296 /// <summary> 297 /// Asserts that the code represented by a delegate throws an exception 298 /// that satisfies the constraint provided. 299 /// </summary> 300 /// <param name="code">A TestDelegate to be executed</param> 301 /// <param name="constraint">A ThrowsConstraint used in the test</param> Expect(TestDelegate code, IResolveConstraint constraint)302 public void Expect(TestDelegate code, IResolveConstraint constraint) 303 { 304 Assert.That((object)code, constraint); 305 } 306 307 #endregion 308 309 #endregion 310 311 #region Expect<T> 312 313 #if CLR_2_0 || CLR_4_0 314 /// <summary> 315 /// Apply a constraint to an actual value, succeeding if the constraint 316 /// is satisfied and throwing an assertion exception on failure. 317 /// </summary> 318 /// <param name="expression">A Constraint to be applied</param> 319 /// <param name="actual">The actual value to test</param> Expect(T actual, IResolveConstraint expression)320 static public void Expect<T>(T actual, IResolveConstraint expression) 321 { 322 Assert.That(actual, expression, null, null); 323 } 324 325 /// <summary> 326 /// Apply a constraint to an actual value, succeeding if the constraint 327 /// is satisfied and throwing an assertion exception on failure. 328 /// </summary> 329 /// <param name="expression">A Constraint to be applied</param> 330 /// <param name="actual">The actual value to test</param> 331 /// <param name="message">The message that will be displayed on failure</param> Expect(T actual, IResolveConstraint expression, string message)332 static public void Expect<T>(T actual, IResolveConstraint expression, string message) 333 { 334 Assert.That(actual, expression, message, null); 335 } 336 337 /// <summary> 338 /// Apply a constraint to an actual value, succeeding if the constraint 339 /// is satisfied and throwing an assertion exception on failure. 340 /// </summary> 341 /// <param name="expression">A Constraint expression to be applied</param> 342 /// <param name="actual">The actual value to test</param> 343 /// <param name="message">The message that will be displayed on failure</param> 344 /// <param name="args">Arguments to be used in formatting the message</param> Expect(T actual, IResolveConstraint expression, string message, params object[] args)345 static public void Expect<T>(T actual, IResolveConstraint expression, string message, params object[] args) 346 { 347 Assert.That(actual, expression, message, args); 348 } 349 350 #endif 351 352 #endregion 353 354 #region Map 355 /// <summary> 356 /// Returns a ListMapper based on a collection. 357 /// </summary> 358 /// <param name="original">The original collection</param> 359 /// <returns></returns> Map( ICollection original )360 public ListMapper Map( ICollection original ) 361 { 362 return new ListMapper( original ); 363 } 364 #endregion 365 } 366 } 367