Skip to content

Commit

Permalink
Add more misc type converters tests (dotnet/corefx#36655)
Browse files Browse the repository at this point in the history
*  Add more misc type converters tests

* PR feedback


Commit migrated from dotnet/corefx@e65153e
  • Loading branch information
hughbe authored and stephentoub committed Apr 9, 2019
1 parent 9af99e3 commit bcec309
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace System.ComponentModel
{
/// <summary>
/// Provides a type converter to convert Unicode character objects
/// to and from various other representations.
/// Provides a type converter to convert Unicode character objects to and from various
/// other representations.
/// </summary>
public class CharConverter : TypeConverter
{
Expand All @@ -26,11 +26,11 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
/// </summary>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is char)
if (destinationType == typeof(string) && value is char charValue)
{
if ((char)value == (char)0)
if (charValue == '\0')
{
return "";
return string.Empty;
}
}

Expand All @@ -55,6 +55,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
{
throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, text, nameof(Char)));
}

return text[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
namespace System.ComponentModel
{
/// <summary>
/// Provides a type converter to convert collection objects to and from various other representations.
/// Provides a type converter to convert collection objects to and from various other
/// representations.
/// </summary>
public class CollectionConverter : TypeConverter
{
Expand All @@ -17,11 +18,6 @@ public class CollectionConverter : TypeConverter
/// </summary>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException(nameof(destinationType));
}

if (destinationType == typeof(string) && value is ICollection)
{
return SR.Collection;
Expand All @@ -31,17 +27,12 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
}

/// <summary>
/// Gets a collection of properties for the type of array specified by the value parameter using
/// the specified context and attributes.
/// Gets a collection of properties for the type of array specified by the value
/// parameter using the specified context and attributes.
/// </summary>
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return new PropertyDescriptorCollection(null);
}

/// <summary>
/// Gets a value indicating whether this object supports properties.
/// </summary>
public override bool GetPropertiesSupported(ITypeDescriptorContext context) => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,49 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Linq;
using Xunit;

namespace System.ComponentModel.Tests
{
public class BooleanConverterTests : ConverterTestBase
public class BooleanConverterTests : TypeConverterTestBase
{
private static BooleanConverter s_converter = new BooleanConverter();
public override TypeConverter Converter => new BooleanConverter();

[Fact]
public static void CanConvertFrom_WithContext()
public override bool StandardValuesSupported => true;
public override bool StandardValuesExclusive => true;

public override IEnumerable<ConvertTest> ConvertToTestData()
{
CanConvertFrom_WithContext(new object[2, 2]
{
{ typeof(string), true },
{ typeof(int), false }
},
BooleanConverterTests.s_converter);
yield return ConvertTest.Valid(true, Boolean.TrueString);
yield return ConvertTest.Valid(1, "1");

yield return ConvertTest.CantConvertTo(true, typeof(bool));
yield return ConvertTest.CantConvertTo(true, typeof(InstanceDescriptor));
yield return ConvertTest.CantConvertTo(true, typeof(object));
}

[Fact]
public static void ConvertFrom_WithContext()
public override IEnumerable<ConvertTest> ConvertFromTestData()
{
ConvertFrom_WithContext(new object[2, 3]
{
{ "false ", false, null },
{ "true", true, CultureInfo.InvariantCulture }
},
BooleanConverterTests.s_converter);
yield return ConvertTest.Valid("false ", false);
yield return ConvertTest.Valid("true", true, CultureInfo.InvariantCulture);

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

yield return ConvertTest.CantConvertFrom(1);
yield return ConvertTest.CantConvertFrom(null);
}

[Fact]
public static void ConvertFrom_WithContext_Negative()
public void StandardValues_Get_ReturnsExpected()
{
Assert.Throws<FormatException>(
() => BooleanConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "1"));
ICollection values = Converter.GetStandardValues();
Assert.Same(values, Converter.GetStandardValues());
Assert.Equal(new object[] { true, false }, values.Cast<object>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,40 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Linq;
using Xunit;

namespace System.ComponentModel.Tests
{
public class CharConverterTests : ConverterTestBase
public class CharConverterTests : TypeConverterTestBase
{
private static TypeConverter s_converter = new CharConverter();
public override TypeConverter Converter => new CharConverter();

[Fact]
public static void CanConvertFrom_WithContext()
public override IEnumerable<ConvertTest> ConvertToTestData()
{
CanConvertFrom_WithContext(new object[2, 2]
{
{ typeof(string), true },
{ typeof(int), false }
},
CharConverterTests.s_converter);
}
yield return ConvertTest.Valid('a', "a");
yield return ConvertTest.Valid('\u20AC', "\u20AC", CultureInfo.InvariantCulture);
yield return ConvertTest.Valid('\0', string.Empty);

[Fact]
public static void ConvertTo_WithContext()
{
ConvertTo_WithContext(new object[3, 3]
{
{'a', "a", null},
{'\0', "", null},
{'\u20AC', "\u20AC", CultureInfo.InvariantCulture}
},
CharConverterTests.s_converter);
yield return ConvertTest.CantConvertTo('a', typeof(char));
yield return ConvertTest.CantConvertTo('a', typeof(InstanceDescriptor));
yield return ConvertTest.CantConvertTo('a', typeof(object));
}

[Fact]
public static void ConvertFrom_WithContext()
public override IEnumerable<ConvertTest> ConvertFromTestData()
{
ConvertFrom_WithContext(new object[3, 3]
{
{ " a ", 'a', CultureInfo.InvariantCulture },
{ " ", '\0', null},
{ "", '\0', null }
},
CharConverterTests.s_converter);
}
yield return ConvertTest.Valid(" a ", 'a');
yield return ConvertTest.Valid(" ", '\0');
yield return ConvertTest.Valid("", '\0');

[Fact]
public static void ConvertFrom_WithContext_Negative()
{
Assert.Throws<FormatException>(
() => CharConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "aaa"));
yield return ConvertTest.Throws<FormatException>("aa");

Assert.Throws<NotSupportedException>(
() => CharConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, null));
yield return ConvertTest.CantConvertFrom(1);
yield return ConvertTest.CantConvertFrom(null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,59 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using Microsoft.DotNet.RemoteExecutor;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using Xunit;

namespace System.ComponentModel.Tests
{
public class CollectionConverterTests : ConverterTestBase
public class CollectionConverterTests : TypeConverterTestBase
{
private static TypeConverter s_converter = new CollectionConverter();
public override TypeConverter Converter => new CollectionConverter();

[Fact]
public static void ConvertTo_WithContext()
public override IEnumerable<ConvertTest> ConvertToTestData()
{
RemoteExecutor.Invoke(() =>
{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;

ConvertTo_WithContext(new object[1, 3]
{
{ new Collection1(), "(Collection)", null }
},
CollectionConverterTests.s_converter);
}).Dispose();
yield return ConvertTest.Valid(new CustomCollection(), "(Collection)").WithInvariantRemoteInvokeCulture();
yield return ConvertTest.Valid(1, "1");

yield return ConvertTest.CantConvertTo(new CustomCollection(), typeof(CustomCollection));
yield return ConvertTest.CantConvertTo(new CustomCollection(), typeof(InstanceDescriptor));
yield return ConvertTest.CantConvertTo(new CustomCollection(), typeof(object));
}

public override IEnumerable<ConvertTest> ConvertFromTestData()
{
yield return ConvertTest.CantConvertFrom(new CustomCollection());
}

public static IEnumerable<object[]> GetProperties_TestData()
{
yield return new object[] { null };
yield return new object[] { new CustomCollection() };
}

[Theory]
[MemberData(nameof(GetProperties_TestData))]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Returns null in .NET Framework")]
public void GetProperties_Invoke_ReturnsEmpty(object value)
{
PropertyDescriptorCollection properties = Converter.GetProperties(value);
Assert.Empty(properties);
}

[Serializable]
public class CustomCollection : ICollection
{
public void CopyTo(Array array, int index) => throw new NotImplementedException();

public int Count => throw new NotImplementedException();

public bool IsSynchronized => throw new NotImplementedException();

public object SyncRoot => throw new NotImplementedException();

public IEnumerator GetEnumerator() => throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override IEnumerable<ConvertTest> ConvertFromTestData()
yield return ConvertTest.Throws<ArgumentException, Exception>("32768");
yield return ConvertTest.Throws<ArgumentException, Exception>("-32769");

yield return ConvertTest.CantConvert(new object());
yield return ConvertTest.CantConvertFrom(new object());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,6 @@ public string ToString(string format, IFormatProvider formatProvider)
public const string Token = "Formatted class.";
}

[Serializable]
public class Collection1 : ICollection
{
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}

public int Count
{
get { throw new NotImplementedException(); }
}

public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}

public object SyncRoot
{
get { throw new NotImplementedException(); }
}

public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}

public class MyTypeListConverter : TypeListConverter
{
public MyTypeListConverter(Type[] types)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static ConvertTest Throws<TNetCoreException, TNetFrameworkException>(obje
};
}

public static ConvertTest CantConvert(object source, Type destinationType = null, CultureInfo culture = null)
public static ConvertTest CantConvertTo(object source, Type destinationType = null, CultureInfo culture = null)
{
return new ConvertTest
{
Expand All @@ -209,6 +209,18 @@ public static ConvertTest CantConvert(object source, Type destinationType = null
};
}

public static ConvertTest CantConvertFrom(object source, CultureInfo culture = null)
{
return new ConvertTest
{
Source = source,
Culture = culture,
NetCoreExceptionType = typeof(NotSupportedException),
NetFrameworkExceptionType = typeof(NotSupportedException),
CanConvert = false
};
}

public ConvertTest WithContext(ITypeDescriptorContext context)
{
Context = context;
Expand Down

0 comments on commit bcec309

Please sign in to comment.