XmlComparer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Xml;
  3. using NUnit.Framework;
  4. namespace MonoTests
  5. {
  6. public class XmlComparer
  7. {
  8. [Flags]
  9. public enum Flags {
  10. IgnoreNone=0,
  11. IgnoreAttribOrder=1,
  12. }
  13. Flags flags;
  14. bool ignoreWS = true;
  15. string lastCompare = "";
  16. string _actual = "";
  17. string _expected = "";
  18. public XmlComparer (Flags flags, bool ignoreWS)
  19. {
  20. this.flags = flags;
  21. this.ignoreWS = ignoreWS;
  22. }
  23. public XmlComparer (Flags flags) : this (flags, true)
  24. {
  25. }
  26. public XmlComparer () : this (Flags.IgnoreAttribOrder)
  27. {
  28. }
  29. public bool AreEqualAttribs (XmlAttributeCollection expected, XmlAttributeCollection actual)
  30. {
  31. if (expected.Count != actual.Count)
  32. return false;
  33. for (int i=0; i<expected.Count; i++) {
  34. if ((flags & Flags.IgnoreAttribOrder) != 0) {
  35. string ln = expected[i].LocalName;
  36. string ns = expected[i].NamespaceURI;
  37. string val = expected[i].Value;
  38. _expected = ns+":"+ln+"="+val;
  39. XmlAttribute atr2 = actual[ln, ns];
  40. _actual = atr2 == null ? "<<missing>>" : ns + ":" + ln + "=" + atr2.Value;
  41. if (atr2 == null || atr2.Value.Trim().ToLower() != val.Trim().ToLower())
  42. return false;
  43. } else {
  44. if (expected [i].LocalName != actual [i].LocalName)
  45. return false;
  46. if (expected [i].NamespaceURI != actual [i].NamespaceURI)
  47. return false;
  48. if (expected [i].Value.Trim().ToLower() != actual [i].Value.Trim().ToLower())
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. public bool AreEqualNodeList (XmlNodeList expected, XmlNodeList actual)
  55. {
  56. if (expected.Count != actual.Count)
  57. return false;
  58. for (int i=0; i<expected.Count; i++) {
  59. if (!AreEqual (expected[i], actual[i]))
  60. return false;
  61. }
  62. return true;
  63. }
  64. public bool AreEqual (XmlNode expected, XmlNode actual)
  65. {
  66. lastCompare = expected.OuterXml + "\n" + actual.OuterXml;
  67. _actual = actual.OuterXml;
  68. _expected = expected.OuterXml;
  69. // skip XmlDeclaration
  70. if ((expected.NodeType == XmlNodeType.XmlDeclaration) &&
  71. (actual.NodeType == XmlNodeType.XmlDeclaration))
  72. return true;
  73. if (expected.NodeType != actual.NodeType)
  74. return false;
  75. if (expected.LocalName != actual.LocalName)
  76. return false;
  77. if (expected.NamespaceURI != actual.NamespaceURI)
  78. return false;
  79. if (expected.Attributes != null && actual.Attributes != null) {
  80. if (!AreEqualAttribs (expected.Attributes, actual.Attributes))
  81. return false;
  82. _actual = actual.OuterXml;
  83. _expected = expected.OuterXml;
  84. }
  85. else //one of nodes has no attrs
  86. if (expected.Attributes != null || actual.Attributes != null)
  87. return false;//and another has some
  88. if (!expected.HasChildNodes && !actual.HasChildNodes) {
  89. string val1 = expected.Value;
  90. string val2 = actual.Value;
  91. if (ignoreWS) //ignore white spaces
  92. {
  93. if (val1 != null)
  94. val1 = val1.Trim().Replace("\r\n", "\n");
  95. if (val2 != null)
  96. val2 = val2.Trim().Replace("\r\n", "\n");
  97. }
  98. return val1 == val2;
  99. }
  100. else {//one of nodes has some children
  101. if (!expected.HasChildNodes || !actual.HasChildNodes)
  102. return false;//and another has none
  103. return AreEqualNodeList (expected.ChildNodes, actual.ChildNodes);
  104. }
  105. }
  106. public string LastCompare
  107. {
  108. get {return lastCompare;}
  109. }
  110. public string Actual
  111. {
  112. get { return _actual; }
  113. }
  114. public string Expected
  115. {
  116. get { return _expected; }
  117. }
  118. public static void AssertAreEqual (string expected, string actual) {
  119. AssertAreEqual (expected, actual, String.Empty);
  120. }
  121. public static void AssertAreEqual (string expected, string actual, string msg) {
  122. try {
  123. XmlDocument or = new XmlDocument ();
  124. or.LoadXml (expected);
  125. XmlDocument dr = new XmlDocument ();
  126. dr.LoadXml (actual);
  127. XmlComparer comparer = new XmlComparer ();
  128. if (!comparer.AreEqual (or, dr))
  129. Assert.AreEqual (comparer.Expected, comparer.Actual, msg);
  130. }
  131. catch (AssertionException) {
  132. throw;
  133. }
  134. catch (Exception e) {
  135. //swallow e when there is XML error and fallback
  136. //to the text comparison
  137. Assert.AreEqual (expected, actual, msg);
  138. }
  139. }
  140. }
  141. }