ExtendedAssert.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.IO;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. namespace System.UnitTesting
  8. {
  9. public static class ExtendedAssert
  10. {
  11. /// <summary>
  12. /// Verifies that the two specified objects are an instance of the same type.
  13. /// </summary>
  14. public static void IsInstanceOfSameType(object expected, object actual)
  15. {
  16. if (expected == null || actual == null)
  17. {
  18. Assert.AreSame(expected, actual);
  19. return;
  20. }
  21. Assert.AreSame(expected.GetType(), actual.GetType());
  22. }
  23. public static void ContainsLines(string value, params string[] lines)
  24. {
  25. StringReader reader = new StringReader(value);
  26. int count = 0;
  27. string line;
  28. while ((line = reader.ReadLine()) != null)
  29. {
  30. if (count == lines.Length)
  31. {
  32. Assert.Fail();
  33. }
  34. StringAssert.Contains(line, lines[count]);
  35. count++;
  36. }
  37. Assert.AreEqual(lines.Length, count, "Expectation: {0}; Result: {1}", String.Join(Environment.NewLine, lines), value);
  38. }
  39. public static void EnumsContainSameValues<TEnum1, TEnum2>()
  40. where TEnum1 : struct
  41. where TEnum2 : struct
  42. {
  43. EnumsContainSameValuesCore<TEnum1, TEnum2>();
  44. EnumsContainSameValuesCore<TEnum2, TEnum1>();
  45. }
  46. private static void EnumsContainSameValuesCore<TEnum1, TEnum2>()
  47. where TEnum1 : struct
  48. where TEnum2 : struct
  49. {
  50. var values = TestServices.GetEnumValues<TEnum1>();
  51. foreach (TEnum1 value in values)
  52. {
  53. string name1 = Enum.GetName(typeof(TEnum1), value);
  54. string name2 = Enum.GetName(typeof(TEnum2), value);
  55. Assert.AreEqual(name1, name2, "{0} contains a value that {1} does not have. These enums need to be in sync.", typeof(TEnum1), typeof(TEnum2));
  56. }
  57. }
  58. }
  59. }