Author: gert
Date: 2007-08-31 05:10:28 -0400 (Fri, 31 Aug 2007)
New Revision: 85092

Modified:
   trunk/mcs/class/System/System.ComponentModel/ChangeLog
   trunk/mcs/class/System/System.ComponentModel/EnumConverter.cs
   trunk/mcs/class/System/System.ComponentModel/TypeConverter.cs
   trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog
   trunk/mcs/class/System/Test/System.ComponentModel/EnumConverterTests.cs
   trunk/mcs/class/System/Test/System.ComponentModel/TypeConverterTests.cs
Log:
* TypeConverter.cs: Fixed GetConvertFromException and
GetConvertToException to match MS, and use these in ConvertFrom and
ConvertTo.
* EnumConverter.cs: Use IsFlags instead of duplicating code that 
checks for FlagsAttribute. When converting to InstanceDescriptor and
no corresponding enum field is found, then let base class throw a
NotSupportedException.
* TypeConverterTest.cs: Added tests for GetConvertFromException and
GetConvertToException.
* EnumConverterTest.cs: Enabled test for converting flag enums to
InstanceDescriptor. 


Modified: trunk/mcs/class/System/System.ComponentModel/ChangeLog
===================================================================
--- trunk/mcs/class/System/System.ComponentModel/ChangeLog      2007-08-31 
06:51:18 UTC (rev 85091)
+++ trunk/mcs/class/System/System.ComponentModel/ChangeLog      2007-08-31 
09:10:28 UTC (rev 85092)
@@ -1,3 +1,13 @@
+2007-08-31  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * TypeConverter.cs: Fixed GetConvertFromException and
+       GetConvertToException to match MS, and use these in ConvertFrom and
+       ConvertTo.
+       * EnumConverter.cs: Use IsFlags instead of duplicating code that
+       checks for FlagsAttribute. When converting to InstanceDescriptor and
+       no corresponding enum field is found, then let base class throw a
+       NotSupportedException.
+
 2007-08-30  Ivan N. Zlatev  <[EMAIL PROTECTED]>
        
        * EnumConverter.cs: Implemented conversion to InstanceDescriptor

Modified: trunk/mcs/class/System/System.ComponentModel/EnumConverter.cs
===================================================================
--- trunk/mcs/class/System/System.ComponentModel/EnumConverter.cs       
2007-08-31 06:51:18 UTC (rev 85091)
+++ trunk/mcs/class/System/System.ComponentModel/EnumConverter.cs       
2007-08-31 09:10:28 UTC (rev 85092)
@@ -84,7 +84,7 @@
                                return Enum.Format (type, value, "G");
                        } else if (destinationType == typeof 
(InstanceDescriptor) && value != null) {
                                string enumString = ConvertToString (context, 
culture, value);
-                               if (type.IsDefined (typeof (FlagsAttribute), 
false) && enumString.IndexOf (",") != -1) {
+                               if (IsFlags && enumString.IndexOf (",") != -1) {
                                        if (value is IConvertible) {
                                                Type underlyingType = 
Enum.GetUnderlyingType (type);
                                                object enumValue = 
((IConvertible)value).ToType (underlyingType, culture);
@@ -94,13 +94,12 @@
                                        }
                                } else {
                                        FieldInfo f = type.GetField 
(enumString);
-                                       if (f == null)
-                                               throw 
CreateValueNotValidException (value);
-                                       return new InstanceDescriptor (f, null);
+                                       if (f != null)
+                                               return new InstanceDescriptor 
(f, null);
                                }
 #if NET_2_0
                        } else if (destinationType == typeof (Enum[]) && value 
!= null) {
-                               if (!type.IsDefined (typeof (FlagsAttribute), 
false)) {
+                               if (!IsFlags) {
                                        return new Enum[] { (Enum) 
Enum.ToObject (type, value) };
                                } else {
                                        long valueLong = Convert.ToInt64 (
@@ -246,5 +245,4 @@
                        }
                }
        }
-
 }

Modified: trunk/mcs/class/System/System.ComponentModel/TypeConverter.cs
===================================================================
--- trunk/mcs/class/System/System.ComponentModel/TypeConverter.cs       
2007-08-31 06:51:18 UTC (rev 85091)
+++ trunk/mcs/class/System/System.ComponentModel/TypeConverter.cs       
2007-08-31 09:10:28 UTC (rev 85092)
@@ -75,11 +75,8 @@
                        if (value is InstanceDescriptor) {
                                return ((InstanceDescriptor) value).Invoke ();
                        }
-                       if (value == null)
-                               throw new NotSupportedException (this.GetType 
().Name
-                                       + " cannot convert from (null).");
-                       throw new NotSupportedException (this.GetType ().Name + 
-                               " cannot be created from '" + 
value.GetType().ToString() + "'");
+
+                       return GetConvertFromException (value);
                }
 
                public object ConvertFromInvariantString (string text)
@@ -125,13 +122,7 @@
                                return String.Empty;
                        }
 
-                       if (value == null)
-                               value = "(null)";
-
-                       throw new NotSupportedException (string.Format (
-                               CultureInfo.InvariantCulture, "'{0}' is unable "
-                               + "to convert '{1}' to '{2}'", this.GetType 
().Name,
-                               value, destinationType.FullName));
+                       return GetConvertToException (value, destinationType);
                }
 
                public string ConvertToInvariantString (object value)
@@ -161,14 +152,28 @@
 
                protected Exception GetConvertFromException (object value)
                {
-                       throw new NotSupportedException (this.ToString() + " 
cannot convert from '" + 
-                                                       
value.GetType().ToString() + "'");
+                       string destinationType;
+                       if (value == null)
+                               destinationType = "(null)";
+                       else
+                               destinationType = value.GetType ().FullName;
+
+                       throw new NotSupportedException (string.Format 
(CultureInfo.InvariantCulture,
+                               "{0} cannot convert from {1}.", this.GetType 
().Name,
+                               destinationType));
                }
 
                protected Exception GetConvertToException (object value, Type 
destinationType)
                {
-                       throw new NotSupportedException (this.ToString() + " 
cannot convert from '" + 
-                                                       
value.GetType().ToString() + "' to '" + destinationType.ToString() + "'");
+                       string sourceType;
+                       if (value == null)
+                               sourceType = "(null)";
+                       else
+                               sourceType = value.GetType ().FullName;
+
+                       throw new NotSupportedException (string.Format 
(CultureInfo.InvariantCulture,
+                               "'{0}' is unable to convert '{1}' to '{2}'.", 
this.GetType ().Name,
+                               sourceType, destinationType.FullName));
                }
 
                public object CreateInstance (IDictionary propertyValues)

Modified: trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog
===================================================================
--- trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog 2007-08-31 
06:51:18 UTC (rev 85091)
+++ trunk/mcs/class/System/Test/System.ComponentModel/ChangeLog 2007-08-31 
09:10:28 UTC (rev 85092)
@@ -1,3 +1,10 @@
+2007-08-31  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * TypeConverterTest.cs: Added tests for GetConvertFromException and
+       GetConvertToException.
+       * EnumConverterTest.cs: Enabled test for converting flag enums to
+       InstanceDescriptor.
+
 2007-08-25  Ivan N. Zlatev  <[EMAIL PROTECTED]>
 
        * NestedContainerTest.cs: add.

Modified: 
trunk/mcs/class/System/Test/System.ComponentModel/EnumConverterTests.cs
===================================================================
--- trunk/mcs/class/System/Test/System.ComponentModel/EnumConverterTests.cs     
2007-08-31 06:51:18 UTC (rev 85091)
+++ trunk/mcs/class/System/Test/System.ComponentModel/EnumConverterTests.cs     
2007-08-31 09:10:28 UTC (rev 85092)
@@ -614,7 +614,6 @@
                }
 
                [Test]
-               [NUnit.Framework.Category ("NotWorking")]
                public void ConvertTo_InstanceDescriptor_Flags ()
                {
                        InstanceDescriptor idesc;
@@ -725,8 +724,9 @@
                                Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#F2");
                                Assert.IsNull (ex.InnerException, "#F3");
                                Assert.IsNotNull (ex.Message, "#F4");
-                               Assert.IsTrue (ex.Message.IndexOf ("'(null)'") 
!= -1, "#F5");
-                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(InstanceDescriptor).FullName + "'") != -1, "#F6");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(EnumConverter).Name + "'") != -1, "#F5");
+                               Assert.IsTrue (ex.Message.IndexOf ("'(null)'") 
!= -1, "#F6");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(InstanceDescriptor).FullName + "'") != -1, "#F7");
                        }
 
                        try {
@@ -761,8 +761,9 @@
                                Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#I2");
                                Assert.IsNull (ex.InnerException, "#I3");
                                Assert.IsNotNull (ex.Message, "#I4");
-                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(E2).FullName + "'") != -1, "#I5");
-                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(InstanceDescriptor).FullName + "'") != -1, "#I6");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(EnumConverter).Name + "'") != -1, "#I5");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(E2).FullName + "'") != -1, "#I6");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(InstanceDescriptor).FullName + "'") != -1, "#I7");
                        }
 
                        converter = new EnumConverter (typeof (F2));
@@ -812,7 +813,6 @@
                                CultureInfo.InvariantCulture, E.Bb | E.Dd,
                                typeof (string)), "#A6");
 
-
                        try {
                                converter.ConvertTo (null, 
CultureInfo.InvariantCulture,
                                        (E) 666, typeof (string));

Modified: 
trunk/mcs/class/System/Test/System.ComponentModel/TypeConverterTests.cs
===================================================================
--- trunk/mcs/class/System/Test/System.ComponentModel/TypeConverterTests.cs     
2007-08-31 06:51:18 UTC (rev 85091)
+++ trunk/mcs/class/System/Test/System.ComponentModel/TypeConverterTests.cs     
2007-08-31 09:10:28 UTC (rev 85092)
@@ -99,6 +99,76 @@
                        properties = converter.GetProperties (null, null, null);
                        Assert.IsNull (properties, "#12");
                }
+
+               [Test]
+               public void GetConvertFromException ()
+               {
+                       MockTypeConverter converter = new MockTypeConverter ();
+
+                       try {
+                               converter.GetConvertFromException (null);
+                               Assert.Fail ("#A1");
+                       } catch (NotSupportedException ex) {
+                               // MockTypeConverter cannot convert from (null)
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsTrue (ex.Message.IndexOf (typeof 
(MockTypeConverter).Name) != -1, "#A5");
+                               Assert.IsTrue (ex.Message.IndexOf ("(null)") != 
-1, "#A6");
+                       }
+
+                       try {
+                               converter.GetConvertFromException ("B");
+                               Assert.Fail ("#B1");
+                       } catch (NotSupportedException ex) {
+                               // MockTypeConverter cannot convert from 
System.String
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#B2");
+                               Assert.IsNull (ex.InnerException, "#B3");
+                               Assert.IsNotNull (ex.Message, "#B4");
+                               Assert.IsTrue (ex.Message.IndexOf (typeof 
(MockTypeConverter).Name) != -1, "#B5");
+                               Assert.IsTrue (ex.Message.IndexOf (typeof 
(string).FullName) != -1, "#B6");
+                       }
+               }
+
+               [Test]
+               public void GetConvertToException ()
+               {
+                       MockTypeConverter converter = new MockTypeConverter ();
+
+                       try {
+                               converter.GetConvertToException (null, typeof 
(DateTime));
+                               Assert.Fail ("#A1");
+                       } catch (NotSupportedException ex) {
+                               // 'MockTypeConverter' is unable to convert 
'(null)'
+                               // to 'System.DateTime'
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(MockTypeConverter).Name + "'") != -1, "#A5");
+                               Assert.IsTrue (ex.Message.IndexOf ("'(null)'") 
!= -1, "#A6");
+                               Assert.IsTrue (ex.Message.IndexOf ("'" + typeof 
(DateTime).FullName + "'") != -1, "#A7");
+                       }
+
+                       try {
+                               converter.GetConvertToException ("B", typeof 
(DateTime));
+                               Assert.Fail ("#B1");
+                       } catch (NotSupportedException ex) {
+                               // 'MockTypeConverter' is unable to convert 
'System.String'
+                               // to 'System.DateTime'
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#B2");
+                               Assert.IsNull (ex.InnerException, "#B3");
+                               Assert.IsNotNull (ex.Message, "#B4");
+                               Assert.IsTrue (ex.Message.IndexOf (typeof 
(MockTypeConverter).Name) != -1, "#B5");
+                               Assert.IsTrue (ex.Message.IndexOf (typeof 
(string).FullName) != -1, "#B6");
+                       }
+               }
+
+               [ExpectedException (typeof (NullReferenceException))]
+               public void GetConvertToException_DestinationType_Null ()
+               {
+                       MockTypeConverter converter = new MockTypeConverter ();
+                       converter.GetConvertToException ("B", (Type) null);
+               }
        }
 
        [TypeConverter (typeof (AConverter))]
@@ -115,6 +185,19 @@
                }
        }
 
+       public class MockTypeConverter : TypeConverter
+       {
+               public new Exception GetConvertFromException (object value)
+               {
+                       return base.GetConvertFromException (value);
+               }
+
+               public new Exception GetConvertToException (object value, Type 
destinationType)
+               {
+                       return base.GetConvertToException (value, 
destinationType);
+               }
+       }
+
        public class AConverter : TypeConverter
        {
                public override PropertyDescriptorCollection GetProperties 
(ITypeDescriptorContext context, object value, Attribute[] attributes)

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to