New tests, updates
[mono.git] / mcs / class / System.XML / Test / System.Xml.XPath / XPathNavigatorCommonTests.cs
index 76b23aa7bb35413553d30b72e9300c6e05998d0f..08afa61e3139382507aea42b1001f890c2e90557 100644 (file)
@@ -8,6 +8,7 @@
 //
 
 using System;
+using System.IO;
 using System.Xml;
 using System.Xml.XPath;
 using NUnit.Framework;
@@ -40,6 +41,13 @@ namespace MonoTests.System.Xml
                        return xpathDocument.CreateNavigator ();
                }
 
+               private XPathNavigator GetXPathDocumentNavigator (XmlNode node, XmlSpace space)
+               {
+                       XmlNodeReader xr = new XmlNodeReader (node);
+                       xpathDocument = new XPathDocument (xr, space);
+                       return xpathDocument.CreateNavigator ();
+               }
+
                private void AssertNavigator (string label, XPathNavigator nav, XPathNodeType type, string prefix, string localName, string ns, string name, string value, bool hasAttributes, bool hasChildren, bool isEmptyElement)
                {
                        label += nav.GetType ();
@@ -308,8 +316,10 @@ namespace MonoTests.System.Xml
 
                        nav.MoveToFirstChild ();        // body
                        // Test difference between Local and ExcludeXml
-                       Assert (!nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
-                       Assert (nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml));
+                       Assert ("Local should fail",
+                               !nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
+                       Assert ("ExcludeXml should succeed",
+                               nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml));
                        AssertNavigator ("#7", nav, XPathNodeType.Namespace,
                                "", "", "", "", xhtml, false, false, false);
 
@@ -327,7 +337,7 @@ namespace MonoTests.System.Xml
                [Test]
                public void MoveToNamespaces ()
                {
-                       string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";\r
+                       string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";
 
                        nav = GetXmlDocumentNavigator (xml);
                        MoveToNamespaces (nav);
@@ -337,20 +347,20 @@ namespace MonoTests.System.Xml
 
                private void MoveToNamespaces (XPathNavigator nav)
                {
-                       XPathNodeIterator iter = nav.Select ("//e");\r
-                       iter.MoveNext ();\r
-                       nav.MoveTo (iter.Current);\r
-                       AssertEquals ("e", nav.Name);\r
-                       nav.MoveToFirstNamespace ();\r
-                       AssertEquals ("x", nav.Name);\r
-                       nav.MoveToNextNamespace ();\r
-                       AssertEquals ("xml", nav.Name);\r
+                       XPathNodeIterator iter = nav.Select ("//e");
+                       iter.MoveNext ();
+                       nav.MoveTo (iter.Current);
+                       AssertEquals ("e", nav.Name);
+                       nav.MoveToFirstNamespace ();
+                       AssertEquals ("x", nav.Name);
+                       nav.MoveToNextNamespace ();
+                       AssertEquals ("xml", nav.Name);
                }
 
                [Test]
                public void IsDescendant ()
                {
-                       string xml = "<a><b/><c/><d><e attr='a'/></d></a>";\r
+                       string xml = "<a><b/><c/><d><e attr='a'/></d></a>";
 
                        nav = GetXmlDocumentNavigator (xml);
                        IsDescendant (nav);
@@ -360,29 +370,29 @@ namespace MonoTests.System.Xml
 
                private void IsDescendant (XPathNavigator nav)
                {
-                       XPathNavigator tmp = nav.Clone ();\r
-                       XPathNodeIterator iter = nav.Select ("//e");\r
-                       iter.MoveNext ();\r
-                       Assert (nav.MoveTo (iter.Current));\r
-                       Assert (nav.MoveToFirstAttribute ());\r
-                       AssertEquals ("attr", nav.Name);\r
-                       AssertEquals ("", tmp.Name);\r
-                       Assert (tmp.IsDescendant (nav));\r
-                       Assert (!nav.IsDescendant (tmp));\r
-                       tmp.MoveToFirstChild ();\r
-                       AssertEquals ("a", tmp.Name);\r
-                       Assert (tmp.IsDescendant (nav));\r
-                       Assert (!nav.IsDescendant (tmp));\r
-                       tmp.MoveTo (iter.Current);\r
-                       AssertEquals ("e", tmp.Name);\r
-                       Assert (tmp.IsDescendant (nav));\r
-                       Assert (!nav.IsDescendant (tmp));\r
+                       XPathNavigator tmp = nav.Clone ();
+                       XPathNodeIterator iter = nav.Select ("//e");
+                       iter.MoveNext ();
+                       Assert (nav.MoveTo (iter.Current));
+                       Assert (nav.MoveToFirstAttribute ());
+                       AssertEquals ("attr", nav.Name);
+                       AssertEquals ("", tmp.Name);
+                       Assert (tmp.IsDescendant (nav));
+                       Assert (!nav.IsDescendant (tmp));
+                       tmp.MoveToFirstChild ();
+                       AssertEquals ("a", tmp.Name);
+                       Assert (tmp.IsDescendant (nav));
+                       Assert (!nav.IsDescendant (tmp));
+                       tmp.MoveTo (iter.Current);
+                       AssertEquals ("e", tmp.Name);
+                       Assert (tmp.IsDescendant (nav));
+                       Assert (!nav.IsDescendant (tmp));
                }
 
                [Test]
                public void LiterallySplittedText ()
                {
-                       string xml = "<root><![CDATA[test]]> string</root>";\r
+                       string xml = "<root><![CDATA[test]]> string</root>";
 
                        nav = GetXmlDocumentNavigator (xml);
                        LiterallySplittedText (nav);
@@ -392,10 +402,10 @@ namespace MonoTests.System.Xml
 
                private void LiterallySplittedText (XPathNavigator nav)
                {
-                       nav.MoveToFirstChild ();\r
-                       nav.MoveToFirstChild ();\r
-                       AssertEquals (XPathNodeType.Text, nav.NodeType);\r
-                       AssertEquals ("test string", nav.Value);\r
+                       nav.MoveToFirstChild ();
+                       nav.MoveToFirstChild ();
+                       AssertEquals (XPathNodeType.Text, nav.NodeType);
+                       AssertEquals ("test string", nav.Value);
                }
 
                // bug #75609
@@ -416,5 +426,67 @@ namespace MonoTests.System.Xml
                        XPathNodeIterator iter = nav.SelectChildren ("foo", "urn:foo");
                        AssertEquals (2, iter.Count);
                }
+
+#if NET_2_0
+
+               [Test]
+               // bug #78067
+               public void OuterXml ()
+               {
+                       string xml = @"<?xml version=""1.0""?>
+<one>
+        <two>Some data.</two>
+</one>";
+
+                       nav = GetXmlDocumentNavigator (xml);
+                       OuterXml (nav);
+                       nav = GetXPathDocumentNavigator (document);
+                       OuterXml (nav);
+               }
+
+               private void OuterXml (XPathNavigator nav)
+               {
+                       string ret = @"<one>
+  <two>Some data.</two>
+</one>";
+                       AssertEquals (ret, nav.OuterXml.Replace ("\r\n", "\n"));
+               }
+
+               [Test]
+               public void ReadSubtreeLookupNamespace ()
+               {
+                       string xml = "<x:foo xmlns:x='urn:x'><bar>x:val</bar></x:foo>";
+                       var doc = new XmlDocument ();
+                       doc.LoadXml (xml);
+                       XPathNavigator nav = doc.LastChild.LastChild.CreateNavigator ();
+                       var xr = nav.ReadSubtree ();
+                       xr.MoveToContent ();
+                       xr.Read (); // should be at x:val
+                       AssertEquals ("urn:x", xr.LookupNamespace ("x"));
+               }
+#endif
+
+               [Test]
+               public void GetNamespaceConsistentTree ()
+               {
+                       document.PreserveWhitespace = true;
+
+                       string xml = "<x:root xmlns:x='urn:x'>  <x:foo xmlns='ns1'> <x:bar /> </x:foo>  <x:foo xmlns:y='ns2'> <x:bar /> </x:foo></x:root>";
+                       nav = GetXmlDocumentNavigator (xml);
+                       GetNamespaceConsistentTree (nav);
+                       nav = GetXPathDocumentNavigator (document, XmlSpace.Preserve);
+                       GetNamespaceConsistentTree (nav);
+               }
+
+               private void GetNamespaceConsistentTree (XPathNavigator nav)
+               {
+                       nav.MoveToFirstChild ();
+                       nav.MoveToFirstChild ();
+                       nav.MoveToNext ();
+                       AssertEquals ("#1." + nav.GetType (), "ns1", nav.GetNamespace (""));
+                       nav.MoveToNext ();
+                       nav.MoveToNext ();
+                       AssertEquals ("#2." + nav.GetType (), "", nav.GetNamespace (""));
+               }
        }
-}
+}
\ No newline at end of file