Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XsltTestUtils.cs « standalone_tests « System.Xml.Xsl « Test « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3cbc75647ba74d864dbfc39b4e19a1f153c6ec23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Collections;
using System.IO;

namespace MonoTests.oasis_xslt {
	class EnvOptions {
		static readonly bool useDomStyle;
		static readonly bool useDomInstance;
		static readonly string outputDir;
		static readonly bool whitespaceStyle;
		static readonly bool whitespaceInstance;
		static readonly bool inverseResults;

		public static bool UseDomStyle {
			get {return useDomStyle;}
		}
		public static bool UseDomInstance {
			get {return useDomInstance;}
		}
		public static string OutputDir {
			get {return outputDir;}
		}
		public static bool WhitespaceStyle {
			get {return whitespaceStyle;}
		}
		public static bool WhitespaceInstance {
			get {return whitespaceInstance;}
		}
		public static bool InverseResults {
			get {return inverseResults;}
		}

		static EnvOptions () {
			IDictionary env = System.Environment.GetEnvironmentVariables();
			if (env.Contains ("XSLTTEST_DOM")) {
				useDomStyle = true;
				useDomInstance = true;
			}
			if (env.Contains ("XSLTTEST_DOMXSL"))
				useDomStyle = true;
			if (env.Contains ("XSLTTEST_DOMINSTANCE"))
				useDomInstance = true;
			if (env.Contains ("XSLTTEST_WS")) {
				whitespaceStyle = true;
				whitespaceInstance = true;
			}
			if (env.Contains ("XSLTTEST_WSXSL"))
				whitespaceStyle = true;
			if (env.Contains ("XSLTTEST_WSSRC"))
				whitespaceInstance = true;
	
			if (env.Contains ("XSLTTEST_INVERSE_RESULTS"))
				inverseResults = true;

			if (useDomStyle || useDomInstance)
				outputDir = "domresults";
			else
				outputDir = "results";
		}
	}

	class Helpers
	{
		public static void ReadStrings (ArrayList array, string filename)
		{
			if (!File.Exists (filename))
				return;

			using (StreamReader reader = new StreamReader (filename)) {
				foreach (string s_ in reader.ReadToEnd ().Split ("\n".ToCharArray ())) {
					string s = s_.Trim ();
					if (s.Length > 0)
						array.Add (s);
				}
			}
		}
	}

	class CatalogTestCase
	{
		string _stylesheet;
		string _srcxml;
		string _outfile;
		public enum CompareType {
			Text,
			HTML,
			XML
		}
		CompareType _compare;
		XmlElement _testCase;
		string _outputDir;

		public CatalogTestCase (string outputDir, XmlElement testCase)
		{
			_testCase = testCase;
			_outputDir = outputDir;
		}

		public bool Process ()
		{
			string relPath = GetRelPath ();

			string path = Path.Combine (Path.Combine ("testsuite", "TESTS"), relPath);
			string outputPath = Path.Combine (_outputDir, relPath);
			if (!Directory.Exists (outputPath))
				Directory.CreateDirectory (outputPath);

			//FIXME: this ignores negative tests. Read README if you want to fix it
			XmlNode scenario = _testCase.SelectSingleNode ("scenario[@operation='standard']");
			if (scenario == null)
				return false;

			ProcessScenario (path, outputPath, scenario);
			return true;
		}

		string GetRelPath ()
		{
			string filePath = _testCase.SelectSingleNode ("file-path").InnerText;
			string submitter = _testCase.SelectSingleNode ("./parent::test-catalog/@submitter").InnerText;
			if (submitter == "Lotus")
				return Path.Combine ("Xalan_Conformance_Tests", filePath);
			else if (submitter == "Microsoft")
				return Path.Combine ("MSFT_Conformance_Tests", filePath);
			else
				throw new System.Exception ("unknown submitter in the catalog");
		}


		void ProcessScenario (string path, string outputPath, XmlNode scenario)
		{ 
			string stylesheetBase = scenario.SelectSingleNode ("input-file[@role='principal-stylesheet']").InnerText;
			_stylesheet = Path.Combine (path, stylesheetBase);
			if (!File.Exists (_stylesheet)) {
				using (StreamWriter wr = new StreamWriter ("missing.lst", true))
					wr.WriteLine (_stylesheet);
			}

			_srcxml = Path.Combine (path, scenario.SelectSingleNode ("input-file[@role='principal-data']").InnerText);
			XmlNode outputNode = scenario.SelectSingleNode ("output-file[@role='principal']");
			if (outputNode != null) {
				_outfile = Path.Combine (outputPath, outputNode.InnerText);
				switch (outputNode.Attributes ["compare"].Value) {
				case "XML":
					_compare = CompareType.XML;
					break;
				case "HTML":
					_compare = CompareType.HTML;
					break;
				default:
					_compare = CompareType.Text;
					break;
				}
			}
			else {
				_outfile = null;
				_compare = CompareType.Text;
			}
		}

		public CompareType Compare {
			get {return _compare;}
		}

		public string StyleSheet {
			get {return _stylesheet;}
		}

		public string SrcXml {
			get {return _srcxml;}
		}

		public string OutFile {
			get {return _outfile;}
		}
	}

	class SingleTestTransform
	{
		CatalogTestCase _testCase;

		public SingleTestTransform (CatalogTestCase testCase)
		{
			_testCase = testCase;
		}

		string _result;
		public string Result {
			get {return _result;}
		}

		System.Exception _exception;
		public System.Exception Exception {
			get {return _exception;}
		}

		public bool Succeeded {
			get {return this.Exception == null;}
		}

		public CatalogTestCase TestCase {
			get {return _testCase;}
		}

		XslTransform LoadTransform ()
		{
			XslTransform trans = new XslTransform ();

			if (EnvOptions.UseDomStyle) {
				XmlDocument styledoc = new XmlDocument ();
				if (EnvOptions.WhitespaceStyle)
					styledoc.PreserveWhitespace = true;
				styledoc.Load (_testCase.StyleSheet);
				trans.Load (styledoc, null, null);
			} else
				trans.Load (new XPathDocument (
					_testCase.StyleSheet,
					EnvOptions.WhitespaceStyle ? XmlSpace.Preserve :
					XmlSpace.Default),
					null, null);
			return trans;
		}

		IXPathNavigable LoadInput ()
		{
			XmlTextReader xtr=null;
			try {
				xtr = new XmlTextReader (_testCase.SrcXml);
				XmlValidatingReader xvr = new XmlValidatingReader (xtr);
				xvr.ValidationType = ValidationType.None;
				IXPathNavigable input = null;
				if (EnvOptions.UseDomInstance) {
					XmlDocument dom = new XmlDocument ();
					if (EnvOptions.WhitespaceInstance)
						dom.PreserveWhitespace = true;
					dom.Load (xvr);
					input = dom;
				} else {
					input = new XPathDocument (xvr,
						EnvOptions.WhitespaceStyle ? XmlSpace.Preserve :
						XmlSpace.Default);
				}
				return input;
			}
			finally {
				if (xtr!=null)
					xtr.Close ();
			}
		}

		public void RunTest ()
		{
			try {
				XslTransform trans = LoadTransform ();
				IXPathNavigable input = LoadInput ();
				using (StringWriter sw = new StringWriter ()) {
					trans.Transform (input, null, sw, null);
					_result = sw.ToString ().Replace ("\r\n", "\n");
				}
			}
			catch (System.Exception e) {
				_exception = e;
			}
		}
	}
}