Browse Source
XAML tests failing because of https://github.com/cwensley/Portable.Xaml/pull/106pull/1684/head
5 changed files with 220 additions and 2 deletions
@ -0,0 +1,30 @@ |
|||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
|
||||
|
namespace Avalonia.Data.Converters |
||||
|
{ |
||||
|
public class StringFormatConverter : IValueConverter |
||||
|
{ |
||||
|
public StringFormatConverter(string format, IValueConverter inner) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(format != null); |
||||
|
|
||||
|
Format = format; |
||||
|
Inner = inner; |
||||
|
} |
||||
|
|
||||
|
public IValueConverter Inner { get; } |
||||
|
public string Format { get; } |
||||
|
|
||||
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
||||
|
{ |
||||
|
value = Inner?.Convert(value, targetType, parameter, culture) ?? value; |
||||
|
return string.Format(Format, value, culture); |
||||
|
} |
||||
|
|
||||
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
||||
|
{ |
||||
|
throw new NotSupportedException("Two way bindings are not supported with a string format"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,146 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Data; |
||||
|
using Avalonia.Data.Converters; |
||||
|
using Avalonia.Data.Core; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Markup.UnitTests.Data |
||||
|
{ |
||||
|
public class BindingTests_Converters |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Converter_Should_Be_Used() |
||||
|
{ |
||||
|
var textBlock = new TextBlock |
||||
|
{ |
||||
|
DataContext = new Class1(), |
||||
|
}; |
||||
|
|
||||
|
var target = new Binding(nameof(Class1.Foo)) |
||||
|
{ |
||||
|
Converter = StringConverters.NullOrEmpty, |
||||
|
}; |
||||
|
|
||||
|
var expressionObserver = (BindingExpression)target.Initiate( |
||||
|
textBlock, |
||||
|
TextBlock.TextProperty).Observable; |
||||
|
|
||||
|
Assert.Same(StringConverters.NullOrEmpty, expressionObserver.Converter); |
||||
|
} |
||||
|
|
||||
|
public class When_Binding_To_String |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void StringFormatConverter_Should_Used_When_Binding_Has_StringFormat() |
||||
|
{ |
||||
|
var textBlock = new TextBlock |
||||
|
{ |
||||
|
DataContext = new Class1(), |
||||
|
}; |
||||
|
|
||||
|
var target = new Binding(nameof(Class1.Foo)) |
||||
|
{ |
||||
|
StringFormat = "Hello {0}", |
||||
|
}; |
||||
|
|
||||
|
var expressionObserver = (BindingExpression)target.Initiate( |
||||
|
textBlock, |
||||
|
TextBlock.TextProperty).Observable; |
||||
|
|
||||
|
Assert.IsType<StringFormatConverter>(expressionObserver.Converter); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class When_Binding_To_Object |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void StringFormatConverter_Should_Used_When_Binding_Has_StringFormat() |
||||
|
{ |
||||
|
var textBlock = new TextBlock |
||||
|
{ |
||||
|
DataContext = new Class1(), |
||||
|
}; |
||||
|
|
||||
|
var target = new Binding(nameof(Class1.Foo)) |
||||
|
{ |
||||
|
StringFormat = "Hello {0}", |
||||
|
}; |
||||
|
|
||||
|
var expressionObserver = (BindingExpression)target.Initiate( |
||||
|
textBlock, |
||||
|
TextBlock.TagProperty).Observable; |
||||
|
|
||||
|
Assert.IsType<StringFormatConverter>(expressionObserver.Converter); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class When_Binding_To_Non_String_Or_Object |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void StringFormatConverter_Should_Not_Be_Used_When_Binding_Has_StringFormat() |
||||
|
{ |
||||
|
var textBlock = new TextBlock |
||||
|
{ |
||||
|
DataContext = new Class1(), |
||||
|
}; |
||||
|
|
||||
|
var target = new Binding(nameof(Class1.Foo)) |
||||
|
{ |
||||
|
StringFormat = "Hello {0}", |
||||
|
}; |
||||
|
|
||||
|
var expressionObserver = (BindingExpression)target.Initiate( |
||||
|
textBlock, |
||||
|
TextBlock.MarginProperty).Observable; |
||||
|
|
||||
|
Assert.Same(DefaultValueConverter.Instance, expressionObserver.Converter); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void StringFormat_Should_Be_Applied() |
||||
|
{ |
||||
|
var textBlock = new TextBlock |
||||
|
{ |
||||
|
DataContext = new Class1(), |
||||
|
}; |
||||
|
|
||||
|
var target = new Binding(nameof(Class1.Foo)) |
||||
|
{ |
||||
|
StringFormat = "Hello {0}", |
||||
|
}; |
||||
|
|
||||
|
textBlock.Bind(TextBlock.TextProperty, target); |
||||
|
|
||||
|
Assert.Equal("Hello foo", textBlock.Text); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void StringFormat_Should_Be_Applied_After_Converter() |
||||
|
{ |
||||
|
var textBlock = new TextBlock |
||||
|
{ |
||||
|
DataContext = new Class1(), |
||||
|
}; |
||||
|
|
||||
|
var target = new Binding(nameof(Class1.Foo)) |
||||
|
{ |
||||
|
Converter = StringConverters.NotNullOrEmpty, |
||||
|
StringFormat = "Hello {0}", |
||||
|
}; |
||||
|
|
||||
|
textBlock.Bind(TextBlock.TextProperty, target); |
||||
|
|
||||
|
Assert.Equal("Hello True", textBlock.Text); |
||||
|
} |
||||
|
|
||||
|
private class Class1 |
||||
|
{ |
||||
|
public string Foo { get; set; } = "foo"; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue