public void PaddingConverter_ConvertFrom_String_ReturnsExpected(string value, object expected)
        {
            var converter = new PaddingConverter();

            Assert.Equal(expected, converter.ConvertFrom(value));
            Assert.Equal(expected, converter.ConvertFrom(null, null, value));
        }
        public void PaddingConverter_ConvertTo_String_ReturnsExpected()
        {
            var converter = new PaddingConverter();

            Assert.Equal("1, 2, 3, 4", converter.ConvertTo(new Padding(1, 2, 3, 4), typeof(string)));
            Assert.Equal("1, 2, 3, 4", converter.ConvertTo(null, null, new Padding(1, 2, 3, 4), typeof(string)));
        }
        public void PaddingConverter_CreateInstance_UnknownInstanceType_ReturnsExpected()
        {
            var converter   = new PaddingConverter();
            var mockContext = new Mock <ITypeDescriptorContext>(MockBehavior.Strict);

            mockContext
            .Setup(c => c.Instance)
            .Returns("abc");
            mockContext
            .Setup(c => c.PropertyDescriptor)
            .Returns(TypeDescriptor.GetProperties(typeof(string))[0]);

            var propertyValues = new Dictionary <string, object>
            {
                { nameof(Padding.All), 1 },
                { nameof(Padding.Left), 2 },
                { nameof(Padding.Top), 2 },
                { nameof(Padding.Right), 3 },
                { nameof(Padding.Bottom), 4 },
            };

            Padding expected = new Padding(2, 2, 3, 4);
            Padding padding  = Assert.IsType <Padding>(converter.CreateInstance(mockContext.Object, propertyValues));

            Assert.Equal(expected, padding);
        }
示例#4
0
        public void CreateInstanceSupported()
        {
            PaddingConverter pc = new PaddingConverter();

            Assert.AreEqual(true, pc.GetCreateInstanceSupported(null), "A1");
            Assert.AreEqual(true, pc.GetPropertiesSupported(null), "A2");
        }
        public void PaddingConverter_CreateInstance_ValidPropertyValuesAll_ReturnsExpected(Padding instance, int all, Padding result)
        {
            var converter   = new PaddingConverter();
            var mockContext = new Mock <ITypeDescriptorContext>(MockBehavior.Strict);

            mockContext
            .Setup(c => c.Instance)
            .Returns(new ClassWithPadding {
                Padding = instance
            });
            mockContext
            .Setup(c => c.PropertyDescriptor)
            .Returns(TypeDescriptor.GetProperties(typeof(ClassWithPadding))[0]);
            Padding padding = Assert.IsType <Padding>(converter.CreateInstance(
                                                          mockContext.Object, new Dictionary <string, object>
            {
                { nameof(Padding.All), all },
                { nameof(Padding.Left), 1 },
                { nameof(Padding.Top), 2 },
                { nameof(Padding.Right), 3 },
                { nameof(Padding.Bottom), 4 }
            })
                                                      );

            Assert.Equal(result, padding);
        }
        public void PaddingConverter_ConvertTo_InstanceDescriptor_ReturnsExpected()
        {
            var converter = new PaddingConverter();
            InstanceDescriptor descriptor = Assert.IsType <InstanceDescriptor>(converter.ConvertTo(new Padding(1, 2, 3, 4), typeof(InstanceDescriptor)));

            Assert.Equal(typeof(Padding).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }), descriptor.MemberInfo);
            Assert.Equal(new object[] { 1, 2, 3, 4 }, descriptor.Arguments);
        }
示例#7
0
        private Padding RoundTripPadding(Padding p)
        {
            PaddingConverter pc = new PaddingConverter();

            string s = (string)pc.ConvertTo(p, typeof(string));

            return((Padding)pc.ConvertFrom(s));
        }
示例#8
0
        /// <summary>
        /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// paddingconverter.ConvertTo&lt;int&gt;(context, culture, value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this PaddingConverter paddingconverter, System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
        {
            if (paddingconverter == null)
            {
                throw new ArgumentNullException("paddingconverter");
            }

            return((T)paddingconverter.ConvertTo(context, culture, value, typeof(T)));
        }
示例#9
0
        /// <summary>
        /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// typeconverter.ConvertTo&lt;int&gt;(value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this PaddingConverter typeconverter, Object value)
        {
            if (typeconverter == null)
            {
                throw new ArgumentNullException("typeconverter");
            }

            return((T)typeconverter.ConvertTo(value, typeof(T)));
        }
示例#10
0
        public void CanConvertTo()
        {
            PaddingConverter c = new PaddingConverter();

            Assert.IsTrue(c.CanConvertTo(null, typeof(string)), "1");
            Assert.IsFalse(c.CanConvertTo(null, typeof(int)), "2");
            Assert.IsFalse(c.CanConvertTo(null, typeof(float)), "3");
            Assert.IsFalse(c.CanConvertTo(null, typeof(object)), "4");
        }
示例#11
0
        public void PaddingConverter_GetProperties_Invoke_ReturnsExpected()
        {
            var converter = new PaddingConverter();
            PropertyDescriptorCollection properties = converter.GetProperties(null);

            Assert.Equal(5, properties.Count);
            Assert.Equal(nameof(Padding.All), properties[0].Name);
            Assert.Equal(nameof(Padding.Left), properties[1].Name);
            Assert.Equal(nameof(Padding.Top), properties[2].Name);
            Assert.Equal(nameof(Padding.Right), properties[3].Name);
            Assert.Equal(nameof(Padding.Bottom), properties[4].Name);
        }
示例#12
0
        public void CreateInstance_NullArguments()
        {
            PaddingConverter c = new PaddingConverter();

            try {
                c.CreateInstance(null, GetPropertiesTable(1, 1, 1, 1, 1));
                Assert.Fail("#1");
            } catch (ArgumentNullException ex) { }
            try {
                c.CreateInstance(GetTypeDescriptorContext(Padding.Empty), null);
                Assert.Fail("#2");
            } catch (ArgumentNullException ex) { }
        }
示例#13
0
        public void ConvertTo()
        {
            PaddingConverter pc = new PaddingConverter();

            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture,
                                          "1{0} 2{0} 3{0} 4", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
                            (string)pc.ConvertTo(new Padding(1, 2, 3, 4), typeof(string)), "A1");
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture,
                                          "1{0} 1{0} 1{0} 1", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
                            (string)pc.ConvertTo(new Padding(1), typeof(string)), "A2");
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture,
                                          "0{0} 0{0} 0{0} 0", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
                            (string)pc.ConvertTo(Padding.Empty, typeof(string)), "A3");
        }
示例#14
0
        public void PaddingConverter_CreateInstance_ValidPropertyValuesNullContext_ReturnsExpected()
        {
            var     converter = new PaddingConverter();
            Padding expected  = new Padding(1, 2, 3, 4);
            Padding padding   = Assert.IsType <Padding>(converter.CreateInstance(
                                                            null, new Dictionary <string, object>
            {
                { nameof(Padding.All), expected.All },
                { nameof(Padding.Left), expected.Left },
                { nameof(Padding.Top), expected.Top },
                { nameof(Padding.Right), expected.Right },
                { nameof(Padding.Bottom), expected.Bottom }
            }));

            Assert.Equal(expected, padding);
        }
示例#15
0
        public void PaddingConverter_CreateInstance_InvalidPropertyValueType_ThrowsArgumentException(IDictionary propertyValues)
        {
            var converter   = new PaddingConverter();
            var mockContext = new Mock <ITypeDescriptorContext>(MockBehavior.Strict);

            mockContext
            .Setup(c => c.Instance)
            .Returns(new ClassWithPadding {
                Padding = new Padding(1)
            });
            mockContext
            .Setup(c => c.PropertyDescriptor)
            .Returns(TypeDescriptor.GetProperties(typeof(ClassWithPadding))[0]);

            Assert.Throws <ArgumentException>("propertyValues", () => converter.CreateInstance(mockContext.Object, propertyValues));
        }
示例#16
0
        public void ConvertTo_InstanceDescriptor()
        {
            PaddingConverter   c = new PaddingConverter();
            Padding            originalPadding    = new Padding(1, 10, 5, 9);
            InstanceDescriptor instanceDescriptor = (InstanceDescriptor)c.ConvertTo(originalPadding,
                                                                                    typeof(InstanceDescriptor));
            Padding resultedPadding = (Padding)instanceDescriptor.Invoke();

            Assert.AreEqual(originalPadding, resultedPadding, "#1");

            originalPadding    = new Padding(99);
            instanceDescriptor = (InstanceDescriptor)c.ConvertTo(originalPadding,
                                                                 typeof(InstanceDescriptor));
            resultedPadding = (Padding)instanceDescriptor.Invoke();
            Assert.AreEqual(originalPadding, resultedPadding, "#2");
        }
示例#17
0
        public void ConvertFrom()
        {
            string           listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
            PaddingConverter pc            = new PaddingConverter();

            Assert.AreEqual(new Padding(1, 2, 3, 4), pc.ConvertFrom(
                                string.Format("1{0} 2{0} 3{0} 4", listSeparator)), "A1");
            Assert.AreEqual(new Padding(1, 2, 3, 4), pc.ConvertFrom(
                                string.Format("1{0}2{0}3{0}4", listSeparator)), "A2");
            Assert.AreEqual(new Padding(1, 2, 3, 4), pc.ConvertFrom(
                                string.Format("1{0}  2{0}  3{0}  4", listSeparator)), "A3");
            Assert.AreEqual(new Padding(1), pc.ConvertFrom(string.Format(
                                                               "1{0} 1{0} 1{0} 1", listSeparator)), "A4");
            Assert.AreEqual(new Padding(), pc.ConvertFrom(string.Format(
                                                              "0{0} 0{0} 0{0} 0", listSeparator)), "A5");
        }
示例#18
0
        public void CreateInstance()
        {
            PaddingConverter c = new PaddingConverter();
            Padding          modified, expected;

            // Non-"All" Tests
            //
            ITypeDescriptorContext context = GetTypeDescriptorContext(new Padding(1, 2, 30, 40));

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(-1, 1, 2, 30, 40));
            expected = new Padding(1, 2, 30, 40);
            Assert.AreEqual(expected, modified, "NonAll_NoChange");

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(-1, 111, 2, 30, 40));
            expected = new Padding(111, 2, 30, 40);
            Assert.AreEqual(expected, modified, "NonAll_ChangeLeft");

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(-1, 1, 222, 30, 40));
            expected = new Padding(1, 222, 30, 40);
            Assert.AreEqual(expected, modified, "NonAll_ChangeTop");

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(555, 1, 2, 30, 40));
            expected = new Padding(555);
            Assert.AreEqual(expected, modified, "NonAll_ChangeAll");

            // "All" tests
            //
            context = GetTypeDescriptorContext(new Padding(1));

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(1, 1, 1, 1, 1));
            expected = new Padding(1, 1, 1, 1);
            Assert.AreEqual(expected, modified, "All_NoChange");

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(1, 111, 1, 1, 1));
            expected = new Padding(111, 1, 1, 1);
            Assert.AreEqual(expected, modified, "All_ChangeLeft");

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(1, 1, 222, 1, 1));
            expected = new Padding(1, 222, 1, 1);
            Assert.AreEqual(expected, modified, "All_ChangeTop");

            modified = (Padding)c.CreateInstance(context, GetPropertiesTable(555, 1, 1, 1, 1));
            expected = new Padding(555);
            Assert.AreEqual(expected, modified, "All_ChangeAll");
        }
示例#19
0
        public void PaddingConverter_CreateInstance_InvalidInstanceType_ThrowsInvalidCastException()
        {
            var converter   = new PaddingConverter();
            var mockContext = new Mock <ITypeDescriptorContext>(MockBehavior.Strict);

            mockContext
            .Setup(c => c.Instance)
            .Returns("abc");
            mockContext
            .Setup(c => c.PropertyDescriptor)
            .Returns(TypeDescriptor.GetProperties(typeof(string))[0]);

            var propertyValues = new Dictionary <string, object>
            {
                { nameof(Padding.All), 1 },
                { nameof(Padding.Left), 2 },
                { nameof(Padding.Top), 2 },
                { nameof(Padding.Right), 3 },
                { nameof(Padding.Bottom), 4 },
            };

            Assert.Throws <InvalidCastException>(() => converter.CreateInstance(mockContext.Object, propertyValues));
        }
示例#20
0
        public void PaddingConverter_CanConvertFrom_Invoke_ReturnsExpected(Type sourceType, bool expected)
        {
            var converter = new PaddingConverter();

            Assert.Equal(expected, converter.CanConvertFrom(sourceType));
        }
示例#21
0
        public void PaddingConverter_CanConvertTo_Invoke_ReturnsExpected(Type destinationType, bool expected)
        {
            var converter = new PaddingConverter();

            Assert.Equal(expected, converter.CanConvertTo(destinationType));
        }
示例#22
0
        public void PaddingConverter_ConvertFrom_InvalidString_ThrowsArgumentException(string value)
        {
            var converter = new PaddingConverter();

            Assert.Throws <ArgumentException>(null, () => converter.ConvertFrom(value));
        }
示例#23
0
        public void PaddingConverter_ConvertFrom_InvalidValue_ThrowsNotSupportedException(object value)
        {
            var converter = new PaddingConverter();

            Assert.Throws <NotSupportedException>(() => converter.ConvertFrom(value));
        }
示例#24
0
        public void PaddingConverter_GetPropertiesSupported_Invoke_ReturnsTrue()
        {
            var converter = new PaddingConverter();

            Assert.True(converter.GetPropertiesSupported());
        }
示例#25
0
        public void PaddingConverter_ConvertTo_NullDestinationType_ThrowsArgumentNullException()
        {
            var converter = new PaddingConverter();

            Assert.Throws <ArgumentNullException>("destinationType", () => converter.ConvertTo(new object(), null));
        }
示例#26
0
        public void PaddingConverter_GetCreateInstanceSupported_Invoke_ReturnsTrue()
        {
            var converter = new PaddingConverter();

            Assert.True(converter.GetCreateInstanceSupported());
        }
示例#27
0
        public void PaddingConverter_CreateInstance_NullPropertyValues_ThrowsArgumentNullException()
        {
            var converter = new PaddingConverter();

            Assert.Throws <ArgumentNullException>("propertyValues", () => converter.CreateInstance(new Mock <ITypeDescriptorContext>(MockBehavior.Strict).Object, null));
        }
示例#28
0
        public void PaddingConverter_CreateInstance_NullContext_ThrowsArgumentNullException()
        {
            var converter = new PaddingConverter();

            Assert.Throws <ArgumentNullException>("context", () => converter.CreateInstance(null, new Dictionary <string, object>()));
        }
示例#29
0
        public void PaddingConverter_ConvertTo_ValueNotPadding_ThrowsNotSupportedException()
        {
            var converter = new PaddingConverter();

            Assert.Throws <NotSupportedException>(() => converter.ConvertTo(1, typeof(InstanceDescriptor)));
        }
示例#30
0
        public void PaddingConverter_ConvertTo_InvalidDestinationType_ThrowsNotSupportedException(Type destinationType)
        {
            var converter = new PaddingConverter();

            Assert.Throws <NotSupportedException>(() => converter.ConvertTo(new Padding(), destinationType));
        }