| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- // 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.Linq;
- using Avalonia.Markup.Parsers;
- using Sprache;
- using Xunit;
- namespace Avalonia.Markup.UnitTests.Parsers
- {
- public class SelectorGrammarTests
- {
- [Fact]
- public void OfType()
- {
- var result = SelectorGrammar.Selector.Parse("Button").ToList();
- Assert.Equal(
- new[] { new SelectorGrammar.OfTypeSyntax { TypeName = "Button", Xmlns = null } },
- result);
- }
- [Fact]
- public void NamespacedOfType()
- {
- var result = SelectorGrammar.Selector.Parse("x|Button").ToList();
- Assert.Equal(
- new[] { new SelectorGrammar.OfTypeSyntax { TypeName = "Button", Xmlns = "x" } },
- result);
- }
- [Fact]
- public void Name()
- {
- var result = SelectorGrammar.Selector.Parse("#foo").ToList();
- Assert.Equal(
- new[] { new SelectorGrammar.NameSyntax { Name = "foo" }, },
- result);
- }
- [Fact]
- public void OfType_Name()
- {
- var result = SelectorGrammar.Selector.Parse("Button#foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.NameSyntax { Name = "foo" },
- },
- result);
- }
- [Fact]
- public void Is()
- {
- var result = SelectorGrammar.Selector.Parse(":is(Button)").ToList();
- Assert.Equal(
- new[] { new SelectorGrammar.IsSyntax { TypeName = "Button", Xmlns = null } },
- result);
- }
- [Fact]
- public void Is_Name()
- {
- var result = SelectorGrammar.Selector.Parse(":is(Button)#foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.IsSyntax { TypeName = "Button" },
- new SelectorGrammar.NameSyntax { Name = "foo" },
- },
- result);
- }
- [Fact]
- public void NamespacedIs_Name()
- {
- var result = SelectorGrammar.Selector.Parse(":is(x|Button)#foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.IsSyntax { TypeName = "Button", Xmlns = "x" },
- new SelectorGrammar.NameSyntax { Name = "foo" },
- },
- result);
- }
- [Fact]
- public void Class()
- {
- var result = SelectorGrammar.Selector.Parse(".foo").ToList();
- Assert.Equal(
- new[] { new SelectorGrammar.ClassSyntax { Class = "foo" } },
- result);
- }
- [Fact]
- public void Pseudoclass()
- {
- var result = SelectorGrammar.Selector.Parse(":foo").ToList();
- Assert.Equal(
- new[] { new SelectorGrammar.ClassSyntax { Class = ":foo" } },
- result);
- }
- [Fact]
- public void OfType_Class()
- {
- var result = SelectorGrammar.Selector.Parse("Button.foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.ClassSyntax { Class = "foo" },
- },
- result);
- }
- [Fact]
- public void OfType_Child_Class()
- {
- var result = SelectorGrammar.Selector.Parse("Button > .foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.ChildSyntax { },
- new SelectorGrammar.ClassSyntax { Class = "foo" },
- },
- result);
- }
- [Fact]
- public void OfType_Child_Class_No_Spaces()
- {
- var result = SelectorGrammar.Selector.Parse("Button>.foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.ChildSyntax { },
- new SelectorGrammar.ClassSyntax { Class = "foo" },
- },
- result);
- }
- [Fact]
- public void OfType_Descendant_Class()
- {
- var result = SelectorGrammar.Selector.Parse("Button .foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.DescendantSyntax { },
- new SelectorGrammar.ClassSyntax { Class = "foo" },
- },
- result);
- }
- [Fact]
- public void OfType_Template_Class()
- {
- var result = SelectorGrammar.Selector.Parse("Button /template/ .foo").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.TemplateSyntax { },
- new SelectorGrammar.ClassSyntax { Class = "foo" },
- },
- result);
- }
- [Fact]
- public void OfType_Property()
- {
- var result = SelectorGrammar.Selector.Parse("Button[Foo=bar]").ToList();
- Assert.Equal(
- new SelectorGrammar.ISyntax[]
- {
- new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
- new SelectorGrammar.PropertySyntax { Property = "Foo", Value = "bar" },
- },
- result);
- }
- [Fact]
- public void Namespace_Alone_Fails()
- {
- Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse("ns|").ToList());
- }
- [Fact]
- public void Dot_Alone_Fails()
- {
- Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse(". dot").ToList());
- }
- [Fact]
- public void Invalid_Identifier_Fails()
- {
- Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse("%foo").ToList());
- }
- [Fact]
- public void Invalid_Class_Fails()
- {
- Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse(".%foo").ToList());
- }
- }
- }
|