XPathNavigatorMatchesTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // MonoTests.System.Xml.XPathNavigatorMatchesTests
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Xml;
  13. using System.Xml.XPath;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Xml
  16. {
  17. [TestFixture]
  18. public class XPathNavigatorMatchesTests : Assertion
  19. {
  20. private XPathNavigator CreateNavigator (string xml)
  21. {
  22. XmlDocument document = new XmlDocument ();
  23. document.LoadXml (xml);
  24. return document.CreateNavigator ();
  25. }
  26. [Test]
  27. public void MatchRoot ()
  28. {
  29. XPathNavigator navigator = CreateNavigator ("<foo />");
  30. Assert (navigator.Matches ("/"));
  31. }
  32. [Test]
  33. public void FalseMatchRoot ()
  34. {
  35. XPathNavigator navigator = CreateNavigator ("<foo />");
  36. Assert (!navigator.Matches ("foo"));
  37. }
  38. [Test]
  39. public void MatchDocumentElement ()
  40. {
  41. XmlDocument document = new XmlDocument ();
  42. document.LoadXml ("<foo />");
  43. XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
  44. Assert (navigator.Matches ("foo"));
  45. }
  46. [Test]
  47. public void MatchAbsoluteDocumentElement ()
  48. {
  49. XmlDocument document = new XmlDocument ();
  50. document.LoadXml ("<foo />");
  51. XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
  52. Assert (navigator.Matches ("/foo"));
  53. }
  54. [Test]
  55. public void MatchDocumentElementChild ()
  56. {
  57. XmlDocument document = new XmlDocument ();
  58. document.LoadXml ("<foo><bar /></foo>");
  59. XPathNavigator navigator = document.DocumentElement.FirstChild.CreateNavigator ();
  60. Assert (navigator.Matches ("bar"));
  61. Assert (navigator.Matches ("foo/bar"));
  62. }
  63. [Test]
  64. public void MatchAttribute ()
  65. {
  66. XmlDocument document = new XmlDocument ();
  67. document.LoadXml ("<foo bar='baz' />");
  68. XPathNavigator navigator = document.DocumentElement.Attributes[0].CreateNavigator ();
  69. Assert (navigator.Matches ("@bar"));
  70. Assert (navigator.Matches ("foo/@bar"));
  71. }
  72. [Test]
  73. public void SlashSlash ()
  74. {
  75. XmlDocument document = new XmlDocument ();
  76. document.LoadXml ("<foo><bar><baz/></bar></foo>");
  77. XPathNavigator navigator = document.DocumentElement.FirstChild.FirstChild.CreateNavigator ();
  78. Assert (navigator.Matches ("foo//baz"));
  79. }
  80. [Test]
  81. public void AbsoluteSlashSlash ()
  82. {
  83. XmlDocument document = new XmlDocument ();
  84. document.LoadXml ("<foo><bar><baz/></bar></foo>");
  85. XPathNavigator navigator = document.DocumentElement.FirstChild.FirstChild.CreateNavigator ();
  86. Assert (navigator.Matches ("//baz"));
  87. }
  88. [Test]
  89. public void MatchDocumentElementWithPredicate ()
  90. {
  91. XmlDocument document = new XmlDocument ();
  92. document.LoadXml ("<foo><bar /></foo>");
  93. XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
  94. Assert (navigator.Matches ("foo[bar]"));
  95. }
  96. [Test]
  97. public void FalseMatchDocumentElementWithPredicate ()
  98. {
  99. XmlDocument document = new XmlDocument ();
  100. document.LoadXml ("<foo><bar /></foo>");
  101. XPathNavigator navigator = document.DocumentElement.CreateNavigator ();
  102. Assert (!navigator.Matches ("foo[baz]"));
  103. }
  104. [Test]
  105. public void MatchesAncestorsButNotCurrent ()
  106. {
  107. XPathNavigator nav = CreateNavigator ("<foo><bar><baz/></bar></foo>");
  108. nav.MoveToFirstChild (); // foo
  109. nav.MoveToFirstChild (); // bar
  110. nav.MoveToFirstChild (); // baz
  111. Assert (nav.Matches ("baz"));
  112. Assert (nav.Matches ("bar/baz"));
  113. Assert (!nav.Matches ("foo/bar"));
  114. }
  115. [Test]
  116. [ExpectedException (typeof (XPathException))]
  117. public void MatchesParentAxis ()
  118. {
  119. XPathNavigator nav = CreateNavigator ("<foo/>");
  120. nav.Matches ("..");
  121. }
  122. [Test]
  123. [ExpectedException (typeof (XPathException))]
  124. public void MatchesPredicatedParentAxis ()
  125. {
  126. XPathNavigator nav = CreateNavigator ("<foo/>");
  127. nav.Matches ("..[1]");
  128. }
  129. }
  130. }