File: SelectNodesTests.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (281 lines) | stat: -rw-r--r-- 8,730 bytes parent folder | download | duplicates (3)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// MonoTests.System.Xml.SelectNodesTests
//
// Author: Jason Diamond <jason@injektilo.org>
// Author: Martin Willemoes Hansen <mwh@sysrq.dk>
//
// (C) 2002 Jason Diamond
// (C) 2003 Martin Willemoes Hansen
//

using System;
using System.Xml;

using NUnit.Framework;

namespace MonoTests.System.Xml.XPath
{
	[TestFixture]
	public class SelectNodesTests : Assertion
	{

		[Test]
		public void Root ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo />");
			XmlNodeList nodes = document.SelectNodes ("/");
			AssertEquals (1, nodes.Count);
			AssertSame (document, nodes [0]);
		}

		[Test]
		public void DocumentElement ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo />");
			XmlNodeList nodes = document.SelectNodes ("/foo");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement, nodes [0]);
		}

		[Test]
		public void BadDocumentElement ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo />");
			XmlNodeList nodes = document.SelectNodes ("/bar");
			AssertEquals (0, nodes.Count);
		}

		[Test]
		public void ElementWildcard ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/*");
			AssertEquals (2, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
			AssertSame (document.DocumentElement.ChildNodes [1], nodes [1]);
		}

		[Test]
		public void OneChildElement ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
		}

		[Test]
		public void OneOtherChildElement ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/baz");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [1], nodes [0]);
		}

		[Test]
		public void TextNode ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo>bar</foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/text()");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
		}

		[Test]
		public void SplitTextNodes ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo>bar<baz />quux</foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/text()");
			AssertEquals (2, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
			AssertSame (document.DocumentElement.ChildNodes [2], nodes [1]);
		}

		[Test]
		public void AbbreviatedParentAxis ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar/..");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement, nodes [0]);
		}

		[Test]
		public void FullParentAxis ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar/parent::node()");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement, nodes [0]);
		}

		[Test]
		public void AbbreviatedAttributeAxis ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo bar='baz' />");
			XmlNodeList nodes = document.SelectNodes ("/foo/@bar");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.Attributes ["bar"], nodes [0]);
		}

		[Test]
		public void FullAttributeAxis ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo bar='baz' />");
			XmlNodeList nodes = document.SelectNodes ("/foo/attribute::bar");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.Attributes ["bar"], nodes [0]);
		}

		[Test]
		public void AbbreviatedAttributeWildcard ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo bar='baz' quux='quuux' />");
			XmlNodeList nodes = document.SelectNodes ("/foo/@*");
			AssertEquals (2, nodes.Count);
			// are the attributes guanteed to be ordered in the node list?
			AssertSame (document.DocumentElement.Attributes ["bar"], nodes [0]);
			AssertSame (document.DocumentElement.Attributes ["quux"], nodes [1]);
		}

		[Test]
		public void AttributeParent ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo bar='baz' />");
			XmlNodeList nodes = document.SelectNodes ("/foo/@bar/..");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement, nodes [0]);
		}
		
		[Test]
		public void UnionOperator ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar|/foo/baz");
			AssertEquals (2, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
			AssertSame (document.DocumentElement.ChildNodes [1], nodes [1]);
		}
		
		[Test]
		public void NodeWildcard ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar />baz<quux /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/node()");
			AssertEquals (3, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
			AssertSame (document.DocumentElement.ChildNodes [1], nodes [1]);
			AssertSame (document.DocumentElement.ChildNodes [2], nodes [2]);
		}

		[Test]
		public void PositionalPredicate ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar>1</bar><bar>2</bar></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar[1]");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [0], nodes [0]);
		}

		[Test]
		public void AllFollowingSiblings ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /><quux /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar/following-sibling::*");
			AssertEquals (2, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [1], nodes [0]);
			AssertSame (document.DocumentElement.ChildNodes [2], nodes [1]);
		}

		[Test]
		public void FollowingSiblingBaz ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /><quux /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar/following-sibling::baz");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [1], nodes [0]);
		}

		[Test]
		public void FollowingSiblingQuux ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo><bar /><baz /><quux /></foo>");
			XmlNodeList nodes = document.SelectNodes ("/foo/bar/following-sibling::quux");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement.ChildNodes [2], nodes [0]);
		}

		[Test]
		public void Union ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo />");
			XmlNodeList nodes = document.SelectNodes ("(/foo) | (/foo)");
			AssertEquals (1, nodes.Count);	// bug #27548
			AssertSame (document.DocumentElement, nodes [0]);
		}

		[Test]
		public void AlphabetDigitMixedName ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<foo1 />");
			XmlNodeList nodes = document.SelectNodes ("/foo1");
			AssertEquals (1, nodes.Count);
			AssertSame (document.DocumentElement, nodes [0]);
		}

		[Test]
		public void NamespaceSelect ()
		{
			XmlDocument document = new XmlDocument ();
			document.LoadXml ("<root xmlns=\"urn:foo1:foo2\"/>");
			XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
			nsmgr.AddNamespace("foons", "urn:foo1:foo2");
			XmlNodeList nodes = document.SelectNodes ("/foons:root", nsmgr);
			AssertEquals (1, nodes.Count);
		}

		[Test]
		public void NamespaceSelectWithNsElasure ()
		{
			XmlDocument doc = new XmlDocument ();

			doc.LoadXml ("<root xmlns='urn:root' xmlns:hoge='urn:hoge'><foo xmlns='urn:foo'><bar xmlns=''><baz/></bar></foo></root>");
			XmlNode n = doc.FirstChild.FirstChild.FirstChild.FirstChild; //baz
			XmlNodeList nl = n.SelectNodes ("namespace::*");
			AssertEquals ("hoge", nl [0].LocalName);
			AssertEquals ("xml", nl [1].LocalName);
			AssertEquals (2, nl.Count);

			n = doc.FirstChild.FirstChild; // foo
			nl = n.SelectNodes ("namespace::*");
			Console.WriteLine ("at foo::");
			AssertEquals ("xmlns", nl [0].LocalName);
			AssertEquals ("hoge", nl [1].LocalName);
			AssertEquals ("xml", nl [2].LocalName);
			AssertEquals (3, nl.Count);
		}
	}
}