BindingTests_Converters.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Controls;
  5. using Avalonia.Data;
  6. using Avalonia.Data.Converters;
  7. using Avalonia.Data.Core;
  8. using Xunit;
  9. namespace Avalonia.Markup.UnitTests.Data
  10. {
  11. public class BindingTests_Converters
  12. {
  13. [Fact]
  14. public void Converter_Should_Be_Used()
  15. {
  16. var textBlock = new TextBlock
  17. {
  18. DataContext = new Class1(),
  19. };
  20. var target = new Binding(nameof(Class1.Foo))
  21. {
  22. Converter = StringConverters.NullOrEmpty,
  23. };
  24. var expressionObserver = (BindingExpression)target.Initiate(
  25. textBlock,
  26. TextBlock.TextProperty).Observable;
  27. Assert.Same(StringConverters.NullOrEmpty, expressionObserver.Converter);
  28. }
  29. public class When_Binding_To_String
  30. {
  31. [Fact]
  32. public void StringFormatConverter_Should_Be_Used_When_Binding_Has_StringFormat()
  33. {
  34. var textBlock = new TextBlock
  35. {
  36. DataContext = new Class1(),
  37. };
  38. var target = new Binding(nameof(Class1.Foo))
  39. {
  40. StringFormat = "Hello {0}",
  41. };
  42. var expressionObserver = (BindingExpression)target.Initiate(
  43. textBlock,
  44. TextBlock.TextProperty).Observable;
  45. Assert.IsType<StringFormatValueConverter>(expressionObserver.Converter);
  46. }
  47. }
  48. public class When_Binding_To_Object
  49. {
  50. [Fact]
  51. public void StringFormatConverter_Should_Be_Used_When_Binding_Has_StringFormat()
  52. {
  53. var textBlock = new TextBlock
  54. {
  55. DataContext = new Class1(),
  56. };
  57. var target = new Binding(nameof(Class1.Foo))
  58. {
  59. StringFormat = "Hello {0}",
  60. };
  61. var expressionObserver = (BindingExpression)target.Initiate(
  62. textBlock,
  63. TextBlock.TagProperty).Observable;
  64. Assert.IsType<StringFormatValueConverter>(expressionObserver.Converter);
  65. }
  66. }
  67. public class When_Binding_To_Non_String_Or_Object
  68. {
  69. [Fact]
  70. public void StringFormatConverter_Should_Not_Be_Used_When_Binding_Has_StringFormat()
  71. {
  72. var textBlock = new TextBlock
  73. {
  74. DataContext = new Class1(),
  75. };
  76. var target = new Binding(nameof(Class1.Foo))
  77. {
  78. StringFormat = "Hello {0}",
  79. };
  80. var expressionObserver = (BindingExpression)target.Initiate(
  81. textBlock,
  82. TextBlock.MarginProperty).Observable;
  83. Assert.Same(DefaultValueConverter.Instance, expressionObserver.Converter);
  84. }
  85. }
  86. [Fact]
  87. public void StringFormat_Should_Be_Applied()
  88. {
  89. var textBlock = new TextBlock
  90. {
  91. DataContext = new Class1(),
  92. };
  93. var target = new Binding(nameof(Class1.Foo))
  94. {
  95. StringFormat = "Hello {0}",
  96. };
  97. textBlock.Bind(TextBlock.TextProperty, target);
  98. Assert.Equal("Hello foo", textBlock.Text);
  99. }
  100. [Fact]
  101. public void StringFormat_Should_Be_Applied_After_Converter()
  102. {
  103. var textBlock = new TextBlock
  104. {
  105. DataContext = new Class1(),
  106. };
  107. var target = new Binding(nameof(Class1.Foo))
  108. {
  109. Converter = StringConverters.NotNullOrEmpty,
  110. StringFormat = "Hello {0}",
  111. };
  112. textBlock.Bind(TextBlock.TextProperty, target);
  113. Assert.Equal("Hello True", textBlock.Text);
  114. }
  115. private class Class1
  116. {
  117. public string Foo { get; set; } = "foo";
  118. }
  119. }
  120. }