In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebTest / XmlComparer.cs
1 using System;
2 using System.Xml;
3
4 namespace MonoTests.stand_alone.WebHarness
5 {
6         /// <summary>
7         /// Summary description for XmlComparer.
8         /// </summary>
9         public class XmlComparer
10         {
11                 [Flags]
12                 public enum Flags {
13                         IgnoreNone=0,
14                         IgnoreAttribOrder=1,
15                 }
16                 Flags flags;
17                 bool ignoreWS = true;
18
19                 string lastCompare = "";
20                 string _actual = "";
21                 string _expected = "";
22
23                 public XmlComparer (Flags flags, bool ignoreWS) 
24                 {
25                         this.flags = flags;
26                         this.ignoreWS = ignoreWS;
27                 }
28
29                 public XmlComparer (Flags flags) : this (flags, true)
30                 {
31                 }
32
33                 public XmlComparer () : this (Flags.IgnoreAttribOrder)
34                 {
35                 }
36
37                 public bool AreEqualAttribs (XmlAttributeCollection expected, XmlAttributeCollection actual)
38                 {
39                         if (expected.Count != actual.Count)
40                                 return false;
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())
50                                                 return false;
51                                 } else {
52                                         if (expected [i].LocalName != actual [i].LocalName)
53                                                 return false;
54                                         if (expected [i].NamespaceURI != actual [i].NamespaceURI)
55                                                 return false;
56                                         if (expected [i].Value.Trim().ToLower() != actual [i].Value.Trim().ToLower())
57                                                 return false;
58                                 }
59                         }
60                         return true;
61                 }
62
63                 public bool AreEqualNodeList (XmlNodeList expected, XmlNodeList actual)
64                 {
65                         if (expected.Count != actual.Count)
66                                 return false;
67                         for (int i=0; i<expected.Count; i++) {
68                                 if (!AreEqual (expected[i], actual[i]))
69                                         return false;
70                         }
71                         return true;
72                 }
73
74                 public bool AreEqual (XmlNode expected, XmlNode actual)
75                 {
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))
82                                 return true;
83                         if (expected.NodeType != actual.NodeType)
84                                 return false;
85                         if (expected.LocalName != actual.LocalName)
86                                 return false;
87                         if (expected.NamespaceURI != actual.NamespaceURI)
88                                 return false;
89                         if (expected.Attributes != null && actual.Attributes != null) {
90                                 if (!AreEqualAttribs (expected.Attributes, actual.Attributes))
91                                         return false;
92
93                                 _actual = actual.OuterXml;
94                                 _expected = expected.OuterXml;
95                         }
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
103                                 { 
104                                         if (val1 != null)
105                                                 val1 = val1.Trim().Replace("\r\n", "\n");
106                                         if (val2 != null)
107                                                 val2 = val2.Trim().Replace("\r\n", "\n");
108                                 }
109                                 return val1 == val2;
110                         }
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);
115                         }
116                 }
117
118                 public string LastCompare 
119                 {
120                         get {return lastCompare;}
121                 }
122
123                 public string Actual
124                 {
125                         get { return _actual; }
126                 }
127
128                 public string Expected
129                 {
130                         get { return _expected; }
131                 }
132         }
133 }