AssertExtensions.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4. namespace MonoTests.Common
  5. {
  6. delegate void AssertThrowsDelegate ();
  7. static class AssertExtensions
  8. {
  9. public static void Throws<ET> (AssertThrowsDelegate code, string message)
  10. {
  11. Throws (typeof (ET), code, message);
  12. }
  13. public static void Throws (Type exceptionType, AssertThrowsDelegate code, string message)
  14. {
  15. if (code == null)
  16. Assert.Fail ("No code provided for the test.");
  17. if (exceptionType == null)
  18. Assert.Fail ("Exception type missing for the test.");
  19. Exception exception = null;
  20. try {
  21. code ();
  22. } catch (Exception ex) {
  23. exception = ex;
  24. }
  25. if (exception == null || exception.GetType () != exceptionType)
  26. Assert.Fail ("{0}{1}Expected: {2}{1}But was: {3}{1}",
  27. message,
  28. Environment.NewLine,
  29. exceptionType,
  30. exception == null ? "null" : exception.GetType ().ToString ());
  31. }
  32. }
  33. }