SearchCollectionNavigatorTests.cs 3.6 KB

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