34d5d282e7b8bb09fa03ccec95f125342300766a
[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                         Helpers.ReadStrings (knownFailures, "knownFailures.lst");
32                         Helpers.ReadStrings (fixmeList, "fixme.lst");
33                         ArrayList exceptionsArray = new ArrayList();
34                         Helpers.ReadStrings (exceptionsArray, exceptionsFilename);
35                         foreach (string s in exceptionsArray) {
36                                 string [] halves = s.Split ('\t');
37                                 expectedExceptions [halves[0]] = halves[1];
38                         }
39                 }
40
41                 public void Build ()
42                 {
43 //                      if (Environment.GetEnvironmentVariables().Contains("START_DEBUG"))
44 //                              System.Diagnostics.Debugger.Launch ();
45                         ReadLists ();
46                         XmlDocument whole = new XmlDocument ();
47                         whole.Load (@"testsuite/TESTS/catalog-fixed.xml");
48
49                         foreach (XmlElement testCase in whole.SelectNodes ("test-suite/test-catalog/test-case")) {
50                                 string testid = testCase.GetAttribute ("id");
51
52                                 if (skipTargets.Contains (testid))
53                                         continue;
54
55                                 CatalogTestCase ctc = new CatalogTestCase(EnvOptions.OutputDir, testCase);
56                                 if (!ctc.Process ())
57                                         continue;
58
59                                 SingleTestTransform stt = new SingleTestTransform (ctc);
60
61                                 string expectedException = (string) expectedExceptions[testid];
62                                 bool isKnownFailure = knownFailures.Contains (testid) || fixmeList.Contains (testid);
63
64                                 _suite.Add (new TestFromCatalog (testid, stt, expectedException,
65                                         EnvOptions.InverseResults, isKnownFailure));
66                         }
67                 }
68
69                 [Suite]
70                 public static TestSuite Suite { 
71                         get {
72                                 TestSuite suite = new TestSuite ("MonoTests.oasis_xslt.SuiteBuilder");
73                                 SuiteBuilder builder = new SuiteBuilder (suite);
74                                 builder.Build ();
75                                 return suite;
76                         }
77                 }
78         }
79
80         class TestFromCatalog: NUnit.Core.TestCase {
81                 bool _inverseResult;
82                 string _testid;
83                 string _expectedException;
84                 SingleTestTransform _transform;
85
86                 public TestFromCatalog (string testid, SingleTestTransform transform,
87                         string expectedException, bool inverseResult, bool isKnownFailure)
88                         :base (null, testid)
89                 {
90                         _testid = testid;
91                         _expectedException = expectedException;
92                         _transform = transform;
93                         _inverseResult = inverseResult;
94                         
95                         ArrayList arr = new ArrayList ();
96                         if (isKnownFailure) {
97                                 arr.Add ("KnownFailures");
98                                 this.IsExplicit = true;
99                         }
100                         else
101                                 arr.Add ("Clean");
102                         Categories = arr;
103                 }
104
105                 string CompareResult (string actual, string expected)
106                 {
107                         //TODO: add xml comparison
108                         if (actual == expected)
109                                 return null;
110                         else
111 #if !FAILURE_DETAILED_MESSAGE
112                                 return "Different.";
113 #else
114                                 return "Different.\nActual*****\n"+actual+"\nReference*****\n"+expected;
115 #endif
116                 }
117
118                 string CompareException (Exception actual, string testid)
119                 {
120                         if (_expectedException == null)
121                                 return "Unexpected exception: " + actual.ToString ();
122
123                         string actualType = actual.GetType ().ToString ();
124                         if (actualType != _expectedException)
125                                 return "Different exception thrown.\nActual*****\n"+actualType+
126                                         "\nReference*****\n"+_expectedException;
127
128             return null;
129                 }
130
131                 void ReportResult (string failureMessage, string stackTrace, TestCaseResult res)
132                 {
133                         if (_inverseResult) {
134                                 if (failureMessage != null)
135                                         res.Success ();
136                                 else
137                                         res.Failure ("The following test was FIXED: "+_testid, null);
138                         }
139                         else {
140                                 if (failureMessage != null)
141                                         res.Failure (failureMessage, stackTrace);
142                                 else
143                                         res.Success ();
144                         }
145                 }
146
147                 public override void Run (TestCaseResult res)
148                 {
149                         _transform.RunTest ();
150
151                         string failureMessage;
152                         string stackTrace = null;
153                         if (_transform.Succeeded) {
154                                 try {
155                                         using (StreamReader sr = new StreamReader (_transform.TestCase.OutFile))
156                                                 failureMessage = CompareResult (_transform.Result, sr.ReadToEnd ());
157                                 }
158                                 catch {
159                                         //if there is no reference result because of expectedException, we
160                                         //are OK, otherwise, rethrow
161                                         if (_expectedException!=null)
162                                                 failureMessage = null;
163                                         else {
164                                                 Console.WriteLine ("ERROR: No reference result, and no expected exception.");
165                                                 throw;
166                                         }
167                                 }
168                         }
169                         else {
170                                 failureMessage = CompareException (_transform.Exception, _testid);
171                                 stackTrace = _transform.Exception.StackTrace;
172                         }
173
174                         ReportResult (failureMessage, stackTrace, res);
175                 }
176         }
177 }