ExtensionsTest2.cs 21 KB

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