XmlReaderCommonTests.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. //
  2. // System.Xml.XmlReaderCommonTests
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. // Note: Most of testcases are moved from XmlTextReaderTests.cs and
  9. // XmlNodeReaderTests.cs.
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Text;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlReaderTests : Assertion
  20. {
  21. [SetUp]
  22. public void GetReady ()
  23. {
  24. document = new XmlDocument ();
  25. document.LoadXml (xml1);
  26. }
  27. XmlDocument document;
  28. const string xml1 = "<root attr1='value1'><child /></root>";
  29. const string xml2 = "<root><foo/><bar>test.</bar></root>";
  30. const string xml3 = "<root> test of <b>mixed</b> string.<![CDATA[ cdata string.]]></root>";
  31. const string xml4 = "<root>test of <b>mixed</b> string.</root>";
  32. XmlTextReader xtr;
  33. XmlNodeReader xnr;
  34. // copy from XmlTextReaderTests
  35. private void AssertStartDocument (XmlReader xmlReader)
  36. {
  37. Assert (xmlReader.ReadState == ReadState.Initial);
  38. Assert (xmlReader.NodeType == XmlNodeType.None);
  39. Assert (xmlReader.Depth == 0);
  40. Assert (!xmlReader.EOF);
  41. }
  42. private void AssertNode (
  43. XmlReader xmlReader,
  44. XmlNodeType nodeType,
  45. int depth,
  46. bool isEmptyElement,
  47. string name,
  48. string prefix,
  49. string localName,
  50. string namespaceURI,
  51. string value,
  52. int attributeCount)
  53. {
  54. Assert ("Read() return value", xmlReader.Read ());
  55. Assert ("ReadState", xmlReader.ReadState == ReadState.Interactive);
  56. Assert ("!EOF", !xmlReader.EOF);
  57. AssertNodeValues (xmlReader, nodeType, depth, isEmptyElement, name, prefix, localName, namespaceURI, value, attributeCount);
  58. }
  59. private void AssertNodeValues (
  60. XmlReader xmlReader,
  61. XmlNodeType nodeType,
  62. int depth,
  63. bool isEmptyElement,
  64. string name,
  65. string prefix,
  66. string localName,
  67. string namespaceURI,
  68. string value,
  69. int attributeCount)
  70. {
  71. AssertEquals ("NodeType", nodeType, xmlReader.NodeType);
  72. AssertEquals ("Depth", depth, xmlReader.Depth);
  73. AssertEquals ("IsEmptyElement", isEmptyElement, xmlReader.IsEmptyElement);
  74. AssertEquals ("name", name, xmlReader.Name);
  75. AssertEquals ("prefix", prefix, xmlReader.Prefix);
  76. AssertEquals ("localName", localName, xmlReader.LocalName);
  77. AssertEquals ("namespaceURI", namespaceURI, xmlReader.NamespaceURI);
  78. AssertEquals ("hasValue", (value != String.Empty), xmlReader.HasValue);
  79. AssertEquals ("Value", value, xmlReader.Value);
  80. AssertEquals ("hasAttributes", attributeCount > 0, xmlReader.HasAttributes);
  81. AssertEquals ("attributeCount", attributeCount, xmlReader.AttributeCount);
  82. }
  83. private void AssertAttribute (
  84. XmlReader xmlReader,
  85. string name,
  86. string prefix,
  87. string localName,
  88. string namespaceURI,
  89. string value)
  90. {
  91. AssertEquals ("value", value, xmlReader [name]);
  92. Assert (xmlReader.GetAttribute (name) == value);
  93. if (namespaceURI != String.Empty) {
  94. Assert (xmlReader[localName, namespaceURI] == value);
  95. Assert (xmlReader.GetAttribute (localName, namespaceURI) == value);
  96. }
  97. }
  98. private void AssertEndDocument (XmlReader xmlReader)
  99. {
  100. Assert ("could read", !xmlReader.Read ());
  101. AssertEquals ("NodeType is not XmlNodeType.None", XmlNodeType.None, xmlReader.NodeType);
  102. AssertEquals ("Depth is not 0", 0, xmlReader.Depth);
  103. AssertEquals ("ReadState is not ReadState.EndOfFile", ReadState.EndOfFile, xmlReader.ReadState);
  104. Assert ("not EOF", xmlReader.EOF);
  105. xmlReader.Close ();
  106. AssertEquals ("ReadState is not ReadState.Cosed", ReadState.Closed, xmlReader.ReadState);
  107. }
  108. private delegate void TestMethod (XmlReader reader);
  109. private void RunTest (string xml, TestMethod method)
  110. {
  111. xtr = new XmlTextReader (new StringReader (xml));
  112. try {
  113. method (xtr);
  114. } catch (AssertionException ex) {
  115. throw new AssertionException ("XmlTextReader failed: " + ex.Message, ex);
  116. }
  117. document.XmlResolver = null;
  118. document.LoadXml (xml);
  119. xnr = new XmlNodeReader (document);
  120. try {
  121. method (xnr);
  122. } catch (AssertionException ex) {
  123. throw new AssertionException ("XmlNodeReader failed: " + ex.Message, ex);
  124. }
  125. }
  126. [Test]
  127. public void InitialState ()
  128. {
  129. RunTest (xml1, new TestMethod (InitialState));
  130. }
  131. private void InitialState (XmlReader reader)
  132. {
  133. AssertEquals ("Depth", 0, reader.Depth);
  134. AssertEquals ("EOF", false, reader.EOF);
  135. AssertEquals ("HasValue", false, reader.HasValue);
  136. AssertEquals ("IsEmptyElement", false, reader.IsEmptyElement);
  137. AssertEquals ("LocalName", String.Empty, reader.LocalName);
  138. AssertEquals ("NodeType", XmlNodeType.None, reader.NodeType);
  139. AssertEquals ("ReadState", ReadState.Initial, reader.ReadState);
  140. }
  141. [Test]
  142. public void Read ()
  143. {
  144. RunTest (xml1, new TestMethod (Read));
  145. }
  146. public void Read (XmlReader reader)
  147. {
  148. reader.Read ();
  149. AssertEquals ("<root>.NodeType", XmlNodeType.Element, reader.NodeType);
  150. AssertEquals ("<root>.Name", "root", reader.Name);
  151. AssertEquals ("<root>.ReadState", ReadState.Interactive, reader.ReadState);
  152. AssertEquals ("<root>.Depth", 0, reader.Depth);
  153. // move to 'child'
  154. reader.Read ();
  155. AssertEquals ("<child/>.Depth", 1, reader.Depth);
  156. AssertEquals ("<child/>.NodeType", XmlNodeType.Element, reader.NodeType);
  157. AssertEquals ("<child/>.Name", "child", reader.Name);
  158. reader.Read ();
  159. AssertEquals ("</root>.Depth", 0, reader.Depth);
  160. AssertEquals ("</root>.NodeType", XmlNodeType.EndElement, reader.NodeType);
  161. AssertEquals ("</root>.Name", "root", reader.Name);
  162. reader.Read ();
  163. AssertEquals ("end.EOF", true, reader.EOF);
  164. AssertEquals ("end.NodeType", XmlNodeType.None, reader.NodeType);
  165. }
  166. [Test]
  167. public void ReadEmptyElement ()
  168. {
  169. RunTest (xml2, new TestMethod (ReadEmptyElement));
  170. }
  171. public void ReadEmptyElement (XmlReader reader)
  172. {
  173. reader.Read (); // root
  174. AssertEquals (false, reader.IsEmptyElement);
  175. reader.Read (); // foo
  176. AssertEquals ("foo", reader.Name);
  177. AssertEquals (true, reader.IsEmptyElement);
  178. reader.Read (); // bar
  179. AssertEquals ("bar", reader.Name);
  180. AssertEquals (false, reader.IsEmptyElement);
  181. }
  182. [Test]
  183. public void ReadStringFromElement ()
  184. {
  185. RunTest (xml3, new TestMethod (ReadStringFromElement));
  186. }
  187. public void ReadStringFromElement (XmlReader reader)
  188. {
  189. // Note: ReadString() test works only when the reader is
  190. // positioned at the container element.
  191. // In case the reader is positioned at the first
  192. // character node, XmlTextReader and XmlNodeReader works
  193. // different!!
  194. reader.Read ();
  195. string s = reader.ReadString ();
  196. AssertEquals ("readString.1.ret_val", " test of ", s);
  197. AssertEquals ("readString.1.Name", "b", reader.Name);
  198. s = reader.ReadString ();
  199. AssertEquals ("readString.2.ret_val", "mixed", s);
  200. AssertEquals ("readString.2.NodeType", XmlNodeType.EndElement, reader.NodeType);
  201. s = reader.ReadString (); // never proceeds.
  202. AssertEquals ("readString.3.ret_val", String.Empty, s);
  203. AssertEquals ("readString.3.NodeType", XmlNodeType.EndElement, reader.NodeType);
  204. reader.Read ();
  205. AssertEquals ("readString.4.NodeType", XmlNodeType.Text, reader.NodeType);
  206. AssertEquals ("readString.4.Value", " string.", reader.Value);
  207. s = reader.ReadString (); // reads the same Text node.
  208. AssertEquals ("readString.5.ret_val", " string. cdata string.", s);
  209. AssertEquals ("readString.5.NodeType", XmlNodeType.EndElement, reader.NodeType);
  210. }
  211. [Test]
  212. public void ReadInnerXml ()
  213. {
  214. const string xml = "<root><foo>test of <b>mixed</b> string.</foo><bar /></root>";
  215. RunTest (xml, new TestMethod (ReadInnerXml));
  216. }
  217. public void ReadInnerXml (XmlReader reader)
  218. {
  219. reader.Read ();
  220. reader.Read ();
  221. AssertEquals ("initial.ReadState", ReadState.Interactive, reader.ReadState);
  222. AssertEquals ("initial.EOF", false, reader.EOF);
  223. AssertEquals ("initial.NodeType", XmlNodeType.Element, reader.NodeType);
  224. string s = reader.ReadInnerXml ();
  225. AssertEquals ("read_all", "test of <b>mixed</b> string.", s);
  226. AssertEquals ("after.Name", "bar", reader.Name);
  227. AssertEquals ("after.NodeType", XmlNodeType.Element, reader.NodeType);
  228. }
  229. [Test]
  230. public void EmptyElement ()
  231. {
  232. RunTest ("<foo/>", new TestMethod (EmptyElement));
  233. }
  234. public void EmptyElement (XmlReader xmlReader)
  235. {
  236. AssertStartDocument (xmlReader);
  237. AssertNode (
  238. xmlReader, // xmlReader
  239. XmlNodeType.Element, // nodeType
  240. 0, // depth
  241. true, // isEmptyElement
  242. "foo", // name
  243. String.Empty, // prefix
  244. "foo", // localName
  245. String.Empty, // namespaceURI
  246. String.Empty, // value
  247. 0 // attributeCount
  248. );
  249. AssertEndDocument (xmlReader);
  250. }
  251. [Test]
  252. public void NestedEmptyTag ()
  253. {
  254. string xml = "<foo><bar/></foo>";
  255. RunTest (xml, new TestMethod (NestedEmptyTag));
  256. }
  257. public void NestedEmptyTag (XmlReader xmlReader)
  258. {
  259. AssertStartDocument (xmlReader);
  260. AssertNode (
  261. xmlReader, // xmlReader
  262. XmlNodeType.Element, // nodeType
  263. 0, //depth
  264. false, // isEmptyElement
  265. "foo", // name
  266. String.Empty, // prefix
  267. "foo", // localName
  268. String.Empty, // namespaceURI
  269. String.Empty, // value
  270. 0 // attributeCount
  271. );
  272. AssertNode (
  273. xmlReader, // xmlReader
  274. XmlNodeType.Element, // nodeType
  275. 1, //depth
  276. true, // isEmptyElement
  277. "bar", // name
  278. String.Empty, // prefix
  279. "bar", // localName
  280. String.Empty, // namespaceURI
  281. String.Empty, // value
  282. 0 // attributeCount
  283. );
  284. AssertNode (
  285. xmlReader, // xmlReader
  286. XmlNodeType.EndElement, // nodeType
  287. 0, //depth
  288. false, // isEmptyElement
  289. "foo", // name
  290. String.Empty, // prefix
  291. "foo", // localName
  292. String.Empty, // namespaceURI
  293. String.Empty, // value
  294. 0 // attributeCount
  295. );
  296. AssertEndDocument (xmlReader);
  297. }
  298. [Test]
  299. public void NestedText ()
  300. {
  301. string xml = "<foo>bar</foo>";
  302. RunTest (xml, new TestMethod (NestedText));
  303. }
  304. public void NestedText (XmlReader xmlReader)
  305. {
  306. AssertStartDocument (xmlReader);
  307. AssertNode (
  308. xmlReader, // xmlReader
  309. XmlNodeType.Element, // nodeType
  310. 0, //depth
  311. false, // isEmptyElement
  312. "foo", // name
  313. String.Empty, // prefix
  314. "foo", // localName
  315. String.Empty, // namespaceURI
  316. String.Empty, // value
  317. 0 // attributeCount
  318. );
  319. AssertNode (
  320. xmlReader, // xmlReader
  321. XmlNodeType.Text, // nodeType
  322. 1, //depth
  323. false, // isEmptyElement
  324. String.Empty, // name
  325. String.Empty, // prefix
  326. String.Empty, // localName
  327. String.Empty, // namespaceURI
  328. "bar", // value
  329. 0 // attributeCount
  330. );
  331. AssertNode (
  332. xmlReader, // xmlReader
  333. XmlNodeType.EndElement, // nodeType
  334. 0, //depth
  335. false, // isEmptyElement
  336. "foo", // name
  337. String.Empty, // prefix
  338. "foo", // localName
  339. String.Empty, // namespaceURI
  340. String.Empty, // value
  341. 0 // attributeCount
  342. );
  343. AssertEndDocument (xmlReader);
  344. }
  345. [Test]
  346. public void EmptyElementWithTwoAttributes ()
  347. {
  348. string xml = @"<foo bar=""baz"" quux='quuux'/>";
  349. RunTest (xml, new TestMethod (EmptyElementWithTwoAttributes ));
  350. }
  351. public void EmptyElementWithTwoAttributes (XmlReader xmlReader)
  352. {
  353. AssertStartDocument (xmlReader);
  354. AssertNode (
  355. xmlReader, // xmlReader
  356. XmlNodeType.Element, // nodeType
  357. 0, //depth
  358. true, // isEmptyElement
  359. "foo", // name
  360. String.Empty, // prefix
  361. "foo", // localName
  362. String.Empty, // namespaceURI
  363. String.Empty, // value
  364. 2 // attributeCount
  365. );
  366. AssertAttribute (
  367. xmlReader, // xmlReader
  368. "bar", // name
  369. String.Empty, // prefix
  370. "bar", // localName
  371. String.Empty, // namespaceURI
  372. "baz" // value
  373. );
  374. AssertAttribute (
  375. xmlReader, // xmlReader
  376. "quux", // name
  377. String.Empty, // prefix
  378. "quux", // localName
  379. String.Empty, // namespaceURI
  380. "quuux" // value
  381. );
  382. AssertEndDocument (xmlReader);
  383. }
  384. [Test]
  385. public void ProcessingInstructionBeforeDocumentElement ()
  386. {
  387. string xml = "<?foo bar?><baz/>";
  388. RunTest (xml, new TestMethod (ProcessingInstructionBeforeDocumentElement));
  389. }
  390. public void ProcessingInstructionBeforeDocumentElement (XmlReader xmlReader)
  391. {
  392. AssertStartDocument (xmlReader);
  393. AssertNode (
  394. xmlReader, // xmlReader
  395. XmlNodeType.ProcessingInstruction, // nodeType
  396. 0, //depth
  397. false, // isEmptyElement
  398. "foo", // name
  399. String.Empty, // prefix
  400. "foo", // localName
  401. String.Empty, // namespaceURI
  402. "bar", // value
  403. 0 // attributeCount
  404. );
  405. AssertNode (
  406. xmlReader, // xmlReader
  407. XmlNodeType.Element, // nodeType
  408. 0, //depth
  409. true, // isEmptyElement
  410. "baz", // name
  411. String.Empty, // prefix
  412. "baz", // localName
  413. String.Empty, // namespaceURI
  414. String.Empty, // value
  415. 0 // attributeCount
  416. );
  417. AssertEndDocument (xmlReader);
  418. }
  419. [Test]
  420. public void CommentBeforeDocumentElement ()
  421. {
  422. string xml = "<!--foo--><bar/>";
  423. RunTest (xml, new TestMethod (CommentBeforeDocumentElement));
  424. }
  425. public void CommentBeforeDocumentElement (XmlReader xmlReader)
  426. {
  427. AssertStartDocument (xmlReader);
  428. AssertNode (
  429. xmlReader, // xmlReader
  430. XmlNodeType.Comment, // nodeType
  431. 0, //depth
  432. false, // isEmptyElement
  433. String.Empty, // name
  434. String.Empty, // prefix
  435. String.Empty, // localName
  436. String.Empty, // namespaceURI
  437. "foo", // value
  438. 0 // attributeCount
  439. );
  440. AssertNode (
  441. xmlReader, // xmlReader
  442. XmlNodeType.Element, // nodeType
  443. 0, //depth
  444. true, // isEmptyElement
  445. "bar", // name
  446. String.Empty, // prefix
  447. "bar", // localName
  448. String.Empty, // namespaceURI
  449. String.Empty, // value
  450. 0 // attributeCount
  451. );
  452. AssertEndDocument (xmlReader);
  453. }
  454. [Test]
  455. public void PredefinedEntities ()
  456. {
  457. string xml = "<foo>&lt;&gt;&amp;&apos;&quot;</foo>";
  458. RunTest (xml, new TestMethod (PredefinedEntities));
  459. }
  460. public void PredefinedEntities (XmlReader xmlReader)
  461. {
  462. AssertStartDocument (xmlReader);
  463. AssertNode (
  464. xmlReader, // xmlReader
  465. XmlNodeType.Element, // nodeType
  466. 0, //depth
  467. false, // isEmptyElement
  468. "foo", // name
  469. String.Empty, // prefix
  470. "foo", // localName
  471. String.Empty, // namespaceURI
  472. String.Empty, // value
  473. 0 // attributeCount
  474. );
  475. AssertNode (
  476. xmlReader, // xmlReader
  477. XmlNodeType.Text, // nodeType
  478. 1, //depth
  479. false, // isEmptyElement
  480. String.Empty, // name
  481. String.Empty, // prefix
  482. String.Empty, // localName
  483. String.Empty, // namespaceURI
  484. "<>&'\"", // value
  485. 0 // attributeCount
  486. );
  487. AssertNode (
  488. xmlReader, // xmlReader
  489. XmlNodeType.EndElement, // nodeType
  490. 0, //depth
  491. false, // isEmptyElement
  492. "foo", // name
  493. String.Empty, // prefix
  494. "foo", // localName
  495. String.Empty, // namespaceURI
  496. String.Empty, // value
  497. 0 // attributeCount
  498. );
  499. AssertEndDocument (xmlReader);
  500. }
  501. [Test]
  502. public void CharacterReferences ()
  503. {
  504. string xml = "<foo>&#70;&#x4F;&#x4f;</foo>";
  505. RunTest (xml, new TestMethod (CharacterReferences));
  506. }
  507. public void CharacterReferences (XmlReader xmlReader)
  508. {
  509. AssertStartDocument (xmlReader);
  510. AssertNode (
  511. xmlReader, // xmlReader
  512. XmlNodeType.Element, // nodeType
  513. 0, //depth
  514. false, // isEmptyElement
  515. "foo", // name
  516. String.Empty, // prefix
  517. "foo", // localName
  518. String.Empty, // namespaceURI
  519. String.Empty, // value
  520. 0 // attributeCount
  521. );
  522. AssertNode (
  523. xmlReader, // xmlReader
  524. XmlNodeType.Text, // nodeType
  525. 1, //depth
  526. false, // isEmptyElement
  527. String.Empty, // name
  528. String.Empty, // prefix
  529. String.Empty, // localName
  530. String.Empty, // namespaceURI
  531. "FOO", // value
  532. 0 // attributeCount
  533. );
  534. AssertNode (
  535. xmlReader, // xmlReader
  536. XmlNodeType.EndElement, // nodeType
  537. 0, //depth
  538. false, // isEmptyElement
  539. "foo", // name
  540. String.Empty, // prefix
  541. "foo", // localName
  542. String.Empty, // namespaceURI
  543. String.Empty, // value
  544. 0 // attributeCount
  545. );
  546. AssertEndDocument (xmlReader);
  547. }
  548. [Test]
  549. public void PredefinedEntitiesInAttribute ()
  550. {
  551. string xml = "<foo bar='&lt;&gt;&amp;&apos;&quot;'/>";
  552. RunTest (xml, new TestMethod (PredefinedEntitiesInAttribute ));
  553. }
  554. public void PredefinedEntitiesInAttribute (XmlReader xmlReader)
  555. {
  556. AssertStartDocument (xmlReader);
  557. AssertNode (
  558. xmlReader, // xmlReader
  559. XmlNodeType.Element, // nodeType
  560. 0, //depth
  561. true, // isEmptyElement
  562. "foo", // name
  563. String.Empty, // prefix
  564. "foo", // localName
  565. String.Empty, // namespaceURI
  566. String.Empty, // value
  567. 1 // attributeCount
  568. );
  569. AssertAttribute (
  570. xmlReader, // xmlReader
  571. "bar", // name
  572. String.Empty, // prefix
  573. "bar", // localName
  574. String.Empty, // namespaceURI
  575. "<>&'\"" // value
  576. );
  577. AssertEndDocument (xmlReader);
  578. }
  579. [Test]
  580. public void CharacterReferencesInAttribute ()
  581. {
  582. string xml = "<foo bar='&#70;&#x4F;&#x4f;'/>";
  583. RunTest (xml, new TestMethod (CharacterReferencesInAttribute));
  584. }
  585. public void CharacterReferencesInAttribute (XmlReader xmlReader)
  586. {
  587. AssertStartDocument (xmlReader);
  588. AssertNode (
  589. xmlReader, // xmlReader
  590. XmlNodeType.Element, // nodeType
  591. 0, //depth
  592. true, // isEmptyElement
  593. "foo", // name
  594. String.Empty, // prefix
  595. "foo", // localName
  596. String.Empty, // namespaceURI
  597. String.Empty, // value
  598. 1 // attributeCount
  599. );
  600. AssertAttribute (
  601. xmlReader, // xmlReader
  602. "bar", // name
  603. String.Empty, // prefix
  604. "bar", // localName
  605. String.Empty, // namespaceURI
  606. "FOO" // value
  607. );
  608. AssertEndDocument (xmlReader);
  609. }
  610. [Test]
  611. public void CDATA ()
  612. {
  613. string xml = "<foo><![CDATA[<>&]]></foo>";
  614. RunTest (xml, new TestMethod (CDATA));
  615. }
  616. public void CDATA (XmlReader xmlReader)
  617. {
  618. AssertStartDocument (xmlReader);
  619. AssertNode (
  620. xmlReader, // xmlReader
  621. XmlNodeType.Element, // nodeType
  622. 0, //depth
  623. false, // isEmptyElement
  624. "foo", // name
  625. String.Empty, // prefix
  626. "foo", // localName
  627. String.Empty, // namespaceURI
  628. String.Empty, // value
  629. 0 // attributeCount
  630. );
  631. AssertNode (
  632. xmlReader, // xmlReader
  633. XmlNodeType.CDATA, // nodeType
  634. 1, //depth
  635. false, // isEmptyElement
  636. String.Empty, // name
  637. String.Empty, // prefix
  638. String.Empty, // localName
  639. String.Empty, // namespaceURI
  640. "<>&", // value
  641. 0 // attributeCount
  642. );
  643. AssertNode (
  644. xmlReader, // xmlReader
  645. XmlNodeType.EndElement, // nodeType
  646. 0, //depth
  647. false, // isEmptyElement
  648. "foo", // name
  649. String.Empty, // prefix
  650. "foo", // localName
  651. String.Empty, // namespaceURI
  652. String.Empty, // value
  653. 0 // attributeCount
  654. );
  655. AssertEndDocument (xmlReader);
  656. }
  657. [Test]
  658. public void EmptyElementInDefaultNamespace ()
  659. {
  660. string xml = @"<foo xmlns='http://foo/' />";
  661. RunTest (xml, new TestMethod (EmptyElementInDefaultNamespace));
  662. }
  663. public void EmptyElementInDefaultNamespace (XmlReader xmlReader)
  664. {
  665. AssertStartDocument (xmlReader);
  666. AssertNode (
  667. xmlReader, // xmlReader
  668. XmlNodeType.Element, // nodeType
  669. 0, // depth
  670. true, // isEmptyElement
  671. "foo", // name
  672. String.Empty, // prefix
  673. "foo", // localName
  674. "http://foo/", // namespaceURI
  675. String.Empty, // value
  676. 1 // attributeCount
  677. );
  678. AssertAttribute (
  679. xmlReader, // xmlReader
  680. "xmlns", // name
  681. String.Empty, // prefix
  682. "xmlns", // localName
  683. "http://www.w3.org/2000/xmlns/", // namespaceURI
  684. "http://foo/" // value
  685. );
  686. AssertEquals ("http://foo/", xmlReader.LookupNamespace (String.Empty));
  687. AssertEndDocument (xmlReader);
  688. }
  689. [Test]
  690. public void ChildElementInNamespace ()
  691. {
  692. string xml = @"<foo:bar xmlns:foo='http://foo/'><baz:quux xmlns:baz='http://baz/' /></foo:bar>";
  693. RunTest (xml, new TestMethod (ChildElementInNamespace));
  694. }
  695. public void ChildElementInNamespace (XmlReader xmlReader)
  696. {
  697. AssertStartDocument (xmlReader);
  698. AssertNode (
  699. xmlReader, // xmlReader
  700. XmlNodeType.Element, // nodeType
  701. 0, // depth
  702. false, // isEmptyElement
  703. "foo:bar", // name
  704. "foo", // prefix
  705. "bar", // localName
  706. "http://foo/", // namespaceURI
  707. String.Empty, // value
  708. 1 // attributeCount
  709. );
  710. AssertAttribute (
  711. xmlReader, // xmlReader
  712. "xmlns:foo", // name
  713. "xmlns", // prefix
  714. "foo", // localName
  715. "http://www.w3.org/2000/xmlns/", // namespaceURI
  716. "http://foo/" // value
  717. );
  718. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  719. AssertNode (
  720. xmlReader, // xmlReader
  721. XmlNodeType.Element, // nodeType
  722. 1, // depth
  723. true, // isEmptyElement
  724. "baz:quux", // name
  725. "baz", // prefix
  726. "quux", // localName
  727. "http://baz/", // namespaceURI
  728. String.Empty, // value
  729. 1 // attributeCount
  730. );
  731. AssertAttribute (
  732. xmlReader, // xmlReader
  733. "xmlns:baz", // name
  734. "xmlns", // prefix
  735. "baz", // localName
  736. "http://www.w3.org/2000/xmlns/", // namespaceURI
  737. "http://baz/" // value
  738. );
  739. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  740. AssertEquals ("http://baz/", xmlReader.LookupNamespace ("baz"));
  741. AssertNode (
  742. xmlReader, // xmlReader
  743. XmlNodeType.EndElement, // nodeType
  744. 0, // depth
  745. false, // isEmptyElement
  746. "foo:bar", // name
  747. "foo", // prefix
  748. "bar", // localName
  749. "http://foo/", // namespaceURI
  750. String.Empty, // value
  751. 0 // attributeCount
  752. );
  753. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  754. AssertNull (xmlReader.LookupNamespace ("baz"));
  755. AssertEndDocument (xmlReader);
  756. }
  757. [Test]
  758. public void ChildElementInDefaultNamespace ()
  759. {
  760. string xml = @"<foo:bar xmlns:foo='http://foo/'><baz xmlns='http://baz/' /></foo:bar>";
  761. RunTest (xml, new TestMethod (ChildElementInDefaultNamespace));
  762. }
  763. public void ChildElementInDefaultNamespace (XmlReader xmlReader)
  764. {
  765. AssertStartDocument (xmlReader);
  766. AssertNode (
  767. xmlReader, // xmlReader
  768. XmlNodeType.Element, // nodeType
  769. 0, // depth
  770. false, // isEmptyElement
  771. "foo:bar", // name
  772. "foo", // prefix
  773. "bar", // localName
  774. "http://foo/", // namespaceURI
  775. String.Empty, // value
  776. 1 // attributeCount
  777. );
  778. AssertAttribute (
  779. xmlReader, // xmlReader
  780. "xmlns:foo", // name
  781. "xmlns", // prefix
  782. "foo", // localName
  783. "http://www.w3.org/2000/xmlns/", // namespaceURI
  784. "http://foo/" // value
  785. );
  786. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  787. AssertNode (
  788. xmlReader, // xmlReader
  789. XmlNodeType.Element, // nodeType
  790. 1, // depth
  791. true, // isEmptyElement
  792. "baz", // name
  793. String.Empty, // prefix
  794. "baz", // localName
  795. "http://baz/", // namespaceURI
  796. String.Empty, // value
  797. 1 // attributeCount
  798. );
  799. AssertAttribute (
  800. xmlReader, // xmlReader
  801. "xmlns", // name
  802. String.Empty, // prefix
  803. "xmlns", // localName
  804. "http://www.w3.org/2000/xmlns/", // namespaceURI
  805. "http://baz/" // value
  806. );
  807. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  808. AssertEquals ("http://baz/", xmlReader.LookupNamespace (String.Empty));
  809. AssertNode (
  810. xmlReader, // xmlReader
  811. XmlNodeType.EndElement, // nodeType
  812. 0, // depth
  813. false, // isEmptyElement
  814. "foo:bar", // name
  815. "foo", // prefix
  816. "bar", // localName
  817. "http://foo/", // namespaceURI
  818. String.Empty, // value
  819. 0 // attributeCount
  820. );
  821. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  822. AssertEndDocument (xmlReader);
  823. }
  824. [Test]
  825. public void AttributeInNamespace ()
  826. {
  827. string xml = @"<foo bar:baz='quux' xmlns:bar='http://bar/' />";
  828. RunTest (xml, new TestMethod (AttributeInNamespace));
  829. }
  830. public void AttributeInNamespace (XmlReader xmlReader)
  831. {
  832. AssertStartDocument (xmlReader);
  833. AssertNode (
  834. xmlReader, // xmlReader
  835. XmlNodeType.Element, // nodeType
  836. 0, // depth
  837. true, // isEmptyElement
  838. "foo", // name
  839. String.Empty, // prefix
  840. "foo", // localName
  841. String.Empty, // namespaceURI
  842. String.Empty, // value
  843. 2 // attributeCount
  844. );
  845. AssertAttribute (
  846. xmlReader, // xmlReader
  847. "bar:baz", // name
  848. "bar", // prefix
  849. "baz", // localName
  850. "http://bar/", // namespaceURI
  851. "quux" // value
  852. );
  853. AssertAttribute (
  854. xmlReader, // xmlReader
  855. "xmlns:bar", // name
  856. "xmlns", // prefix
  857. "bar", // localName
  858. "http://www.w3.org/2000/xmlns/", // namespaceURI
  859. "http://bar/" // value
  860. );
  861. AssertEquals ("http://bar/", xmlReader.LookupNamespace ("bar"));
  862. AssertEndDocument (xmlReader);
  863. }
  864. [Test]
  865. public void MoveToElementFromAttribute ()
  866. {
  867. string xml = @"<foo bar=""baz"" />";
  868. RunTest (xml, new TestMethod (MoveToElementFromAttribute));
  869. }
  870. public void MoveToElementFromAttribute (XmlReader xmlReader)
  871. {
  872. Assert (xmlReader.Read ());
  873. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  874. Assert (xmlReader.MoveToFirstAttribute ());
  875. AssertEquals (XmlNodeType.Attribute, xmlReader.NodeType);
  876. Assert (xmlReader.MoveToElement ());
  877. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  878. }
  879. [Test]
  880. public void MoveToElementFromElement ()
  881. {
  882. string xml = @"<foo bar=""baz"" />";
  883. RunTest (xml, new TestMethod (MoveToElementFromElement));
  884. }
  885. public void MoveToElementFromElement (XmlReader xmlReader)
  886. {
  887. Assert (xmlReader.Read ());
  888. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  889. Assert (!xmlReader.MoveToElement ());
  890. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  891. }
  892. [Test]
  893. public void MoveToFirstAttributeWithNoAttributes ()
  894. {
  895. string xml = @"<foo />";
  896. RunTest (xml, new TestMethod (MoveToFirstAttributeWithNoAttributes));
  897. }
  898. public void MoveToFirstAttributeWithNoAttributes (XmlReader xmlReader)
  899. {
  900. Assert (xmlReader.Read ());
  901. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  902. Assert (!xmlReader.MoveToFirstAttribute ());
  903. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  904. }
  905. [Test]
  906. public void MoveToNextAttributeWithNoAttributes ()
  907. {
  908. string xml = @"<foo />";
  909. RunTest (xml, new TestMethod (MoveToNextAttributeWithNoAttributes));
  910. }
  911. public void MoveToNextAttributeWithNoAttributes (XmlReader xmlReader)
  912. {
  913. Assert (xmlReader.Read ());
  914. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  915. Assert (!xmlReader.MoveToNextAttribute ());
  916. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  917. }
  918. [Test]
  919. public void MoveToNextAttribute()
  920. {
  921. string xml = @"<foo bar=""baz"" quux='quuux'/>";
  922. RunTest (xml, new TestMethod (MoveToNextAttribute));
  923. }
  924. public void MoveToNextAttribute (XmlReader xmlReader)
  925. {
  926. AssertStartDocument (xmlReader);
  927. AssertNode (
  928. xmlReader, // xmlReader
  929. XmlNodeType.Element, // nodeType
  930. 0, //depth
  931. true, // isEmptyElement
  932. "foo", // name
  933. String.Empty, // prefix
  934. "foo", // localName
  935. String.Empty, // namespaceURI
  936. String.Empty, // value
  937. 2 // attributeCount
  938. );
  939. AssertAttribute (
  940. xmlReader, // xmlReader
  941. "bar", // name
  942. String.Empty, // prefix
  943. "bar", // localName
  944. String.Empty, // namespaceURI
  945. "baz" // value
  946. );
  947. AssertAttribute (
  948. xmlReader, // xmlReader
  949. "quux", // name
  950. String.Empty, // prefix
  951. "quux", // localName
  952. String.Empty, // namespaceURI
  953. "quuux" // value
  954. );
  955. Assert (xmlReader.MoveToNextAttribute ());
  956. Assert ("bar" == xmlReader.Name || "quux" == xmlReader.Name);
  957. Assert ("baz" == xmlReader.Value || "quuux" == xmlReader.Value);
  958. Assert (xmlReader.MoveToNextAttribute ());
  959. Assert ("bar" == xmlReader.Name || "quux" == xmlReader.Name);
  960. Assert ("baz" == xmlReader.Value || "quuux" == xmlReader.Value);
  961. Assert (!xmlReader.MoveToNextAttribute ());
  962. Assert (xmlReader.MoveToElement ());
  963. AssertNodeValues (
  964. xmlReader, // xmlReader
  965. XmlNodeType.Element, // nodeType
  966. 0, //depth
  967. true, // isEmptyElement
  968. "foo", // name
  969. String.Empty, // prefix
  970. "foo", // localName
  971. String.Empty, // namespaceURI
  972. String.Empty, // value
  973. 2 // attributeCount
  974. );
  975. AssertEndDocument (xmlReader);
  976. }
  977. [Test]
  978. public void AttributeOrder ()
  979. {
  980. string xml = @"<foo _1='1' _2='2' _3='3' />";
  981. RunTest (xml, new TestMethod (AttributeOrder));
  982. }
  983. public void AttributeOrder (XmlReader xmlReader)
  984. {
  985. Assert (xmlReader.Read ());
  986. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  987. Assert (xmlReader.MoveToFirstAttribute ());
  988. AssertEquals ("_1", xmlReader.Name);
  989. Assert (xmlReader.MoveToNextAttribute ());
  990. AssertEquals ("_2", xmlReader.Name);
  991. Assert (xmlReader.MoveToNextAttribute ());
  992. AssertEquals ("_3", xmlReader.Name);
  993. Assert (!xmlReader.MoveToNextAttribute ());
  994. }
  995. }
  996. }