Adhere to desktop implementation of [Can]ConvertFrom (dotnet/corefx#35760)
authorAdeel Mujahid <adeelbm@outlook.com>
Mon, 4 Mar 2019 23:30:57 +0000 (01:30 +0200)
committerSantiago Fernandez Madero <safern@microsoft.com>
Mon, 4 Mar 2019 23:30:57 +0000 (15:30 -0800)
* Adhere to desktop implementation of [Can]ConvertFrom

* Add tests for InstanceDescriptor in TypeConverter

Commit migrated from https://github.com/dotnet/corefx/commit/f603cea8ca5bdb62b33b0d67c67ab84d6d50300f

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeConverter.cs
src/libraries/System.ComponentModel.TypeConverter/tests/TypeConverterTests.cs

index 52f9241..1b6fb46 100644 (file)
@@ -3,6 +3,7 @@
 // See the LICENSE file in the project root for more information.
 
 using System.Collections;
+using System.ComponentModel.Design.Serialization;
 using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 
@@ -23,7 +24,8 @@ namespace System.ComponentModel
         /// Gets a value indicating whether this converter can convert an object in the given
         /// source type to the native type of the converter using the context.
         /// </summary>
-        public virtual bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => false;
+        public virtual bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
+            => sourceType == typeof(InstanceDescriptor);
 
         /// <summary>
         /// Gets a value indicating whether this converter can convert an object to the given
@@ -50,6 +52,10 @@ namespace System.ComponentModel
         /// </summary>
         public virtual object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
         {
+            if (value is InstanceDescriptor instanceDescriptor)
+            {
+                return instanceDescriptor.Invoke();
+            }
             throw GetConvertFromException(value);
         }
 
index 781d5dc..e328003 100644 (file)
@@ -2,8 +2,10 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.ComponentModel.Design.Serialization;
 using System.Diagnostics;
 using System.Globalization;
+using System.Reflection;
 using Xunit;
 
 namespace System.ComponentModel.Tests
@@ -18,66 +20,87 @@ namespace System.ComponentModel.Tests
         [Fact]
         public static void CanConvertFrom_string()
         {
-            Assert.False(TypeConverterTests.s_converter.CanConvertFrom(typeof(string)));
+            Assert.False(s_converter.CanConvertFrom(typeof(string)));
+        }
+
+        [Fact]
+        public static void CanConvertFrom_InstanceDescriptor()
+        {
+            Assert.True(s_converter.CanConvertFrom(typeof(InstanceDescriptor)));
         }
 
         [Fact]
         public static void CanConvertFrom_string_WithContext()
         {
-            Assert.False(TypeConverterTests.s_converter.CanConvertFrom(
-                TypeConverterTests.s_context, typeof(string)));
+            Assert.False(s_converter.CanConvertFrom(s_context, typeof(string)));
         }
 
         [Fact]
         public static void CanConvertTo_string()
         {
-            Assert.True(TypeConverterTests.s_converter.CanConvertTo(typeof(string)));
+            Assert.True(s_converter.CanConvertTo(typeof(string)));
         }
 
         [Fact]
         public static void CanConvertTo_string_WithContext()
         {
-            Assert.True(TypeConverterTests.s_converter.CanConvertTo(
-                TypeConverterTests.s_context, typeof(string)));
+            Assert.True(s_converter.CanConvertTo(s_context, typeof(string)));
         }
 
         [Fact]
         public static void ConvertFrom_Negative()
         {
-            Assert.Throws<NotSupportedException>(
-                () => TypeConverterTests.s_converter.ConvertFrom("1"));
-            Assert.Throws<NotSupportedException>(
-                () => TypeConverterTests.s_converter.ConvertFrom(null));
-            Assert.Throws<NotSupportedException>(
-                () => TypeConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "1"));
+            Assert.Throws<NotSupportedException>(() => s_converter.ConvertFrom("1"));
+            Assert.Throws<NotSupportedException>(() => s_converter.ConvertFrom(null));
+            Assert.Throws<NotSupportedException>(() => s_converter.ConvertFrom(s_context, null, "1"));
         }
 
         [Fact]
         public static void ConvertFromInvariantString()
         {
-            Assert.Throws<NotSupportedException>(
-                () => TypeConverterTests.s_converter.ConvertFromInvariantString("1"));
+            Assert.Throws<NotSupportedException>(() => s_converter.ConvertFromInvariantString("1"));
         }
 
         [Fact]
         public static void ConvertFromString()
         {
-            Assert.Throws<NotSupportedException>(
-                () => TypeConverterTests.s_converter.ConvertFromString("1"));
+            Assert.Throws<NotSupportedException>(() => s_converter.ConvertFromString("1"));
+        }
+
+        [Fact]
+        public static void ConvertFrom_InstanceDescriptor()
+        {
+            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
+            DateTime testDateAndTime = DateTime.UtcNow;
+            ConstructorInfo ctor = typeof(DateTime).GetConstructor(new Type[]
+            {
+                typeof(int), typeof(int), typeof(int), typeof(int),
+                typeof(int), typeof(int), typeof(int)
+            });
+
+            InstanceDescriptor descriptor = new InstanceDescriptor(ctor, new object[]
+            {
+                testDateAndTime.Year, testDateAndTime.Month, testDateAndTime.Day, testDateAndTime.Hour,
+                testDateAndTime.Minute, testDateAndTime.Second, testDateAndTime.Millisecond
+            });
+
+            const string format = "dd MMM yyyy hh:mm";
+            object o = s_converter.ConvertFrom(descriptor);
+            Assert.Equal(testDateAndTime.ToString(format), ((DateTime)o).ToString(format));
         }
 
         [Fact]
         public static void ConvertFromString_WithContext()
         {
             Assert.Throws<NotSupportedException>(
-                () => TypeConverterTests.s_converter.ConvertFromString(TypeConverterTests.s_context, null, "1"));
+                () => s_converter.ConvertFromString(s_context, null, "1"));
         }
 
         [Fact]
         public static void ConvertTo_string()
         {
-            object o = TypeConverterTests.s_converter.ConvertTo(TypeConverterTests.c_conversionInputValue, typeof(string));
-            TypeConverterTests.VerifyConversionToString(o);
+            object o = s_converter.ConvertTo(c_conversionInputValue, typeof(string));
+            VerifyConversionToString(o);
         }
 
         [Fact]
@@ -88,25 +111,24 @@ namespace System.ComponentModel.Tests
                 CultureInfo.CurrentCulture = new CultureInfo("pl-PL");
 
                 Assert.Throws<ArgumentNullException>(
-                () => TypeConverterTests.s_converter.ConvertTo(TypeConverterTests.s_context, null, TypeConverterTests.c_conversionInputValue, null));
+                    () => s_converter.ConvertTo(s_context, null, c_conversionInputValue, null));
 
                 Assert.Throws<NotSupportedException>(
-                    () => TypeConverterTests.s_converter.ConvertTo(TypeConverterTests.s_context, null, TypeConverterTests.c_conversionInputValue, typeof(int)));
+                    () => s_converter.ConvertTo(s_context, null, c_conversionInputValue, typeof(int)));
 
-                object o = TypeConverterTests.s_converter.ConvertTo(
-                    TypeConverterTests.s_context, null, TypeConverterTests.c_conversionInputValue, typeof(string));
-                TypeConverterTests.VerifyConversionToString(o);
+                object o = s_converter.ConvertTo(s_context, null, c_conversionInputValue, typeof(string));
+                VerifyConversionToString(o);
 
-                o = TypeConverterTests.s_converter.ConvertTo(
-                    TypeConverterTests.s_context, CultureInfo.CurrentCulture, TypeConverterTests.c_conversionInputValue, typeof(string));
-                TypeConverterTests.VerifyConversionToString(o);
+                o = s_converter.ConvertTo(
+                    s_context, CultureInfo.CurrentCulture, c_conversionInputValue, typeof(string));
+                VerifyConversionToString(o);
 
-                o = TypeConverterTests.s_converter.ConvertTo(
-                    TypeConverterTests.s_context, CultureInfo.InvariantCulture, TypeConverterTests.c_conversionInputValue, typeof(string));
-                TypeConverterTests.VerifyConversionToString(o);
+                o = s_converter.ConvertTo(
+                    s_context, CultureInfo.InvariantCulture, c_conversionInputValue, typeof(string));
+                VerifyConversionToString(o);
 
-                string s = TypeConverterTests.s_converter.ConvertTo(
-                    TypeConverterTests.s_context, CultureInfo.InvariantCulture, new FormattableClass(), typeof(string)) as string;
+                string s = s_converter.ConvertTo(
+                    s_context, CultureInfo.InvariantCulture, new FormattableClass(), typeof(string)) as string;
                 Assert.NotNull(s);
                 Assert.Equal(FormattableClass.Token, s);
                 return SuccessExitCode;
@@ -116,25 +138,22 @@ namespace System.ComponentModel.Tests
         [Fact]
         public static void ConvertToInvariantString()
         {
-            object o = TypeConverterTests.s_converter.ConvertToInvariantString(
-                TypeConverterTests.c_conversionInputValue);
-            TypeConverterTests.VerifyConversionToString(o);
+            object o = s_converter.ConvertToInvariantString(c_conversionInputValue);
+            VerifyConversionToString(o);
         }
 
         [Fact]
         public static void ConvertToString()
         {
-            object o = TypeConverterTests.s_converter.ConvertToString(
-                TypeConverterTests.c_conversionInputValue);
-            TypeConverterTests.VerifyConversionToString(o);
+            object o = s_converter.ConvertToString(c_conversionInputValue);
+            VerifyConversionToString(o);
         }
 
         [Fact]
         public static void ConvertToString_WithContext()
         {
-            object o = TypeConverterTests.s_converter.ConvertToString(
-                TypeConverterTests.s_context, null, TypeConverterTests.c_conversionInputValue);
-            TypeConverterTests.VerifyConversionToString(o);
+            object o = s_converter.ConvertToString(s_context, null, c_conversionInputValue);
+            VerifyConversionToString(o);
         }
 
         [Fact]
@@ -152,7 +171,7 @@ namespace System.ComponentModel.Tests
         private static void VerifyConversionToString(object o)
         {
             Assert.True(o is string);
-            Assert.Equal(TypeConverterTests.c_conversionResult, (string)o);
+            Assert.Equal(c_conversionResult, (string)o);
         }
 
         private class TypeConverterHelper : TypeConverter