CollectionNavigatorTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Threading;
  2. using Xunit;
  3. namespace Terminal.Gui.Core {
  4. public class CollectionNavigatorTests {
  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 ShouldAcceptNegativeOne ()
  14. {
  15. var n = new CollectionNavigator (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.GetNextMatchingItem (-1, 'b'));
  19. }
  20. [Fact]
  21. public void OutOfBoundsShouldBeIgnored ()
  22. {
  23. var n = new CollectionNavigator (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.GetNextMatchingItem (500, 'b'));
  27. }
  28. [Fact]
  29. public void Cycling ()
  30. {
  31. var n = new CollectionNavigator (simpleStrings);
  32. Assert.Equal (2, n.GetNextMatchingItem (0, 'b'));
  33. Assert.Equal (3, n.GetNextMatchingItem (2, 'b'));
  34. // if 4 (candle) is selected it should loop back to bat
  35. Assert.Equal (2, n.GetNextMatchingItem (4, 'b'));
  36. }
  37. [Fact]
  38. public void ToSearchText ()
  39. {
  40. var strings = new string []{
  41. "appricot",
  42. "arm",
  43. "bat",
  44. "batman",
  45. "bbfish",
  46. "candle"
  47. };
  48. int current = 0;
  49. var n = new CollectionNavigator (strings);
  50. Assert.Equal (2, current = n.GetNextMatchingItem (current, 'b')); // match bat
  51. Assert.Equal (4, current = n.GetNextMatchingItem (current, 'b')); // match bbfish
  52. // another 'b' means searching for "bbb" which does not exist
  53. // so we go back to looking for "b" as a fresh key strike
  54. Assert.Equal (2, current = n.GetNextMatchingItem (current, 'b')); // match bat
  55. }
  56. [Fact]
  57. public void FullText ()
  58. {
  59. var strings = new string []{
  60. "appricot",
  61. "arm",
  62. "ta",
  63. "target",
  64. "text",
  65. "egg",
  66. "candle"
  67. };
  68. var n = new CollectionNavigator (strings);
  69. Assert.Equal (2, n.GetNextMatchingItem (0, 't'));
  70. // should match "te" in "text"
  71. Assert.Equal (4, n.GetNextMatchingItem (2, 'e'));
  72. // still matches text
  73. Assert.Equal (4, n.GetNextMatchingItem (4, 'x'));
  74. // nothing starts texa so it jumps to a for appricot
  75. Assert.Equal (0, n.GetNextMatchingItem (4, 'a'));
  76. }
  77. [Fact]
  78. public void Unicode ()
  79. {
  80. var strings = new string []{
  81. "appricot",
  82. "arm",
  83. "ta",
  84. "丗丙业丞",
  85. "丗丙丛",
  86. "text",
  87. "egg",
  88. "candle"
  89. };
  90. var n = new CollectionNavigator (strings);
  91. Assert.Equal (3, n.GetNextMatchingItem (0, '丗'));
  92. // 丗丙业丞 is as good a match as 丗丙丛
  93. // so when doing multi character searches we should
  94. // prefer to stay on the same index unless we invalidate
  95. // our typed text
  96. Assert.Equal (3, n.GetNextMatchingItem (3, '丙'));
  97. // No longer matches 丗丙业丞 and now only matches 丗丙丛
  98. // so we should move to the new match
  99. Assert.Equal (4, n.GetNextMatchingItem (3, '丛'));
  100. // nothing starts "丗丙丛a" so it jumps to a for appricot
  101. Assert.Equal (0, n.GetNextMatchingItem (4, 'a'));
  102. }
  103. [Fact]
  104. public void AtSymbol ()
  105. {
  106. var strings = new string []{
  107. "appricot",
  108. "arm",
  109. "ta",
  110. "@bob",
  111. "@bb",
  112. "text",
  113. "egg",
  114. "candle"
  115. };
  116. var n = new CollectionNavigator (strings);
  117. Assert.Equal (3, n.GetNextMatchingItem (0, '@'));
  118. Assert.Equal (3, n.GetNextMatchingItem (3, 'b'));
  119. Assert.Equal (4, n.GetNextMatchingItem (3, 'b'));
  120. }
  121. [Fact]
  122. public void Word ()
  123. {
  124. var strings = new string []{
  125. "appricot",
  126. "arm",
  127. "bat",
  128. "batman",
  129. "bates hotel",
  130. "candle"
  131. };
  132. int current = 0;
  133. var n = new CollectionNavigator (strings);
  134. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'b')); // match bat
  135. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'a')); // match bat
  136. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 't')); // match bat
  137. Assert.Equal (strings.IndexOf ("bates hotel"), current = n.GetNextMatchingItem (current, 'e')); // match bates hotel
  138. Assert.Equal (strings.IndexOf ("bates hotel"), current = n.GetNextMatchingItem (current, 's')); // match bates hotel
  139. Assert.Equal (strings.IndexOf ("bates hotel"), current = n.GetNextMatchingItem (current, ' ')); // match bates hotel
  140. // another 'b' means searching for "bates b" which does not exist
  141. // so we go back to looking for "b" as a fresh key strike
  142. Assert.Equal (strings.IndexOf<string> ("bat"), current = n.GetNextMatchingItem (current, 'b')); // match bat
  143. }
  144. [Fact]
  145. public void Symbols ()
  146. {
  147. var strings = new string []{
  148. "$$",
  149. "$100.00",
  150. "$101.00",
  151. "$101.10",
  152. "$200.00",
  153. "appricot"
  154. };
  155. int current = 0;
  156. var n = new CollectionNavigator (strings);
  157. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  158. Assert.Equal ("a", n.SearchString);
  159. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  160. Assert.Equal ("$", n.SearchString);
  161. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '1'));
  162. Assert.Equal ("$1", n.SearchString);
  163. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '0'));
  164. Assert.Equal ("$10", n.SearchString);
  165. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '1'));
  166. Assert.Equal ("$101", n.SearchString);
  167. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '.'));
  168. Assert.Equal ("$101.", n.SearchString);
  169. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  170. Assert.Equal ("a", n.SearchString);
  171. // another '$' means searching for "$" again
  172. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  173. Assert.Equal ("$", n.SearchString);
  174. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  175. Assert.Equal ("$$", n.SearchString);
  176. }
  177. [Fact]
  178. public void Delay ()
  179. {
  180. var strings = new string []{
  181. "$$",
  182. "$100.00",
  183. "$101.00",
  184. "$101.10",
  185. "$200.00",
  186. "appricot"
  187. };
  188. int current = 0;
  189. var n = new CollectionNavigator (strings);
  190. // No delay
  191. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  192. Assert.Equal ("a", n.SearchString);
  193. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  194. Assert.Equal ("$", n.SearchString);
  195. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  196. Assert.Equal ("$$", n.SearchString);
  197. // Delay
  198. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  199. Assert.Equal ("a", n.SearchString);
  200. Thread.Sleep (n.TypingDelay + 10);
  201. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  202. Assert.Equal ("$", n.SearchString);
  203. Thread.Sleep (n.TypingDelay + 10);
  204. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '$'));
  205. Assert.Equal ("$", n.SearchString);
  206. Thread.Sleep (n.TypingDelay + 10);
  207. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '$'));
  208. Assert.Equal ("$", n.SearchString);
  209. Thread.Sleep (n.TypingDelay + 10);
  210. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
  211. Assert.Equal ("$", n.SearchString);
  212. Thread.Sleep (n.TypingDelay + 10);
  213. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '2')); // Shouldn't move
  214. Assert.Equal ("2", n.SearchString);
  215. }
  216. [Fact]
  217. public void MinimizeMovement_False_ShouldMoveIfMultipleMatches ()
  218. {
  219. var strings = new string [] {
  220. "$$",
  221. "$100.00",
  222. "$101.00",
  223. "$101.10",
  224. "$200.00",
  225. "appricot",
  226. "c",
  227. "car",
  228. "cart",
  229. };
  230. int current = 0;
  231. var n = new CollectionNavigator (strings);
  232. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", false));
  233. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", false));
  234. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", false)); // back to top
  235. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", false));
  236. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$", false));
  237. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, "$", false));
  238. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$", false));
  239. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", false)); // back to top
  240. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, "a", false));
  241. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", false)); // back to top
  242. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$100.00", false));
  243. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$", false));
  244. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00", false));
  245. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2", false));
  246. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$200.00", false));
  247. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00", false));
  248. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2", false));
  249. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00", false));
  250. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2", false));
  251. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", false));
  252. Assert.Equal (strings.IndexOf ("cart"), current = n.GetNextMatchingItem (current, "car", false));
  253. Assert.Equal (-1, current = n.GetNextMatchingItem (current, "x", false));
  254. }
  255. [Fact]
  256. public void MinimizeMovement_True_ShouldStayOnCurrentIfMultipleMatches ()
  257. {
  258. var strings = new string [] {
  259. "$$",
  260. "$100.00",
  261. "$101.00",
  262. "$101.10",
  263. "$200.00",
  264. "appricot",
  265. "c",
  266. "car",
  267. "cart",
  268. };
  269. int current = 0;
  270. var n = new CollectionNavigator (strings);
  271. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true));
  272. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", true));
  273. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true)); // back to top
  274. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$1", true));
  275. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
  276. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
  277. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
  278. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
  279. Assert.Equal (-1, current = n.GetNextMatchingItem (current, "x", true));
  280. }
  281. }
  282. }