SelectorGrammarTests.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (c) The Avalonia 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 Avalonia.Markup.Parsers;
  5. using Sprache;
  6. using Xunit;
  7. namespace Avalonia.Markup.UnitTests.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", Xmlns = null } },
  17. result);
  18. }
  19. [Fact]
  20. public void NamespacedOfType()
  21. {
  22. var result = SelectorGrammar.Selector.Parse("x|Button").ToList();
  23. Assert.Equal(
  24. new[] { new SelectorGrammar.OfTypeSyntax { TypeName = "Button", Xmlns = "x" } },
  25. result);
  26. }
  27. [Fact]
  28. public void Name()
  29. {
  30. var result = SelectorGrammar.Selector.Parse("#foo").ToList();
  31. Assert.Equal(
  32. new[] { new SelectorGrammar.NameSyntax { Name = "foo" }, },
  33. result);
  34. }
  35. [Fact]
  36. public void OfType_Name()
  37. {
  38. var result = SelectorGrammar.Selector.Parse("Button#foo").ToList();
  39. Assert.Equal(
  40. new SelectorGrammar.ISyntax[]
  41. {
  42. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  43. new SelectorGrammar.NameSyntax { Name = "foo" },
  44. },
  45. result);
  46. }
  47. [Fact]
  48. public void Is()
  49. {
  50. var result = SelectorGrammar.Selector.Parse(":is(Button)").ToList();
  51. Assert.Equal(
  52. new[] { new SelectorGrammar.IsSyntax { TypeName = "Button", Xmlns = null } },
  53. result);
  54. }
  55. [Fact]
  56. public void Is_Name()
  57. {
  58. var result = SelectorGrammar.Selector.Parse(":is(Button)#foo").ToList();
  59. Assert.Equal(
  60. new SelectorGrammar.ISyntax[]
  61. {
  62. new SelectorGrammar.IsSyntax { TypeName = "Button" },
  63. new SelectorGrammar.NameSyntax { Name = "foo" },
  64. },
  65. result);
  66. }
  67. [Fact]
  68. public void NamespacedIs_Name()
  69. {
  70. var result = SelectorGrammar.Selector.Parse(":is(x|Button)#foo").ToList();
  71. Assert.Equal(
  72. new SelectorGrammar.ISyntax[]
  73. {
  74. new SelectorGrammar.IsSyntax { TypeName = "Button", Xmlns = "x" },
  75. new SelectorGrammar.NameSyntax { Name = "foo" },
  76. },
  77. result);
  78. }
  79. [Fact]
  80. public void Class()
  81. {
  82. var result = SelectorGrammar.Selector.Parse(".foo").ToList();
  83. Assert.Equal(
  84. new[] { new SelectorGrammar.ClassSyntax { Class = "foo" } },
  85. result);
  86. }
  87. [Fact]
  88. public void Pseudoclass()
  89. {
  90. var result = SelectorGrammar.Selector.Parse(":foo").ToList();
  91. Assert.Equal(
  92. new[] { new SelectorGrammar.ClassSyntax { Class = ":foo" } },
  93. result);
  94. }
  95. [Fact]
  96. public void OfType_Class()
  97. {
  98. var result = SelectorGrammar.Selector.Parse("Button.foo").ToList();
  99. Assert.Equal(
  100. new SelectorGrammar.ISyntax[]
  101. {
  102. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  103. new SelectorGrammar.ClassSyntax { Class = "foo" },
  104. },
  105. result);
  106. }
  107. [Fact]
  108. public void OfType_Child_Class()
  109. {
  110. var result = SelectorGrammar.Selector.Parse("Button > .foo").ToList();
  111. Assert.Equal(
  112. new SelectorGrammar.ISyntax[]
  113. {
  114. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  115. new SelectorGrammar.ChildSyntax { },
  116. new SelectorGrammar.ClassSyntax { Class = "foo" },
  117. },
  118. result);
  119. }
  120. [Fact]
  121. public void OfType_Child_Class_No_Spaces()
  122. {
  123. var result = SelectorGrammar.Selector.Parse("Button>.foo").ToList();
  124. Assert.Equal(
  125. new SelectorGrammar.ISyntax[]
  126. {
  127. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  128. new SelectorGrammar.ChildSyntax { },
  129. new SelectorGrammar.ClassSyntax { Class = "foo" },
  130. },
  131. result);
  132. }
  133. [Fact]
  134. public void OfType_Descendant_Class()
  135. {
  136. var result = SelectorGrammar.Selector.Parse("Button .foo").ToList();
  137. Assert.Equal(
  138. new SelectorGrammar.ISyntax[]
  139. {
  140. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  141. new SelectorGrammar.DescendantSyntax { },
  142. new SelectorGrammar.ClassSyntax { Class = "foo" },
  143. },
  144. result);
  145. }
  146. [Fact]
  147. public void OfType_Template_Class()
  148. {
  149. var result = SelectorGrammar.Selector.Parse("Button /template/ .foo").ToList();
  150. Assert.Equal(
  151. new SelectorGrammar.ISyntax[]
  152. {
  153. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  154. new SelectorGrammar.TemplateSyntax { },
  155. new SelectorGrammar.ClassSyntax { Class = "foo" },
  156. },
  157. result);
  158. }
  159. [Fact]
  160. public void OfType_Property()
  161. {
  162. var result = SelectorGrammar.Selector.Parse("Button[Foo=bar]").ToList();
  163. Assert.Equal(
  164. new SelectorGrammar.ISyntax[]
  165. {
  166. new SelectorGrammar.OfTypeSyntax { TypeName = "Button" },
  167. new SelectorGrammar.PropertySyntax { Property = "Foo", Value = "bar" },
  168. },
  169. result);
  170. }
  171. [Fact]
  172. public void Namespace_Alone_Fails()
  173. {
  174. Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse("ns|").ToList());
  175. }
  176. [Fact]
  177. public void Dot_Alone_Fails()
  178. {
  179. Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse(". dot").ToList());
  180. }
  181. [Fact]
  182. public void Invalid_Identifier_Fails()
  183. {
  184. Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse("%foo").ToList());
  185. }
  186. [Fact]
  187. public void Invalid_Class_Fails()
  188. {
  189. Assert.Throws<ParseException>(() => SelectorGrammar.Selector.Parse(".%foo").ToList());
  190. }
  191. }
  192. }