4 namespace MonoTests.stand_alone.WebHarness
7 /// Summary description for XmlComparer.
9 public class XmlComparer
19 string lastCompare = "";
21 string _expected = "";
23 public XmlComparer (Flags flags, bool ignoreWS)
26 this.ignoreWS = ignoreWS;
29 public XmlComparer (Flags flags) : this (flags, true)
33 public XmlComparer () : this (Flags.IgnoreAttribOrder)
37 public bool AreEqualAttribs (XmlAttributeCollection expected, XmlAttributeCollection actual)
39 if (expected.Count != actual.Count)
41 for (int i=0; i<expected.Count; i++) {
42 if ((flags & Flags.IgnoreAttribOrder) != 0) {
43 string ln = expected[i].LocalName;
44 string ns = expected[i].NamespaceURI;
45 string val = expected[i].Value;
46 _expected = ns+":"+ln+"="+val;
47 XmlAttribute atr2 = actual[ln, ns];
48 _actual = atr2 == null ? "<<missing>>" : ns + ":" + ln + "=" + atr2.Value;
49 if (atr2 == null || atr2.Value.Trim().ToLower() != val.Trim().ToLower())
52 if (expected [i].LocalName != actual [i].LocalName)
54 if (expected [i].NamespaceURI != actual [i].NamespaceURI)
56 if (expected [i].Value.Trim().ToLower() != actual [i].Value.Trim().ToLower())
63 public bool AreEqualNodeList (XmlNodeList expected, XmlNodeList actual)
65 if (expected.Count != actual.Count)
67 for (int i=0; i<expected.Count; i++) {
68 if (!AreEqual (expected[i], actual[i]))
74 public bool AreEqual (XmlNode expected, XmlNode actual)
76 lastCompare = expected.OuterXml + "\n" + actual.OuterXml;
77 _actual = actual.OuterXml;
78 _expected = expected.OuterXml;
79 // skip XmlDeclaration
80 if ((expected.NodeType == XmlNodeType.XmlDeclaration) &&
81 (actual.NodeType == XmlNodeType.XmlDeclaration))
83 if (expected.NodeType != actual.NodeType)
85 if (expected.LocalName != actual.LocalName)
87 if (expected.NamespaceURI != actual.NamespaceURI)
89 if (expected.Attributes != null && actual.Attributes != null) {
90 if (!AreEqualAttribs (expected.Attributes, actual.Attributes))
93 _actual = actual.OuterXml;
94 _expected = expected.OuterXml;
96 else //one of nodes has no attrs
97 if (expected.Attributes != null || actual.Attributes != null)
98 return false;//and another has some
99 if (!expected.HasChildNodes && !actual.HasChildNodes) {
100 string val1 = expected.Value;
101 string val2 = actual.Value;
102 if (ignoreWS) //ignore white spaces
105 val1 = val1.Trim().Replace("\r\n", "\n");
107 val2 = val2.Trim().Replace("\r\n", "\n");
111 else {//one of nodes has some children
112 if (!expected.HasChildNodes || !actual.HasChildNodes)
113 return false;//and another has none
114 return AreEqualNodeList (expected.ChildNodes, actual.ChildNodes);
118 public string LastCompare
120 get {return lastCompare;}
125 get { return _actual; }
128 public string Expected
130 get { return _expected; }