SearchCollectionNavigatorTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Terminal.Gui;
  2. using Xunit;
  3. namespace Terminal.Gui.Core {
  4. public class SearchCollectionNavigatorTests {
  5. [Fact]
  6. public void TestSearchCollectionNavigator_Cycling ()
  7. {
  8. var strings = new string []{
  9. "appricot",
  10. "arm",
  11. "bat",
  12. "batman",
  13. "candle"
  14. };
  15. var n = new SearchCollectionNavigator (strings);
  16. Assert.Equal (2, n.CalculateNewIndex ( 0, 'b'));
  17. Assert.Equal (3, n.CalculateNewIndex ( 2, 'b'));
  18. // if 4 (candle) is selected it should loop back to bat
  19. Assert.Equal (2, n.CalculateNewIndex ( 4, 'b'));
  20. }
  21. [Fact]
  22. public void TestSearchCollectionNavigator_ToSearchText ()
  23. {
  24. var strings = new string []{
  25. "appricot",
  26. "arm",
  27. "bat",
  28. "batman",
  29. "bbfish",
  30. "candle"
  31. };
  32. var n = new SearchCollectionNavigator (strings);
  33. Assert.Equal (2, n.CalculateNewIndex (0, 'b'));
  34. Assert.Equal (4, n.CalculateNewIndex (2, 'b'));
  35. // another 'b' means searching for "bbb" which does not exist
  36. // so we go back to looking for "b" as a fresh key strike
  37. Assert.Equal (4, n.CalculateNewIndex (2, 'b'));
  38. }
  39. [Fact]
  40. public void TestSearchCollectionNavigator_FullText ()
  41. {
  42. var strings = new string []{
  43. "appricot",
  44. "arm",
  45. "ta",
  46. "target",
  47. "text",
  48. "egg",
  49. "candle"
  50. };
  51. var n = new SearchCollectionNavigator (strings);
  52. Assert.Equal (2, n.CalculateNewIndex (0, 't'));
  53. // should match "te" in "text"
  54. Assert.Equal (4, n.CalculateNewIndex (2, 'e'));
  55. // still matches text
  56. Assert.Equal (4, n.CalculateNewIndex (4, 'x'));
  57. // nothing starts texa so it jumps to a for appricot
  58. Assert.Equal (0, n.CalculateNewIndex (4, 'a'));
  59. }
  60. [Fact]
  61. public void TestSearchCollectionNavigator_Unicode ()
  62. {
  63. var strings = new string []{
  64. "appricot",
  65. "arm",
  66. "ta",
  67. "丗丙业丞",
  68. "丗丙丛",
  69. "text",
  70. "egg",
  71. "candle"
  72. };
  73. var n = new SearchCollectionNavigator (strings);
  74. Assert.Equal (3, n.CalculateNewIndex (0, '丗'));
  75. // 丗丙业丞 is as good a match as 丗丙丛
  76. // so when doing multi character searches we should
  77. // prefer to stay on the same index unless we invalidate
  78. // our typed text
  79. Assert.Equal (3, n.CalculateNewIndex (3, '丙'));
  80. // No longer matches 丗丙业丞 and now only matches 丗丙丛
  81. // so we should move to the new match
  82. Assert.Equal (4, n.CalculateNewIndex (3, '丛'));
  83. // nothing starts "丗丙丛a" so it jumps to a for appricot
  84. Assert.Equal (0, n.CalculateNewIndex (4, 'a'));
  85. }
  86. [Fact]
  87. public void TestSearchCollectionNavigator_AtSymbol ()
  88. {
  89. var strings = new string []{
  90. "appricot",
  91. "arm",
  92. "ta",
  93. "@bob",
  94. "@bb",
  95. "text",
  96. "egg",
  97. "candle"
  98. };
  99. var n = new SearchCollectionNavigator (strings);
  100. Assert.Equal (3, n.CalculateNewIndex (0, '@'));
  101. Assert.Equal (3, n.CalculateNewIndex (3, 'b'));
  102. Assert.Equal (4, n.CalculateNewIndex (3, 'b'));
  103. }
  104. }
  105. }