Author: miguel
Date: 2008-02-11 00:59:50 -0500 (Mon, 11 Feb 2008)
New Revision: 95417
Modified:
trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs
trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
trunk/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs
trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs
Log:
2008-02-11 Miguel de Icaza <[EMAIL PROTECTED]>
* XAttribute.cs, XElement.cs: typecasting operators that take
values that can be null (nullable types and string) should not
throw an exception if the XAttribute is null, but return null instead.
* Added awesome test suite using awesome C-x ( C-x ) C-x e based
code generation.
Modified: trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/ChangeLog
===================================================================
--- trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/ChangeLog 2008-02-11
05:44:55 UTC (rev 95416)
+++ trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/ChangeLog 2008-02-11
05:59:50 UTC (rev 95417)
@@ -1,3 +1,12 @@
+2008-02-11 Miguel de Icaza <[EMAIL PROTECTED]>
+
+ * XAttribute.cs, XElement.cs: typecasting operators that take
+ values that can be null (nullable types and string) should not
+ throw an exception if the XAttribute is null, but return null instead.
+
+ * Added awesome test suite using awesome C-x ( C-x ) C-x e based
+ code generation.
+
2008-02-10 Atsushi Enomoto <[EMAIL PROTECTED]>
* XElement.cs : .ctor(XName,object) was trying to set args as its
Modified: trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs
===================================================================
--- trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs
2008-02-11 05:44:55 UTC (rev 95416)
+++ trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XAttribute.cs
2008-02-11 05:59:50 UTC (rev 95417)
@@ -70,7 +70,8 @@
public static explicit operator bool? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (bool?) null :
XmlConvert.ToBoolean (attribute.value);
}
@@ -84,7 +85,8 @@
public static explicit operator DateTime? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (DateTime?) null :
XmlConvert.ToDateTime (attribute.value,
XmlDateTimeSerializationMode.RoundtripKind);
}
@@ -98,7 +100,8 @@
public static explicit operator decimal? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (decimal?) null :
XmlConvert.ToDecimal (attribute.value);
}
@@ -112,7 +115,8 @@
public static explicit operator double? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (double?) null :
XmlConvert.ToDouble (attribute.value);
}
@@ -126,7 +130,8 @@
public static explicit operator float? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (float?) null :
XmlConvert.ToSingle (attribute.value);
}
@@ -140,7 +145,8 @@
public static explicit operator Guid? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (Guid?) null :
XmlConvert.ToGuid (attribute.value);
}
@@ -154,7 +160,8 @@
public static explicit operator int? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (int?) null :
XmlConvert.ToInt32 (attribute.value);
}
@@ -168,7 +175,8 @@
public static explicit operator long? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (long?) null :
XmlConvert.ToInt64 (attribute.value);
}
@@ -184,7 +192,8 @@
public static explicit operator uint? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (uint?) null :
XmlConvert.ToUInt32 (attribute.value);
}
@@ -200,7 +209,8 @@
public static explicit operator ulong? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (ulong?) null :
XmlConvert.ToUInt64 (attribute.value);
}
@@ -214,14 +224,16 @@
public static explicit operator TimeSpan? (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value == null ? (TimeSpan?) null :
XmlConvert.ToTimeSpan (attribute.value);
}
public static explicit operator string (XAttribute attribute)
{
if (attribute == null)
- throw new ArgumentNullException ("attribute");
+ return null;
+
return attribute.value;
}
Modified: trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
===================================================================
--- trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs 2008-02-11
05:44:55 UTC (rev 95416)
+++ trunk/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs 2008-02-11
05:59:50 UTC (rev 95417)
@@ -88,7 +88,8 @@
public static explicit operator bool? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (bool?) null :
XmlConvert.ToBoolean (element.Value);
}
@@ -102,7 +103,8 @@
public static explicit operator DateTime? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (DateTime?) null :
XmlConvert.ToDateTime (element.Value,
XmlDateTimeSerializationMode.RoundtripKind);
}
@@ -116,7 +118,8 @@
public static explicit operator decimal? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (decimal?) null :
XmlConvert.ToDecimal (element.Value);
}
@@ -130,7 +133,8 @@
public static explicit operator double? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (double?) null :
XmlConvert.ToDouble (element.Value);
}
@@ -144,7 +148,8 @@
public static explicit operator float? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (float?) null :
XmlConvert.ToSingle (element.Value);
}
@@ -158,7 +163,8 @@
public static explicit operator Guid? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (Guid?) null :
XmlConvert.ToGuid (element.Value);
}
@@ -172,7 +178,8 @@
public static explicit operator int? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (int?) null :
XmlConvert.ToInt32 (element.Value);
}
@@ -186,7 +193,8 @@
public static explicit operator long? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (long?) null :
XmlConvert.ToInt64 (element.Value);
}
@@ -202,7 +210,8 @@
public static explicit operator uint? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (uint?) null :
XmlConvert.ToUInt32 (element.Value);
}
@@ -218,7 +227,8 @@
public static explicit operator ulong? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (ulong?) null :
XmlConvert.ToUInt64 (element.Value);
}
@@ -232,14 +242,16 @@
public static explicit operator TimeSpan? (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value == null ? (TimeSpan?) null :
XmlConvert.ToTimeSpan (element.Value);
}
public static explicit operator string (XElement element)
{
if (element == null)
- throw new ArgumentNullException ("element");
+ return null;
+
return element.Value;
}
Modified: trunk/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs
===================================================================
--- trunk/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs
2008-02-11 05:44:55 UTC (rev 95416)
+++ trunk/mcs/class/System.Xml.Linq/System.Xml.XPath/Extensions.cs
2008-02-11 05:59:50 UTC (rev 95417)
@@ -72,9 +72,10 @@
public static IEnumerable<XElement> XPathSelectElements (this
XNode node, string xpath, IXmlNamespaceResolver nsResolver)
{
XPathNodeIterator iter = CreateNavigator (node).Select
(xpath, nsResolver);
- foreach (XPathNavigator nav in iter)
+ foreach (XPathNavigator nav in iter){
if (nav.UnderlyingObject is XElement)
yield return (XElement)
nav.UnderlyingObject;
+ }
}
}
}
Modified: trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
===================================================================
--- trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
2008-02-11 05:44:55 UTC (rev 95416)
+++ trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XAttributeTest.cs
2008-02-11 05:59:50 UTC (rev 95417)
@@ -107,5 +107,24 @@
a = new XAttribute (XName.Get ("a"), " >_< ");
Assert.AreEqual ("a=\" >_< \"", a.ToString ());
}
+
+ [Test]
+ public void NullCasts ()
+ {
+ XAttribute a = null;
+
+ Assert.AreEqual (null, (bool?) a, "bool?");
+ Assert.AreEqual (null, (DateTime?) a, "DateTime?");
+ Assert.AreEqual (null, (decimal?) a, "decimal?");
+ Assert.AreEqual (null, (double?) a, "double?");
+ Assert.AreEqual (null, (float?) a, "float?");
+ Assert.AreEqual (null, (Guid?) a, "Guid?");
+ Assert.AreEqual (null, (int?) a, "int?");
+ Assert.AreEqual (null, (long?) a, "long?");
+ Assert.AreEqual (null, (uint?) a, "uint?");
+ Assert.AreEqual (null, (ulong?) a, "ulong?");
+ Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
+ Assert.AreEqual (null, (string) a, "string");
+ }
}
}
Modified: trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs
===================================================================
--- trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs
2008-02-11 05:44:55 UTC (rev 95416)
+++ trunk/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs
2008-02-11 05:59:50 UTC (rev 95417)
@@ -338,5 +338,24 @@
Assert.AreEqual ("x", ((XElement)
bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
Assert.AreEqual (null, ((XElement)
bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
}
+
+ [Test]
+ public void NullCasts ()
+ {
+ XElement a = null;
+
+ Assert.AreEqual (null, (bool?) a, "bool?");
+ Assert.AreEqual (null, (DateTime?) a, "DateTime?");
+ Assert.AreEqual (null, (decimal?) a, "decimal?");
+ Assert.AreEqual (null, (double?) a, "double?");
+ Assert.AreEqual (null, (float?) a, "float?");
+ Assert.AreEqual (null, (Guid?) a, "Guid?");
+ Assert.AreEqual (null, (int?) a, "int?");
+ Assert.AreEqual (null, (long?) a, "long?");
+ Assert.AreEqual (null, (uint?) a, "uint?");
+ Assert.AreEqual (null, (ulong?) a, "ulong?");
+ Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
+ Assert.AreEqual (null, (string) a, "string");
+ }
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches