PropertyPathGrammarTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Avalonia.Markup.Parsers;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests.Data.Core
  6. {
  7. public class PropertyPathGrammarTests
  8. {
  9. static void Check(string s, params PropertyPathGrammar.ISyntax[] expected)
  10. {
  11. var parsed = PropertyPathGrammar.Parse(s).ToList();
  12. Assert.Equal(expected.Length, parsed.Count);
  13. for (var c = 0; c < parsed.Count; c++)
  14. Assert.Equal(expected[c], parsed[c]);
  15. }
  16. [Fact]
  17. public void PropertyPath_Should_Support_Simple_Properties()
  18. {
  19. Check("SomeProperty", new PropertyPathGrammar.PropertySyntax {Name = "SomeProperty"});
  20. }
  21. [Fact]
  22. public void PropertyPath_Should_Ignore_Trailing_Whitespace()
  23. {
  24. Check(" SomeProperty ", new PropertyPathGrammar.PropertySyntax {Name = "SomeProperty"});
  25. }
  26. [Fact]
  27. public void PropertyPath_Should_Support_Qualified_Properties()
  28. {
  29. Check(" ( somens:SomeType.SomeProperty ) ",
  30. new PropertyPathGrammar.TypeQualifiedPropertySyntax()
  31. {
  32. Name = "SomeProperty", TypeName = "SomeType", TypeNamespace = "somens"
  33. });
  34. }
  35. [Fact]
  36. public void PropertyPath_Should_Support_Property_Paths()
  37. {
  38. Check(" ( somens:SomeType.SomeProperty ).Child . SubChild ",
  39. new PropertyPathGrammar.TypeQualifiedPropertySyntax()
  40. {
  41. Name = "SomeProperty", TypeName = "SomeType", TypeNamespace = "somens"
  42. },
  43. PropertyPathGrammar.ChildTraversalSyntax.Instance,
  44. new PropertyPathGrammar.PropertySyntax {Name = "Child"},
  45. PropertyPathGrammar.ChildTraversalSyntax.Instance,
  46. new PropertyPathGrammar.PropertySyntax {Name = "SubChild"}
  47. );
  48. }
  49. [Fact]
  50. public void PropertyPath_Should_Support_Casts()
  51. {
  52. Check(" ( somens:SomeType.SomeProperty ) :> SomeType.Child as somens:SomeType . SubChild ",
  53. new PropertyPathGrammar.TypeQualifiedPropertySyntax()
  54. {
  55. Name = "SomeProperty", TypeName = "SomeType", TypeNamespace = "somens"
  56. },
  57. new PropertyPathGrammar.CastTypeSyntax
  58. {
  59. TypeName = "SomeType"
  60. },
  61. PropertyPathGrammar.ChildTraversalSyntax.Instance,
  62. new PropertyPathGrammar.PropertySyntax {Name = "Child"},
  63. new PropertyPathGrammar.CastTypeSyntax
  64. {
  65. TypeName = "SomeType",
  66. TypeNamespace = "somens"
  67. },
  68. PropertyPathGrammar.ChildTraversalSyntax.Instance,
  69. new PropertyPathGrammar.PropertySyntax {Name = "SubChild"}
  70. );
  71. }
  72. [Fact]
  73. public void PropertyPath_Should_Support_Ensure_Type()
  74. {
  75. Check(" ( somens:SomeType.SomeProperty ) := SomeType.Child := somens:SomeType . SubChild ",
  76. new PropertyPathGrammar.TypeQualifiedPropertySyntax()
  77. {
  78. Name = "SomeProperty", TypeName = "SomeType", TypeNamespace = "somens"
  79. },
  80. new PropertyPathGrammar.EnsureTypeSyntax
  81. {
  82. TypeName = "SomeType"
  83. },
  84. PropertyPathGrammar.ChildTraversalSyntax.Instance,
  85. new PropertyPathGrammar.PropertySyntax {Name = "Child"},
  86. new PropertyPathGrammar.EnsureTypeSyntax
  87. {
  88. TypeName = "SomeType",
  89. TypeNamespace = "somens"
  90. },
  91. PropertyPathGrammar.ChildTraversalSyntax.Instance,
  92. new PropertyPathGrammar.PropertySyntax {Name = "SubChild"}
  93. );
  94. }
  95. }
  96. }