New test.
[mono.git] / mcs / class / System.XML / Test / System.Xml.XPath / XPathNavigatorTests.cs
index 484012241ba04f5ea9a06eb8be2d86005d4f216b..70527943fbea6e3214a87b7f5b9fdaa008ae5c61 100644 (file)
@@ -13,6 +13,7 @@ using System;
 using System.IO;
 using System.Xml;
 using System.Xml.XPath;
+using System.Xml.Xsl;
 
 using NUnit.Framework;
 
@@ -284,6 +285,128 @@ namespace MonoTests.System.Xml
                                nav.NodeType);
                }
 
+               [Test]
+               public void VariableReference ()
+               {
+                       XPathDocument xpd = new XPathDocument (
+                               new StringReader ("<root>sample text</root>"));
+                       XPathNavigator nav = xpd.CreateNavigator ();
+
+                       XPathExpression expr = nav.Compile ("foo(string(.),$idx)");
+                       XsltArgumentList args = new XsltArgumentList ();
+                       args.AddParam ("idx", "", 5);
+                       MyContext ctx = new MyContext (nav.NameTable as NameTable, args);
+                       ctx.AddNamespace ("x", "urn:foo");
+
+                       expr.SetContext (ctx);
+
+                       XPathNodeIterator iter = nav.Select ("/root");
+                       iter.MoveNext ();
+                       AssertEquals ("e", iter.Current.Evaluate (expr));
+               }
+
+               class MyContext : XsltContext
+               {
+                       XsltArgumentList args;
+
+                       public MyContext (NameTable nt, XsltArgumentList args)
+                               : base (nt)
+                       {
+                               this.args = args;
+                       }
+
+                       public override IXsltContextFunction ResolveFunction (
+                               string prefix, string name, XPathResultType [] argtypes)
+                       {
+                               if (name == "foo")
+                                       return new MyFunction (argtypes);
+                               return null;
+                       }
+
+                       public override IXsltContextVariable ResolveVariable (string prefix, string name)
+                       {
+                               return new MyVariable (name);
+                       }
+
+                       public override bool PreserveWhitespace (XPathNavigator nav)
+                       {
+                               return false;
+                       }
+
+                       public override int CompareDocument (string uri1, string uri2)
+                       {
+                               return String.CompareOrdinal (uri1, uri2);
+                       }
+
+                       public override bool Whitespace {
+                               get { return false; }
+                       }
+
+                       public object GetParam (string name, string ns)
+                       {
+                               return args.GetParam (name, ns);
+                       }
+               }
+
+               public class MyFunction : IXsltContextFunction
+               {
+                       XPathResultType [] argtypes;
+
+                       public MyFunction (XPathResultType [] argtypes)
+                       {
+                               this.argtypes = argtypes;
+                       }
+
+                       public XPathResultType [] ArgTypes {
+                               get { return argtypes; }
+                       }
+
+                       public int Maxargs {
+                               get { return 2; }
+                       }
+
+                       public int Minargs {
+                               get { return 2; }
+                       }
+
+                       public XPathResultType ReturnType {
+                               get { return XPathResultType.String; }
+                       }
+
+                       public object Invoke (XsltContext xsltContext,
+                               object [] args, XPathNavigator instanceContext)
+                       {
+                               return ((string) args [0]) [(int) (double) args [1]].ToString ();
+                       }
+               }
+
+               public class MyVariable : IXsltContextVariable
+               {
+                       string name;
+
+                       public MyVariable (string name)
+                       {
+                               this.name = name;
+                       }
+
+                       public object Evaluate (XsltContext ctx)
+                       {
+                               return ((MyContext) ctx).GetParam (name, String.Empty);
+                       }
+
+                       public bool IsLocal {
+                               get { return false; }
+                       }
+
+                       public bool IsParam {
+                               get { return false; }
+                       }
+
+                       public XPathResultType VariableType {
+                               get { return XPathResultType.Any; }
+                       }
+               }
+
 #if NET_2_0
                [Test]
                public void ValueAsBoolean ()
@@ -412,6 +535,63 @@ namespace MonoTests.System.Xml
                        nav.MoveToFirstChild ();
                        long l = nav.ValueAsLong;
                }
+
+               [Test] // bug #79874
+               public void InnerXmlText ()
+               {
+                       StringReader sr = new StringReader ("<Abc><Foo>Hello</Foo></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo");
+                       iter.MoveNext ();
+                       AssertEquals ("#1", "Hello", iter.Current.InnerXml);
+                       AssertEquals ("#2", "<Foo>Hello</Foo>", iter.Current.OuterXml);
+                       iter = nav.Select ("/Abc/Foo/text()");
+                       iter.MoveNext ();
+                       AssertEquals ("#3", String.Empty, iter.Current.InnerXml);
+                       AssertEquals ("#4", "Hello", iter.Current.OuterXml);
+               }
+
+               [Test] // bug #79875
+               public void InnerXmlAttribute ()
+               {
+                       StringReader sr = new StringReader ("<Abc><Foo attr='val1'/></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
+                       iter.MoveNext ();
+                       AssertEquals ("val1", iter.Current.InnerXml);
+               }
+
+               [Test]
+               public void InnerXmlTextEscape ()
+               {
+                       StringReader sr = new StringReader ("<Abc><Foo>Hello&lt;\r\nInnerXml</Foo></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo");
+                       iter.MoveNext ();
+                       AssertEquals ("#1", "Hello&lt;\r\nInnerXml", iter.Current.InnerXml);
+                       AssertEquals ("#2", "<Foo>Hello&lt;\r\nInnerXml</Foo>", iter.Current.OuterXml);
+                       iter = nav.Select ("/Abc/Foo/text()");
+                       iter.MoveNext ();
+                       AssertEquals ("#3", String.Empty, iter.Current.InnerXml);
+                       AssertEquals ("#4", "Hello&lt;\r\nInnerXml", iter.Current.OuterXml);
+               }
+
+               [Test]
+               [Category ("NotDotNet")] // .NET bug; it should escape value
+               public void InnerXmlAttributeEscape ()
+               {
+                       StringReader sr = new StringReader ("<Abc><Foo attr='val&quot;1&#13;&#10;&gt;'/></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
+                       iter.MoveNext ();
+                       AssertEquals ("val&quot;1&#10;&gt;", iter.Current.InnerXml);
+               }
 #endif
        }
 }