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 fd5cd82bf94eb983506f26c5058100d5dd2c0fd5..f93bbabc3d88e21bca92fc48e741c150f261a945 100644 (file)
@@ -29,6 +29,7 @@ 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;
@@ -169,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"));
@@ -273,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 ()
                {
@@ -805,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");
@@ -818,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");
@@ -1023,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");
@@ -1658,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");
@@ -2081,7 +2084,6 @@ namespace MonoTests.System.Xml.Linq
                        public XElement Content;
                }
 
-#if NET_4_5
                [Test]
                // Bug #12571
                public void DeserializeXElement ()
@@ -2096,6 +2098,77 @@ namespace MonoTests.System.Xml.Linq
                        var xe = (SerializableClass)res;
                        Assert.AreEqual (xe.Content.ToString (), "<Data />", "#3");
                }
-#endif
+
+               [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);
+               }
        }
 }