SelectorTests_PropertyEquals.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Linq;
  2. using System.Reactive.Linq;
  3. using System.Threading.Tasks;
  4. using Avalonia.Controls;
  5. using Xunit;
  6. namespace Avalonia.Styling.UnitTests
  7. {
  8. public class SelectorTests_PropertyEquals
  9. {
  10. [Fact]
  11. public async Task PropertyEquals_Matches_When_Property_Has_Matching_Value()
  12. {
  13. var control = new TextBlock();
  14. var target = default(Selector).PropertyEquals(TextBlock.TextProperty, "foo");
  15. var activator = target.Match(control).Activator.ToObservable();
  16. Assert.False(await activator.Take(1));
  17. control.Text = "foo";
  18. Assert.True(await activator.Take(1));
  19. control.Text = null;
  20. Assert.False(await activator.Take(1));
  21. }
  22. [Theory]
  23. [InlineData("Bar", FooBar.Bar)]
  24. [InlineData("352", 352)]
  25. [InlineData("0.1", 0.1)]
  26. public async Task PropertyEquals_Matches_When_Property_Has_Matching_Value_And_Different_Type(string literal, object value)
  27. {
  28. var control = new TextBlock();
  29. var target = default(Selector).PropertyEquals(TextBlock.TagProperty, literal);
  30. var activator = target.Match(control).Activator.ToObservable();
  31. Assert.False(await activator.Take(1));
  32. control.Tag = value;
  33. Assert.True(await activator.Take(1));
  34. control.Tag = null;
  35. Assert.False(await activator.Take(1));
  36. }
  37. [Fact]
  38. public void OfType_PropertyEquals_Doesnt_Match_Control_Of_Wrong_Type()
  39. {
  40. var control = new TextBlock();
  41. var target = default(Selector).OfType<Border>().PropertyEquals(TextBlock.TextProperty, "foo");
  42. Assert.Equal(SelectorMatchResult.NeverThisType, target.Match(control).Result);
  43. }
  44. [Fact]
  45. public void PropertyEquals_Selector_Should_Have_Correct_String_Representation()
  46. {
  47. var target = default(Selector)
  48. .OfType<TextBlock>()
  49. .PropertyEquals(TextBlock.TextProperty, "foo");
  50. Assert.Equal("TextBlock[Text=foo]", target.ToString());
  51. }
  52. private enum FooBar
  53. {
  54. Foo,
  55. Bar
  56. }
  57. }
  58. }