2 using System.Collections;
6 using System.Xml.XPath;
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 ();
20 public SuiteBuilder ()
26 string exceptionsFilename = Path.Combine (EnvOptions.OutputDir, "res-exceptions.lst");
28 Helpers.ReadStrings (skipTargets, "ignore.lst");
30 Helpers.ReadStrings (knownFailures, "knownFailures.jvm.lst");
32 Helpers.ReadStrings (knownFailures, "knownFailures.lst");
34 Helpers.ReadStrings (fixmeList, "fixme.lst");
35 ArrayList exceptionsArray = new ArrayList();
36 Helpers.ReadStrings (exceptionsArray, exceptionsFilename);
37 foreach (string s in exceptionsArray) {
38 string [] halves = s.Split ('\t');
39 expectedExceptions [halves[0]] = halves[1];
43 void Build (TestSuite suite)
45 // if (Environment.GetEnvironmentVariables().Contains("START_DEBUG"))
46 // System.Diagnostics.Debugger.Launch ();
48 XmlDocument whole = new XmlDocument ();
49 whole.Load (@"testsuite/TESTS/catalog-fixed.xml");
51 foreach (XmlElement testCase in whole.SelectNodes ("test-suite/test-catalog/test-case")) {
52 string testid = testCase.GetAttribute ("id");
54 if (skipTargets.Contains (testid))
57 CatalogTestCase ctc = new CatalogTestCase(EnvOptions.OutputDir, testCase);
61 SingleTestTransform stt = new SingleTestTransform (ctc);
63 string expectedException = (string) expectedExceptions[testid];
64 bool isKnownFailure = knownFailures.Contains (testid) || fixmeList.Contains (testid);
66 suite.Add (new TestFromCatalog (testid, stt, expectedException,
67 EnvOptions.InverseResults, isKnownFailure));
71 static object lock_obj = new object ();
72 static TestSuite _suite;
75 public static TestSuite Suite {
77 if (_suite == null) { lock (lock_obj) {
78 TestSuite suite = new TestSuite ("MonoTests.oasis_xslt.SuiteBuilder");
79 new SuiteBuilder ().Build (suite);
87 class TestFromCatalog: NUnit.Core.TestCase {
90 string _expectedException;
91 SingleTestTransform _transform;
93 public TestFromCatalog (string testid, SingleTestTransform transform,
94 string expectedException, bool inverseResult, bool isKnownFailure)
98 _expectedException = expectedException;
99 _transform = transform;
100 _inverseResult = inverseResult;
102 ArrayList arr = new ArrayList ();
103 if (isKnownFailure) {
104 arr.Add ("KnownFailures");
105 //this.IsExplicit = true;
112 static string EscapeString (string res)
114 MemoryStream s = new MemoryStream ();
115 XmlTextWriter w = new XmlTextWriter (s, System.Text.Encoding.ASCII);
119 StringBuilder sb = new StringBuilder (res.Length);
120 byte [] arr = s.ToArray ();
121 foreach (byte b in arr)
122 sb.Append (Convert.ToChar (b));
124 return sb.ToString ();
127 string CompareResult (string actual, string expected, CatalogTestCase.CompareType compare)
129 //TODO: add html comparison
130 if (compare== CatalogTestCase.CompareType.XML) {
132 XmlDocument actDoc = new XmlDocument();
133 XmlDocument expDoc = new XmlDocument();
134 actDoc.LoadXml (actual);
135 expDoc.LoadXml (expected);
136 XmlCompare.XmlCompare cmp = new XmlCompare.XmlCompare(XmlCompare.XmlCompare.Flags.IgnoreAttribOrder);
137 if (cmp.AreEqual (actDoc, expDoc)) {
141 catch (Exception ex) {
142 //could not compare as xml, fallback to text
143 if (actual == expected)
148 if (actual == expected)
151 string res = "Different.\nActual*****\n"+actual+"\nReference*****\n"+expected;
152 return EscapeString (res);
155 string CompareException (Exception actual, string testid)
157 if (_expectedException == null)
158 return "Unexpected exception: " + actual.ToString ();
160 string actualType = actual.GetType ().ToString ();
161 if (actualType != _expectedException)
162 return "Different exception thrown.\nActual*****\n"+actualType+
163 "\nReference*****\n"+_expectedException;
168 void ReportResult (string failureMessage, string stackTrace, TestCaseResult res)
170 if (_inverseResult) {
171 if (failureMessage != null)
174 res.Failure ("The following test was FIXED: "+_testid, null);
177 if (failureMessage != null)
178 res.Failure (failureMessage, stackTrace);
184 public override void Run (TestCaseResult res)
186 _transform.RunTest ();
188 string failureMessage;
189 string stackTrace = null;
190 if (_transform.Succeeded) {
192 using (StreamReader sr = new StreamReader (_transform.TestCase.OutFile))
193 failureMessage = CompareResult (_transform.Result, sr.ReadToEnd ().Replace ("\r\n", "\n"), _transform.TestCase.Compare);
196 //if there is no reference result because of expectedException, we
197 //are OK, otherwise, rethrow
198 if (_expectedException!=null)
199 failureMessage = null;
201 Console.WriteLine (_transform.TestCase.OutFile);
202 Console.WriteLine ("ERROR: No reference result, and no expected exception.");
208 failureMessage = CompareException (_transform.Exception, _testid);
209 stackTrace = _transform.Exception.StackTrace;
212 ReportResult (failureMessage, stackTrace, res);