From 3e9c86054b71efafe67c146d59334e099b2792e7 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Mon, 22 Nov 2021 13:00:15 +0100 Subject: [PATCH] feat(Style): Add test to check using attached properties in attribute selector --- .../Parsers/SelectorGrammarTests.cs | 36 ++++++++++ .../Parsers/SelectorParserTests.cs | 49 ++++++++++++++ .../SelectorTests_PropertyEquals.cs | 67 +++++++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/tests/Avalonia.Markup.UnitTests/Parsers/SelectorGrammarTests.cs b/tests/Avalonia.Markup.UnitTests/Parsers/SelectorGrammarTests.cs index 568f6deaf2..6fbf024ff1 100644 --- a/tests/Avalonia.Markup.UnitTests/Parsers/SelectorGrammarTests.cs +++ b/tests/Avalonia.Markup.UnitTests/Parsers/SelectorGrammarTests.cs @@ -197,6 +197,42 @@ namespace Avalonia.Markup.UnitTests.Parsers result); } + [Fact] + public void OfType_AttachedProperty() + { + var result = SelectorGrammar.Parse("Button[(Grid.Column)=1]"); + + Assert.Equal( + new SelectorGrammar.ISyntax[] + { + new SelectorGrammar.OfTypeSyntax { TypeName = "Button" }, + new SelectorGrammar.AttachedPropertySyntax { + Xmlns = string.Empty, + TypeName="Grid", + Property = "Column", + Value = "1" }, + }, + result); + } + + [Fact] + public void OfType_AttachedProperty_WithNamespace() + { + var result = SelectorGrammar.Parse("Button[(x|Grid.Column)=1]"); + + Assert.Equal( + new SelectorGrammar.ISyntax[] + { + new SelectorGrammar.OfTypeSyntax { TypeName = "Button" }, + new SelectorGrammar.AttachedPropertySyntax { + Xmlns = "x", + TypeName="Grid", + Property = "Column", + Value = "1" }, + }, + result); + } + [Fact] public void Not_OfType() { diff --git a/tests/Avalonia.Markup.UnitTests/Parsers/SelectorParserTests.cs b/tests/Avalonia.Markup.UnitTests/Parsers/SelectorParserTests.cs index 1c0cba56c9..338bdb3ae1 100644 --- a/tests/Avalonia.Markup.UnitTests/Parsers/SelectorParserTests.cs +++ b/tests/Avalonia.Markup.UnitTests/Parsers/SelectorParserTests.cs @@ -7,6 +7,25 @@ namespace Avalonia.Markup.UnitTests.Parsers { public class SelectorParserTests { + static SelectorParserTests() + { + //Ensure the attached properties are registered before run tests + System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Grid).TypeHandle); + System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Auth).TypeHandle); + } + + class Auth + { + public readonly static AttachedProperty NameProperty = + AvaloniaProperty.RegisterAttached("Name"); + + public static string GetName(AvaloniaObject avaloniaObject) => + avaloniaObject.GetValue(NameProperty); + + public static void SetName(AvaloniaObject avaloniaObject, string value) => + avaloniaObject.SetValue(NameProperty, value); + } + [Fact] public void Parses_Boolean_Property_Selector() { @@ -14,6 +33,36 @@ namespace Avalonia.Markup.UnitTests.Parsers var result = target.Parse("TextBlock[IsPointerOver=True]"); } + [Fact] + public void Parses_AttacchedProperty_Selector_With_Namespace() + { + var target = new SelectorParser((ns, type) => + { + return (ns, type) switch + { + ("", nameof(TextBlock)) => typeof(TextBlock), + ("l",nameof(Auth)) => typeof(Auth), + _ => null + }; + }); + var result = target.Parse("TextBlock[(l|Auth.Name)=Admin]"); + } + + [Fact] + public void Parses_AttacchedProperty_Selector() + { + var target = new SelectorParser((ns, type) => + { + return (ns, type) switch + { + ("", nameof(TextBlock)) => typeof(TextBlock), + ("", nameof(Grid)) => typeof(Grid), + _ => null + }; + }); + var result = target.Parse("TextBlock[(Grid.Column)=1]"); + } + [Fact] public void Parses_Comma_Separated_Selectors() { diff --git a/tests/Avalonia.Styling.UnitTests/SelectorTests_PropertyEquals.cs b/tests/Avalonia.Styling.UnitTests/SelectorTests_PropertyEquals.cs index 7689a458ae..729b42196b 100644 --- a/tests/Avalonia.Styling.UnitTests/SelectorTests_PropertyEquals.cs +++ b/tests/Avalonia.Styling.UnitTests/SelectorTests_PropertyEquals.cs @@ -8,6 +8,73 @@ namespace Avalonia.Styling.UnitTests { public class SelectorTests_PropertyEquals { + static SelectorTests_PropertyEquals() + { + //Ensure the attached properties are registered before run tests + System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Grid).TypeHandle); + System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Auth).TypeHandle); + } + + class Auth + { + public readonly static AttachedProperty NameProperty = + AvaloniaProperty.RegisterAttached("Name"); + + public static string GetName(AvaloniaObject avaloniaObject) => + avaloniaObject.GetValue(NameProperty); + + public static void SetName(AvaloniaObject avaloniaObject, string value) => + avaloniaObject.SetValue(NameProperty, value); + } + + [Fact] + public async Task PropertyEquals_Attached_Property_Matching_Value() + { + var target = new Markup.Parsers.SelectorParser((ns, type) => + { + return (ns, type) switch + { + ("", nameof(TextBlock)) => typeof(TextBlock), + ("", nameof(Grid)) => typeof(Grid), + _ => null + }; + }).Parse("TextBlock[(Grid.Column)=1]"); + + + var control = new TextBlock(); + var activator = target.Match(control).Activator.ToObservable(); + + Assert.False(await activator.Take(1)); + Grid.SetColumn(control, 1); + Assert.True(await activator.Take(1)); + Grid.SetColumn(control, 0); + Assert.False(await activator.Take(1)); + } + + [Fact] + public async Task PropertyEquals_Attached_Property_With_Namespace_Matching_Value() + { + var target = new Markup.Parsers.SelectorParser((ns, type) => + { + return (ns, type) switch + { + ("", nameof(TextBlock)) => typeof(TextBlock), + ("l", nameof(Auth)) => typeof(Auth), + _ => null + }; + }).Parse("TextBlock[(l|Auth.Name)=Admin]"); + + + var control = new TextBlock(); + var activator = target.Match(control).Activator.ToObservable(); + + Assert.False(await activator.Take(1)); + Auth.SetName(control, "Admin"); + Assert.True(await activator.Take(1)); + Auth.SetName(control, null); + Assert.False(await activator.Take(1)); + } + [Fact] public async Task PropertyEquals_Matches_When_Property_Has_Matching_Value() {