SelectorGrammarTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) The Perspex 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.Linq;
  4. using Perspex.Markup.Xaml.Parsers;
  5. using Sprache;
  6. using Xunit;
  7. namespace Perspex.Xaml.Base.UnitTest.Parsers
  8. {
  9. public class SelectorGrammarTests
  10. {
  11. [Fact]
  12. public void OfType()
  13. {
  14. var result = SelectorGrammar.Selector.Parse("Button").ToList();
  15. Assert.Equal(
  16. new[] { new SelectorGrammar.OfTypeSyntax { TypeName = "Button" } },
  17. result);
  18. }
  19. [Fact]
  20. public void Name()
  21. {
  22. var result = SelectorGrammar.Selector.Parse("#foo").ToList();
  23. Assert.Equal(
  24. new[] { new SelectorGrammar.NameSyntax { Name = "foo" }, },
  25. result);
  26. }
  27. [Fact]
  28. public void OfType_Name()
  29. {
  30. var result = SelectorGrammar.Selector.Parse("Button#foo").ToList();
  31. Assert.Equal(
  32. new SelectorGrammar.ISyntax[]
  33. {
  34. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  35. new SelectorGrammar.NameSyntax { Name = "foo" },
  36. },
  37. result);
  38. }
  39. [Fact]
  40. public void Class()
  41. {
  42. var result = SelectorGrammar.Selector.Parse(".foo").ToList();
  43. Assert.Equal(
  44. new[] { new SelectorGrammar.ClassSyntax { Class = "foo" } },
  45. result);
  46. }
  47. [Fact]
  48. public void Pseudoclass()
  49. {
  50. var result = SelectorGrammar.Selector.Parse(":foo").ToList();
  51. Assert.Equal(
  52. new[] { new SelectorGrammar.ClassSyntax { Class = ":foo" } },
  53. result);
  54. }
  55. [Fact]
  56. public void OfType_Class()
  57. {
  58. var result = SelectorGrammar.Selector.Parse("Button.foo").ToList();
  59. Assert.Equal(
  60. new SelectorGrammar.ISyntax[]
  61. {
  62. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  63. new SelectorGrammar.ClassSyntax { Class = "foo" },
  64. },
  65. result);
  66. }
  67. [Fact]
  68. public void OfType_Child_Class()
  69. {
  70. var result = SelectorGrammar.Selector.Parse("Button < .foo").ToList();
  71. Assert.Equal(
  72. new SelectorGrammar.ISyntax[]
  73. {
  74. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  75. new SelectorGrammar.ChildSyntax { },
  76. new SelectorGrammar.ClassSyntax { Class = "foo" },
  77. },
  78. result);
  79. }
  80. [Fact]
  81. public void OfType_Descendent_Class()
  82. {
  83. var result = SelectorGrammar.Selector.Parse("Button .foo").ToList();
  84. Assert.Equal(
  85. new SelectorGrammar.ISyntax[]
  86. {
  87. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  88. new SelectorGrammar.DescendentSyntax { },
  89. new SelectorGrammar.ClassSyntax { Class = "foo" },
  90. },
  91. result);
  92. }
  93. [Fact]
  94. public void OfType_Template_Class()
  95. {
  96. var result = SelectorGrammar.Selector.Parse("Button /template/ .foo").ToList();
  97. Assert.Equal(
  98. new SelectorGrammar.ISyntax[]
  99. {
  100. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  101. new SelectorGrammar.TemplateSyntax { },
  102. new SelectorGrammar.ClassSyntax { Class = "foo" },
  103. },
  104. result);
  105. }
  106. [Fact]
  107. public void OfType_Property()
  108. {
  109. var result = SelectorGrammar.Selector.Parse("Button[Foo=bar]").ToList();
  110. Assert.Equal(
  111. new SelectorGrammar.ISyntax[]
  112. {
  113. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  114. new SelectorGrammar.PropertySyntax { Property = "Foo", Value = "bar" },
  115. },
  116. result);
  117. }
  118. }
  119. }