1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections.Generic; 6 using System.Globalization; 7 using Xunit; 8 9 namespace System.ComponentModel.TypeConverterTests 10 { 11 public abstract class StringTypeConverterTestBase<T> 12 { 13 protected abstract T Default { get; } 14 protected abstract TypeConverter Converter { get; } 15 protected abstract bool StandardValuesSupported { get; } 16 protected abstract bool StandardValuesExclusive { get; } 17 protected abstract bool CreateInstanceSupported { get; } 18 protected abstract bool IsGetPropertiesSupported { get; } 19 20 protected virtual IEnumerable<Tuple<T, Dictionary<string, object>>> CreateInstancePairs 21 { 22 get { yield break; } 23 } 24 25 [Fact] GetStandardValuesSupported()26 public void GetStandardValuesSupported() 27 { 28 Assert.Equal(StandardValuesSupported, Converter.GetStandardValuesSupported()); 29 Assert.Equal(StandardValuesSupported, Converter.GetStandardValuesSupported(null)); 30 } 31 32 [Fact] GetStandardValues()33 public void GetStandardValues() 34 { 35 if (!StandardValuesSupported) 36 { 37 Assert.Null(Converter.GetStandardValues()); 38 } 39 } 40 41 [Fact] GetStandardValuesExclusive()42 public void GetStandardValuesExclusive() 43 { 44 Assert.Equal(StandardValuesExclusive, Converter.GetStandardValuesExclusive()); 45 } 46 CanConvertFrom(Type type)47 protected void CanConvertFrom(Type type) 48 { 49 Assert.True(Converter.CanConvertFrom(type)); 50 Assert.True(Converter.CanConvertFrom(null, type)); 51 } 52 CannotConvertFrom(Type type)53 protected void CannotConvertFrom(Type type) 54 { 55 Assert.False(Converter.CanConvertFrom(type)); 56 Assert.False(Converter.CanConvertFrom(null, type)); 57 } 58 CanConvertTo(Type type)59 protected void CanConvertTo(Type type) 60 { 61 Assert.True(Converter.CanConvertTo(type)); 62 Assert.True(Converter.CanConvertTo(null, type)); 63 } 64 CannotConvertTo(Type type)65 protected void CannotConvertTo(Type type) 66 { 67 Assert.False(Converter.CanConvertTo(type)); 68 Assert.False(Converter.CanConvertTo(null, type)); 69 } 70 TestConvertFromString(T value, string str)71 protected void TestConvertFromString(T value, string str) 72 { 73 Assert.Equal(value, (T)Converter.ConvertFrom(null, CultureInfo.InvariantCulture, str)); 74 } 75 TestConvertToString(T value, string str)76 protected void TestConvertToString(T value, string str) 77 { 78 Assert.Equal(str, (string)Converter.ConvertTo(null, CultureInfo.InvariantCulture, value, typeof(string))); 79 } 80 ConvertFromThrowsArgumentExceptionForString(string value)81 protected void ConvertFromThrowsArgumentExceptionForString(string value) 82 { 83 AssertExtensions.Throws<ArgumentException>(null, () => 84 { 85 Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); 86 }); 87 } 88 ConvertFromThrowsFormatInnerExceptionForString(string value)89 protected void ConvertFromThrowsFormatInnerExceptionForString(string value) 90 { 91 var ex = AssertExtensions.Throws<ArgumentException, Exception>(() => 92 { 93 Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); 94 }); 95 Assert.NotNull(ex.InnerException); 96 Assert.IsType<FormatException>(ex.InnerException); 97 } 98 ConvertFromThrowsNotSupportedFor(object value)99 protected void ConvertFromThrowsNotSupportedFor(object value) 100 { 101 Assert.Throws<NotSupportedException>(() => 102 { 103 Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); 104 }); 105 } 106 ConvertToThrowsNotSupportedForType(Type type)107 protected void ConvertToThrowsNotSupportedForType(Type type) 108 { 109 Assert.Throws<NotSupportedException>(() => 110 { 111 Converter.ConvertTo(null, CultureInfo.InvariantCulture, Default, type); 112 }); 113 } 114 115 [Fact] GetCreateInstanceSupported()116 public void GetCreateInstanceSupported() 117 { 118 Assert.Equal(CreateInstanceSupported, Converter.GetCreateInstanceSupported()); 119 Assert.Equal(CreateInstanceSupported, Converter.GetCreateInstanceSupported(null)); 120 } 121 122 [Fact] CreateInstance()123 public void CreateInstance() 124 { 125 foreach (var pair in CreateInstancePairs) 126 { 127 Assert.Equal(pair.Item1, Converter.CreateInstance(pair.Item2)); 128 } 129 } 130 131 [Fact] GetPropertiesSupported()132 public void GetPropertiesSupported() 133 { 134 Assert.Equal(IsGetPropertiesSupported, Converter.GetPropertiesSupported()); 135 Assert.Equal(IsGetPropertiesSupported, Converter.GetPropertiesSupported(null)); 136 } 137 ConvertFromInvariantStringThrowsArgumentException(string str)138 protected void ConvertFromInvariantStringThrowsArgumentException(string str) 139 { 140 AssertExtensions.Throws<ArgumentException>(null, () => 141 { 142 Converter.ConvertFromInvariantString(str); 143 }); 144 } 145 ConvertFromInvariantStringThrowsFormatInnerException(string str)146 protected void ConvertFromInvariantStringThrowsFormatInnerException(string str) 147 { 148 var ex = AssertExtensions.Throws<ArgumentException, Exception>(() => 149 { 150 Converter.ConvertFromInvariantString(str); 151 }); 152 Assert.NotNull(ex.InnerException); 153 Assert.IsType<FormatException>(ex.InnerException); 154 } 155 ConvertFromStringThrowsArgumentException(string str)156 protected void ConvertFromStringThrowsArgumentException(string str) 157 { 158 AssertExtensions.Throws<ArgumentException>(null, () => 159 { 160 Converter.ConvertFromString(str); 161 }); 162 } 163 ConvertFromStringThrowsFormatInnerException(string str)164 protected void ConvertFromStringThrowsFormatInnerException(string str) 165 { 166 var ex = AssertExtensions.Throws<ArgumentException, Exception>(() => 167 { 168 Converter.ConvertFromString(str); 169 }); 170 Assert.NotNull(ex.InnerException); 171 Assert.IsType<FormatException>(ex.InnerException); 172 } 173 } 174 } 175