2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.XML / Test / System.Xml.XPath / XPathNavigatorEvaluateTests.cs
1 //
2 // MonoTests.System.Xml.XPathNavigatorEvaluateTests
3 //
4 // Authors:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //   Martin Willemoes Hansen <mwh@sysrq.dk>
7 //
8 // (C) 2002 Kral Ferch
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 XPathNavigatorEvaluateTests : Assertion
22         {
23                 XmlDocument document;
24                 XPathNavigator navigator;
25                 XmlDocument document2;
26                 XPathNavigator navigator2;
27                 XmlDocument document3;
28                 XPathNavigator navigator3;
29                 XPathExpression expression;
30                 XPathNodeIterator iterator;
31
32                 [SetUp]
33                 public void GetReady ()
34                 {
35                         document = new XmlDocument ();
36                         document.LoadXml ("<foo><bar/><baz/><qux/><squonk/></foo>");
37                         navigator = document.CreateNavigator ();
38
39                         document2 = new XmlDocument ();
40                         document2.LoadXml ("<foo><bar baz='1'/><bar baz='2'/><bar baz='3'/></foo>");
41                         navigator2 = document2.CreateNavigator ();
42
43                         document3 = new XmlDocument ();
44                         document3.LoadXml ("<foo><bar/><baz/><qux/></foo>");
45                         navigator3 = document3.CreateNavigator ();
46                 }
47
48                 // Testing Core Funcetion Library functions defined at: http://www.w3.org/TR/xpath#corelib
49                 [Test]
50 #if !NET_2_0
51                 // .NET 2.0 is fixed for last() to return 1 for the
52                 // initial context position. Someone thinks we should follow
53                 // the fix, but in reality we should just discard this
54                 // pointless test. The positional value under those conditions
55                 // does not make any sense.
56                 [Category ("NotDotNet")]
57 #endif
58                 public void CoreFunctionNodeSetLast ()
59                 {
60                         expression = navigator.Compile("last()");
61                         iterator = navigator.Select("/foo");
62                         AssertEquals ("1", navigator.Evaluate ("last()").ToString());
63                         AssertEquals ("1", navigator.Evaluate (expression, null).ToString ());
64                         AssertEquals ("1", navigator.Evaluate (expression, iterator).ToString ());
65                         iterator = navigator.Select("/foo/*");
66                         AssertEquals ("4", navigator.Evaluate (expression, iterator).ToString ());
67                         
68                         AssertEquals("3", navigator2.Evaluate ("string(//bar[last()]/@baz)"));
69                 }
70
71                 [Test]
72 #if !NET_2_0
73                 // .NET 2.0 is fixed for position() to return 1 for the
74                 // initial context position. Someone thinks we should follow
75                 // the fix, but in reality we should just discard this
76                 // pointless test. The positional value under those conditions
77                 // does not make any sense.
78 //              [Category ("NotWorking")]
79                 [Category ("NotDotNet")]
80 #endif
81                 public void CoreFunctionNodeSetPosition ()
82                 {
83                         expression = navigator.Compile("position()");
84                         iterator = navigator.Select("/foo");
85                         AssertEquals ("#1", "1", navigator.Evaluate ("position()").ToString ());
86                         AssertEquals ("#2", "1", navigator.Evaluate (expression, null).ToString ());
87                         AssertEquals ("#3", "0", navigator.Evaluate (expression, iterator).ToString ());
88                         iterator = navigator.Select("/foo/*");
89                         AssertEquals ("#4", "0", navigator.Evaluate (expression, iterator).ToString ());
90                         iterator.MoveNext();
91                         AssertEquals ("#5", "1", navigator.Evaluate (expression, iterator).ToString ());
92                         iterator.MoveNext ();
93                         AssertEquals ("#6", "2", navigator.Evaluate (expression, iterator).ToString ());
94                         iterator.MoveNext ();
95                         AssertEquals ("#7", "3", navigator.Evaluate (expression, iterator).ToString ());
96                 }
97
98                 [Test]
99                 public void CoreFunctionNodeSetCount ()
100                 {
101                         AssertEquals ("5", navigator.Evaluate ("count(//*)").ToString ());
102                         AssertEquals ("1", navigator.Evaluate ("count(//foo)").ToString ());
103                         AssertEquals ("1", navigator.Evaluate ("count(/foo)").ToString ());
104                         AssertEquals ("1", navigator.Evaluate ("count(/foo/bar)").ToString ());
105
106                         AssertEquals ("3", navigator2.Evaluate ("count(//bar)").ToString ());
107                 }
108
109                 public void saveTestCoreFunctionNodeSetID ()
110                 {
111                         document.LoadXml (
112                                 "<!DOCTYPE foo [" +
113                                 "<!ELEMENT foo (bar)>" +
114                                 "<!ELEMENT bar EMPTY>" +
115                                 "<!ATTLIST bar baz ID #REQUIRED>" +
116                                 "]>" +
117                                 "<foo><bar baz='1' qux='hello' /><bar baz='2' qux='world' /></foo>");
118                         navigator = document.CreateNavigator();
119
120                         AssertEquals ("hello", navigator.Evaluate ("string(id('1')/@qux)").ToString ());
121                         AssertEquals ("world", navigator.Evaluate ("string(id('2')/@qux)").ToString ());
122                 }
123
124                 [Test]
125                 public void CoreFunctionLocalName ()
126                 {
127                         AssertEquals ("", navigator.Evaluate ("local-name()").ToString ());
128                         AssertEquals ("", navigator.Evaluate ("local-name(/bogus)").ToString ());
129                         AssertEquals ("foo", navigator.Evaluate ("local-name(/foo)").ToString ());
130                         AssertEquals ("bar", navigator3.Evaluate ("local-name(/foo/*)").ToString ());
131                 }
132
133                 // TODO:  umm.  Unable to make this return a namespace-uri so far...
134                 [Test]
135                 public void CoreFunctionNamespaceURI ()
136                 {
137                         document.LoadXml ("<foo:bar xmlns:foo='#foo'><foo:baz><foo:qux /></foo:baz></foo:bar>");
138                         navigator = document.CreateNavigator ();
139
140                         AssertEquals ("", navigator.Evaluate ("namespace-uri()").ToString ());
141                         AssertEquals ("", navigator.Evaluate ("namespace-uri(/bogus)").ToString ());
142                         //AssertEquals("foo", navigator.Evaluate ("namespace-uri(/bar)").ToString ());
143                         AssertEquals ("", navigator2.Evaluate ("namespace-uri(//bar)").ToString ());
144                 }
145
146                 public void saveTestCoreFunctionString ()
147                 {
148                         document.LoadXml ("<foo>hello<bar>world</bar><baz>how are you</baz></foo>");
149                         navigator = document.CreateNavigator ();
150
151                         AssertEquals ("world", navigator.Evaluate ("string(/foo/*)").ToString ());
152                         AssertEquals ("NaN", navigator.Evaluate ("string(0 div 0)").ToString ());
153                         
154                         try {
155                                 navigator.Evaluate ("string(+0)");
156                                 Fail ("Expected an XPathException to be thrown.");
157                         } catch (XPathException) {}
158                         
159                         AssertEquals ("0", navigator.Evaluate ("string(-0)").ToString ());                      
160                         AssertEquals ("Infinity", navigator.Evaluate ("string(1 div 0)").ToString ());
161                         AssertEquals ("-Infinity", navigator.Evaluate ("string(-1 div 0)").ToString ());
162                         AssertEquals ("45", navigator.Evaluate ("string(45)").ToString ());
163                         AssertEquals ("-22", navigator.Evaluate ("string(-22)").ToString ());
164                         AssertEquals ("0.25", navigator.Evaluate ("string(.25)").ToString ());
165                         AssertEquals ("-0.25", navigator.Evaluate ("string(-.25)").ToString ());
166                         AssertEquals ("2", navigator.Evaluate ("string(2.0)").ToString ());
167                         AssertEquals ("2.01", navigator.Evaluate ("string(2.01)").ToString ());
168                         AssertEquals ("-3", navigator.Evaluate ("string(-3.0)").ToString ());
169                         AssertEquals ("3.45", navigator.Evaluate ("string(3.45)").ToString ());
170
171                         // Wonder what this will look like under a different platform.
172                         AssertEquals("0.33333333333333331", navigator.Evaluate ("string(1 div 3)").ToString ());
173                 }
174
175                 [Test]
176                 public void CoreFunctionConcat ()
177                 {
178                         try {
179                                 navigator.Evaluate ("concat()");
180                                 Fail ("Expected an XPathException to be thrown.");
181                         } catch (XPathException) {}
182
183                         try {
184                                 navigator.Evaluate ("concat('foo')");
185                                 Fail ("Expected an XPathException to be thrown.");
186                         } catch (XPathException) {}
187
188                         AssertEquals ("foobar", navigator.Evaluate ("concat('foo', 'bar')").ToString ());
189                         AssertEquals ("foobarbaz", navigator.Evaluate ("concat('foo', 'bar', 'baz')").ToString ());
190                         AssertEquals ("foobarbazqux", navigator.Evaluate ("concat('foo', 'bar', 'baz', 'qux')").ToString ());
191                         AssertEquals ("foobarbazquxquux", navigator.Evaluate ("concat('foo', 'bar', 'baz', 'qux', 'quux')").ToString ());
192                 }
193
194                 [Test]
195                 public void CoreFunctionStartsWith ()
196                 {
197                         try {
198                                 navigator.Evaluate ("starts-with()");
199                                 Fail ("Expected an XPathException to be thrown.");
200                         } catch (XPathException) {}
201
202                         try {
203                                 navigator.Evaluate ("starts-with('foo')");
204                                 Fail ("Expected an XPathException to be thrown.");
205                         } catch (XPathException) {}
206
207                         try {
208                                 navigator.Evaluate ("starts-with('foo', 'bar', 'baz')");
209                                 Fail ("Expected an XPathException to be thrown.");
210                         } catch (XPathException) {}
211
212                         Assert ((bool)navigator.Evaluate ("starts-with('foobar', 'foo')"));
213                         Assert (!(bool)navigator.Evaluate ("starts-with('foobar', 'bar')"));
214                 }
215
216                 [Test]
217                 public void CoreFunctionContains ()
218                 {
219                         try {
220                                 navigator.Evaluate ("contains()");
221                                 Fail ("Expected an XPathException to be thrown.");
222                         } catch (XPathException) {}
223
224                         try {
225                                 navigator.Evaluate ("contains('foo')");
226                                 Fail ("Expected an XPathException to be thrown.");
227                         } catch (XPathException) {}
228
229                         try {
230                                 navigator.Evaluate ("contains('foobar', 'oob', 'baz')");
231                                 Fail ("Expected an XPathException to be thrown.");
232                         } catch (XPathException) {}
233
234                         Assert ((bool)navigator.Evaluate ("contains('foobar', 'oob')"));
235                         Assert (!(bool)navigator.Evaluate ("contains('foobar', 'baz')"));
236                 }
237         }
238 }