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