PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Assert.cs

#
C# | 884 lines | 525 code | 63 blank | 296 comment | 66 complexity | 310b436865472530fc64a4b10db4774f MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Runtime.Serialization;
  6. using Delta.Utilities.Helpers;
  7. using NUnit.Framework;
  8. namespace Delta.Utilities
  9. {
  10. /// <summary>
  11. /// Assert helper class for testing and debugging. Provides static methods
  12. /// that help you figuring out if values are equal, not equal, less, greater,
  13. /// true, false, etc. This way xUnit, NUnit, etc. is not required and we can
  14. /// make sure all unit tests can be executed on all supported platforms.
  15. /// </summary>
  16. [EditorBrowsable(EditorBrowsableState.Never)]
  17. public static class Assert
  18. {
  19. #region AssertException Class
  20. /// <summary>
  21. /// Assert exception, this is just a very simple exception. Can be derived
  22. /// and used for other exceptions too (sometimes used for testing too).
  23. /// </summary>
  24. [Serializable]
  25. public sealed class AssertException : Exception
  26. {
  27. #region LineNumber (Public)
  28. /// <summary>
  29. /// The number of the code line where this exception has happened.
  30. /// </summary>
  31. public string LineNumber
  32. {
  33. get;
  34. private set;
  35. }
  36. #endregion
  37. #region Constructors
  38. /// <summary>
  39. /// Create assert exception with just an assert message.
  40. /// We use mostly just this constructor to display the message string.
  41. /// </summary>
  42. /// <param name="setMessage">Message describing the problem</param>
  43. public AssertException(string setMessage)
  44. : base(setMessage)
  45. {
  46. }
  47. /// <summary>
  48. /// Create assert exception with both an assert message and an inner
  49. /// exception. Usually used when something very bad happened and we even
  50. /// need to display the inner exception.
  51. /// </summary>
  52. /// <param name="setMessage">Message describing the problem</param>
  53. /// <param name="setInnerException">Inner exception</param>
  54. public AssertException(string setMessage, Exception setInnerException)
  55. : base(setMessage, setInnerException)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the System.Exception class with
  60. /// serialized data. This is required e.g. for serializing an exception
  61. /// from another worker domain.
  62. /// </summary>
  63. /// <remarks>
  64. /// This constructor will be called after the public constructor.
  65. /// </remarks>
  66. /// <param name="info">
  67. /// Serialization info to pass to the base Exception.
  68. /// </param>
  69. /// <param name="context">Context for the base Exception</param>
  70. private AssertException(SerializationInfo info,
  71. StreamingContext context)
  72. : base(info, context)
  73. {
  74. // To reconstruct the info about our "ExceptionLine" we are only
  75. // interested in the last line of the stack trace (which was build now
  76. // by the base class) because this contains the info at which code line
  77. // this exception was thrown, so grab now the name of the method name
  78. // of the code first
  79. int splitIndex = StackTrace.LastIndexOf("at ");
  80. string lastExceptionLine = (splitIndex != MathHelper.InvalidIndex)
  81. ? StackTrace.Substring(splitIndex + "at ".Length)
  82. : StackTrace;
  83. // but still keep the information about the code line number which is
  84. // important to identify which Assert it was exactly
  85. splitIndex = lastExceptionLine.LastIndexOf(' ');
  86. if (splitIndex != MathHelper.InvalidIndex)
  87. {
  88. LineNumber = lastExceptionLine.Substring(splitIndex + 1);
  89. }
  90. }
  91. #endregion
  92. }
  93. #endregion
  94. #region Delegates
  95. /// <summary>
  96. /// Helper delegate for the Throws method below to test if code throws
  97. /// a specific exception. Same delegate as Base.RunDelegate, but we have
  98. /// no access to that one here.
  99. /// </summary>
  100. [EditorBrowsable(EditorBrowsableState.Never)]
  101. public delegate void ThrowsDelegate();
  102. #endregion
  103. #region Equals (Static)
  104. /// <summary>
  105. /// Equals, just trying to hide it. The Equals() method is not officially
  106. /// supported for 'Assert', please use the 'Equal()' method instead.
  107. /// </summary>
  108. [EditorBrowsable(EditorBrowsableState.Never)]
  109. public new static bool Equals(object objA, object objB)
  110. {
  111. return object.Equals(objA, objB);
  112. }
  113. #endregion
  114. #region ReferenceEquals (Static)
  115. /// <summary>
  116. /// Hide method. The 'ReferenceEquals()' method is not officially
  117. /// supported for 'Assert', please use the 'Equal()' method instead.
  118. /// </summary>
  119. [EditorBrowsable(EditorBrowsableState.Never)]
  120. public new static bool ReferenceEquals(object objA, object objB)
  121. {
  122. return object.ReferenceEquals(objA, objB);
  123. }
  124. #endregion
  125. #region Equal (Static)
  126. /// <summary>
  127. /// Are two values equal? All they need is some comparer (see IsEqual).
  128. /// Throws an AssertException if the values are not equal. Note: Other
  129. /// unit test frameworks use expected and actual values for the parameters
  130. /// here, but we don't care about the order, usually it is programmed in
  131. /// a natural way so you just say Assert.Equal(Numbers.Add(10, 7), 17),
  132. /// but you can also do it the xUnit/NUnit way: Assert.Equal(17,
  133. /// Numbers.Add(10, 7).
  134. /// </summary>
  135. /// <typeparam name="T">Type to compare</typeparam>
  136. /// <param name="firstValue">First value</param>
  137. /// <param name="secondValue">Second value</param>
  138. /// <exception cref="AssertException">Exception is thrown if two values
  139. /// are not equal</exception>
  140. public static void Equal<T>(T firstValue, T secondValue)
  141. {
  142. if (IsEqual(firstValue, secondValue) == false)
  143. {
  144. throw new AssertException(
  145. "The two values were not equal: '" + firstValue + "' was not " +
  146. "equal to '" + secondValue + "'.");
  147. }
  148. }
  149. #endregion
  150. #region NotEqual (Static)
  151. /// <summary>
  152. /// These values should not be equal, if they are an exception is thrown.
  153. /// </summary>
  154. /// <param name="firstValue">Expected value</param>
  155. /// <param name="secondValue">Actual value</param>
  156. public static void NotEqual<T>(T firstValue, T secondValue)
  157. {
  158. if (IsEqual(firstValue, secondValue))
  159. {
  160. throw new AssertException(
  161. "The two values were equal, but they should be different: '" +
  162. firstValue + "' was equal to '" + secondValue + "'!");
  163. }
  164. }
  165. #endregion
  166. #region NearlyEqual (Static)
  167. /// <summary>
  168. /// Nearly equal check, pretty much the same as the Equal check above,
  169. /// but it only allows floats and it will still return true if
  170. /// expectedValue and actualValue are less than given max. difference.
  171. /// </summary>
  172. /// <param name="firstValue">First parameter to compare</param>
  173. /// <param name="secondValue">Second parameter to compare</param>
  174. /// <param name="maxDifference">
  175. /// Maximum difference allowed, MathHelper.Epsilon by default.
  176. /// </param>
  177. public static void NearlyEqual(float firstValue, float secondValue,
  178. float maxDifference = MathHelper.Epsilon)
  179. {
  180. float difference = Math.Abs(secondValue - firstValue);
  181. if (difference > maxDifference)
  182. {
  183. throw new AssertException(
  184. "The two values were not nearly equal: '" + firstValue + "' and '" +
  185. secondValue + "' (max. difference is '" + maxDifference + "')");
  186. }
  187. }
  188. #endregion
  189. #region Between (Static)
  190. /// <summary>
  191. /// Checks if valueToCheck is in between lowerLimit and upperLimit,
  192. /// lowerLimit must be smaller than upperLimit!
  193. /// </summary>
  194. /// <param name="valueToCheck">Value we want to check that should be
  195. /// between lowerLimit and upperLimit (both exclusive)</param>
  196. /// <param name="lowerLimit">Lower limit value</param>
  197. /// <param name="upperLimit">Upper limit value</param>
  198. public static void Between(float valueToCheck, float lowerLimit,
  199. float upperLimit)
  200. {
  201. if (lowerLimit > upperLimit)
  202. {
  203. throw new ArgumentException(
  204. "Why is lowerLimit '" + lowerLimit + "' bigger than upperLimit '" +
  205. upperLimit + "'? Please make sure lowerLimit is a smaller value " +
  206. "than upperLimit.");
  207. }
  208. if (valueToCheck < lowerLimit ||
  209. valueToCheck > upperLimit)
  210. {
  211. throw new AssertException(
  212. "The value '" + valueToCheck + "' is not in between '" + lowerLimit +
  213. "' and '" + upperLimit + "'.");
  214. }
  215. }
  216. #endregion
  217. #region Contains (Static)
  218. /// <summary>
  219. /// Simple string contains check that allows strings to be case-insensitive.
  220. /// Will throw an AssertException if expectedInString is not found in
  221. /// fullString. Note: Currently only supports strings, but this way
  222. /// we save one intellisense space (else we have 2 Contains methods).
  223. /// </summary>
  224. /// <typeparam name="T">Type to compare</typeparam>
  225. /// <param name="expectedInString">String value we search for</param>
  226. /// <param name="fullString">The string where expectedInString should be
  227. /// contained somewhere</param>
  228. public static void Contains<T>(T fullString, T expectedInString)
  229. {
  230. string expectedInStringValue = expectedInString as string;
  231. string fullStringValue = fullString as string;
  232. if (expectedInStringValue == null ||
  233. fullStringValue == null)
  234. {
  235. throw new ArithmeticException(
  236. "You are only allowed to call this method with 2 strings");
  237. }
  238. // Note: We don't have the StringHelper here, so we need to do this
  239. // by hand.
  240. else if (fullStringValue.ToLower().Contains(
  241. expectedInStringValue.ToLower()) == false)
  242. {
  243. throw new AssertException(
  244. "The string '" + expectedInString + "' was expected to be found " +
  245. "in '" + fullString + "', but it is just not in there.");
  246. }
  247. }
  248. /// <summary>
  249. /// Contains check for any IEnumerable (array, list, etc.). Will fail
  250. /// with an AssertException if the expectedValue is not in someArray.
  251. /// </summary>
  252. /// <typeparam name="T">Type to compare</typeparam>
  253. /// <param name="someArray">Some Array</param>
  254. /// <param name="expectedValue">Expected Value</param>
  255. public static void Contains<T>(IEnumerable<T> someArray, T expectedValue)
  256. {
  257. if (someArray == null)
  258. {
  259. throw new NullReferenceException(
  260. "Provided array is null!\n" +
  261. "Array needs to be initialized for checking 'Contains'.");
  262. }
  263. foreach (T arrayValue in someArray)
  264. {
  265. if (arrayValue.Equals(expectedValue))
  266. {
  267. return;
  268. }
  269. }
  270. throw new AssertException(
  271. "The value '" + expectedValue + "' was expected to be found in the " +
  272. "array '" + someArray + "', but it is just not in there.");
  273. }
  274. #endregion
  275. #region Throws (Static)
  276. /// <summary>
  277. /// Expects the testCode to throw an exception of exceptionType and catches
  278. /// it (no exception is thrown to the caller), if it does not throw the
  279. /// specified exceptionType, an AssertException is thrown.
  280. /// </summary>
  281. /// <param name="exceptionType">Exception Type</param>
  282. /// <param name="testCode">Test Code</param>
  283. /// <exception cref="AssertException">
  284. /// This is thrown if the code does not throw the expected exception.
  285. /// </exception>
  286. public static void Throws(Type exceptionType, ThrowsDelegate testCode)
  287. {
  288. try
  289. {
  290. testCode();
  291. throw new AssertException(
  292. "Expected an '" + exceptionType + "', but the code succeeded.");
  293. }
  294. catch (Exception ex)
  295. {
  296. if (exceptionType.Equals(ex.GetType()) == false &&
  297. // Also allow derived exception types
  298. exceptionType.IsInstanceOfType(ex) == false)
  299. {
  300. throw new AssertException(
  301. "Expected an '" + exceptionType + "', but got an '" +
  302. ex.GetType() + "'", ex);
  303. }
  304. }
  305. // Else everything is fine!
  306. }
  307. #endregion
  308. #region True (Static)
  309. /// <summary>
  310. /// Simple check if a condition is true. Since we do not know why this
  311. /// failed (if it is not true), you can optionally supply a why string!
  312. /// </summary>
  313. /// <param name="expectedTrueValue">
  314. /// Expected value that should be true
  315. /// </param>
  316. /// <exception cref="AssertException">
  317. /// If the value was not 'true' as expected!
  318. /// </exception>
  319. public static void True(bool expectedTrueValue)
  320. {
  321. if (expectedTrueValue != true)
  322. {
  323. throw new AssertException("The value was not 'true' as expected");
  324. }
  325. }
  326. /// <summary>
  327. /// Simple check if a condition is true. Since we do not know why this
  328. /// failed (if it is not true), you can optionally supply a why string!
  329. /// </summary>
  330. /// <param name="expectedTrueValue">
  331. /// Expected value that should be true.
  332. /// </param>
  333. /// <param name="why">
  334. /// Extra string to explain why this is wrong
  335. /// </param>
  336. /// <exception cref="AssertException">
  337. /// If the value was not 'true' as expected!
  338. /// </exception>
  339. public static void True(bool expectedTrueValue, string why)
  340. {
  341. if (expectedTrueValue != true)
  342. {
  343. throw new AssertException(
  344. "The value was not 'true' as expected" +
  345. (why != null
  346. ? ": " + why
  347. : ""));
  348. }
  349. }
  350. #endregion
  351. #region False (Static)
  352. /// <summary>
  353. /// Simple check if a condition is false. Since we do not know why this
  354. /// failed (if it is not false), you can optionally supply a why string!
  355. /// </summary>
  356. /// <param name="expectedFalseValue">
  357. /// Expected value that should be false.
  358. /// </param>
  359. /// <exception cref="AssertException">
  360. /// If the value was not 'false' as expected!
  361. /// </exception>
  362. public static void False(bool expectedFalseValue)
  363. {
  364. if (expectedFalseValue)
  365. {
  366. throw new AssertException("The value was not 'false' as expected");
  367. }
  368. }
  369. /// <summary>
  370. /// Simple check if a condition is false. Since we do not know why this
  371. /// failed (if it is not false), you can optionally supply a why string!
  372. /// </summary>
  373. /// <param name="expectedFalseValue">
  374. /// Expected value that should be false.
  375. /// </param>
  376. /// <param name="why">
  377. /// Why did this happen? Optional extra information text that will be added
  378. /// when the expectedFalseValue is not false.
  379. /// </param>
  380. /// <exception cref="AssertException">
  381. /// If the value was not 'false' as expected!
  382. /// </exception>
  383. public static void False(bool expectedFalseValue, string why)
  384. {
  385. if (expectedFalseValue)
  386. {
  387. throw new AssertException(
  388. "The value was not 'false' as expected" +
  389. (why != null
  390. ? ": " + why
  391. : ""));
  392. }
  393. }
  394. #endregion
  395. #region Null (Static)
  396. /// <summary>
  397. /// Simple check if a value is null.
  398. /// </summary>
  399. /// <param name="expectedNullValue">
  400. /// Expected value that should be null.
  401. /// </param>
  402. /// <exception cref="AssertException">
  403. /// If the value was not 'null' as expected!
  404. /// </exception>
  405. public static void Null(object expectedNullValue)
  406. {
  407. if (expectedNullValue != null)
  408. {
  409. throw new AssertException(
  410. "The value was expected to be 'null': " + expectedNullValue);
  411. }
  412. }
  413. #endregion
  414. #region NotNull (Static)
  415. /// <summary>
  416. /// Simple check if a condition is false. Since we do not know why this
  417. /// failed (if it is not false), you must supply a why string!
  418. /// </summary>
  419. /// <param name="expectedValue">
  420. /// Expected value that should not be null
  421. /// </param>
  422. /// <exception cref="AssertException">
  423. /// If the value was 'null', but we expected a not 'null' value!
  424. /// </exception>
  425. public static void NotNull(object expectedValue)
  426. {
  427. if (expectedValue == null)
  428. {
  429. throw new AssertException(
  430. "The value was expected not to be 'null'!");
  431. }
  432. }
  433. #endregion
  434. #region Methods (Private)
  435. #region IsEqual
  436. /// <summary>
  437. /// Helper method to figure out if two values are equal.
  438. /// Value types can easily be compared via Equals, reference types have to
  439. /// be from the same type and then we try to compare them IComparable, then
  440. /// IEquatable and finally checking if enumerating works. If all fails we
  441. /// still can fallback to the Equals method (same as for value types), but
  442. /// this usually fails if two references are not exactly the same pointer.
  443. /// </summary>
  444. /// <exception cref="AssertException">
  445. /// Exception if the two types are not equal (can be different types,
  446. /// different values, different lists lengths, etc.)
  447. /// </exception>
  448. private static bool IsEqual<T>(T A, T B)
  449. {
  450. // Value types can easily be compared via Equals
  451. Type objectType = typeof(T);
  452. // Class types need some extra checking
  453. if (objectType.IsValueType == false)
  454. {
  455. // Is normally "null" for classes
  456. Object defaultValue = default(T);
  457. // If is A == null ?
  458. if (Object.Equals(A, defaultValue))
  459. {
  460. // and B == null ?
  461. if (Object.Equals(B, defaultValue))
  462. {
  463. // the A and B are equal (-> null)
  464. return true;
  465. }
  466. // -> A == null and B != null
  467. return false;
  468. }
  469. if (Object.Equals(B, defaultValue))
  470. {
  471. // -> B == null and A != null
  472. return false;
  473. }
  474. }
  475. // At first we have to check the IComparable implementation (if exists)
  476. // -> generic
  477. IComparable<T> genericComparer = A as IComparable<T>;
  478. if (genericComparer != null)
  479. {
  480. return genericComparer.CompareTo(B) == 0;
  481. }
  482. // -> non-generic
  483. IComparable comparer = A as IComparable;
  484. if (comparer != null)
  485. {
  486. return comparer.CompareTo(B) == 0;
  487. }
  488. // At second the if is a IEquatable interface implemented for checking the
  489. // equality
  490. IEquatable<T> equatable = A as IEquatable<T>;
  491. if (equatable != null)
  492. {
  493. return equatable.Equals(B);
  494. }
  495. // In the case we have two lists, let's check the elements in there for
  496. // equality
  497. IEnumerable aEnumerable = A as IEnumerable;
  498. IEnumerable bEnumerable = B as IEnumerable;
  499. if (aEnumerable != null &&
  500. bEnumerable != null)
  501. {
  502. IEnumerator aEnumerator = A as IEnumerator;
  503. IEnumerator bEnumerator = B as IEnumerator;
  504. // Also check if we got an IList here
  505. if (aEnumerator == null ||
  506. bEnumerator == null)
  507. {
  508. ICollection aCollection = A as ICollection;
  509. ICollection bCollection = B as ICollection;
  510. if (aCollection == null ||
  511. bCollection == null)
  512. {
  513. return A.Equals(B);
  514. } // if
  515. if (aCollection.Count !=
  516. bCollection.Count)
  517. {
  518. throw new AssertException(
  519. "The element count of the two lists were not equal: '" +
  520. aCollection.Count + "' was not equal to '" + bCollection.Count +
  521. "'.");
  522. } // else if
  523. aEnumerator = aCollection.GetEnumerator();
  524. bEnumerator = bCollection.GetEnumerator();
  525. }
  526. int elementIndex = -1;
  527. do
  528. {
  529. elementIndex++;
  530. bool hadANext = aEnumerator.MoveNext();
  531. bool hadBNext = bEnumerator.MoveNext();
  532. // End reached?
  533. if (hadANext == false ||
  534. hadBNext == false)
  535. {
  536. // if one list is longer than the other one, they can't be equal
  537. return hadANext == hadBNext;
  538. }
  539. } while (Object.Equals(aEnumerator.Current, bEnumerator.Current));
  540. // The objects in the list are different
  541. throw new AssertException(
  542. "The element of the two lists at index '" + elementIndex +
  543. "' was not equal: '" + aEnumerator.Current + "' is not equal to '" +
  544. bEnumerator.Current + "'.");
  545. }
  546. // If the type of the value types doesn't match, we can't compare it
  547. // because "StructA" can be totally different to "StructB" then it's
  548. // not allowed to derive value types
  549. if (A.GetType() !=
  550. B.GetType())
  551. {
  552. return false;
  553. }
  554. // If there is really no "compare interface", then let's just check by
  555. // the default (object) comparer
  556. return A.Equals(B);
  557. }
  558. #endregion
  559. #endregion
  560. /// <summary>
  561. /// Tests, which can be tested with Delta.Tools.TestRunner.
  562. /// </summary>
  563. internal class AssertTests
  564. {
  565. #region Contains (LongRunning)
  566. /// <summary>
  567. /// Contains. Note: Too slow for a dynamic unit test.
  568. /// </summary>
  569. [Test, NUnit.Framework.Category("LongRunning")]
  570. public void Contains()
  571. {
  572. // Is some string in another one?
  573. Assert.Contains("Chin contain the word hi", "hi");
  574. // And test if it can fail too
  575. Assert.Throws(typeof(AssertException), delegate
  576. {
  577. Assert.Contains("a", "b");
  578. });
  579. // Try with switched parameters, should fail too!
  580. Assert.Throws(typeof(AssertException), delegate
  581. {
  582. Assert.Contains("hi", "Chin contain the word hi");
  583. });
  584. // Next test the collection method
  585. Assert.Contains(new[]
  586. {
  587. 1, 2, 3
  588. }, 1);
  589. Assert.Contains(new[]
  590. {
  591. "a", "b", "c"
  592. }, "b");
  593. List<int> someList = new List<int>();
  594. someList.Add(359);
  595. someList.Add(984);
  596. Assert.Contains(someList, 984);
  597. // And finally some fail checks
  598. Assert.Throws(typeof(AssertException), delegate
  599. {
  600. Assert.Contains(someList, 123);
  601. });
  602. // This fails too because "hi" is not equal to "hi there"
  603. Assert.Throws(typeof(AssertException), delegate
  604. {
  605. Assert.Contains(new[]
  606. {
  607. "hi there", "whattup"
  608. }, "hi");
  609. });
  610. }
  611. #endregion
  612. #region Throws (LongRunning)
  613. /// <summary>
  614. /// Throws. Note: Too slow for a dynamic unit test.
  615. /// </summary>
  616. [Test]
  617. [NUnit.Framework.Category("LongRunning")]
  618. public void Throws()
  619. {
  620. // Testing to throw some expected exceptions
  621. Assert.Throws(typeof(NullReferenceException), delegate
  622. {
  623. throw new NullReferenceException("Shit happens");
  624. });
  625. Assert.Throws(typeof(NullReferenceException), delegate
  626. {
  627. object nullObject = null;
  628. // Crashes here
  629. nullObject.ToString();
  630. });
  631. Assert.Throws(typeof(AssertException), delegate
  632. {
  633. // This throws AssertException because 1 is not equal to 2
  634. Assert.Equal(1, 2);
  635. });
  636. // We can also cascade Assert.Throws to check the opposite
  637. Assert.Throws(typeof(AssertException), delegate
  638. {
  639. Assert.Throws(typeof(Exception), delegate
  640. {
  641. // The following code does NOT throw an exception and will
  642. // therefore fail the Assert.Throws, which throws the
  643. // AssertException caught above.
  644. string abc = "abc";
  645. abc += "def";
  646. });
  647. });
  648. // Throwing the wrong exception will also fail
  649. Assert.Throws(typeof(AssertException), delegate
  650. {
  651. Assert.Throws(typeof(InvalidOperationException), delegate
  652. {
  653. throw new NullReferenceException("Again?");
  654. });
  655. });
  656. // However, throwing something from a derived exception should work
  657. Assert.Throws(typeof(Exception), delegate
  658. {
  659. throw new NullReferenceException("Seems we like this one ..");
  660. });
  661. }
  662. #endregion
  663. #region NearlyEqual (LongRunning)
  664. /// <summary>
  665. /// Nearly equal
  666. /// </summary>
  667. [Test]
  668. [NUnit.Framework.Category("LongRunning")]
  669. public void NearlyEqual()
  670. {
  671. // Just checking some equal values
  672. Assert.NearlyEqual(1, 1);
  673. Assert.NearlyEqual(1.25f, 1.25f);
  674. // Next check some different values, that are nearly equal
  675. Assert.NearlyEqual(1.00002f, 1.00001f);
  676. Assert.NearlyEqual((float)Math.Asin(Math.Sin(0.5f)), 0.5f);
  677. // We expect an exception if the different values are not equal
  678. Assert.Throws(typeof(AssertException), delegate
  679. {
  680. // This difference is just too big!
  681. Assert.NearlyEqual(1.2f, 1.1f);
  682. });
  683. }
  684. #endregion
  685. #region Between
  686. /// <summary>
  687. /// Between
  688. /// </summary>
  689. [Test]
  690. public void Between()
  691. {
  692. // 1 is between 0 and 2
  693. Assert.Between(1, 0, 2);
  694. // 54.43 is between 50 and 60
  695. Assert.Between(54.43f, 50, 60);
  696. // Also check if it can fail
  697. Assert.Throws(typeof(AssertException), delegate
  698. {
  699. Assert.Between(44.43f, 50, 60);
  700. });
  701. }
  702. #endregion
  703. #region Equal
  704. /// <summary>
  705. /// Is equal
  706. /// </summary>
  707. [Test]
  708. public void Equal()
  709. {
  710. // Just checking some doubles
  711. Assert.Equal(3.0, 3.0);
  712. // "null" references
  713. Assert.Equal(null, (Object)null);
  714. // and strings for equality
  715. Assert.Equal("Test", "Test");
  716. // We expect an exception if the different values are not equal
  717. Assert.Throws(typeof(AssertException), delegate
  718. {
  719. Assert.Equal(3.0, 1.0);
  720. });
  721. Assert.Equal(new float[]
  722. {
  723. 2, 3, 4, 5
  724. },
  725. new float[]
  726. {
  727. 2, 3, 4, 5
  728. });
  729. }
  730. #endregion
  731. #region False
  732. /// <summary>
  733. /// False
  734. /// </summary>
  735. [Test]
  736. public void False()
  737. {
  738. Assert.False(false, "False is always false");
  739. Assert.False(1 == 2, "Self build non-equal check");
  740. // And try to fail too
  741. Assert.Throws(typeof(AssertException), delegate
  742. {
  743. Assert.False(2 == 2, "Self build non-equal check failing");
  744. });
  745. }
  746. #endregion
  747. #region NotEqual
  748. /// <summary>
  749. /// Not equal
  750. /// </summary>
  751. [Test]
  752. public void NotEqual()
  753. {
  754. // Just checking some ints
  755. Assert.NotEqual(1, 3);
  756. // reference types
  757. Assert.NotEqual(new Object(), null);
  758. // and strings for non-equality
  759. Assert.NotEqual("TEst", "Test");
  760. // We expect an exception if equal values will check for non-equality
  761. Assert.Throws(typeof(AssertException), delegate
  762. {
  763. Assert.NotEqual("Test", "Test");
  764. });
  765. }
  766. #endregion
  767. #region NotNull
  768. /// <summary>
  769. /// NotNull
  770. /// </summary>
  771. [Test]
  772. public void NotNull()
  773. {
  774. Assert.NotNull("Hi");
  775. const string someObject = "nag nag";
  776. Assert.NotNull(someObject);
  777. // And try to fail too
  778. Assert.Throws(typeof(AssertException), delegate
  779. {
  780. Assert.NotNull(null);
  781. });
  782. }
  783. #endregion
  784. #region Null
  785. /// <summary>
  786. /// Null
  787. /// </summary>
  788. [Test]
  789. public void Null()
  790. {
  791. // For same strange reason null is really null
  792. Assert.Null(null);
  793. // Objects can be null too
  794. string someObject = null;
  795. Assert.Null(someObject);
  796. // And try to fail too
  797. Assert.Throws(typeof(AssertException), delegate
  798. {
  799. Assert.Null("Hi");
  800. });
  801. }
  802. #endregion
  803. #region True
  804. /// <summary>
  805. /// True
  806. /// </summary>
  807. [Test]
  808. public void True()
  809. {
  810. Assert.True(true, "True is always true");
  811. Assert.True(1 == 1, "Self build equal check");
  812. // And try to fail too
  813. Assert.Throws(typeof(AssertException), delegate
  814. {
  815. Assert.True(1 == 2, "Self build equal check failing");
  816. });
  817. }
  818. #endregion
  819. }
  820. }
  821. }