PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/NUnit.Silverlight.Framework/Assert.cs

https://bitbucket.org/jesperll/nunit-silverlight
C# | 3789 lines | 1384 code | 384 blank | 2021 comment | 30 complexity | 1ff03d0813cd4880e65ee6333c1ea8ed MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. // ****************************************************************
  2. // Copyright 2009, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. // ****************************************************************
  7. // Generated by the NUnit Syntax Generator
  8. //
  9. // Command Line: GenSyntax.exe SyntaxElements.txt
  10. //
  11. // DO NOT MODIFY THIS FILE DIRECTLY
  12. // ****************************************************************
  13. using System;
  14. using System.Collections;
  15. using System.ComponentModel;
  16. using NUnit.Framework.Constraints;
  17. namespace NUnit.Framework
  18. {
  19. /// <summary>
  20. /// Delegate used by tests that execute code and
  21. /// capture any thrown exception.
  22. /// </summary>
  23. public delegate void TestDelegate();
  24. /// <summary>
  25. /// The Assert class contains a collection of static methods that
  26. /// implement the most common assertions used in NUnit.
  27. /// </summary>
  28. public class Assert
  29. {
  30. #region Constructor
  31. /// <summary>
  32. /// We don't actually want any instances of this object, but some people
  33. /// like to inherit from it to add other static methods. Hence, the
  34. /// protected constructor disallows any instances of this object.
  35. /// </summary>
  36. protected Assert() { }
  37. #endregion
  38. #region Assert Counting
  39. private static int counter = 0;
  40. /// <summary>
  41. /// Gets the number of assertions executed so far and
  42. /// resets the counter to zero.
  43. /// </summary>
  44. public static int Counter
  45. {
  46. get
  47. {
  48. int cnt = counter;
  49. counter = 0;
  50. return cnt;
  51. }
  52. }
  53. private static void IncrementAssertCount()
  54. {
  55. ++counter;
  56. }
  57. #endregion
  58. #region Equals and ReferenceEquals
  59. /// <summary>
  60. /// The Equals method throws an AssertionException. This is done
  61. /// to make sure there is no mistake by calling this function.
  62. /// </summary>
  63. /// <param name="a"></param>
  64. /// <param name="b"></param>
  65. [EditorBrowsable(EditorBrowsableState.Never)]
  66. public static new bool Equals(object a, object b)
  67. {
  68. // TODO: This should probably be InvalidOperationException
  69. throw new AssertionException("Assert.Equals should not be used for Assertions");
  70. }
  71. /// <summary>
  72. /// override the default ReferenceEquals to throw an AssertionException. This
  73. /// implementation makes sure there is no mistake in calling this function
  74. /// as part of Assert.
  75. /// </summary>
  76. /// <param name="a"></param>
  77. /// <param name="b"></param>
  78. public static new void ReferenceEquals(object a, object b)
  79. {
  80. throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
  81. }
  82. #endregion
  83. #region Helper Methods
  84. /// <summary>
  85. /// Helper for Assert.AreEqual(double expected, double actual, ...)
  86. /// allowing code generation to work consistently.
  87. /// </summary>
  88. /// <param name="expected">The expected value</param>
  89. /// <param name="actual">The actual value</param>
  90. /// <param name="delta">The maximum acceptable difference between the
  91. /// the expected and the actual</param>
  92. /// <param name="message">The message to display in case of failure</param>
  93. /// <param name="args">Array of objects to be used in formatting the message</param>
  94. protected static void AssertDoublesAreEqual(double expected, double actual, double delta, string message, object[] args)
  95. {
  96. if (double.IsNaN(expected) || double.IsInfinity(expected))
  97. Assert.That(actual, Is.EqualTo(expected), message, args);
  98. else
  99. Assert.That(actual, Is.EqualTo(expected).Within(delta), message, args);
  100. }
  101. #endregion
  102. #region Utility Asserts
  103. #region Pass
  104. /// <summary>
  105. /// Throws a <see cref="SuccessException"/> with the message and arguments
  106. /// that are passed in. This allows a test to be cut short, with a result
  107. /// of success returned to NUnit.
  108. /// </summary>
  109. /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
  110. /// <param name="args">Arguments to be used in formatting the message</param>
  111. static public void Pass(string message, params object[] args)
  112. {
  113. if (message == null) message = string.Empty;
  114. else if (args != null && args.Length > 0)
  115. message = string.Format(message, args);
  116. throw new SuccessException(message);
  117. }
  118. /// <summary>
  119. /// Throws a <see cref="SuccessException"/> with the message and arguments
  120. /// that are passed in. This allows a test to be cut short, with a result
  121. /// of success returned to NUnit.
  122. /// </summary>
  123. /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
  124. static public void Pass(string message)
  125. {
  126. Assert.Pass(message, null);
  127. }
  128. /// <summary>
  129. /// Throws a <see cref="SuccessException"/> with the message and arguments
  130. /// that are passed in. This allows a test to be cut short, with a result
  131. /// of success returned to NUnit.
  132. /// </summary>
  133. static public void Pass()
  134. {
  135. Assert.Pass(string.Empty, null);
  136. }
  137. #endregion
  138. #region Fail
  139. /// <summary>
  140. /// Throws an <see cref="AssertionException"/> with the message and arguments
  141. /// that are passed in. This is used by the other Assert functions.
  142. /// </summary>
  143. /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
  144. /// <param name="args">Arguments to be used in formatting the message</param>
  145. static public void Fail(string message, params object[] args)
  146. {
  147. if (message == null) message = string.Empty;
  148. else if (args != null && args.Length > 0)
  149. message = string.Format(message, args);
  150. throw new AssertionException(message);
  151. }
  152. /// <summary>
  153. /// Throws an <see cref="AssertionException"/> with the message that is
  154. /// passed in. This is used by the other Assert functions.
  155. /// </summary>
  156. /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
  157. static public void Fail(string message)
  158. {
  159. Assert.Fail(message, null);
  160. }
  161. /// <summary>
  162. /// Throws an <see cref="AssertionException"/>.
  163. /// This is used by the other Assert functions.
  164. /// </summary>
  165. static public void Fail()
  166. {
  167. Assert.Fail(string.Empty, null);
  168. }
  169. #endregion
  170. #region Ignore
  171. /// <summary>
  172. /// Throws an <see cref="IgnoreException"/> with the message and arguments
  173. /// that are passed in. This causes the test to be reported as ignored.
  174. /// </summary>
  175. /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
  176. /// <param name="args">Arguments to be used in formatting the message</param>
  177. static public void Ignore(string message, params object[] args)
  178. {
  179. if (message == null) message = string.Empty;
  180. else if (args != null && args.Length > 0)
  181. message = string.Format(message, args);
  182. throw new IgnoreException(message);
  183. }
  184. /// <summary>
  185. /// Throws an <see cref="IgnoreException"/> with the message that is
  186. /// passed in. This causes the test to be reported as ignored.
  187. /// </summary>
  188. /// <param name="message">The message to initialize the <see cref="AssertionException"/> with.</param>
  189. static public void Ignore(string message)
  190. {
  191. Assert.Ignore(message, null);
  192. }
  193. /// <summary>
  194. /// Throws an <see cref="IgnoreException"/>.
  195. /// This causes the test to be reported as ignored.
  196. /// </summary>
  197. static public void Ignore()
  198. {
  199. Assert.Ignore(string.Empty, null);
  200. }
  201. #endregion
  202. #region InConclusive
  203. /// <summary>
  204. /// Throws an <see cref="InconclusiveException"/> with the message and arguments
  205. /// that are passed in. This causes the test to be reported as inconclusive.
  206. /// </summary>
  207. /// <param name="message">The message to initialize the <see cref="InconclusiveException"/> with.</param>
  208. /// <param name="args">Arguments to be used in formatting the message</param>
  209. static public void Inconclusive(string message, params object[] args)
  210. {
  211. if (message == null) message = string.Empty;
  212. else if (args != null && args.Length > 0)
  213. message = string.Format(message, args);
  214. throw new InconclusiveException(message);
  215. }
  216. /// <summary>
  217. /// Throws an <see cref="InconclusiveException"/> with the message that is
  218. /// passed in. This causes the test to be reported as inconclusive.
  219. /// </summary>
  220. /// <param name="message">The message to initialize the <see cref="InconclusiveException"/> with.</param>
  221. static public void Inconclusive(string message)
  222. {
  223. Assert.Inconclusive(message, null);
  224. }
  225. /// <summary>
  226. /// Throws an <see cref="InconclusiveException"/>.
  227. /// This causes the test to be reported as Inconclusive.
  228. /// </summary>
  229. static public void Inconclusive()
  230. {
  231. Assert.Inconclusive(string.Empty, null);
  232. }
  233. #endregion
  234. #endregion
  235. #region Assert.That
  236. #region Object
  237. /// <summary>
  238. /// Apply a constraint to an actual value, succeeding if the constraint
  239. /// is satisfied and throwing an assertion exception on failure.
  240. /// </summary>
  241. /// <param name="expression">A Constraint to be applied</param>
  242. /// <param name="actual">The actual value to test</param>
  243. static public void That(object actual, IResolveConstraint expression)
  244. {
  245. Assert.That(actual, expression, null, null);
  246. }
  247. /// <summary>
  248. /// Apply a constraint to an actual value, succeeding if the constraint
  249. /// is satisfied and throwing an assertion exception on failure.
  250. /// </summary>
  251. /// <param name="expression">A Constraint to be applied</param>
  252. /// <param name="actual">The actual value to test</param>
  253. /// <param name="message">The message that will be displayed on failure</param>
  254. static public void That(object actual, IResolveConstraint expression, string message)
  255. {
  256. Assert.That(actual, expression, message, null);
  257. }
  258. /// <summary>
  259. /// Apply a constraint to an actual value, succeeding if the constraint
  260. /// is satisfied and throwing an assertion exception on failure.
  261. /// </summary>
  262. /// <param name="expression">A Constraint expression to be applied</param>
  263. /// <param name="actual">The actual value to test</param>
  264. /// <param name="message">The message that will be displayed on failure</param>
  265. /// <param name="args">Arguments to be used in formatting the message</param>
  266. static public void That(object actual, IResolveConstraint expression, string message, params object[] args)
  267. {
  268. Constraint constraint = expression.Resolve();
  269. Assert.IncrementAssertCount();
  270. if (!constraint.Matches(actual))
  271. {
  272. MessageWriter writer = new TextMessageWriter(message, args);
  273. constraint.WriteMessageTo(writer);
  274. throw new AssertionException(writer.ToString());
  275. }
  276. }
  277. #endregion
  278. #region ActualValueDelegate
  279. /// <summary>
  280. /// Apply a constraint to an actual value, succeeding if the constraint
  281. /// is satisfied and throwing an assertion exception on failure.
  282. /// </summary>
  283. /// <param name="expr">A Constraint expression to be applied</param>
  284. /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
  285. static public void That(ActualValueDelegate del, IResolveConstraint expr)
  286. {
  287. Assert.That(del, expr.Resolve(), null, null);
  288. }
  289. /// <summary>
  290. /// Apply a constraint to an actual value, succeeding if the constraint
  291. /// is satisfied and throwing an assertion exception on failure.
  292. /// </summary>
  293. /// <param name="expr">A Constraint expression to be applied</param>
  294. /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
  295. /// <param name="message">The message that will be displayed on failure</param>
  296. static public void That(ActualValueDelegate del, IResolveConstraint expr, string message)
  297. {
  298. Assert.That(del, expr.Resolve(), message, null);
  299. }
  300. /// <summary>
  301. /// Apply a constraint to an actual value, succeeding if the constraint
  302. /// is satisfied and throwing an assertion exception on failure.
  303. /// </summary>
  304. /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
  305. /// <param name="expr">A Constraint expression to be applied</param>
  306. /// <param name="message">The message that will be displayed on failure</param>
  307. /// <param name="args">Arguments to be used in formatting the message</param>
  308. static public void That(ActualValueDelegate del, IResolveConstraint expr, string message, params object[] args)
  309. {
  310. Constraint constraint = expr.Resolve();
  311. Assert.IncrementAssertCount();
  312. if (!constraint.Matches(del))
  313. {
  314. MessageWriter writer = new TextMessageWriter(message, args);
  315. constraint.WriteMessageTo(writer);
  316. throw new AssertionException(writer.ToString());
  317. }
  318. }
  319. #endregion
  320. #region ref Object
  321. #if NET_2_0
  322. /// <summary>
  323. /// Apply a constraint to a referenced value, succeeding if the constraint
  324. /// is satisfied and throwing an assertion exception on failure.
  325. /// </summary>
  326. /// <param name="expression">A Constraint to be applied</param>
  327. /// <param name="actual">The actual value to test</param>
  328. static public void That<T>(ref T actual, IResolveConstraint expression)
  329. {
  330. Assert.That(ref actual, expression.Resolve(), null, null);
  331. }
  332. /// <summary>
  333. /// Apply a constraint to a referenced value, succeeding if the constraint
  334. /// is satisfied and throwing an assertion exception on failure.
  335. /// </summary>
  336. /// <param name="expression">A Constraint to be applied</param>
  337. /// <param name="actual">The actual value to test</param>
  338. /// <param name="message">The message that will be displayed on failure</param>
  339. static public void That<T>(ref T actual, IResolveConstraint expression, string message)
  340. {
  341. Assert.That(ref actual, expression.Resolve(), message, null);
  342. }
  343. /// <summary>
  344. /// Apply a constraint to a referenced value, succeeding if the constraint
  345. /// is satisfied and throwing an assertion exception on failure.
  346. /// </summary>
  347. /// <param name="expression">A Constraint to be applied</param>
  348. /// <param name="actual">The actual value to test</param>
  349. /// <param name="message">The message that will be displayed on failure</param>
  350. /// <param name="args">Arguments to be used in formatting the message</param>
  351. static public void That<T>(ref T actual, IResolveConstraint expression, string message, params object[] args)
  352. {
  353. Constraint constraint = expression.Resolve();
  354. Assert.IncrementAssertCount();
  355. if (!constraint.Matches(ref actual))
  356. {
  357. MessageWriter writer = new TextMessageWriter(message, args);
  358. constraint.WriteMessageTo(writer);
  359. throw new AssertionException(writer.ToString());
  360. }
  361. }
  362. #else
  363. /// <summary>
  364. /// Apply a constraint to a referenced boolean, succeeding if the constraint
  365. /// is satisfied and throwing an assertion exception on failure.
  366. /// </summary>
  367. /// <param name="constraint">A Constraint to be applied</param>
  368. /// <param name="actual">The actual value to test</param>
  369. static public void That(ref bool actual, IResolveConstraint constraint)
  370. {
  371. Assert.That(ref actual, constraint.Resolve(), null, null);
  372. }
  373. /// <summary>
  374. /// Apply a constraint to a referenced value, succeeding if the constraint
  375. /// is satisfied and throwing an assertion exception on failure.
  376. /// </summary>
  377. /// <param name="constraint">A Constraint to be applied</param>
  378. /// <param name="actual">The actual value to test</param>
  379. /// <param name="message">The message that will be displayed on failure</param>
  380. static public void That(ref bool actual, IResolveConstraint constraint, string message)
  381. {
  382. Assert.That(ref actual, constraint.Resolve(), message, null);
  383. }
  384. /// <summary>
  385. /// Apply a constraint to a referenced value, succeeding if the constraint
  386. /// is satisfied and throwing an assertion exception on failure.
  387. /// </summary>
  388. /// <param name="constraint">A Constraint to be applied</param>
  389. /// <param name="actual">The actual value to test</param>
  390. /// <param name="message">The message that will be displayed on failure</param>
  391. /// <param name="args">Arguments to be used in formatting the message</param>
  392. static public void That(ref bool actual, IResolveConstraint expression, string message, params object[] args)
  393. {
  394. Constraint constraint = expression.Resolve();
  395. Assert.IncrementAssertCount();
  396. if (!constraint.Matches(ref actual))
  397. {
  398. MessageWriter writer = new TextMessageWriter(message, args);
  399. constraint.WriteMessageTo(writer);
  400. throw new AssertionException(writer.ToString());
  401. }
  402. }
  403. #endif
  404. #endregion
  405. #region Boolean
  406. /// <summary>
  407. /// Asserts that a condition is true. If the condition is false the method throws
  408. /// an <see cref="AssertionException"/>.
  409. /// </summary>
  410. /// <param name="condition">The evaluated condition</param>
  411. /// <param name="message">The message to display if the condition is false</param>
  412. /// <param name="args">Arguments to be used in formatting the message</param>
  413. static public void That(bool condition, string message, params object[] args)
  414. {
  415. Assert.That(condition, Is.True, message, args);
  416. }
  417. /// <summary>
  418. /// Asserts that a condition is true. If the condition is false the method throws
  419. /// an <see cref="AssertionException"/>.
  420. /// </summary>
  421. /// <param name="condition">The evaluated condition</param>
  422. /// <param name="message">The message to display if the condition is false</param>
  423. static public void That(bool condition, string message)
  424. {
  425. Assert.That(condition, Is.True, message, null);
  426. }
  427. /// <summary>
  428. /// Asserts that a condition is true. If the condition is false the method throws
  429. /// an <see cref="AssertionException"/>.
  430. /// </summary>
  431. /// <param name="condition">The evaluated condition</param>
  432. static public void That(bool condition)
  433. {
  434. Assert.That(condition, Is.True, null, null);
  435. }
  436. #endregion
  437. /// <summary>
  438. /// Asserts that the code represented by a delegate throws an exception
  439. /// that satisfies the constraint provided.
  440. /// </summary>
  441. /// <param name="code">A TestDelegate to be executed</param>
  442. /// <param name="constraint">A ThrowsConstraint used in the test</param>
  443. static public void That(TestDelegate code, IResolveConstraint constraint)
  444. {
  445. Assert.That((object)code, constraint);
  446. }
  447. #endregion
  448. #region Throws, Catch and DoesNotThrow
  449. #region Throws
  450. /// <summary>
  451. /// Verifies that a delegate throws a particular exception when called.
  452. /// </summary>
  453. /// <param name="expression">A constraint to be satisfied by the exception</param>
  454. /// <param name="code">A TestSnippet delegate</param>
  455. /// <param name="message">The message that will be displayed on failure</param>
  456. /// <param name="args">Arguments to be used in formatting the message</param>
  457. public static Exception Throws(IResolveConstraint expression, TestDelegate code, string message, params object[] args)
  458. {
  459. Exception caughtException = null;
  460. try
  461. {
  462. code();
  463. }
  464. catch (Exception ex)
  465. {
  466. caughtException = ex;
  467. }
  468. Assert.That(caughtException, expression, message, args);
  469. return caughtException;
  470. }
  471. /// <summary>
  472. /// Verifies that a delegate throws a particular exception when called.
  473. /// </summary>
  474. /// <param name="expression">A constraint to be satisfied by the exception</param>
  475. /// <param name="code">A TestSnippet delegate</param>
  476. /// <param name="message">The message that will be displayed on failure</param>
  477. public static Exception Throws(IResolveConstraint expression, TestDelegate code, string message)
  478. {
  479. return Throws(expression, code, message, null);
  480. }
  481. /// <summary>
  482. /// Verifies that a delegate throws a particular exception when called.
  483. /// </summary>
  484. /// <param name="expression">A constraint to be satisfied by the exception</param>
  485. /// <param name="code">A TestSnippet delegate</param>
  486. public static Exception Throws(IResolveConstraint expression, TestDelegate code)
  487. {
  488. return Throws(expression, code, string.Empty, null);
  489. }
  490. /// <summary>
  491. /// Verifies that a delegate throws a particular exception when called.
  492. /// </summary>
  493. /// <param name="expectedExceptionType">The exception Type expected</param>
  494. /// <param name="code">A TestSnippet delegate</param>
  495. /// <param name="message">The message that will be displayed on failure</param>
  496. /// <param name="args">Arguments to be used in formatting the message</param>
  497. public static Exception Throws(Type expectedExceptionType, TestDelegate code, string message, params object[] args)
  498. {
  499. return Throws(new ExactTypeConstraint(expectedExceptionType), code, message, args);
  500. }
  501. /// <summary>
  502. /// Verifies that a delegate throws a particular exception when called.
  503. /// </summary>
  504. /// <param name="expectedExceptionType">The exception Type expected</param>
  505. /// <param name="code">A TestSnippet delegate</param>
  506. /// <param name="message">The message that will be displayed on failure</param>
  507. public static Exception Throws(Type expectedExceptionType, TestDelegate code, string message)
  508. {
  509. return Throws(new ExactTypeConstraint(expectedExceptionType), code, message, null);
  510. }
  511. /// <summary>
  512. /// Verifies that a delegate throws a particular exception when called.
  513. /// </summary>
  514. /// <param name="expectedExceptionType">The exception Type expected</param>
  515. /// <param name="code">A TestSnippet delegate</param>
  516. public static Exception Throws(Type expectedExceptionType, TestDelegate code)
  517. {
  518. return Throws(new ExactTypeConstraint(expectedExceptionType), code, string.Empty, null);
  519. }
  520. #endregion
  521. #region Throws<T>
  522. #if NET_2_0
  523. /// <summary>
  524. /// Verifies that a delegate throws a particular exception when called.
  525. /// </summary>
  526. /// <typeparam name="T">Type of the expected exception</typeparam>
  527. /// <param name="code">A TestSnippet delegate</param>
  528. /// <param name="message">The message that will be displayed on failure</param>
  529. /// <param name="args">Arguments to be used in formatting the message</param>
  530. public static T Throws<T>(TestDelegate code, string message, params object[] args) where T : Exception
  531. {
  532. return (T)Throws(typeof(T), code, message, args);
  533. }
  534. /// <summary>
  535. /// Verifies that a delegate throws a particular exception when called.
  536. /// </summary>
  537. /// <typeparam name="T">Type of the expected exception</typeparam>
  538. /// <param name="code">A TestSnippet delegate</param>
  539. /// <param name="message">The message that will be displayed on failure</param>
  540. public static T Throws<T>(TestDelegate code, string message) where T : Exception
  541. {
  542. return Throws<T>(code, message, null);
  543. }
  544. /// <summary>
  545. /// Verifies that a delegate throws a particular exception when called.
  546. /// </summary>
  547. /// <typeparam name="T">Type of the expected exception</typeparam>
  548. /// <param name="code">A TestSnippet delegate</param>
  549. public static T Throws<T>(TestDelegate code) where T : Exception
  550. {
  551. return Throws<T>(code, string.Empty, null);
  552. }
  553. #endif
  554. #endregion
  555. #region Catch
  556. /// <summary>
  557. /// Verifies that a delegate throws an exception when called
  558. /// and returns it.
  559. /// </summary>
  560. /// <param name="code">A TestDelegate</param>
  561. /// <param name="message">The message that will be displayed on failure</param>
  562. /// <param name="args">Arguments to be used in formatting the message</param>
  563. public static Exception Catch(TestDelegate code, string message, params object[] args)
  564. {
  565. return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code, message, args);
  566. }
  567. /// <summary>
  568. /// Verifies that a delegate throws an exception when called
  569. /// and returns it.
  570. /// </summary>
  571. /// <param name="code">A TestDelegate</param>
  572. /// <param name="message">The message that will be displayed on failure</param>
  573. public static Exception Catch(TestDelegate code, string message)
  574. {
  575. return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code, message);
  576. }
  577. /// <summary>
  578. /// Verifies that a delegate throws an exception when called
  579. /// and returns it.
  580. /// </summary>
  581. /// <param name="code">A TestDelegate</param>
  582. public static Exception Catch(TestDelegate code)
  583. {
  584. return Throws(new InstanceOfTypeConstraint(typeof(Exception)), code);
  585. }
  586. /// <summary>
  587. /// Verifies that a delegate throws an exception of a certain Type
  588. /// or one derived from it when called and returns it.
  589. /// </summary>
  590. /// <param name="expectedExceptionType">The expected Exception Type</param>
  591. /// <param name="code">A TestDelegate</param>
  592. /// <param name="message">The message that will be displayed on failure</param>
  593. /// <param name="args">Arguments to be used in formatting the message</param>
  594. public static Exception Catch(Type expectedExceptionType, TestDelegate code, string message, params object[] args)
  595. {
  596. return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code, message, args);
  597. }
  598. /// <summary>
  599. /// Verifies that a delegate throws an exception of a certain Type
  600. /// or one derived from it when called and returns it.
  601. /// </summary>
  602. /// <param name="expectedExceptionType">The expected Exception Type</param>
  603. /// <param name="code">A TestDelegate</param>
  604. /// <param name="message">The message that will be displayed on failure</param>
  605. public static Exception Catch(Type expectedExceptionType, TestDelegate code, string message)
  606. {
  607. return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code, message);
  608. }
  609. /// <summary>
  610. /// Verifies that a delegate throws an exception of a certain Type
  611. /// or one derived from it when called and returns it.
  612. /// </summary>
  613. /// <param name="expectedExceptionType">The expected Exception Type</param>
  614. /// <param name="code">A TestDelegate</param>
  615. public static Exception Catch(Type expectedExceptionType, TestDelegate code)
  616. {
  617. return Throws(new InstanceOfTypeConstraint(expectedExceptionType), code);
  618. }
  619. #endregion
  620. #region Catch<T>
  621. #if NET_2_0
  622. /// <summary>
  623. /// Verifies that a delegate throws an exception of a certain Type
  624. /// or one derived from it when called and returns it.
  625. /// </summary>
  626. /// <typeparam name="T">The expected Exception Type</typeparam>
  627. /// <param name="code">A TestDelegate</param>
  628. /// <param name="message">The message that will be displayed on failure</param>
  629. /// <param name="args">Arguments to be used in formatting the message</param>
  630. public static T Catch<T>(TestDelegate code, string message, params object[] args) where T : System.Exception
  631. {
  632. return (T)Throws(new InstanceOfTypeConstraint(typeof(T)), code, message, args);
  633. }
  634. /// <summary>
  635. /// Verifies that a delegate throws an exception of a certain Type
  636. /// or one derived from it when called and returns it.
  637. /// </summary>
  638. /// <typeparam name="T">The expected Exception Type</typeparam>
  639. /// <param name="code">A TestDelegate</param>
  640. /// <param name="message">The message that will be displayed on failure</param>
  641. public static T Catch<T>(TestDelegate code, string message) where T : System.Exception
  642. {
  643. return (T)Throws(new InstanceOfTypeConstraint(typeof(T)), code, message);
  644. }
  645. /// <summary>
  646. /// Verifies that a delegate throws an exception of a certain Type
  647. /// or one derived from it when called and returns it.
  648. /// </summary>
  649. /// <typeparam name="T">The expected Exception Type</typeparam>
  650. /// <param name="code">A TestDelegate</param>
  651. public static T Catch<T>(TestDelegate code) where T : System.Exception
  652. {
  653. return (T)Throws(new InstanceOfTypeConstraint(typeof(T)), code);
  654. }
  655. #endif
  656. #endregion
  657. #region DoesNotThrow
  658. /// <summary>
  659. /// Verifies that a delegate does not throw an exception
  660. /// </summary>
  661. /// <param name="code">A TestSnippet delegate</param>
  662. /// <param name="message">The message that will be displayed on failure</param>
  663. /// <param name="args">Arguments to be used in formatting the message</param>
  664. public static void DoesNotThrow(TestDelegate code, string message, params object[] args)
  665. {
  666. try
  667. {
  668. code();
  669. }
  670. catch (Exception ex)
  671. {
  672. TextMessageWriter writer = new TextMessageWriter(message, args);
  673. writer.WriteLine("Unexpected exception: {0}", ex.GetType());
  674. Assert.Fail(writer.ToString());
  675. }
  676. }
  677. /// <summary>
  678. /// Verifies that a delegate does not throw an exception.
  679. /// </summary>
  680. /// <param name="code">A TestSnippet delegate</param>
  681. /// <param name="message">The message that will be displayed on failure</param>
  682. public static void DoesNotThrow(TestDelegate code, string message)
  683. {
  684. DoesNotThrow(code, message, null);
  685. }
  686. /// <summary>
  687. /// Verifies that a delegate does not throw an exception.
  688. /// </summary>
  689. /// <param name="code">A TestSnippet delegate</param>
  690. public static void DoesNotThrow(TestDelegate code)
  691. {
  692. DoesNotThrow(code, string.Empty, null);
  693. }
  694. #endregion
  695. #endregion
  696. #region True
  697. /// <summary>
  698. /// Asserts that a condition is true. If the condition is false the method throws
  699. /// an <see cref="AssertionException"/>.
  700. /// </summary>
  701. /// <param name="condition">The evaluated condition</param>
  702. /// <param name="message">The message to display in case of failure</param>
  703. /// <param name="args">Array of objects to be used in formatting the message</param>
  704. public static void True(bool condition, string message, params object[] args)
  705. {
  706. Assert.That(condition, Is.True, message, args);
  707. }
  708. /// <summary>
  709. /// Asserts that a condition is true. If the condition is false the method throws
  710. /// an <see cref="AssertionException"/>.
  711. /// </summary>
  712. /// <param name="condition">The evaluated condition</param>
  713. /// <param name="message">The message to display in case of failure</param>
  714. public static void True(bool condition, string message)
  715. {
  716. Assert.That(condition, Is.True, message, null);
  717. }
  718. /// <summary>
  719. /// Asserts that a condition is true. If the condition is false the method throws
  720. /// an <see cref="AssertionException"/>.
  721. /// </summary>
  722. /// <param name="condition">The evaluated condition</param>
  723. public static void True(bool condition)
  724. {
  725. Assert.That(condition, Is.True, null, null);
  726. }
  727. /// <summary>
  728. /// Asserts that a condition is true. If the condition is false the method throws
  729. /// an <see cref="AssertionException"/>.
  730. /// </summary>
  731. /// <param name="condition">The evaluated condition</param>
  732. /// <param name="message">The message to display in case of failure</param>
  733. /// <param name="args">Array of objects to be used in formatting the message</param>
  734. public static void IsTrue(bool condition, string message, params object[] args)
  735. {
  736. Assert.That(condition, Is.True, message, args);
  737. }
  738. /// <summary>
  739. /// Asserts that a condition is true. If the condition is false the method throws
  740. /// an <see cref="AssertionException"/>.
  741. /// </summary>
  742. /// <param name="condition">The evaluated condition</param>
  743. /// <param name="message">The message to display in case of failure</param>
  744. public static void IsTrue(bool condition, string message)
  745. {
  746. Assert.That(condition, Is.True, message, null);
  747. }
  748. /// <summary>
  749. /// Asserts that a condition is true. If the condition is false the method throws
  750. /// an <see cref="AssertionException"/>.
  751. /// </summary>
  752. /// <param name="condition">The evaluated condition</param>
  753. public static void IsTrue(bool condition)
  754. {
  755. Assert.That(condition, Is.True, null, null);
  756. }
  757. #endregion
  758. #region False
  759. /// <summary>
  760. /// Asserts that a condition is false. If the condition is true the method throws
  761. /// an <see cref="AssertionException"/>.
  762. /// </summary>
  763. /// <param name="condition">The evaluated condition</param>
  764. /// <param name="message">The message to display in case of failure</param>
  765. /// <param name="args">Array of objects to be used in formatting the message</param>
  766. public static void False(bool condition, string message, params object[] args)
  767. {
  768. Assert.That(condition, Is.False, message, args);
  769. }
  770. /// <summary>
  771. /// Asserts that a condition is false. If the condition is true the method throws
  772. /// an <see cref="AssertionException"/>.
  773. /// </summary>
  774. /// <param name="condition">The evaluated condition</param>
  775. /// <param name="message">The message to display in case of failure</param>
  776. public static void False(bool condition, string message)
  777. {
  778. Assert.That(condition, Is.False, message, null);
  779. }
  780. /// <summary>
  781. /// Asserts that a condition is false. If the condition is true the method throws
  782. /// an <see cref="AssertionException"/>.
  783. /// </summary>
  784. /// <param name="condition">The evaluated condition</param>
  785. public static void False(bool condition)
  786. {
  787. Assert.That(condition, Is.False, null, null);
  788. }
  789. /// <summary>
  790. /// Asserts that a condition is false. If the condition is true the method throws
  791. /// an <see cref="AssertionException"/>.
  792. /// </summary>
  793. /// <param name="condition">The evaluated condition</param>
  794. /// <param name="message">The message to display in case of failure</param>
  795. /// <param name="args">Array of objects to be used in formatting the message</param>
  796. public static void IsFalse(bool condition, string message, params object[] args)
  797. {
  798. Assert.That(condition, Is.False, message, args);
  799. }
  800. /// <summary>
  801. /// Asserts that a condition is false. If the condition is true the method throws
  802. /// an <see cref="AssertionException"/>.
  803. /// </summary>
  804. /// <param name="condition">The evaluated condition</param>
  805. /// <param name="message">The message to display in case of failure</param>
  806. public static void IsFalse(bool condition, string message)
  807. {
  808. Assert.That(condition, Is.False, message, null);
  809. }
  810. /// <summary>
  811. /// Asserts that a condition is false. If the condition is true the method throws
  812. /// an <see cref="AssertionException"/>.
  813. /// </summary>
  814. /// <param name="condition">The evaluated condition</param>
  815. public static void IsFalse(bool condition)
  816. {
  817. Assert.That(condition, Is.False, null, null);
  818. }
  819. #endregion
  820. #region NotNull
  821. /// <summary>
  822. /// Verifies that the object that is passed in is not equal to <code>null</code>
  823. /// If the object is <code>null</code> then an <see cref="AssertionException"/>
  824. /// is thrown.
  825. /// </summary>
  826. /// <param name="anObject">The object that is to be tested</param>
  827. /// <param name="message">The message to display in case of failure</param>
  828. /// <param name="args">Array of objects to be used in formatting the message</param>
  829. public static void NotNull(object anObject, string message, params object[] args)
  830. {
  831. Assert.That(anObject, Is.Not.Null, message, args);
  832. }
  833. /// <summary>
  834. /// Verifies that the object that is passed in is not equal to <code>null</code>
  835. /// If the object is <code>null</code> then an <see cref="AssertionException"/>
  836. /// is thrown.
  837. /// </summary>
  838. /// <param name="anObject">The object that is to be tested</param>
  839. /// <param name="message">The message to display in case of failure</param>
  840. public static void NotNull(object anObject, string message)
  841. {
  842. Assert.That(anObject, Is.Not.Null, message, null);
  843. }
  844. /// <summary>
  845. /// Verifies that the object that is passed in is not equal to <code>null</code>
  846. /// If the object is <code>null</code> then an <see cref="AssertionException"/>
  847. /// is thrown.
  848. /// </summary>
  849. /// <param name="anObject">The object that is to be tested</param>
  850. public static void NotNull(object anObject)
  851. {
  852. Assert.That(anObject, Is.Not.Null, null, null);
  853. }
  854. /// <summary>
  855. /// Verifies that the object that is passed in is not equal to <code>null</code>
  856. /// If the object is <code>null</code> then an <see cref="AssertionException"/>
  857. /// is thrown.
  858. /// </summary>
  859. /// <param name="anObject">The object that is to be tested</param>
  860. /// <param name="message">The message to display in case of failure</param>
  861. /// <param name="args">Array of objects to be used in formatting the message</param>
  862. public static void IsNotNull(object anObject, string message, params object[] args)
  863. {
  864. Assert.That(anObject, Is.Not.Null, message, args);
  865. }
  866. /// <summary>
  867. /// Verifies that the object that is passed in is not equal to <code>null</code>
  868. /// If the object is <code>null</code> then an <see cref="AssertionException"/>
  869. /// is thrown.
  870. /// </summary>
  871. /// <param name="anObject">The object that is to be tested</param>
  872. /// <param name="message">The message to display in case of failure</param>
  873. public static void IsNotNull(object anObject, string message)
  874. {
  875. Assert.That(anObject, Is.Not.Null, message, null);
  876. }
  877. /// <summary>
  878. /// Verifies that the object that is passed in is not equal to <code>null</code>
  879. /// If the object is <code>null</code> then an <see cref="AssertionException"/>
  880. /// is thrown.
  881. /// </summary>
  882. /// <param name="anObject">The object that is to be tested</param>
  883. public static void IsNotNull(object anObject)
  884. {
  885. Assert.That(anObject, Is.Not.Null, null, null);
  886. }
  887. #endregion
  888. #region Null
  889. /// <summary>
  890. /// Verifies that the object that is passed in is equal to <code>null</code>
  891. /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
  892. /// is thrown.
  893. /// </summary>
  894. /// <param name="anObject">The object that is to be tested</param>
  895. /// <param name="message">The message to display in case of failure</param>
  896. /// <param name="args">Array of objects to be used in formatting the message</param>
  897. public static void Null(object anObject, string message, params object[] args)
  898. {
  899. Assert.That(anObject, Is.Null, message, args);
  900. }
  901. /// <summary>
  902. /// Verifies that the object that is passed in is equal to <code>null</code>
  903. /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
  904. /// is thrown.
  905. /// </summary>
  906. /// <param name="anObject">The object that is to be tested</param>
  907. /// <param name="message">The message to display in case of failure</param>
  908. public static void Null(object anObject, string message)
  909. {
  910. Assert.That(anObject, Is.Null, message, null);
  911. }
  912. /// <summary>
  913. /// Verifies that the object that is passed in is equal to <code>null</code>
  914. /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
  915. /// is thrown.
  916. /// </summary>
  917. /// <param name="anObject">The object that is to be tested</param>
  918. public static void Null(object anObject)
  919. {
  920. Assert.That(anObject, Is.Null, null, null);
  921. }
  922. /// <summary>
  923. /// Verifies that the object that is passed in is equal to <code>null</code>
  924. /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
  925. /// is thrown.
  926. /// </summary>
  927. /// <param name="anObject">The object that is to be tested</param>
  928. /// <param name="message">The message to display in case of failure</param>
  929. /// <param name="args">Array of objects to be used in formatting the message</param>
  930. public static void IsNull(object anObject, string message, params object[] args)
  931. {
  932. Assert.That(anObject, Is.Null, message, args);
  933. }
  934. /// <summary>
  935. /// Verifies that the object that is passed in is equal to <code>null</code>
  936. /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
  937. /// is thrown.
  938. /// </summary>
  939. /// <param name="anObject">The object that is to be tested</param>
  940. /// <param name="message">The message to display in case of failure</param>
  941. public static void IsNull(object anObject, string message)
  942. {
  943. Assert.That(anObject, Is.Null, message, null);
  944. }
  945. /// <summary>
  946. /// Verifies that the object that is passed in is equal to <code>null</code>
  947. /// If the object is not <code>null</code> then an <see cref="AssertionException"/>
  948. /// is thrown.
  949. /// </summary>
  950. /// <param name="anObject">The object that is to be tested</param>
  951. public static void IsNull(object anObject)
  952. {
  953. Assert.That(anObject, Is.Null, null, null);
  954. }
  955. #endregion
  956. #region IsNaN
  957. /// <summary>
  958. /// Verifies that the double that is passed in is an <code>NaN</code> value.
  959. /// If the object is not <code>NaN</code> then an <see cref="AssertionException"/>
  960. /// is thrown.
  961. /// </summary>
  962. /// <param name="aDouble">The value that is to be tested</param>
  963. /// <param name="message">The message to display in case of failure</param>
  964. /// <param name="args">Array of objects to be used in formatting the message</param>
  965. public static void IsNaN(double aDouble, string message, params object[] args)
  966. {
  967. Assert.That(aDouble, Is.NaN, message, args);
  968. }
  969. /// <summary>
  970. /// Verifies that the double that is passed in is an <code>NaN</code> value.
  971. /// If the object is not <code>NaN</code> then an <see cref="AssertionException"/>
  972. /// is thrown.
  973. /// </summary>
  974. /// <param name="aDouble">The value that is to be tested</param>
  975. /// <param name="message">The message to display in case of failure</param>
  976. public static void IsNaN(double aDouble, string message)
  977. {
  978. Assert.That(aDouble, Is.NaN, message, null);
  979. }
  980. /// <summary>
  981. /// Verifies that the double that is passed in is an <code>NaN</code> value.
  982. /// If the object is not <code>NaN</code> then an <see cref="AssertionException"/>
  983. /// is thrown.
  984. /// </summary>
  985. /// <param name="aDouble">The value that is to be tested</param>
  986. public static void IsNaN(double aDouble)
  987. {
  988. Assert.That(aDouble, Is.NaN, null, null);
  989. }
  990. #if NET_2_0
  991. /// <summary>
  992. /// Verifies that the double that is passed in is an <code>NaN</code> value.
  993. /// If the object is not <code>NaN</code> then an <see cref="AssertionException"/>
  994. /// is thrown.
  995. /// </summary>
  996. /// <param name="aDouble">The value that is to be tested</param>
  997. /// <param name="message">The message to display in case of failure</param>
  998. /// <param name="args">Array of objects to be used in formatting the message</param>
  999. public static void IsNaN(double? aDouble, string message, params object[] args)
  1000. {
  1001. Assert.That(aDouble, Is.NaN, message, args);
  1002. }
  1003. /// <summary>
  1004. /// Verifies that the double that is passed in is an <code>NaN</code> value.
  1005. /// If the object is not <code>NaN</code> then an <see cref="AssertionException"/>
  1006. /// is thrown.
  1007. /// </summary>
  1008. /// <param name="aDouble">The value that is to be tested</param>
  1009. /// <param name="message">The message to display in case of failure</param>
  1010. public static void IsNaN(double? aDouble, string message)
  1011. {
  1012. Assert.That(aDouble, Is.NaN, message, null);
  1013. }
  1014. /// <summary>
  1015. /// Ve…

Large files files are truncated, but you can click here to view the full file