ExtensionsTest2.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. #if NET_2_0
  354. [Test]
  355. public void ValueAsBoolean ()
  356. {
  357. string xml = "<root>1</root>";
  358. XmlDocument doc = new XmlDocument ();
  359. doc.LoadXml (xml);
  360. XPathNavigator nav = doc.CreateNavigator ();
  361. nav.MoveToFirstChild ();
  362. Assert.AreEqual (true, nav.ValueAsBoolean, "#1");
  363. nav.MoveToFirstChild ();
  364. Assert.AreEqual (true, nav.ValueAsBoolean, "#2");
  365. }
  366. [Test]
  367. [ExpectedException (typeof (FormatException))]
  368. public void ValueAsBooleanFail ()
  369. {
  370. string xml = "<root>1.0</root>";
  371. XmlDocument doc = new XmlDocument ();
  372. doc.LoadXml (xml);
  373. XPathNavigator nav = doc.CreateNavigator ();
  374. nav.MoveToFirstChild ();
  375. bool i = nav.ValueAsBoolean;
  376. }
  377. [Test]
  378. public void ValueAsDateTime ()
  379. {
  380. DateTime time = new DateTime (2005, 12, 13);
  381. string xml = "<root>2005-12-13</root>";
  382. XmlDocument doc = new XmlDocument ();
  383. doc.LoadXml (xml);
  384. XPathNavigator nav = doc.CreateNavigator ();
  385. nav.MoveToFirstChild ();
  386. Assert.AreEqual (time, nav.ValueAsDateTime, "#1");
  387. nav.MoveToFirstChild ();
  388. Assert.AreEqual (time, nav.ValueAsDateTime, "#2");
  389. }
  390. [Test]
  391. [ExpectedException (typeof (FormatException))]
  392. public void ValueAsDateTimeFail ()
  393. {
  394. string xml = "<root>dating time</root>";
  395. XmlDocument doc = new XmlDocument ();
  396. doc.LoadXml (xml);
  397. XPathNavigator nav = doc.CreateNavigator ();
  398. nav.MoveToFirstChild ();
  399. DateTime time = nav.ValueAsDateTime;
  400. }
  401. [Test]
  402. public void ValueAsDouble ()
  403. {
  404. string xml = "<root>3.14159265359</root>";
  405. XmlDocument doc = new XmlDocument ();
  406. doc.LoadXml (xml);
  407. XPathNavigator nav = doc.CreateNavigator ();
  408. nav.MoveToFirstChild ();
  409. Assert.AreEqual (3.14159265359, nav.ValueAsDouble, "#1");
  410. nav.MoveToFirstChild ();
  411. Assert.AreEqual (3.14159265359, nav.ValueAsDouble, "#2");
  412. }
  413. [Test]
  414. [ExpectedException (typeof (FormatException))]
  415. public void ValueAsDoubleFail ()
  416. {
  417. string xml = "<root>Double Dealer</root>";
  418. XmlDocument doc = new XmlDocument ();
  419. doc.LoadXml (xml);
  420. XPathNavigator nav = doc.CreateNavigator ();
  421. nav.MoveToFirstChild ();
  422. Double dealer = nav.ValueAsDouble;
  423. }
  424. [Test]
  425. public void ValueAsInt ()
  426. {
  427. string xml = "<root>1</root>";
  428. XmlDocument doc = new XmlDocument ();
  429. doc.LoadXml (xml);
  430. XPathNavigator nav = doc.CreateNavigator ();
  431. nav.MoveToFirstChild ();
  432. Assert.AreEqual (1, nav.ValueAsInt, "#1");
  433. nav.MoveToFirstChild ();
  434. Assert.AreEqual (1, nav.ValueAsInt, "#2");
  435. }
  436. [Test]
  437. // Here, it seems to be using XQueryConvert (whatever was called)
  438. [ExpectedException (typeof (FormatException))]
  439. public void ValueAsIntFail ()
  440. {
  441. string xml = "<root>1.0</root>";
  442. XmlDocument doc = new XmlDocument ();
  443. doc.LoadXml (xml);
  444. XPathNavigator nav = doc.CreateNavigator ();
  445. nav.MoveToFirstChild ();
  446. int i = nav.ValueAsInt;
  447. }
  448. [Test]
  449. public void ValueAsLong ()
  450. {
  451. string xml = "<root>10000000000000000</root>";
  452. XmlDocument doc = new XmlDocument ();
  453. doc.LoadXml (xml);
  454. XPathNavigator nav = doc.CreateNavigator ();
  455. nav.MoveToFirstChild ();
  456. Assert.AreEqual (10000000000000000, nav.ValueAsLong, "#1");
  457. nav.MoveToFirstChild ();
  458. Assert.AreEqual (10000000000000000, nav.ValueAsLong, "#2");
  459. }
  460. [Test]
  461. // Here, it seems to be using XQueryConvert (whatever was called)
  462. [ExpectedException (typeof (FormatException))]
  463. public void ValueAsLongFail ()
  464. {
  465. string xml = "<root>0x10000000000000000</root>";
  466. XmlDocument doc = new XmlDocument ();
  467. doc.LoadXml (xml);
  468. XPathNavigator nav = doc.CreateNavigator ();
  469. nav.MoveToFirstChild ();
  470. long l = nav.ValueAsLong;
  471. }
  472. [Test] // bug #79874
  473. public void InnerXmlText ()
  474. {
  475. StringReader sr = new StringReader ("<Abc><Foo>Hello</Foo></Abc>");
  476. XPathDocument doc = new XPathDocument (sr);
  477. XPathNavigator nav = doc.CreateNavigator ();
  478. XPathNodeIterator iter = nav.Select ("/Abc/Foo");
  479. iter.MoveNext ();
  480. Assert.AreEqual ("Hello", iter.Current.InnerXml, "#1");
  481. Assert.AreEqual ("<Foo>Hello</Foo>", iter.Current.OuterXml, "#2");
  482. iter = nav.Select ("/Abc/Foo/text()");
  483. iter.MoveNext ();
  484. Assert.AreEqual (String.Empty, iter.Current.InnerXml, "#3");
  485. Assert.AreEqual ("Hello", iter.Current.OuterXml, "#4");
  486. }
  487. [Test] // bug #79875
  488. public void InnerXmlAttribute ()
  489. {
  490. StringReader sr = new StringReader ("<Abc><Foo attr='val1'/></Abc>");
  491. XPathDocument doc = new XPathDocument (sr);
  492. XPathNavigator nav = doc.CreateNavigator ();
  493. XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
  494. iter.MoveNext ();
  495. Assert.AreEqual ("val1", iter.Current.InnerXml, "#1");
  496. }
  497. [Test]
  498. public void InnerXmlTextEscape ()
  499. {
  500. StringReader sr = new StringReader ("<Abc><Foo>Hello&lt;\r\nInnerXml</Foo></Abc>");
  501. XPathDocument doc = new XPathDocument (sr);
  502. XPathNavigator nav = doc.CreateNavigator ();
  503. XPathNodeIterator iter = nav.Select ("/Abc/Foo");
  504. iter.MoveNext ();
  505. Assert.AreEqual ("Hello&lt;\r\nInnerXml", iter.Current.InnerXml, "#1");
  506. Assert.AreEqual ("<Foo>Hello&lt;\r\nInnerXml</Foo>", iter.Current.OuterXml, "#2");
  507. iter = nav.Select ("/Abc/Foo/text()");
  508. iter.MoveNext ();
  509. Assert.AreEqual (String.Empty, iter.Current.InnerXml, "#3");
  510. Assert.AreEqual ("Hello&lt;\r\nInnerXml", iter.Current.OuterXml, "#4");
  511. }
  512. [Test]
  513. [Category ("NotDotNet")] // .NET bug; it should escape value
  514. public void InnerXmlAttributeEscape ()
  515. {
  516. StringReader sr = new StringReader ("<Abc><Foo attr='val&quot;1&#13;&#10;&gt;'/></Abc>");
  517. XPathDocument doc = new XPathDocument (sr);
  518. XPathNavigator nav = doc.CreateNavigator ();
  519. XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
  520. iter.MoveNext ();
  521. Assert.AreEqual ("val&quot;1&#10;&gt;", iter.Current.InnerXml, "#1");
  522. }
  523. [Test]
  524. public void WriterAttributePrefix ()
  525. {
  526. XmlDocument doc = new XmlDocument ();
  527. XmlWriter w = doc.CreateNavigator ().AppendChild ();
  528. w.WriteStartElement ("foo");
  529. w.WriteAttributeString ("xmlns", "x", "http://www.w3.org/2000/xmlns/", "urn:foo");
  530. Assert.AreEqual ("x", w.LookupPrefix ("urn:foo"), "#0");
  531. w.WriteStartElement (null, "bar", "urn:foo");
  532. w.WriteAttributeString (null, "ext", "urn:foo", "bah");
  533. w.WriteEndElement ();
  534. w.WriteEndElement ();
  535. w.Close ();
  536. Assert.AreEqual ("x", doc.FirstChild.FirstChild.Prefix, "#1");
  537. Assert.AreEqual ("x", doc.FirstChild.FirstChild.Attributes [0].Prefix, "#2");
  538. }
  539. [Test]
  540. public void ValueAs ()
  541. {
  542. string xml = "<root>1</root>";
  543. XPathNavigator nav = new XPathDocument (XmlReader.Create (new StringReader (xml))).CreateNavigator ();
  544. nav.MoveToFirstChild ();
  545. nav.MoveToFirstChild ();
  546. Assert.AreEqual ("1", nav.ValueAs (typeof (string), null), "#1");
  547. Assert.AreEqual (1, nav.ValueAs (typeof (int), null), "#2");
  548. }
  549. [Test]
  550. public void MoveToFollowingNodeTypeAll ()
  551. {
  552. XmlDocument doc = new XmlDocument ();
  553. doc.LoadXml ("<root><child/><child2/></root>");
  554. XPathNavigator nav = doc.CreateNavigator ();
  555. Assert.IsTrue (nav.MoveToFollowing (XPathNodeType.All), "#1");
  556. Assert.IsTrue (nav.MoveToFollowing (XPathNodeType.All), "#2");
  557. Assert.AreEqual ("child", nav.LocalName, "#3");
  558. Assert.IsTrue (nav.MoveToNext (XPathNodeType.All), "#4");
  559. Assert.AreEqual ("child2", nav.LocalName, "#5");
  560. }
  561. [Test] // bug #324606.
  562. public void XPathDocumentFromSubtreeNodes ()
  563. {
  564. string xml = "<root><child1><nest1><nest2>hello!</nest2></nest1></child1><child2/><child3/></root>";
  565. XmlReader r = new XmlTextReader (new StringReader (xml));
  566. while (r.Read ()) {
  567. if (r.Name == "child1")
  568. break;
  569. }
  570. XPathDocument d = new XPathDocument (r);
  571. XPathNavigator n = d.CreateNavigator ();
  572. string result = @"<child1>
  573. <nest1>
  574. <nest2>hello!</nest2>
  575. </nest1>
  576. </child1>
  577. <child2 />
  578. <child3 />";
  579. Assert.AreEqual (result, n.OuterXml.Replace ("\r\n", "\n"), "#1");
  580. }
  581. [Test] // bug #376191
  582. public void InnerXmlOnRoot ()
  583. {
  584. string xml = @"<test>
  585. <node>z</node>
  586. <node>a</node>
  587. <node>b</node>
  588. <node>q</node>
  589. </test>";
  590. navigator = XDocument.Parse (xml).CreateNavigator ();
  591. Assert.AreEqual (navigator.OuterXml, navigator.InnerXml, "#1");
  592. }
  593. [Test] // bug #515136
  594. public void SelectChildrenEmpty ()
  595. {
  596. string s = "<root> <foo> </foo> </root>";
  597. XPathNavigator nav = XDocument.Parse (s).CreateNavigator ();
  598. XPathNodeIterator it = nav.SelectChildren (String.Empty, String.Empty);
  599. foreach (XPathNavigator xpn in it)
  600. return;
  601. Assert.Fail ("no selection");
  602. }
  603. #endif
  604. }
  605. }