Esempio n. 1
0
        public void ConvertFrom_DestinationType_Success()
        {
            Assert.All(ConvertFromTestData(), convertTest =>
            {
                // We need to duplicate this test code as RemoteInvoke can't
                // create "this" as the declaring type is an abstract class.
                if (convertTest.RemoteInvokeCulture == null)
                {
                    if (convertTest.Source != null)
                    {
                        Assert.Equal(convertTest.CanConvert, Converter.CanConvertFrom(convertTest.Context, convertTest.Source.GetType()));
                    }

                    if (convertTest.NetCoreExceptionType == null)
                    {
                        object actual = Converter.ConvertFrom(convertTest.Context, convertTest.Culture, convertTest.Source);
                        Assert.Equal(convertTest.Expected, actual);
                    }
                    else
                    {
                        AssertExtensions.Throws(convertTest.NetCoreExceptionType, convertTest.NetFrameworkExceptionType, () => Converter.ConvertFrom(convertTest.Context, convertTest.Culture, convertTest.Source));
                    }
                }
                else
                {
                    RemoteExecutor.Invoke((typeName, testString) =>
                    {
                        // Deserialize the current test.
                        TypeConverterTestBase testBase = (TypeConverterTestBase)Activator.CreateInstance(Type.GetType(typeName));
                        ConvertTest test = ConvertTest.FromSerializedString(testString);

                        CultureInfo.CurrentCulture = test.RemoteInvokeCulture;
                        if (test.Source != null)
                        {
                            Assert.Equal(test.CanConvert, testBase.Converter.CanConvertFrom(test.Context, test.Source.GetType()));
                        }

                        if (test.NetCoreExceptionType == null)
                        {
                            object actual = testBase.Converter.ConvertFrom(test.Context, test.Culture, test.Source);
                            Assert.Equal(test.Expected, actual);
                        }
                        else
                        {
                            AssertExtensions.Throws(test.NetCoreExceptionType, test.NetFrameworkExceptionType, () => testBase.Converter.ConvertFrom(test.Context, test.Culture, test.Source));
                        }

                        return(RemoteExecutor.SuccessExitCode);
                    }, this.GetType().AssemblyQualifiedName, convertTest.GetSerializedString()).Dispose();
                }
            });
        }
        public void ConvertTo_DestinationType_Success()
        {
            Assert.All(ConvertToTestData(), convertTest =>
            {
                // We need to duplicate this test code as RemoteInvoke can't
                // create "this" as the declaring type is an abstract class.
                if (convertTest.RemoteInvokeCulture == null)
                {
                    Assert.Equal(convertTest.CanConvert, Converter.CanConvertTo(convertTest.Context, convertTest.DestinationType));

                    if (convertTest.CanConvert)
                    {
                        object actual = Converter.ConvertTo(convertTest.Context, convertTest.Culture, convertTest.Source, convertTest.DestinationType);
                        AssertEqualInstanceDescriptor(convertTest.Expected, actual);
                    }
                    else
                    {
                        Assert.Throws <NotSupportedException>(() => Converter.ConvertTo(convertTest.Context, convertTest.Culture, convertTest.Source, convertTest.DestinationType));
                    }
                }
                else
                {
                    RemoteExecutorForUap.Invoke((typeName, testString) =>
                    {
                        // Deserialize the current test.
                        TypeConverterTestBase testBase = (TypeConverterTestBase)Activator.CreateInstance(Type.GetType(typeName));
                        ConvertTest test = ConvertTest.FromSerializedString(testString);

                        using (new ThreadCultureChange(test.RemoteInvokeCulture))
                        {
                            Assert.Equal(test.CanConvert, testBase.Converter.CanConvertTo(test.Context, test.DestinationType));

                            if (test.CanConvert)
                            {
                                object actual = testBase.Converter.ConvertTo(test.Context, test.Culture, test.Source, test.DestinationType);
                                Assert.Equal(test.Expected, actual);
                            }
                            else
                            {
                                Assert.Throws <NotSupportedException>(() => testBase.Converter.ConvertTo(test.Context, test.Culture, test.Source, test.DestinationType));
                            }
                        }
                    }, this.GetType().AssemblyQualifiedName, convertTest.GetSerializedString()).Dispose();
                }
            });
        }