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