Skip to content

Commit

Permalink
Merge pull request #1 from jeromelaban/dev/converter-unsetvalue-fix
Browse files Browse the repository at this point in the history
test: Add unit tests for UnsetValue returning converter
  • Loading branch information
alove-kahua authored Nov 19, 2020
2 parents 3a9b164 + fae76c5 commit 501456a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/Uno.UI.Tests/BinderTests/Given_Binder.Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace Uno.UI.Tests.BinderTests
{
[TestClass]
public partial class Given_Binder_Converter
{
[TestMethod]
public void When_Converter_Returns_UnsetValue()
{
var SUT = new MyControl();

var myTestConverter = new MyTestConverter();

SUT.SetBinding(
MyControl.MyPropertyProperty,
new Windows.UI.Xaml.Data.Binding()
{
Path = new PropertyPath("."),
Converter = myTestConverter,
FallbackValue = "fallback value",
TargetNullValue = "null value"
}
);

myTestConverter.OutputValue = v => v;
SUT.DataContext = "hello";
Assert.AreEqual("hello", SUT.MyProperty);

myTestConverter.OutputValue = v => DependencyProperty.UnsetValue;
SUT.DataContext = "hello 3";
Assert.AreEqual("fallback value", SUT.MyProperty);
}

internal class MyTestConverter : IValueConverter
{
public Func<object, object?> OutputValue { get; set; } = o => null;

public object? Convert(object value, Type targetType, object parameter, string language) => OutputValue(value);

public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
}

public partial class MyControl : DependencyObject
{
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}

public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), new FrameworkPropertyMetadata(null));
}
}

}

0 comments on commit 501456a

Please sign in to comment.