AssertExtensions.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. Exception exception = null;
  18. try {
  19. code ();
  20. } catch (Exception ex) {
  21. exception = ex;
  22. }
  23. if (exceptionType == null) {
  24. if (exception == null)
  25. Assert.Fail ("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
  26. message, Environment.NewLine);
  27. return;
  28. }
  29. if (exception == null || exception.GetType () != exceptionType)
  30. Assert.Fail ("{0}{1}Expected: {2}{1}But was: {3}{1}",
  31. message,
  32. Environment.NewLine,
  33. exceptionType,
  34. exception == null ? "null" : exception.GetType ().ToString ());
  35. }
  36. public static void Throws (AssertThrowsDelegate code, string message)
  37. {
  38. Throws (null, code, message);
  39. }
  40. }
  41. }