public void ConvertTo_ReturnsDefaultForValueTypes_WhenValueIsNull()
        {
            var valueProviderResult = new ValueProviderResult(null, null, CultureInfo.InvariantCulture);

            var convertedValue = valueProviderResult.ConvertTo(typeof(int));

            Assert.Equal(0, convertedValue);
        }
        public void ConvertTo_ReturnsNullForReferenceTypes_WhenValueIsNull()
        {
            var valueProviderResult = new ValueProviderResult(null, null, CultureInfo.InvariantCulture);

            var convertedValue = valueProviderResult.ConvertTo(typeof(string));

            Assert.Equal(null, convertedValue);
        }
        public void ConvertTo_PopulatesArray_WhenSingleNeedsToBeConvertedToArray()
        {
            // Arrange
            var valueProviderResult = new ValueProviderResult(DayOfWeek.Friday, "It's Friday!", CultureInfo.InvariantCulture);

            // Act
            var convertedValue = (DayOfWeek[])valueProviderResult.ConvertTo(typeof(DayOfWeek[]));

            // Assert
            Assert.Single(convertedValue, DayOfWeek.Friday);
        }
        public void ConvertTo_PopulatesArray_WhenSourceArrayNeedsToBeConvertedToArray()
        {
            // Arrange
            string[] values = new[] { "3", "2", "1" };
            var valueProviderResult = new ValueProviderResult(values, values.ToString(), CultureInfo.InvariantCulture);

            // Act
            var convertedValue = valueProviderResult.ConvertTo(typeof(int[]));

            // Assert
            Assert.Equal(new[] { 3, 2, 1 }, convertedValue);
        }
        public void ConvertTo_PopulatesArray_WhenListNeedsToBeConvertedToArray()
        {
            // Arrange
            List<string> values = new List<string> { "-1", "0", "1" };
            var valueProviderResult = new ValueProviderResult(values, values.ToString(), CultureInfo.InvariantCulture);

            // Act
            var convertedValue = valueProviderResult.ConvertTo(typeof(int[]));

            // Assert
            Assert.IsType<int[]>(convertedValue);
            Assert.Equal(new[] { -1, 0, 1 }, convertedValue);
        }
        static void Main(string[] args)
        {
            ValueProviderResult result1 = new ValueProviderResult(new string[] { "1", "2", "3" }, "", null);
            ValueProviderResult result2 = new ValueProviderResult("123", "", null);

            //string[] => int[]
            int[] value1 = (int[])result1.ConvertTo(typeof(int[]));
            //string[] => int
            int value2 = (int)result1.ConvertTo(typeof(int));
            //string => int[]
            int[] value3 = (int[])result2.ConvertTo(typeof(int[]));

            Console.WriteLine("{0,-16}{1}", "RawValue", "NewValue");
            Console.WriteLine("{0,-16}{1}", result1.RawValue.ConvertToString(), value1.ConvertToString());
            Console.WriteLine("{0,-16}{1}", result1.RawValue.ConvertToString(), value2.ConvertToString());
            Console.WriteLine("{0,-16}{1}", result2.RawValue.ConvertToString(), value3.ConvertToString());
        }
        public void ConvertTo_ReturnsFirstValue_WhenSequenceNeedsToConvertedToSingleValue(IList value, object expected)
        {
            // Arrange
            var valueProviderResult = new ValueProviderResult(value, value.ToString(), CultureInfo.InvariantCulture);

            // Act
            var convertedValue = valueProviderResult.ConvertTo(expected.GetType());

            // Assert
            Assert.Equal(expected, convertedValue);
        }