Exemple #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 override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Valid("  a  ", 'a'));

            yield return(ConvertTest.Valid("    ", '\0'));

            yield return(ConvertTest.Valid("", '\0'));

            yield return(ConvertTest.Throws <FormatException>("aa"));

            yield return(ConvertTest.CantConvertFrom(1));

            yield return(ConvertTest.CantConvertFrom(null));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            yield return(ConvertTest.Valid('a', "a"));

            yield return(ConvertTest.Valid('\u20AC', "\u20AC", CultureInfo.InvariantCulture));

            yield return(ConvertTest.Valid('\0', string.Empty));

            yield return(ConvertTest.CantConvertTo('a', typeof(char)));

            yield return(ConvertTest.CantConvertTo('a', typeof(InstanceDescriptor)));

            yield return(ConvertTest.CantConvertTo('a', typeof(object)));
        }
Exemple #4
0
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            yield return(ConvertTest.Valid((short)-1, "-1"));

            yield return(ConvertTest.Valid((short)2, (short)2, CultureInfo.InvariantCulture));

            yield return(ConvertTest.Valid((short)3, (float)3.0));

            yield return(ConvertTest.Valid((short)-1, "?1", new CustomPositiveSymbolCulture()));

            yield return(ConvertTest.CantConvertTo((short)3, typeof(InstanceDescriptor)));

            yield return(ConvertTest.CantConvertTo((short)3, typeof(object)));
        }
        public void IsEmptyTest()
        {
            object value = null;

            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = "test";
            Assert.IsFalse(ConvertEx.IsEmpty(value));

            value = "";
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = int.MinValue;
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = int.MaxValue;
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = 0;
            Assert.IsFalse(ConvertEx.IsEmpty(value));

            value = new char();
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = DateTime.MinValue;
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = DateTime.Parse("7/4/2008");
            Assert.IsFalse(ConvertEx.IsEmpty(value));

            value = String.Empty;
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = ConvertTest.Empty;
            Assert.IsTrue(ConvertEx.IsEmpty(value));

            value = new ConvertTest()
            {
                X = 5, Y = 10
            };
            Assert.IsFalse(ConvertEx.IsEmpty(value));

            value = new ConvertTest()
            {
                X = 0, Y = 0
            };
            Assert.IsTrue(ConvertEx.IsEmpty(value));
        }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Valid("  1000.00:00:00  ", new TimeSpan(1000, 0, 0, 0, 0), CultureInfo.InvariantCulture));

            yield return(ConvertTest.Valid("1000", new TimeSpan(1000, 0, 0, 0, 0)));

            yield return(ConvertTest.Throws <FormatException>("invalid"));

            yield return(ConvertTest.Throws <FormatException>("  "));

            yield return(ConvertTest.Throws <FormatException>(string.Empty));

            yield return(ConvertTest.CantConvertFrom(1));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            var timeSpan = new TimeSpan(1000, 0, 0, 0, 0);

            yield return(ConvertTest.Valid(timeSpan, timeSpan.ToString()));

            yield return(ConvertTest.Valid(
                             timeSpan,
                             new InstanceDescriptor(
                                 typeof(TimeSpan).GetMethod(nameof(TimeSpan.Parse), new Type[] { typeof(string) }),
                                 new object[] { timeSpan.ToString() }
                                 )
                             ));

            yield return(ConvertTest.CantConvertTo(new TimeSpan(1000, 0, 0, 0, 0), typeof(object)));
        }
Exemple #8
0
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Valid(" {30da92c0-23e8-42a0-ae7c-734a0e5d2782}", new Guid(0x30da92c0, 0x23e8, 0x42a0, 0xae, 0x7c, 0x73, 0x4a, 0x0e, 0x5d, 0x27, 0x82)));

            yield return(ConvertTest.Valid("{30da92c0-23e8-42a0-ae7c-734a0e5d2782}", new Guid(0x30da92c0, 0x23e8, 0x42a0, 0xae, 0x7c, 0x73, 0x4a, 0x0e, 0x5d, 0x27, 0x82)));

            yield return(ConvertTest.Valid(" \t\r\n {30da92c0-23e8-42a0-ae7c-734a0e5d2782} \t\r\n ", new Guid(0x30da92c0, 0x23e8, 0x42a0, 0xae, 0x7c, 0x73, 0x4a, 0x0e, 0x5d, 0x27, 0x82)));

            yield return(ConvertTest.Throws <FormatException>(""));

            yield return(ConvertTest.Throws <FormatException>("invalid"));

            yield return(ConvertTest.CantConvertFrom(1));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
        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();
                }
            });
        }
Exemple #10
0
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            const string GuidString = "30da92c0-23e8-42a0-ae7c-734a0e5d2782";

            yield return(ConvertTest.Valid(new Guid(0x30da92c0, 0x23e8, 0x42a0, 0xae, 0x7c, 0x73, 0x4a, 0x0e, 0x5d, 0x27, 0x82), GuidString));

            yield return(ConvertTest.Valid(
                             new Guid(0x30da92c0, 0x23e8, 0x42a0, 0xae, 0x7c, 0x73, 0x4a, 0x0e, 0x5d, 0x27, 0x82),
                             new InstanceDescriptor(
                                 typeof(Guid).GetConstructor(new Type[] { typeof(string) }),
                                 new object[] { GuidString }
                                 )
                             ));

            yield return(ConvertTest.CantConvertTo(new Guid(), typeof(Guid)));

            yield return(ConvertTest.CantConvertTo(new Guid(), typeof(int)));
        }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            // This does actually succeed despite CanConvertFrom returning false.
            ConvertTest noContext = ConvertTest.Valid("reference name", null).WithContext(null);

            noContext.CanConvert = false;
            yield return(noContext);

            // No IReferenceService or IContainer.
            yield return(ConvertTest.Valid("", null));

            yield return(ConvertTest.Valid("   ", null));

            yield return(ConvertTest.Valid("(none)", null).WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid("nothing", null));

            // IReferenceService.
            var component        = new TestComponent();
            var referenceService = new TestReferenceService();

            referenceService.AddReference("reference name", component);
            var contextWithReferenceCollection = new TestTypeDescriptorContext(referenceService);

            yield return(ConvertTest.Valid("reference name", component).WithContext(contextWithReferenceCollection));

            yield return(ConvertTest.Valid("no such reference", null).WithContext(contextWithReferenceCollection));

            // IContainer.
            var container = new Container();

            container.Add(component, "reference name");
            var contextWithContainer = new TestTypeDescriptorContext {
                Container = container
            };

            yield return(ConvertTest.Valid("reference name", component).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid("no such reference", null).WithContext(contextWithContainer));

            yield return(ConvertTest.CantConvertFrom(1));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            yield return(ConvertTest.Valid((decimal) - 1, "-1"));

            yield return(ConvertTest.Valid(1.1m, 1.1m.ToString()));

            yield return(ConvertTest.Valid(3.3m, (float)3.3));

            yield return(ConvertTest.Valid((decimal) - 1, "?1", new CustomPositiveSymbolCulture()));

            yield return(ConvertTest.Valid((decimal)3, new InstanceDescriptor(
                                               typeof(decimal).GetConstructor(new Type[] { typeof(int[]) }),
                                               new object[] { new int[] { 3, 0, 0, 0 } }
                                               )));

            yield return(ConvertTest.CantConvertTo((decimal)3, typeof(decimal)));

            yield return(ConvertTest.CantConvertTo((decimal)3, typeof(object)));
        }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            DateTime date = new DateTime(1998, 12, 5);

            yield return(ConvertTest.Valid("", DateTime.MinValue));

            yield return(ConvertTest.Valid("    ", DateTime.MinValue));

            yield return(ConvertTest.Valid(date.ToString(), date));

            yield return(ConvertTest.Valid(date.ToString(CultureInfo.InvariantCulture.DateTimeFormat), date, CultureInfo.InvariantCulture));

            yield return(ConvertTest.Valid(" " + date.ToString(CultureInfo.InvariantCulture.DateTimeFormat) + " ", date, CultureInfo.InvariantCulture));

            yield return(ConvertTest.Throws <FormatException>("invalid"));

            yield return(ConvertTest.CantConvertFrom(new object()));

            yield return(ConvertTest.CantConvertFrom(1));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            yield return(ConvertTest.Valid(new CustomCultureInfo(), "nl-BE"));

            yield return(ConvertTest.Valid(null, "(Default)"));

            yield return(ConvertTest.Valid(CultureInfo.InvariantCulture, "(Default)"));

            yield return(ConvertTest.Valid(CultureInfo.InvariantCulture, "(Default)", CultureInfo.InvariantCulture));

            yield return(ConvertTest.Valid(new CultureInfo("nl-BE"), "nl-BE"));

            yield return(ConvertTest.Valid(1, "1"));

            yield return(ConvertTest.CantConvertTo(CultureInfo.InvariantCulture, typeof(object)));

            yield return(ConvertTest.CantConvertTo(CultureInfo.InvariantCulture, typeof(CultureInfo)));

            yield return(ConvertTest.CantConvertTo(CultureInfo.InvariantCulture, typeof(int)));
        }
Exemple #15
0
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            TimeOnly timeOnly = new TimeOnly(10, 30, 50);

            yield return(ConvertTest.Valid("", TimeOnly.MinValue));

            yield return(ConvertTest.Valid("    ", TimeOnly.MinValue));

            yield return(ConvertTest.Valid(timeOnly.ToString(), TimeOnly.Parse(timeOnly.ToString())));

            yield return(ConvertTest.Valid(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat),
                                           TimeOnly.Parse(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat))));

            yield return(ConvertTest.Valid(" " + timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat) + " ",
                                           TimeOnly.Parse(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat))));

            yield return(ConvertTest.Throws <FormatException>("invalid"));

            yield return(ConvertTest.CantConvertFrom(new object()));

            yield return(ConvertTest.CantConvertFrom(1));
        }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Valid("1", (short)1));

            yield return(ConvertTest.Valid("-1  ", (short)-1));

            yield return(ConvertTest.Valid("#2", (short)2));

            yield return(ConvertTest.Valid("+7", (short)7));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("8.0"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>(""));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("bad"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("32768"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("-32769"));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
Exemple #17
0
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Valid("1", (sbyte)1));

            yield return(ConvertTest.Valid(" -1 ", (sbyte)-1));

            yield return(ConvertTest.Valid("#2", (sbyte)2));

            yield return(ConvertTest.Valid(" #2 ", (sbyte)2));

            yield return(ConvertTest.Valid("0x3", (sbyte)3));

            yield return(ConvertTest.Valid("0X3", (sbyte)3));

            yield return(ConvertTest.Valid(" 0X3 ", (sbyte)3));

            yield return(ConvertTest.Valid("&h4", (sbyte)4));

            yield return(ConvertTest.Valid("&H4", (sbyte)4));

            yield return(ConvertTest.Valid(" &H4 ", (sbyte)4));

            yield return(ConvertTest.Valid("+5", (sbyte)5));

            yield return(ConvertTest.Valid(" +5 ", (sbyte)5));

            yield return(ConvertTest.Valid("!1", (sbyte)1, new CustomPositiveSymbolCulture()));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("128"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("-129"));

            foreach (ConvertTest test in base.ConvertFromTestData())
            {
                yield return(test);
            }
        }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Valid("1", (float)1));

            yield return(ConvertTest.Valid(1.1.ToString(), 1.1f));

            yield return(ConvertTest.Valid(" -1 ", (float)-1));

            yield return(ConvertTest.Valid("+5", (float)5));

            yield return(ConvertTest.Valid(" +5 ", (float)5));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("#2"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>(" #2 "));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("0x3"));

            if (!PlatformDetection.IsFullFramework)
            {
                yield return(ConvertTest.Throws <ArgumentException>("0X3"));

                yield return(ConvertTest.Throws <ArgumentException>(" 0X3 "));

                yield return(ConvertTest.Throws <ArgumentException>("&h4"));

                yield return(ConvertTest.Throws <ArgumentException>("&H4"));

                yield return(ConvertTest.Throws <ArgumentException>(" &H4 "));
            }

            foreach (ConvertTest test in base.ConvertFromTestData())
            {
                yield return(test);
            }
        }
Exemple #19
0
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            yield return(ConvertTest.Throws <ArgumentException, Exception>(""));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("bad"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("0x"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("0X"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("1x1"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("0y1"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("&h"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("&H"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("0h1"));

            yield return(ConvertTest.Throws <ArgumentException, Exception>("&i1"));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
Exemple #20
0
 public override IEnumerable <ConvertTest> ConvertFromTestData()
 {
     yield return(ConvertTest.CantConvertFrom(new CustomCollection()));
 }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            // This does actually succeed despite CanConvertFrom returning false.
            ConvertTest noContext = ConvertTest.Valid("reference name", null).WithContext(null);

            noContext.CanConvert = false;
            yield return(noContext);

            // No IReferenceService or IContainer.
            yield return(ConvertTest.Valid(string.Empty, null));

            yield return(ConvertTest.Valid("   ", null));

            yield return(ConvertTest.Valid("(none)", null).WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid("nothing", null));

            // IReferenceService.
            var nullReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(null);
                }
            };

            yield return(ConvertTest.Valid("reference name", null).WithContext(nullReferenceServiceContext));

            var invalidReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(new object());
                }
            };

            yield return(ConvertTest.Valid("reference name", null).WithContext(invalidReferenceServiceContext));

            var component1       = new TestComponent();
            var component2       = new TestComponent();
            var nonComponent     = new object();
            var referenceService = new MockReferenceService();

            referenceService.AddReference("reference name", component1);
            referenceService.AddReference(string.Empty, component2);
            referenceService.AddReference("non component", nonComponent);
            var validReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(referenceService);
                }
            };

            yield return(ConvertTest.Valid("reference name", component1).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid("  reference name  ", component1).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(string.Empty, component2).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid("non component", nonComponent).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid("no such reference", null).WithContext(validReferenceServiceContext));

            // IContainer.
            var containerComponent1 = new TestComponent();
            var containerComponent2 = new TestComponent();
            var container           = new Container();

            container.Add(containerComponent1, "reference name");
            container.Add(containerComponent2, string.Empty);
            var contextWithContainer = new MockTypeDescriptorContext {
                Container = container
            };

            yield return(ConvertTest.Valid("reference name", containerComponent1).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid(string.Empty, containerComponent2).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid("no such reference", null).WithContext(contextWithContainer));

            yield return(ConvertTest.CantConvertFrom(1));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            var component1   = new TestComponent();
            var component2   = new TestComponent();
            var component3   = new TestComponent();
            var nonComponent = new object();

            // No Context.
            yield return(ConvertTest.Valid(null, "(none)").WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid(null, "(none)").WithContext(null).WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid(string.Empty, string.Empty).WithContext(null));

            // IReferenceCollection.
            var nullReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(null);
                }
            };

            yield return(ConvertTest.Valid(component1, string.Empty).WithContext(nullReferenceServiceContext));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(nullReferenceServiceContext));

            var invalidReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(new object());
                }
            };

            yield return(ConvertTest.Valid(component1, string.Empty).WithContext(invalidReferenceServiceContext));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(invalidReferenceServiceContext));

            var referenceService = new MockReferenceService();

            referenceService.AddReference("reference name", component1);
            referenceService.AddReference(string.Empty, component2);
            referenceService.AddReference(null, component3);
            referenceService.AddReference("non component", nonComponent);
            var validReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(referenceService);
                }
            };

            yield return(ConvertTest.Valid(component1, "reference name").WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(component2, string.Empty).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(component3, string.Empty).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(nonComponent, "non component").WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(validReferenceServiceContext));

            // IContainer.
            var containerComponent1 = new TestComponent();
            var containerComponent2 = new TestComponent();
            var container           = new Container();

            container.Add(containerComponent1, "reference name");
            container.Add(containerComponent2, string.Empty);
            var contextWithContainer = new MockTypeDescriptorContext {
                Container = container
            };

            yield return(ConvertTest.Valid(containerComponent1, "reference name").WithContext(contextWithContainer));

            yield return(ConvertTest.Valid(containerComponent2, string.Empty).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(contextWithContainer));

            yield return(ConvertTest.CantConvertTo(1, typeof(int)));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            CultureInfo        polandCulture = new CultureInfo("pl-PL");
            DateTimeFormatInfo formatInfo    = CultureInfo.CurrentCulture.DateTimeFormat;
            DateTime           date          = new DateTime(1998, 12, 5);

            yield return(ConvertTest.Valid(date, date.ToString(formatInfo.ShortDatePattern)));

            yield return(ConvertTest.Valid(date, date.ToString(polandCulture.DateTimeFormat.ShortDatePattern, polandCulture.DateTimeFormat))
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(date, "1998-12-05", CultureInfo.InvariantCulture)
                         .WithRemoteInvokeCulture(polandCulture));

            DateTime dateWithTime = new DateTime(1998, 12, 5, 22, 30, 30);

            yield return(ConvertTest.Valid(dateWithTime, dateWithTime.ToString(formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern)));

            yield return(ConvertTest.Valid(dateWithTime, dateWithTime.ToString(polandCulture.DateTimeFormat.ShortDatePattern + " " + polandCulture.DateTimeFormat.ShortTimePattern, polandCulture.DateTimeFormat))
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(dateWithTime, "12/05/1998 22:30:30", CultureInfo.InvariantCulture)
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(DateTime.MinValue, string.Empty));

            yield return(ConvertTest.Valid(
                             new DateTime(),
                             new InstanceDescriptor(
                                 typeof(DateTime).GetConstructor(new Type[] { typeof(long) }),
                                 new object[] { (long)0 }
                                 )
                             ));

            yield return(ConvertTest.Valid(
                             date,
                             new InstanceDescriptor(
                                 typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }),
                                 new object[] { 1998, 12, 5, 0, 0, 0, 0 }
                                 )
                             ));

            yield return(ConvertTest.Valid(
                             dateWithTime,
                             new InstanceDescriptor(
                                 typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }),
                                 new object[] { 1998, 12, 5, 22, 30, 30, 0 }
                                 )
                             ));

            yield return(ConvertTest.Valid(
                             dateWithTime,
                             new InstanceDescriptor(
                                 typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }),
                                 new object[] { 1998, 12, 5, 22, 30, 30, 0 }
                                 ),
                             CultureInfo.InvariantCulture
                             ));

            yield return(ConvertTest.CantConvertTo(new DateTime(), typeof(DateTime)));

            yield return(ConvertTest.CantConvertTo(new DateTime(), typeof(int)));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            CultureInfo        polandCulture = new CultureInfo("pl-PL");
            DateTimeFormatInfo formatInfo    = CultureInfo.CurrentCulture.DateTimeFormat;
            DateTimeOffset     offset        = new DateTimeOffset(new DateTime(1998, 12, 5));

            yield return(ConvertTest.Valid(offset, offset.ToString(formatInfo.ShortDatePattern + " zzz")));

            yield return(ConvertTest.Valid(offset, offset.ToString(polandCulture.DateTimeFormat.ShortDatePattern + " zzz", polandCulture.DateTimeFormat))
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(offset, offset.ToString("yyyy-MM-dd zzz", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture)
                         .WithRemoteInvokeCulture(polandCulture));

            DateTimeOffset offsetWithTime = new DateTimeOffset(new DateTime(1998, 12, 5, 22, 30, 30));

            yield return(ConvertTest.Valid(offsetWithTime, offsetWithTime.ToString(formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern + " zzz")));

            yield return(ConvertTest.Valid(offsetWithTime, offsetWithTime.ToString(polandCulture.DateTimeFormat.ShortDatePattern + " " + polandCulture.DateTimeFormat.ShortTimePattern + " zzz", polandCulture.DateTimeFormat))
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(offsetWithTime, offsetWithTime.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture)
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(DateTimeOffset.MinValue, string.Empty));

            yield return(ConvertTest.Valid(
                             new DateTimeOffset(),
                             new InstanceDescriptor(
                                 typeof(DateTimeOffset).GetConstructor(new Type[] { typeof(long) }),
                                 new object[] { (long)0 }
                                 )
                             ));

            yield return(ConvertTest.Valid(
                             offset,
                             new InstanceDescriptor(
                                 typeof(DateTimeOffset).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(TimeSpan) }),
                                 new object[] { 1998, 12, 5, 0, 0, 0, 0, offset.Offset }
                                 )
                             ));

            yield return(ConvertTest.Valid(
                             offsetWithTime,
                             new InstanceDescriptor(
                                 typeof(DateTimeOffset).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(TimeSpan) }),
                                 new object[] { 1998, 12, 5, 22, 30, 30, 0, offsetWithTime.Offset }
                                 )
                             ));

            yield return(ConvertTest.Valid(
                             offsetWithTime,
                             new InstanceDescriptor(
                                 typeof(DateTimeOffset).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(TimeSpan) }),
                                 new object[] { 1998, 12, 5, 22, 30, 30, 0, offsetWithTime.Offset }
                                 ),
                             CultureInfo.InvariantCulture
                             ));

            yield return(ConvertTest.CantConvertTo(new DateTimeOffset(), typeof(DateTimeOffset)));

            yield return(ConvertTest.CantConvertTo(new DateTimeOffset(), typeof(int)));
        }