BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Xml.Linq / Test / System.Xml.XPath / ExtensionsTest2.cs
1 //
2 // Authors:
3 //   Jason Diamond <jason@injektilo.org>
4 //   Martin Willemoes Hansen <mwh@sysrq.dk>
5 //   Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2002 Jason Diamond
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2004-2006 Novell, Inc.
10 // (C) 2003 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 //
33 // imported from XPathNavigatorTests
34 //
35
36 using System;
37 using System.IO;
38 using System.Linq;
39 using System.Xml;
40 using System.Xml.Linq;
41 using System.Xml.XPath;
42 using System.Xml.Xsl;
43
44 using NUnit.Framework;
45
46 namespace MonoTests.System.Xml
47 {
48         [TestFixture]
49         public class ExtensionsTest2
50         {
51                 XPathNavigator navigator;
52                 
53                 [Test]
54                 public void CreateNavigator ()
55                 {
56                         navigator = XDocument.Parse ("<foo />").CreateNavigator ();
57                         Assert.IsNotNull (navigator);
58                 }
59
60                 [Test]
61                 public void PropertiesOnDocument ()
62                 {
63                         navigator = XDocument.Parse ("<foo:bar xmlns:foo='#foo' />").CreateNavigator ();
64                         
65                         Assert.AreEqual (XPathNodeType.Root, navigator.NodeType, "#1");
66                         Assert.AreEqual (String.Empty, navigator.Name, "#2");
67                         Assert.AreEqual (String.Empty, navigator.LocalName, "#3");
68                         Assert.AreEqual (String.Empty, navigator.NamespaceURI, "#4");
69                         Assert.AreEqual (String.Empty, navigator.Prefix, "#5");
70                         Assert.IsTrue (!navigator.HasAttributes, "#6");
71                         Assert.IsTrue (navigator.HasChildren, "#7");
72                         Assert.IsTrue (!navigator.IsEmptyElement, "#8");
73                 }
74
75                 [Test]
76                 public void PropertiesOnElement ()
77                 {
78                         navigator = XDocument.Parse ("<foo:bar xmlns:foo='#foo' />").FirstNode.CreateNavigator ();
79                         
80                         Assert.AreEqual (XPathNodeType.Element, navigator.NodeType, "#1");
81                         Assert.AreEqual ("foo:bar", navigator.Name, "#2");
82                         Assert.AreEqual ("bar", navigator.LocalName, "#3");
83                         Assert.AreEqual ("#foo", navigator.NamespaceURI, "#4");
84                         Assert.AreEqual ("foo", navigator.Prefix, "#5");
85                         Assert.IsTrue (!navigator.HasAttributes, "#6");
86                         Assert.IsTrue (!navigator.HasChildren, "#7");
87                         Assert.IsTrue (navigator.IsEmptyElement, "#8");
88                 }
89
90                 [Test]
91                 public void Navigation ()
92                 {
93                         navigator = XDocument.Parse ("<foo><bar /><baz /></foo>").FirstNode.CreateNavigator ();
94                         
95                         Assert.AreEqual ("foo", navigator.Name, "#1");
96                         Assert.IsTrue (navigator.MoveToFirstChild (), "#2");
97                         Assert.AreEqual ("bar", navigator.Name, "#3");
98                         Assert.IsTrue (navigator.MoveToNext (), "#4");
99                         Assert.AreEqual ("baz", navigator.Name, "#5");
100                         Assert.IsTrue (!navigator.MoveToNext (), "#6");
101                         Assert.AreEqual ("baz", navigator.Name, "#7");
102                         Assert.IsTrue (navigator.MoveToPrevious (), "#8");
103                         Assert.AreEqual ("bar", navigator.Name, "#9");
104                         Assert.IsTrue (!navigator.MoveToPrevious (), "#10");
105                         Assert.IsTrue (navigator.MoveToParent (), "#11");
106                         Assert.AreEqual ("foo", navigator.Name, "#12");
107                         navigator.MoveToRoot ();
108                         Assert.AreEqual (XPathNodeType.Root, navigator.NodeType, "#13");
109                         Assert.IsTrue (!navigator.MoveToParent (), "#14");
110                         Assert.AreEqual (XPathNodeType.Root, navigator.NodeType, "#15");
111                         Assert.IsTrue (navigator.MoveToFirstChild (), "#16");
112                         Assert.AreEqual ("foo", navigator.Name, "#17");
113                         Assert.IsTrue (navigator.MoveToFirst (), "#18");
114                         Assert.AreEqual ("foo", navigator.Name, "#19");
115                         Assert.IsTrue (navigator.MoveToFirstChild (), "#20");
116                         Assert.AreEqual ("bar", navigator.Name, "#21");
117                         Assert.IsTrue (navigator.MoveToNext (), "#22");
118                         Assert.AreEqual ("baz", navigator.Name, "#23");
119                         Assert.IsTrue (navigator.MoveToFirst (), "#24");
120                         Assert.AreEqual ("bar", navigator.Name, "#25");
121                 }
122
123                 [Test]
124                 [Category ("NotDotNet")] // fails to differentiate document instances
125                 public void MoveToAndIsSamePosition ()
126                 {
127                         var doc1 = XDocument.Parse ("<foo><bar /></foo>");
128                         XPathNavigator navigator1a = doc1.FirstNode.CreateNavigator ();
129                         XPathNavigator navigator1b = doc1.FirstNode.CreateNavigator ();
130
131                         var doc2 = XDocument.Parse ("<foo><bar /></foo>");
132                         XPathNavigator navigator2 = doc2.FirstNode.CreateNavigator ();
133
134                         Assert.AreEqual ("foo", navigator1a.Name, "#1");
135                         Assert.IsTrue (navigator1a.MoveToFirstChild (), "#2");
136                         Assert.AreEqual ("bar", navigator1a.Name, "#3");
137
138                         Assert.IsTrue (!navigator1b.IsSamePosition (navigator1a), "#4");
139                         Assert.AreEqual ("foo", navigator1b.Name, "#5");
140                         Assert.IsTrue (navigator1b.MoveTo (navigator1a), "#6");
141                         Assert.IsTrue (navigator1b.IsSamePosition (navigator1a), "#7");
142                         Assert.AreEqual ("bar", navigator1b.Name, "#8");
143
144                         Assert.IsTrue (!navigator2.IsSamePosition (navigator1a), "#9");
145                         Assert.AreEqual ("foo", navigator2.Name, "#10");
146                         Assert.IsFalse (navigator2.MoveTo (navigator1a), "#11");
147                         Assert.AreEqual ("foo", navigator2.Name, "#12");
148                 }
149
150                 [Test]
151                 public void AttributeNavigation ()
152                 {
153                         navigator = XDocument.Parse ("<foo bar='baz' quux='quuux' />").FirstNode.CreateNavigator ();
154
155                         Assert.AreEqual (XPathNodeType.Element, navigator.NodeType, "#1");
156                         Assert.AreEqual ("foo", navigator.Name, "#2");
157                         Assert.IsTrue (navigator.MoveToFirstAttribute (), "#3");
158                         Assert.AreEqual (XPathNodeType.Attribute, navigator.NodeType, "#4");
159                         Assert.AreEqual ("bar", navigator.Name, "#5");
160                         Assert.AreEqual ("baz", navigator.Value, "#6");
161                         Assert.IsTrue (navigator.MoveToNextAttribute (), "#7");
162                         Assert.AreEqual (XPathNodeType.Attribute, navigator.NodeType, "#8");
163                         Assert.AreEqual ("quux", navigator.Name, "#9");
164                         Assert.AreEqual ("quuux", navigator.Value, "#10");
165                 }
166
167                 [Test]
168                 public void ElementAndRootValues()
169                 {
170                         navigator = XDocument.Parse ("<foo><bar>baz</bar><quux>quuux</quux></foo>").FirstNode.CreateNavigator ();
171
172                         Assert.AreEqual (XPathNodeType.Element, navigator.NodeType, "#1");
173                         Assert.AreEqual ("foo", navigator.Name, "#2");
174                         //Assert.AreEqual ("bazquuux", navigator.Value, "#3");
175
176                         navigator.MoveToRoot ();
177                         //Assert.AreEqual ("bazquuux", navigator.Value, "#4");
178                 }
179
180                 [Test]
181                 public void DocumentWithXmlDeclaration ()
182                 {
183                         navigator = XDocument.Parse ("<?xml version=\"1.0\" standalone=\"yes\"?><Root><foo>bar</foo></Root>").CreateNavigator ();
184
185                         navigator.MoveToRoot ();
186                         navigator.MoveToFirstChild ();
187                         Assert.AreEqual (XPathNodeType.Element, navigator.NodeType, "#1");
188                         Assert.AreEqual ("Root", navigator.Name, "#2");
189                 }
190
191                 [Test]
192                 public void DocumentWithProcessingInstruction ()
193                 {
194                         navigator = XDocument.Parse ("<?xml-stylesheet href='foo.xsl' type='text/xsl' ?><foo />").CreateNavigator ();
195
196                         Assert.IsTrue (navigator.MoveToFirstChild ());
197                         Assert.AreEqual (XPathNodeType.ProcessingInstruction, navigator.NodeType, "#1");
198                         Assert.AreEqual ("xml-stylesheet", navigator.Name, "#2");
199
200                         XPathNodeIterator iter = navigator.SelectChildren (XPathNodeType.Element);
201                         Assert.AreEqual (0, iter.Count, "#3");
202                 }
203
204                 /*
205                 [Test]
206                 public void SelectFromOrphan ()
207                 {
208                         // SelectSingleNode () from node without parent.
209                         XmlDocument doc = new XmlDocument ();
210                         doc.LoadXml ("<foo><include id='original' /></foo>");
211
212                         XmlNode node = doc.CreateElement ("child");
213                         node.InnerXml = "<include id='new' />";
214
215                         XmlNode new_include = node.SelectSingleNode ("//include");
216                         Assert.AreEqual ("<include id=\"new\" />", new_include.OuterXml, "#1");
217
218                         // In this case 'node2' has parent 'node'
219                         doc = new XmlDocument ();
220                         doc.LoadXml ("<foo><include id='original' /></foo>");
221
222                         node = doc.CreateElement ("child");
223                         XmlNode node2 = doc.CreateElement ("grandchild");
224                         node.AppendChild (node2);
225                         node2.InnerXml = "<include id='new' />";
226
227                         new_include = node2.SelectSingleNode ("/");
228                         Assert.AreEqual ("<child><grandchild><include id=\"new\" /></grandchild></child>",
229                                 new_include.OuterXml, "#2");
230                 }
231                 */
232
233                 [Test]
234                 [ExpectedException (typeof (NotSupportedException))]
235                 public void XPathDocumentMoveToId ()
236                 {
237                         string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root id ID #REQUIRED>]>";
238                         string xml = dtd + "<root id='aaa'/>";
239                         XPathNavigator nav = navigator = XDocument.Parse (xml).CreateNavigator ();
240                         Assert.IsTrue (nav.MoveToId ("aaa"), "ctor() from TextReader");
241
242                         XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
243                         nav = new XPathDocument (xvr).CreateNavigator ();
244                         nav.MoveToId ("aaa"); // it does not support this method
245                 }
246
247                 [Test]
248                 public void SignificantWhitespaceConstruction ()
249                 {
250                         string xml = @"<root>
251         <child xml:space='preserve'>    <!-- -->   </child>
252         <child xml:space='preserve'>    </child>
253 </root>";
254                         XPathNavigator nav = XDocument.Parse (xml, LoadOptions.PreserveWhitespace).CreateNavigator ();
255                         nav.MoveToFirstChild ();
256                         nav.MoveToFirstChild ();
257                         Assert.AreEqual (XPathNodeType.Text, nav.NodeType, "#1"); // not Whitespace but Text
258                         nav.MoveToNext ();
259                         nav.MoveToFirstChild ();
260                         Assert.AreEqual (XPathNodeType.Text, nav.NodeType, "#2"); // not SignificantWhitespace but Text
261                 }
262
263                 [Test]
264                 public void VariableReference ()
265                 {
266                         XPathDocument xpd = new XPathDocument (
267                                 new StringReader ("<root>sample text</root>"));
268                         XPathNavigator nav = xpd.CreateNavigator ();
269
270                         XPathExpression expr = nav.Compile ("foo(string(.),$idx)");
271                         XsltArgumentList args = new XsltArgumentList ();
272                         args.AddParam ("idx", "", 5);
273                         MyContext ctx = new MyContext (nav.NameTable as NameTable, args);
274                         ctx.AddNamespace ("x", "urn:foo");
275
276                         expr.SetContext (ctx);
277
278                         XPathNodeIterator iter = nav.Select ("/root");
279                         iter.MoveNext ();
280                         Assert.AreEqual ("e", iter.Current.Evaluate (expr), "#1");
281                 }
282
283                 class MyContext : XsltContext
284                 {
285                         XsltArgumentList args;
286
287                         public MyContext (NameTable nt, XsltArgumentList args)
288                                 : base (nt)
289                         {
290                                 this.args = args;
291                         }
292
293                         public override IXsltContextFunction ResolveFunction (
294                                 string prefix, string name, XPathResultType [] argtypes)
295                         {
296                                 if (name == "foo")
297                                         return new MyFunction (argtypes);
298                                 return null;
299                         }
300
301                         public override IXsltContextVariable ResolveVariable (string prefix, string name)
302                         {
303                                 return new MyVariable (name);
304                         }
305
306                         public override bool PreserveWhitespace (XPathNavigator nav)
307                         {
308                                 return false;
309                         }
310
311                         public override int CompareDocument (string uri1, string uri2)
312                         {
313                                 return String.CompareOrdinal (uri1, uri2);
314                         }
315
316                         public override bool Whitespace {
317                                 get { return false; }
318                         }
319
320                         public object GetParam (string name, string ns)
321                         {
322                                 return args.GetParam (name, ns);
323                         }
324                 }
325
326                 public class MyFunction : IXsltContextFunction
327                 {
328                         XPathResultType [] argtypes;
329
330                         public MyFunction (XPathResultType [] argtypes)
331                         {
332                                 this.argtypes = argtypes;
333                         }
334
335                         public XPathResultType [] ArgTypes {
336                                 get { return argtypes; }
337                         }
338
339                         public int Maxargs {
340                                 get { return 2; }
341                         }
342
343                         public int Minargs {
344                                 get { return 2; }
345                         }
346
347                         public XPathResultType ReturnType {
348                                 get { return XPathResultType.String; }
349                         }
350
351                         public object Invoke (XsltContext xsltContext,
352                                 object [] args, XPathNavigator instanceContext)
353                         {
354                                 return ((string) args [0]) [(int) (double) args [1]].ToString ();
355                         }
356                 }
357
358                 public class MyVariable : IXsltContextVariable
359                 {
360                         string name;
361
362                         public MyVariable (string name)
363                         {
364                                 this.name = name;
365                         }
366
367                         public object Evaluate (XsltContext ctx)
368                         {
369                                 return ((MyContext) ctx).GetParam (name, String.Empty);
370                         }
371
372                         public bool IsLocal {
373                                 get { return false; }
374                         }
375
376                         public bool IsParam {
377                                 get { return false; }
378                         }
379
380                         public XPathResultType VariableType {
381                                 get { return XPathResultType.Any; }
382                         }
383                 }
384
385                 [Test]
386                 public void TextMatchesWhitespace ()
387                 {
388                         string xml = "<root><ws>   </ws><sws xml:space='preserve'> </sws></root>";
389                         XmlDocument doc = new XmlDocument ();
390                         doc.PreserveWhitespace = true;
391                         doc.LoadXml (xml);
392                         XPathNavigator nav = doc.CreateNavigator ();
393                         nav.MoveToFirstChild (); // root
394                         nav.MoveToFirstChild (); // ws
395                         nav.MoveToFirstChild (); // '   '
396                         Assert.AreEqual (true, nav.Matches ("text()"), "#1");
397                         nav.MoveToParent ();
398                         nav.MoveToNext (); // sws
399                         nav.MoveToFirstChild (); // ' '
400                         Assert.AreEqual (true, nav.Matches ("text()"), "#2");
401                 }
402
403                 [Test]
404                 public void Bug456103 ()
405                 {
406                         XmlDocument doc = new XmlDocument ();
407                         doc.LoadXml ("<root><X/></root>");
408
409                         XPathNavigator nav = doc.DocumentElement.CreateNavigator ();
410                         // ".//*" does not reproduce the bug.
411                         var i = nav.Select ("descendant::*");
412
413                         // without this call to get_Count() the bug does not reproduce.
414                         Assert.AreEqual (1, i.Count, "#1");
415
416                         Assert.IsTrue (i.MoveNext (), "#2");
417                 }
418
419 #if NET_2_0
420                 [Test]
421                 public void ValueAsBoolean ()
422                 {
423                         string xml = "<root>1</root>";
424                         XmlDocument doc = new XmlDocument ();
425                         doc.LoadXml (xml);
426                         XPathNavigator nav = doc.CreateNavigator ();
427                         nav.MoveToFirstChild ();
428                         Assert.AreEqual (true, nav.ValueAsBoolean, "#1");
429                         nav.MoveToFirstChild ();
430                         Assert.AreEqual (true, nav.ValueAsBoolean, "#2");
431                 }
432
433                 [Test]
434                 [ExpectedException (typeof (FormatException))]
435                 public void ValueAsBooleanFail ()
436                 {
437                         string xml = "<root>1.0</root>";
438                         XmlDocument doc = new XmlDocument ();
439                         doc.LoadXml (xml);
440                         XPathNavigator nav = doc.CreateNavigator ();
441                         nav.MoveToFirstChild ();
442                         bool i = nav.ValueAsBoolean;
443                 }
444
445                 [Test]
446                 public void ValueAsDateTime ()
447                 {
448                         DateTime time = new DateTime (2005, 12, 13);
449                         string xml = "<root>2005-12-13</root>";
450                         XmlDocument doc = new XmlDocument ();
451                         doc.LoadXml (xml);
452                         XPathNavigator nav = doc.CreateNavigator ();
453                         nav.MoveToFirstChild ();
454                         Assert.AreEqual (time, nav.ValueAsDateTime, "#1");
455                         nav.MoveToFirstChild ();
456                         Assert.AreEqual (time, nav.ValueAsDateTime, "#2");
457                 }
458
459                 [Test]
460                 [ExpectedException (typeof (FormatException))]
461                 public void ValueAsDateTimeFail ()
462                 {
463                         string xml = "<root>dating time</root>";
464                         XmlDocument doc = new XmlDocument ();
465                         doc.LoadXml (xml);
466                         XPathNavigator nav = doc.CreateNavigator ();
467                         nav.MoveToFirstChild ();
468                         DateTime time = nav.ValueAsDateTime;
469                 }
470
471                 [Test]
472                 public void ValueAsDouble ()
473                 {
474                         string xml = "<root>3.14159265359</root>";
475                         XmlDocument doc = new XmlDocument ();
476                         doc.LoadXml (xml);
477                         XPathNavigator nav = doc.CreateNavigator ();
478                         nav.MoveToFirstChild ();
479                         Assert.AreEqual (3.14159265359, nav.ValueAsDouble, "#1");
480                         nav.MoveToFirstChild ();
481                         Assert.AreEqual (3.14159265359, nav.ValueAsDouble, "#2");
482                 }
483
484                 [Test]
485                 [ExpectedException (typeof (FormatException))]
486                 public void ValueAsDoubleFail ()
487                 {
488                         string xml = "<root>Double Dealer</root>";
489                         XmlDocument doc = new XmlDocument ();
490                         doc.LoadXml (xml);
491                         XPathNavigator nav = doc.CreateNavigator ();
492                         nav.MoveToFirstChild ();
493                         Double dealer = nav.ValueAsDouble;
494                 }
495
496                 [Test]
497                 public void ValueAsInt ()
498                 {
499                         string xml = "<root>1</root>";
500                         XmlDocument doc = new XmlDocument ();
501                         doc.LoadXml (xml);
502                         XPathNavigator nav = doc.CreateNavigator ();
503                         nav.MoveToFirstChild ();
504                         Assert.AreEqual (1, nav.ValueAsInt, "#1");
505                         nav.MoveToFirstChild ();
506                         Assert.AreEqual (1, nav.ValueAsInt, "#2");
507                 }
508
509                 [Test]
510                 // Here, it seems to be using XQueryConvert (whatever was called)
511                 [ExpectedException (typeof (FormatException))]
512                 public void ValueAsIntFail ()
513                 {
514                         string xml = "<root>1.0</root>";
515                         XmlDocument doc = new XmlDocument ();
516                         doc.LoadXml (xml);
517                         XPathNavigator nav = doc.CreateNavigator ();
518                         nav.MoveToFirstChild ();
519                         int i = nav.ValueAsInt;
520                 }
521
522                 [Test]
523                 public void ValueAsLong ()
524                 {
525                         string xml = "<root>10000000000000000</root>";
526                         XmlDocument doc = new XmlDocument ();
527                         doc.LoadXml (xml);
528                         XPathNavigator nav = doc.CreateNavigator ();
529                         nav.MoveToFirstChild ();
530                         Assert.AreEqual (10000000000000000, nav.ValueAsLong, "#1");
531                         nav.MoveToFirstChild ();
532                         Assert.AreEqual (10000000000000000, nav.ValueAsLong, "#2");
533                 }
534
535                 [Test]
536                 // Here, it seems to be using XQueryConvert (whatever was called)
537                 [ExpectedException (typeof (FormatException))]
538                 public void ValueAsLongFail ()
539                 {
540                         string xml = "<root>0x10000000000000000</root>";
541                         XmlDocument doc = new XmlDocument ();
542                         doc.LoadXml (xml);
543                         XPathNavigator nav = doc.CreateNavigator ();
544                         nav.MoveToFirstChild ();
545                         long l = nav.ValueAsLong;
546                 }
547
548                 [Test] // bug #79874
549                 public void InnerXmlText ()
550                 {
551                         StringReader sr = new StringReader ("<Abc><Foo>Hello</Foo></Abc>");
552                         XPathDocument doc = new XPathDocument (sr);
553                         XPathNavigator nav = doc.CreateNavigator ();
554                         XPathNodeIterator iter = nav.Select ("/Abc/Foo");
555                         iter.MoveNext ();
556                         Assert.AreEqual ("Hello", iter.Current.InnerXml, "#1");
557                         Assert.AreEqual ("<Foo>Hello</Foo>", iter.Current.OuterXml, "#2");
558                         iter = nav.Select ("/Abc/Foo/text()");
559                         iter.MoveNext ();
560                         Assert.AreEqual (String.Empty, iter.Current.InnerXml, "#3");
561                         Assert.AreEqual ("Hello", iter.Current.OuterXml, "#4");
562                 }
563
564                 [Test] // bug #79875
565                 public void InnerXmlAttribute ()
566                 {
567                         StringReader sr = new StringReader ("<Abc><Foo attr='val1'/></Abc>");
568                         XPathDocument doc = new XPathDocument (sr);
569                         XPathNavigator nav = doc.CreateNavigator ();
570
571                         XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
572                         iter.MoveNext ();
573                         Assert.AreEqual ("val1", iter.Current.InnerXml, "#1");
574                 }
575
576                 [Test]
577                 public void InnerXmlTextEscape ()
578                 {
579                         StringReader sr = new StringReader ("<Abc><Foo>Hello&lt;\r\nInnerXml</Foo></Abc>");
580                         XPathDocument doc = new XPathDocument (sr);
581                         XPathNavigator nav = doc.CreateNavigator ();
582                         XPathNodeIterator iter = nav.Select ("/Abc/Foo");
583                         iter.MoveNext ();
584                         Assert.AreEqual ("Hello&lt;\r\nInnerXml", iter.Current.InnerXml, "#1");
585                         Assert.AreEqual ("<Foo>Hello&lt;\r\nInnerXml</Foo>", iter.Current.OuterXml, "#2");
586                         iter = nav.Select ("/Abc/Foo/text()");
587                         iter.MoveNext ();
588                         Assert.AreEqual (String.Empty, iter.Current.InnerXml, "#3");
589                         Assert.AreEqual ("Hello&lt;\r\nInnerXml", iter.Current.OuterXml, "#4");
590                 }
591
592                 [Test]
593                 [Category ("NotDotNet")] // .NET bug; it should escape value
594                 public void InnerXmlAttributeEscape ()
595                 {
596                         StringReader sr = new StringReader ("<Abc><Foo attr='val&quot;1&#13;&#10;&gt;'/></Abc>");
597                         XPathDocument doc = new XPathDocument (sr);
598                         XPathNavigator nav = doc.CreateNavigator ();
599
600                         XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
601                         iter.MoveNext ();
602                         Assert.AreEqual ("val&quot;1&#10;&gt;", iter.Current.InnerXml, "#1");
603                 }
604
605                 [Test]
606                 public void WriterAttributePrefix ()
607                 {
608                         XmlDocument doc = new XmlDocument ();
609                         XmlWriter w = doc.CreateNavigator ().AppendChild ();
610                         w.WriteStartElement ("foo");
611                         w.WriteAttributeString ("xmlns", "x", "http://www.w3.org/2000/xmlns/", "urn:foo");
612                         Assert.AreEqual ("x", w.LookupPrefix ("urn:foo"), "#0");
613                         w.WriteStartElement (null, "bar", "urn:foo");
614                         w.WriteAttributeString (null, "ext", "urn:foo", "bah");
615                         w.WriteEndElement ();
616                         w.WriteEndElement ();
617                         w.Close ();
618                         Assert.AreEqual ("x", doc.FirstChild.FirstChild.Prefix, "#1");
619                         Assert.AreEqual ("x", doc.FirstChild.FirstChild.Attributes [0].Prefix, "#2");
620                 }
621
622                 [Test]
623                 public void ValueAs ()
624                 {
625                         string xml = "<root>1</root>";
626                         XPathNavigator nav = new XPathDocument (XmlReader.Create (new StringReader (xml))).CreateNavigator ();
627                         nav.MoveToFirstChild ();
628                         nav.MoveToFirstChild ();
629                         Assert.AreEqual ("1", nav.ValueAs (typeof (string), null), "#1");
630                         Assert.AreEqual (1, nav.ValueAs (typeof (int), null), "#2");
631                 }
632
633                 [Test]
634                 public void MoveToFollowingNodeTypeAll ()
635                 {
636                         XmlDocument doc = new XmlDocument ();
637                         doc.LoadXml ("<root><child/><child2/></root>");
638                         XPathNavigator nav = doc.CreateNavigator ();
639                         Assert.IsTrue (nav.MoveToFollowing (XPathNodeType.All), "#1");
640                         Assert.IsTrue (nav.MoveToFollowing (XPathNodeType.All), "#2");
641                         Assert.AreEqual ("child", nav.LocalName, "#3");
642                         Assert.IsTrue (nav.MoveToNext (XPathNodeType.All), "#4");
643                         Assert.AreEqual ("child2", nav.LocalName, "#5");
644                 }
645
646                 [Test] // bug #324606.
647                 public void XPathDocumentFromSubtreeNodes ()
648                 {
649                         string xml = "<root><child1><nest1><nest2>hello!</nest2></nest1></child1><child2/><child3/></root>";
650                         XmlReader r = new XmlTextReader (new StringReader (xml));
651                         while (r.Read ()) {
652                                 if (r.Name == "child1")
653                                         break;
654                         }
655                         XPathDocument d = new XPathDocument (r);
656                         XPathNavigator n = d.CreateNavigator ();
657                         string result = @"<child1>
658   <nest1>
659     <nest2>hello!</nest2>
660   </nest1>
661 </child1>
662 <child2 />
663 <child3 />";
664                         Assert.AreEqual (result, n.OuterXml.Replace ("\r\n", "\n"), "#1");
665                 }
666
667                 [Test] // bug #376191
668                 public void InnerXmlOnRoot ()
669                 {
670                         string xml = @"<test>
671                         <node>z</node>
672                         <node>a</node>
673                         <node>b</node>
674                         <node>q</node>
675                         </test>";
676                         navigator = XDocument.Parse (xml).CreateNavigator ();
677                         Assert.AreEqual (navigator.OuterXml, navigator.InnerXml, "#1");
678                 }
679
680                 [Test] // bug #515136
681                 public void SelectChildrenEmpty ()
682                 {
683                         string s = "<root> <foo> </foo> </root>";
684                         XPathNavigator nav = XDocument.Parse (s).CreateNavigator ();
685                         XPathNodeIterator it = nav.SelectChildren (String.Empty, String.Empty);
686                         foreach (XPathNavigator xpn in it)
687                                 return;
688                         Assert.Fail ("no selection");
689                 }
690 #endif
691         }
692 }