2 // MonoTests.System.Xml.XPathNavigatorTests
5 // Jason Diamond <jason@injektilo.org>
6 // Martin Willemoes Hansen <mwh@sysrq.dk>
8 // (C) 2002 Jason Diamond
9 // (C) 2003 Martin Willemoes Hansen
15 using System
.Xml
.XPath
;
17 using NUnit
.Framework
;
19 namespace MonoTests
.System
.Xml
22 public class XPathNavigatorTests
: Assertion
25 XPathNavigator navigator
;
28 public void GetReady ()
30 document
= new XmlDocument ();
34 public void CreateNavigator ()
36 document
.LoadXml ("<foo />");
37 navigator
= document
.CreateNavigator ();
38 AssertNotNull (navigator
);
42 public void PropertiesOnDocument ()
44 document
.LoadXml ("<foo:bar xmlns:foo='#foo' />");
45 navigator
= document
.CreateNavigator ();
47 AssertEquals (XPathNodeType
.Root
, navigator
.NodeType
);
48 AssertEquals (String
.Empty
, navigator
.Name
);
49 AssertEquals (String
.Empty
, navigator
.LocalName
);
50 AssertEquals (String
.Empty
, navigator
.NamespaceURI
);
51 AssertEquals (String
.Empty
, navigator
.Prefix
);
52 Assert (!navigator
.HasAttributes
);
53 Assert (navigator
.HasChildren
);
54 Assert (!navigator
.IsEmptyElement
);
58 public void PropertiesOnElement ()
60 document
.LoadXml ("<foo:bar xmlns:foo='#foo' />");
61 navigator
= document
.DocumentElement
.CreateNavigator ();
63 AssertEquals (XPathNodeType
.Element
, navigator
.NodeType
);
64 AssertEquals ("foo:bar", navigator
.Name
);
65 AssertEquals ("bar", navigator
.LocalName
);
66 AssertEquals ("#foo", navigator
.NamespaceURI
);
67 AssertEquals ("foo", navigator
.Prefix
);
68 Assert (!navigator
.HasAttributes
);
69 Assert (!navigator
.HasChildren
);
70 Assert (navigator
.IsEmptyElement
);
74 public void PropertiesOnAttribute ()
76 document
.LoadXml ("<foo bar:baz='quux' xmlns:bar='#bar' />");
77 navigator
= document
.DocumentElement
.GetAttributeNode("baz", "#bar").CreateNavigator ();
79 AssertEquals (XPathNodeType
.Attribute
, navigator
.NodeType
);
80 AssertEquals ("bar:baz", navigator
.Name
);
81 AssertEquals ("baz", navigator
.LocalName
);
82 AssertEquals ("#bar", navigator
.NamespaceURI
);
83 AssertEquals ("bar", navigator
.Prefix
);
84 Assert (!navigator
.HasAttributes
);
85 Assert (!navigator
.HasChildren
);
86 Assert (!navigator
.IsEmptyElement
);
90 public void PropertiesOnNamespace ()
92 document
.LoadXml ("<root xmlns='urn:foo' />");
93 navigator
= document
.DocumentElement
.Attributes
[0].CreateNavigator ();
94 AssertEquals (XPathNodeType
.Namespace
, navigator
.NodeType
);
98 public void Navigation ()
100 document
.LoadXml ("<foo><bar /><baz /></foo>");
101 navigator
= document
.DocumentElement
.CreateNavigator ();
103 AssertEquals ("foo", navigator
.Name
);
104 Assert (navigator
.MoveToFirstChild ());
105 AssertEquals ("bar", navigator
.Name
);
106 Assert (navigator
.MoveToNext ());
107 AssertEquals ("baz", navigator
.Name
);
108 Assert (!navigator
.MoveToNext ());
109 AssertEquals ("baz", navigator
.Name
);
110 Assert (navigator
.MoveToPrevious ());
111 AssertEquals ("bar", navigator
.Name
);
112 Assert (!navigator
.MoveToPrevious ());
113 Assert (navigator
.MoveToParent ());
114 AssertEquals ("foo", navigator
.Name
);
115 navigator
.MoveToRoot ();
116 AssertEquals (XPathNodeType
.Root
, navigator
.NodeType
);
117 Assert (!navigator
.MoveToParent ());
118 AssertEquals (XPathNodeType
.Root
, navigator
.NodeType
);
119 Assert (navigator
.MoveToFirstChild ());
120 AssertEquals ("foo", navigator
.Name
);
121 Assert (navigator
.MoveToFirst ());
122 AssertEquals ("foo", navigator
.Name
);
123 Assert (navigator
.MoveToFirstChild ());
124 AssertEquals ("bar", navigator
.Name
);
125 Assert (navigator
.MoveToNext ());
126 AssertEquals ("baz", navigator
.Name
);
127 Assert (navigator
.MoveToFirst ());
128 AssertEquals ("bar", navigator
.Name
);
132 public void MoveToAndIsSamePosition ()
134 XmlDocument document1
= new XmlDocument ();
135 document1
.LoadXml ("<foo><bar /></foo>");
136 XPathNavigator navigator1a
= document1
.DocumentElement
.CreateNavigator ();
137 XPathNavigator navigator1b
= document1
.DocumentElement
.CreateNavigator ();
139 XmlDocument document2
= new XmlDocument ();
140 document2
.LoadXml ("<foo><bar /></foo>");
141 XPathNavigator navigator2
= document2
.DocumentElement
.CreateNavigator ();
143 AssertEquals ("foo", navigator1a
.Name
);
144 Assert (navigator1a
.MoveToFirstChild ());
145 AssertEquals ("bar", navigator1a
.Name
);
147 Assert (!navigator1b
.IsSamePosition (navigator1a
));
148 AssertEquals ("foo", navigator1b
.Name
);
149 Assert (navigator1b
.MoveTo (navigator1a
));
150 Assert (navigator1b
.IsSamePosition (navigator1a
));
151 AssertEquals ("bar", navigator1b
.Name
);
153 Assert (!navigator2
.IsSamePosition (navigator1a
));
154 AssertEquals ("foo", navigator2
.Name
);
155 Assert (!navigator2
.MoveTo (navigator1a
));
156 AssertEquals ("foo", navigator2
.Name
);
160 public void AttributeNavigation ()
162 document
.LoadXml ("<foo bar='baz' quux='quuux' />");
163 navigator
= document
.DocumentElement
.CreateNavigator ();
165 AssertEquals (XPathNodeType
.Element
, navigator
.NodeType
);
166 AssertEquals ("foo", navigator
.Name
);
167 Assert (navigator
.MoveToFirstAttribute ());
168 AssertEquals (XPathNodeType
.Attribute
, navigator
.NodeType
);
169 AssertEquals ("bar", navigator
.Name
);
170 AssertEquals ("baz", navigator
.Value
);
171 Assert (navigator
.MoveToNextAttribute ());
172 AssertEquals (XPathNodeType
.Attribute
, navigator
.NodeType
);
173 AssertEquals ("quux", navigator
.Name
);
174 AssertEquals ("quuux", navigator
.Value
);
178 public void ElementAndRootValues()
180 document
.LoadXml ("<foo><bar>baz</bar><quux>quuux</quux></foo>");
181 navigator
= document
.DocumentElement
.CreateNavigator ();
183 AssertEquals (XPathNodeType
.Element
, navigator
.NodeType
);
184 AssertEquals ("foo", navigator
.Name
);
185 //AssertEquals ("bazquuux", navigator.Value);
187 navigator
.MoveToRoot ();
188 //AssertEquals ("bazquuux", navigator.Value);
192 public void DocumentWithXmlDeclaration ()
194 document
.LoadXml ("<?xml version=\"1.0\" standalone=\"yes\"?><Root><foo>bar</foo></Root>");
195 navigator
= document
.CreateNavigator ();
197 navigator
.MoveToRoot ();
198 navigator
.MoveToFirstChild ();
199 AssertEquals (XPathNodeType
.Element
, navigator
.NodeType
);
200 AssertEquals ("Root", navigator
.Name
);
204 public void DocumentWithProcessingInstruction ()
206 document
.LoadXml ("<?xml-stylesheet href='foo.xsl' type='text/xsl' ?><foo />");
207 navigator
= document
.CreateNavigator ();
209 Assert (navigator
.MoveToFirstChild ());
210 AssertEquals (XPathNodeType
.ProcessingInstruction
, navigator
.NodeType
);
211 AssertEquals ("xml-stylesheet", navigator
.Name
);
213 XPathNodeIterator iter
= navigator
.SelectChildren (XPathNodeType
.Element
);
214 AssertEquals (0, iter
.Count
);
218 public void SelectFromOrphan ()
220 // SelectSingleNode () from node without parent.
221 XmlDocument doc
= new XmlDocument ();
222 doc
.LoadXml ("<foo><include id='original' /></foo>");
224 XmlNode node
= doc
.CreateElement ("child");
225 node
.InnerXml
= "<include id='new' />";
227 XmlNode new_include
= node
.SelectSingleNode ("//include");
228 AssertEquals ("<include id=\"new\" />", new_include
.OuterXml
);
230 // In this case 'node2' has parent 'node'
231 doc
= new XmlDocument ();
232 doc
.LoadXml ("<foo><include id='original' /></foo>");
234 node
= doc
.CreateElement ("child");
235 XmlNode node2
= doc
.CreateElement ("grandchild");
236 node
.AppendChild (node2
);
237 node2
.InnerXml
= "<include id='new' />";
239 new_include
= node2
.SelectSingleNode ("/");
240 AssertEquals ("<child><grandchild><include id=\"new\" /></grandchild></child>",
241 new_include
.OuterXml
);
245 public void XPathDocumentMoveToId ()
247 string dtd
= "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root id ID #REQUIRED>]>";
248 string xml
= dtd
+ "<root id='aaa'/>";
249 StringReader sr
= new StringReader (xml
);
250 XPathNavigator nav
= new XPathDocument (sr
).CreateNavigator ();
251 Assert ("ctor() from TextReader", nav
.MoveToId ("aaa"));
253 XmlValidatingReader xvr
= new XmlValidatingReader (xml
, XmlNodeType
.Document
, null);
254 nav
= new XPathDocument (xvr
).CreateNavigator ();
255 Assert ("ctor() from XmlValidatingReader", nav
.MoveToId ("aaa"));
257 // When it is XmlTextReader, XPathDocument fails.
258 XmlTextReader xtr
= new XmlTextReader (xml
, XmlNodeType
.Document
, null);
259 nav
= new XPathDocument (xtr
).CreateNavigator ();
260 Assert ("ctor() from XmlTextReader", !nav
.MoveToId ("aaa"));