[System.Xml.Linq] Add error checking to the XElement constructors
authorAlan McGovern <alan.mcgovern@gmail.com>
Thu, 16 Aug 2012 14:23:17 +0000 (15:23 +0100)
committerAlan McGovern <alan.mcgovern@gmail.com>
Thu, 16 Aug 2012 14:24:42 +0000 (15:24 +0100)
Added matching tests.

mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs

index 7bd4e5635d4bf8fcc60d6ace2e52e50e1fa7a4a9..524324e47f1a341f4403bca6fafda1ebf48c3ea3 100644 (file)
@@ -52,12 +52,16 @@ namespace System.Xml.Linq
 
                public XElement (XName name, object content)
                {
+                       if (name == null)
+                               throw new ArgumentNullException ("name");
                        this.name = name;
                        Add (content);
                }
 
                public XElement (XElement other)
                {
+                       if (other == null)
+                               throw new ArgumentNullException ("other");
                        name = other.name;
                        Add (other.Attributes ());
                        Add (other.Nodes ());
@@ -65,17 +69,23 @@ namespace System.Xml.Linq
 
                public XElement (XName name)
                {
+                       if (name == null)
+                               throw new ArgumentNullException ("name");
                        this.name = name;
                }
 
                public XElement (XName name, params object [] content)
                {
+                       if (name == null)
+                               throw new ArgumentNullException ("name");
                        this.name = name;
                        Add (content);
                }
 
                public XElement (XStreamingElement other)
                {
+                       if (other == null)
+                               throw new ArgumentNullException ("other");
                        this.name = other.Name;
                        Add (other.Contents);
                }
index eb8012229ad0614b32c4b1e5019f8c496541c4fe..15bad621e9c584a1ab0d8fb4181d47db339fa8c7 100644 (file)
@@ -62,19 +62,11 @@ namespace MonoTests.System.Xml.Linq
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentNullException))]
-               public void ConstructorNameNull ()
+               public void Constructor_NullParameters ()
                {
-                       XAttribute a = new XAttribute (null, "v");
-                       
-               }
-
-               [Test]
-               [ExpectedException (typeof (ArgumentNullException))]
-               public void ConstructorValueNull ()
-               {
-                       XAttribute a = new XAttribute (XName.Get ("a"), null);
-                       
+                       AssertThrows<ArgumentNullException>(() => new XAttribute(null, "v"), "#1");
+                       AssertThrows<ArgumentNullException>(() => new XAttribute(XName.Get("a"), null), "#2");
+                       AssertThrows<ArgumentNullException>(() => new XAttribute((XAttribute) null), "#3");
                }
 
                [Test]
index e5cb66bc67dca0050132de635cad8c60a378eea7..59a3854316448cd5382f8638274399b305a9987e 100644 (file)
@@ -40,6 +40,20 @@ namespace MonoTests.System.Xml.Linq
        [TestFixture]
        public class XElementTest
        {
+
+               [Test]
+               public void Constructor_NullParameters()
+               {
+                       AssertThrows<ArgumentNullException>(() => new XElement((XName)null), "#1");
+                       AssertThrows<ArgumentNullException>(() => new XElement((XElement)null), "#2");
+                       AssertThrows<ArgumentNullException>(() => new XElement((XStreamingElement)null), "#3");
+                       AssertThrows<ArgumentNullException>(() => new XElement((XName)null, null), "#4");
+                       AssertThrows<ArgumentNullException>(() => new XElement((XName)null, null, null, null), "#5");
+
+                       // This is acceptable though
+                       new XElement(XName.Get("foo"), null);
+               }
+
                [Test] // xml declaration is skipped.
                public void LoadWithXmldecl ()
                {