From 25f57600c18b66a5edf2a2ed2c79db4ea49e0410 Mon Sep 17 00:00:00 2001 From: Markus Date: Thu, 14 May 2020 15:54:50 +0200 Subject: [PATCH] Added failing tests --- .../Data/DefaultValueConverterTests.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/Avalonia.Base.UnitTests/Data/DefaultValueConverterTests.cs b/tests/Avalonia.Base.UnitTests/Data/DefaultValueConverterTests.cs index f70769ac39..efa81dcc1b 100644 --- a/tests/Avalonia.Base.UnitTests/Data/DefaultValueConverterTests.cs +++ b/tests/Avalonia.Base.UnitTests/Data/DefaultValueConverterTests.cs @@ -6,6 +6,7 @@ using System.Windows.Input; using System; using Avalonia.Data.Converters; using Avalonia.Layout; +using System.ComponentModel; namespace Avalonia.Base.UnitTests.Data.Converters { @@ -35,6 +36,54 @@ namespace Avalonia.Base.UnitTests.Data.Converters Assert.Equal(5.0, result); } + [Fact] + public void Do_Not_Throw_On_InvalidInput_For_NullableInt() + { + var result = DefaultValueConverter.Instance.Convert( + "", + typeof(int?), + null, + CultureInfo.InvariantCulture); + + Assert.IsType(typeof(BindingNotification), result); + } + + [Fact] + public void Can_Convert_Decimal_To_NullableDouble() + { + var result = DefaultValueConverter.Instance.Convert( + 5m, + typeof(double?), + null, + CultureInfo.InvariantCulture); + + Assert.Equal(5.0, result); + } + + [Fact] + public void Can_Convert_CustomType_To_Int() + { + var result = DefaultValueConverter.Instance.Convert( + new CustomType(123), + typeof(int), + null, + CultureInfo.InvariantCulture); + + Assert.Equal(123, result); + } + + [Fact] + public void Can_Convert_Int_To_CustomType() + { + var result = DefaultValueConverter.Instance.Convert( + 123, + typeof(CustomType), + null, + CultureInfo.InvariantCulture); + + Assert.Equal(new CustomType(123), result); + } + [Fact] public void Can_Convert_String_To_Enum() { @@ -187,5 +236,44 @@ namespace Avalonia.Base.UnitTests.Data.Converters return v.Value; } } + + [TypeConverter(typeof(CustomTypeConverter))] + private class CustomType { + + public int Value { get; } + + public CustomType(int value) + { + Value = value; + } + + public override bool Equals(object obj) + { + return obj is CustomType other && this.Value == other.Value; + } + } + + private class CustomTypeConverter : TypeConverter + { + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + { + return destinationType == typeof(int); + } + + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof(int); + } + + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + return ((CustomType)value).Value; + } + + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + return new CustomType((int)value); + } + } } }