Update PointConverter.cs
[mono.git] / mcs / class / System.XML / Test / System.Xml.Xsl / standalone_tests / xslttest.cs
1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.XPath;
7 using System.Xml.Xsl;
8 using NUnit.Core;
9 using NUnit.Framework;
10
11 namespace MonoTests.oasis_xslt {
12         public class SuiteBuilder {
13                 #region test list fields
14                 IDictionary expectedExceptions = new Hashtable ();
15                 ArrayList skipTargets = new ArrayList ();
16                 ArrayList knownFailures = new ArrayList ();
17                 ArrayList fixmeList = new ArrayList ();
18                 #endregion
19         
20                 TestSuite _suite;
21                 SuiteBuilder (TestSuite suite)
22                 {
23                         _suite = suite;
24                 }
25
26                 void ReadLists ()
27                 {
28                         string exceptionsFilename = Path.Combine (EnvOptions.OutputDir, "res-exceptions.lst");
29
30                         Helpers.ReadStrings (skipTargets, "ignore.lst");
31 #if TARGET_JVM
32                         Helpers.ReadStrings (knownFailures, "knownFailures.jvm.lst");
33 #else
34                         Helpers.ReadStrings (knownFailures, "knownFailures.lst");
35 #endif
36                         Helpers.ReadStrings (fixmeList, "fixme.lst");
37                         ArrayList exceptionsArray = new ArrayList();
38                         Helpers.ReadStrings (exceptionsArray, exceptionsFilename);
39                         foreach (string s in exceptionsArray) {
40                                 string [] halves = s.Split ('\t');
41                                 expectedExceptions [halves[0]] = halves[1];
42                         }
43                 }
44
45                 public void Build ()
46                 {
47 //                      if (Environment.GetEnvironmentVariables().Contains("START_DEBUG"))
48 //                              System.Diagnostics.Debugger.Launch ();
49                         ReadLists ();
50                         XmlDocument whole = new XmlDocument ();
51                         whole.Load (@"testsuite/TESTS/catalog-fixed.xml");
52
53                         foreach (XmlElement testCase in whole.SelectNodes ("test-suite/test-catalog/test-case")) {
54                                 string testid = testCase.GetAttribute ("id");
55
56                                 if (skipTargets.Contains (testid))
57                                         continue;
58
59                                 CatalogTestCase ctc = new CatalogTestCase(EnvOptions.OutputDir, testCase);
60                                 if (!ctc.Process ())
61                                         continue;
62
63                                 SingleTestTransform stt = new SingleTestTransform (ctc);
64
65                                 string expectedException = (string) expectedExceptions[testid];
66                                 bool isKnownFailure = knownFailures.Contains (testid) || fixmeList.Contains (testid);
67
68                                 _suite.Add (new TestFromCatalog (testid, stt, expectedException,
69                                         EnvOptions.InverseResults, isKnownFailure));
70                         }
71                 }
72
73                 [Suite]
74                 public static TestSuite Suite { 
75                         get {
76                                 TestSuite suite = new TestSuite ("MonoTests.oasis_xslt.SuiteBuilder");
77                                 SuiteBuilder builder = new SuiteBuilder (suite);
78                                 builder.Build ();
79                                 return suite;
80                         }
81                 }
82         }
83
84         class TestFromCatalog: NUnit.Core.TestCase {
85                 bool _inverseResult;
86                 string _testid;
87                 string _expectedException;
88                 SingleTestTransform _transform;
89
90                 public TestFromCatalog (string testid, SingleTestTransform transform,
91                         string expectedException, bool inverseResult, bool isKnownFailure)
92                         :base (null, testid)
93                 {
94                         _testid = testid;
95                         _expectedException = expectedException;
96                         _transform = transform;
97                         _inverseResult = inverseResult;
98                         
99                         ArrayList arr = new ArrayList ();
100                         if (isKnownFailure) {
101                                 arr.Add ("KnownFailures");
102                                 this.IsExplicit = true;
103                         }
104                         else
105                                 arr.Add ("Clean");
106                         Categories = arr;
107                 }
108
109                 static string EscapeString (string res)
110                 {
111                         MemoryStream s = new MemoryStream ();
112                         XmlTextWriter w = new XmlTextWriter (s, System.Text.Encoding.ASCII);
113                         w.WriteString (res);
114                         w.Close ();     
115                         
116                         StringBuilder sb = new StringBuilder (res.Length);
117                         byte [] arr = s.ToArray ();
118                         foreach (byte b in arr)
119                                 sb.Append (Convert.ToChar (b));
120
121                         return sb.ToString ();
122                 }
123
124                 string CompareResult (string actual, string expected, CatalogTestCase.CompareType compare)
125                 {
126                         //TODO: add html comparison
127                         if (compare== CatalogTestCase.CompareType.XML) {
128                                 try {
129                                         XmlDocument actDoc = new XmlDocument();
130                                         XmlDocument expDoc = new XmlDocument();
131                                         actDoc.LoadXml (actual);
132                                         expDoc.LoadXml (expected);
133                                         XmlCompare.XmlCompare cmp = new XmlCompare.XmlCompare(XmlCompare.XmlCompare.Flags.IgnoreAttribOrder);
134                                         if (cmp.AreEqual (actDoc, expDoc)) {
135                                                 return null;
136                                         }
137                                 }
138                                 catch (Exception ex) {
139                                         //could not compare as xml, fallback to text
140                                         if (actual == expected)
141                                                 return null;
142                                 }
143                         }
144                         else
145                                 if (actual == expected)
146                                         return null;
147
148                         string res = "Different.\nActual*****\n"+actual+"\nReference*****\n"+expected;
149                         return EscapeString (res);
150                 }
151
152                 string CompareException (Exception actual, string testid)
153                 {
154                         if (_expectedException == null)
155                                 return "Unexpected exception: " + actual.ToString ();
156
157                         string actualType = actual.GetType ().ToString ();
158                         if (actualType != _expectedException)
159                                 return "Different exception thrown.\nActual*****\n"+actualType+
160                                         "\nReference*****\n"+_expectedException;
161
162             return null;
163                 }
164
165                 void ReportResult (string failureMessage, string stackTrace, TestCaseResult res)
166                 {
167                         if (_inverseResult) {
168                                 if (failureMessage != null)
169                                         res.Success ();
170                                 else
171                                         res.Failure ("The following test was FIXED: "+_testid, null);
172                         }
173                         else {
174                                 if (failureMessage != null)
175                                         res.Failure (failureMessage, stackTrace);
176                                 else
177                                         res.Success ();
178                         }
179                 }
180
181                 public override void Run (TestCaseResult res)
182                 {
183                         _transform.RunTest ();
184
185                         string failureMessage;
186                         string stackTrace = null;
187                         if (_transform.Succeeded) {
188                                 try {
189                                         using (StreamReader sr = new StreamReader (_transform.TestCase.OutFile))
190                                                 failureMessage = CompareResult (_transform.Result, sr.ReadToEnd ().Replace ("\r\n", "\n"), _transform.TestCase.Compare);
191                                 }
192                                 catch {
193                                         //if there is no reference result because of expectedException, we
194                                         //are OK, otherwise, rethrow
195                                         if (_expectedException!=null)
196                                                 failureMessage = null;
197                                         else {
198                                                 Console.WriteLine (_transform.TestCase.OutFile);
199                                                 Console.WriteLine ("ERROR: No reference result, and no expected exception.");
200                                                 throw;
201                                         }
202                                 }
203                         }
204                         else {
205                                 failureMessage = CompareException (_transform.Exception, _testid);
206                                 stackTrace = _transform.Exception.StackTrace;
207                         }
208
209                         ReportResult (failureMessage, stackTrace, res);
210                 }
211         }
212 }