AssertExtensions.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 AreEqual (byte[] expected, byte[] data, string message)
  10. {
  11. if (expected == null) {
  12. if (data == null)
  13. return;
  14. Assert.Fail ("{0}{1}Expected: null{1}Got: byte array with {2} elements and of rank {3}{1}",
  15. message, Environment.NewLine, data.Length, data.Rank);
  16. }
  17. if (data == null)
  18. Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: null{1}",
  19. message, Environment.NewLine, expected.Length, expected.Rank);
  20. if (expected.Rank > 1)
  21. Assert.Fail ("Only single-dimensional arrays are supported.");
  22. if (expected.Rank != data.Rank || expected.Length != data.Length)
  23. Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: byte array with {4} elements and rank {5}{1}",
  24. message, Environment.NewLine, expected.Length, expected.Rank, data.Length, data.Rank);
  25. int max = expected.Length;
  26. for (int i = 0; i < max; i++) {
  27. if (expected[i] != data[i])
  28. Assert.Fail ("{0}{1}Arrays differ at index {2}.{1}Expected 0x{3:X} got 0x{4:X}{1}",
  29. message, Environment.NewLine, i, expected[i], data[i]);
  30. }
  31. }
  32. public static void Throws<ET> (AssertThrowsDelegate code, string message)
  33. {
  34. Throws(typeof(ET), code, message);
  35. }
  36. public static void Throws(Type exceptionType, AssertThrowsDelegate code, string message)
  37. {
  38. if (code == null)
  39. Assert.Fail("No code provided for the test.");
  40. Exception exception = null;
  41. try
  42. {
  43. code();
  44. }
  45. catch (Exception ex)
  46. {
  47. exception = ex;
  48. }
  49. if (exceptionType == null)
  50. {
  51. if (exception == null)
  52. Assert.Fail("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
  53. message, Environment.NewLine);
  54. return;
  55. }
  56. if (exception == null || exception.GetType() != exceptionType)
  57. Assert.Fail("{0}{1}Expected: {2}{1}But was: {3}{1}{4}{1}",
  58. message,
  59. Environment.NewLine,
  60. exceptionType,
  61. exception == null ? "no exception" : exception.GetType().ToString(),
  62. exception == null ? "no exception" : exception.ToString());
  63. }
  64. public static void Throws(AssertThrowsDelegate code, string message)
  65. {
  66. Throws(null, code, message);
  67. }
  68. }
  69. }