Merge pull request #3809 from lateralusX/jlorenss/win-api-family-support-cleanup
[mono.git] / mcs / class / System.Xml.Linq / Test / System.Xml.Linq / XElementTest.cs
index 5a5f255094c74c579df300882bd87538267d25e7..f93bbabc3d88e21bca92fc48e741c150f261a945 100644 (file)
@@ -29,9 +29,11 @@ using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Linq;
+using System.Text;
 using System.Threading;
 using System.Xml;
 using System.Xml.Linq;
+using System.Xml.Serialization;
 
 using NUnit.Framework;
 
@@ -168,6 +170,7 @@ namespace MonoTests.System.Xml.Linq
                [Test]
                [ExpectedException (typeof (ArgumentException))]
                [Category ("NotDotNet")]
+               [Ignore ("see inline comment")]
                public void AddXDeclarationToElement ()
                {
                        XElement el = new XElement (XName.Get ("foo"));
@@ -272,7 +275,7 @@ namespace MonoTests.System.Xml.Linq
                [Test]
                [ExpectedException (typeof (ArgumentException))]
                [Category ("NotDotNet")]
-               [Category ("NotWorking")]
+               [Ignore ("see code comment")]
                // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
                public void AddAfterSelfXDeclaration ()
                {
@@ -804,7 +807,7 @@ namespace MonoTests.System.Xml.Linq
                {
                        XElement a = new XElement ("a", "7");
                        XElement b = new XElement ("b", new XCData ("  42 "));
-                       XElement c = new XElement ("c", " \r\n   13 \t  ");
+                       XElement c = new XElement ("c", " \r\n   13 \t  ".Replace ("\r\n", Environment.NewLine));
                        XElement d = new XElement ("d", -101);
                        XElement o = new XElement ("o", "0");
                        XElement l = new XElement ("l", "1");
@@ -817,13 +820,13 @@ namespace MonoTests.System.Xml.Linq
                        // Verify expected "cloning" and basic conversion behaviour as prerequisites
                        Assert.IsFalse (a.IsEmpty, "#1-1");
                        Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
-                       Assert.AreEqual (" \r\n   13 \t  ", c.Value, "#2-1");
+                       Assert.AreEqual (" \r\n   13 \t  ".Replace ("\r\n", Environment.NewLine), c.Value, "#2-1");
                        Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
                        Assert.AreNotSame (o, new XElement (o), "#3-1");
                        Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
                        Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
                        Assert.AreEqual ("<b><![CDATA[  42 ]]></b>", b.ToString (), "#3-3b");
-                       Assert.AreEqual ("<c> \r\n   13 \t  </c>", c.ToString (), "#3-3c");
+                       Assert.AreEqual ("<c> \r\n   13 \t  </c>".Replace ("\r\n", Environment.NewLine), c.ToString (), "#3-3c");
                        Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
                        Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
                        Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
@@ -1022,7 +1025,7 @@ namespace MonoTests.System.Xml.Linq
                        AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
                        Assert.AreEqual ("7", (string) new XElement (a), "a:string");
                        Assert.AreEqual ("  42 ", (string) new XElement (b), "b:string");
-                       Assert.AreEqual (" \r\n   13 \t  ", (string) new XElement (c), "c:string");
+                       Assert.AreEqual (" \r\n   13 \t  ".Replace ("\r\n", Environment.NewLine), (string) new XElement (c), "c:string");
                        Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
                        Assert.AreEqual ("0", (string) new XElement (o), "o:string");
                        Assert.AreEqual ("1", (string) new XElement (l), "l:string");
@@ -1657,6 +1660,7 @@ namespace MonoTests.System.Xml.Linq
                // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
                [ExpectedException (typeof (ArgumentException))]
                [Category ("NotDotNet")]
+               [Ignore ("see code comment")]
                public void SetValueXDeclaration ()
                {
                        var el = new XElement ("foo");
@@ -2050,5 +2054,121 @@ namespace MonoTests.System.Xml.Linq
                        XElement newElement = new XElement(ns + "geoloc");
                        Assert.AreEqual ("<geoloc xmlns=\"http://jabber.org/protocol/geoloc\" />", newElement.ToString (), "#1");
                }
+               
+               [Test] // bug #10194
+               public void SetElementValueNullOnNonExistingElement ()
+               {
+                       var xd = XDocument.Parse ("<foo />");
+                       xd.Root.SetElementValue (XName.Get ("bar"), null);
+               }
+               
+               [Test] // bug #11298
+               public void ReplaceAttributesIteratesContentsFirstThenRemove ()
+               {
+                       var xmlString = "<Class Id='1' Name='' Cluster='' xmlns='urn:x' />";
+                       var e = XDocument.Parse (xmlString).Root;
+                       var attrs = e.Attributes ()
+                               .Where (a => !a.IsNamespaceDeclaration)
+                               .Select (a => a.Name.Namespace != XNamespace.None ?
+                                                new XAttribute (XName.Get(a.Name.LocalName), a.Value) : a);
+                       e.ReplaceAttributes (attrs);
+                       Assert.IsNotNull (e.Attribute ("Id"), "#1");
+                       Assert.IsNotNull (e.Attribute ("Name"), "#2");
+                       Assert.IsNotNull (e.Attribute ("Cluster"), "#3");
+               }
+
+               [XmlType ("Root")]
+               public class SerializableClass
+               {
+                       [XmlAnyElement]
+                       public XElement Content;
+               }
+
+               [Test]
+               // Bug #12571
+               public void DeserializeXElement ()
+               {
+                       var xmlString = "<Root><Data /></Root>";
+
+                       var serializer = new XmlSerializer (typeof (SerializableClass));
+                       var res = serializer.Deserialize (new StringReader (xmlString));
+
+                       Assert.IsNotNull (res, "#1");
+                       Assert.AreEqual (typeof (SerializableClass), res.GetType (), "#2");
+                       var xe = (SerializableClass)res;
+                       Assert.AreEqual (xe.Content.ToString (), "<Data />", "#3");
+               }
+
+               [XmlType ("Root")]
+               public class DeserializeXElementArray_Data
+               {
+                       [XmlAnyElement]
+                       public XElement[] Content;
+               }
+
+               [Test]
+               public void DeserializeXElementArray ()
+               {
+                       var xmlString = "<Root><Data /></Root>";
+
+                       var serializer = new XmlSerializer (typeof (DeserializeXElementArray_Data));
+                       var res = serializer.Deserialize (new StringReader (xmlString));
+
+                       Assert.IsNotNull (res, "#1");
+                       Assert.AreEqual (typeof (DeserializeXElementArray_Data), res.GetType (), "#2");
+                       var xe = (DeserializeXElementArray_Data)res;
+                       Assert.AreEqual (xe.Content [0].ToString (), "<Data />", "#3");
+               }
+
+               [Test] // Bug #20151
+               public void XElementFromArrayWithNullValuesAsObject ()
+               {
+                       string[] content = {null, "content1", null, "content2"};
+                       var el = new XElement ("test", (object)content);
+                       Assert.AreEqual ("<test>content1content2</test>", el.ToString ());
+               }
+
+               [Test] // bug #14856
+               public void PossibleDuplicateNamespaces ()
+               {
+            string testXML =
+                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
+                "<TestElement xmlns=\"http://www.test.com/TestNamespace\" />";
+
+            using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (testXML)))
+            {
+                var root = XElement.Load (stream);
+                using (var savedStream = new MemoryStream ())
+                {
+                    var options = SaveOptions.None;
+
+                    // Comment out this line to make it not crash.
+                    options |= SaveOptions.OmitDuplicateNamespaces;
+
+                    root.Save (savedStream, options);
+                    savedStream.Flush ();
+                    savedStream.Position = 0;
+
+                    var settings = new XmlReaderSettings { IgnoreComments = true, IgnoreWhitespace = true };
+                    using (var xmlReader = XmlReader.Create (savedStream, settings))
+                    {
+                        while (xmlReader.Read ())
+                        {
+                        }
+                    }
+                }
+            }
+               }
+
+               [Test]
+               public void ParseVsReadXml ()
+               {
+                       var p = XElement.Parse ("<root xmlns='urn:foo'><foo><xxx /></foo><x:bar xmlns:x='urn:bar'><yyy /></x:bar><baz xmlns=''><zzz /></baz></root>");
+                       var r = XElement.Parse ("<foo />");
+                       XmlReader xr = XmlReader.Create (new StringReader ("<root xmlns='urn:foo'><foo><xxx /></foo><x:bar xmlns:x='urn:bar'><yyy /></x:bar><baz xmlns=''><zzz /></baz></root>"), new XmlReaderSettings ());
+                       ((IXmlSerializable)r).ReadXml (xr);
+
+                       Assert.IsTrue (XNode.DeepEquals (p, r), "The XElements were not equal.\nParse() expected:\n{0}\n\nBut ReadXml() was:\n{1}\n", p, r);
+               }
        }
 }