Browse Source

feat(Style): Add test to check using attached properties in attribute selector

pull/6992/head
Giuseppe Lippolis 5 years ago
parent
commit
3e9c86054b
  1. 36
      tests/Avalonia.Markup.UnitTests/Parsers/SelectorGrammarTests.cs
  2. 49
      tests/Avalonia.Markup.UnitTests/Parsers/SelectorParserTests.cs
  3. 67
      tests/Avalonia.Styling.UnitTests/SelectorTests_PropertyEquals.cs

36
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()
{

49
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<string> NameProperty =
AvaloniaProperty.RegisterAttached<Auth, AvaloniaObject, string>("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()
{

67
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<string> NameProperty =
AvaloniaProperty.RegisterAttached<Auth, AvaloniaObject, string>("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()
{

Loading…
Cancel
Save