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