CollectionNavigatorTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using System.Threading;
  3. using Terminal.Gui;
  4. using Xunit;
  5. namespace Terminal.Gui.TextTests {
  6. public class CollectionNavigatorTests {
  7. static string [] simpleStrings = new string []{
  8. "appricot", // 0
  9. "arm", // 1
  10. "bat", // 2
  11. "batman", // 3
  12. "candle" // 4
  13. };
  14. [Fact]
  15. public void ShouldAcceptNegativeOne ()
  16. {
  17. var n = new CollectionNavigator (simpleStrings);
  18. // Expect that index of -1 (i.e. no selection) should work correctly
  19. // and select the first entry of the letter 'b'
  20. Assert.Equal (2, n.GetNextMatchingItem (-1, 'b'));
  21. }
  22. [Fact]
  23. public void OutOfBoundsShouldBeIgnored ()
  24. {
  25. var n = new CollectionNavigator (simpleStrings);
  26. // Expect saying that index 500 is the current selection should not cause
  27. // error and just be ignored (treated as no selection)
  28. Assert.Equal (2, n.GetNextMatchingItem (500, 'b'));
  29. }
  30. [Fact]
  31. public void Cycling ()
  32. {
  33. var n = new CollectionNavigator (simpleStrings);
  34. Assert.Equal (2, n.GetNextMatchingItem (0, 'b'));
  35. Assert.Equal (3, n.GetNextMatchingItem (2, 'b'));
  36. // if 4 (candle) is selected it should loop back to bat
  37. Assert.Equal (2, n.GetNextMatchingItem (4, 'b'));
  38. }
  39. [Fact]
  40. public void 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 CollectionNavigator (strings);
  52. int current = 0;
  53. Assert.Equal (strings.IndexOf ("ta"), current = n.GetNextMatchingItem (current, 't'));
  54. // should match "te" in "text"
  55. Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'e'));
  56. // still matches text
  57. Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'x'));
  58. // nothing starts texa so it should NOT jump to appricot
  59. Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'a'));
  60. Thread.Sleep (n.TypingDelay + 100);
  61. // nothing starts "texa". Since were past timedelay we DO jump to appricot
  62. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  63. }
  64. [Fact]
  65. public void Unicode ()
  66. {
  67. var strings = new string []{
  68. "appricot",
  69. "arm",
  70. "ta",
  71. "丗丙业丞",
  72. "丗丙丛",
  73. "text",
  74. "egg",
  75. "candle"
  76. };
  77. var n = new CollectionNavigator (strings);
  78. int current = 0;
  79. Assert.Equal (strings.IndexOf ("丗丙业丞"), current = n.GetNextMatchingItem (current, '丗'));
  80. // 丗丙业丞 is as good a match as 丗丙丛
  81. // so when doing multi character searches we should
  82. // prefer to stay on the same index unless we invalidate
  83. // our typed text
  84. Assert.Equal (strings.IndexOf ("丗丙业丞"), current = n.GetNextMatchingItem (current, '丙'));
  85. // No longer matches 丗丙业丞 and now only matches 丗丙丛
  86. // so we should move to the new match
  87. Assert.Equal (strings.IndexOf ("丗丙丛"), current = n.GetNextMatchingItem (current, '丛'));
  88. // nothing starts "丗丙丛a". Since were still in the timedelay we do not jump to appricot
  89. Assert.Equal (strings.IndexOf ("丗丙丛"), current = n.GetNextMatchingItem (current, 'a'));
  90. Thread.Sleep (n.TypingDelay + 100);
  91. // nothing starts "丗丙丛a". Since were past timedelay we DO jump to appricot
  92. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  93. }
  94. [Fact]
  95. public void AtSymbol ()
  96. {
  97. var strings = new string []{
  98. "appricot",
  99. "arm",
  100. "ta",
  101. "@bob",
  102. "@bb",
  103. "text",
  104. "egg",
  105. "candle"
  106. };
  107. var n = new CollectionNavigator (strings);
  108. Assert.Equal (3, n.GetNextMatchingItem (0, '@'));
  109. Assert.Equal (3, n.GetNextMatchingItem (3, 'b'));
  110. Assert.Equal (4, n.GetNextMatchingItem (3, 'b'));
  111. }
  112. [Fact]
  113. public void Word ()
  114. {
  115. var strings = new string []{
  116. "appricot",
  117. "arm",
  118. "bat",
  119. "batman",
  120. "bates hotel",
  121. "candle"
  122. };
  123. int current = 0;
  124. var n = new CollectionNavigator (strings);
  125. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'b')); // match bat
  126. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'a')); // match bat
  127. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 't')); // match bat
  128. Assert.Equal (strings.IndexOf ("bates hotel"), current = n.GetNextMatchingItem (current, 'e')); // match bates hotel
  129. Assert.Equal (strings.IndexOf ("bates hotel"), current = n.GetNextMatchingItem (current, 's')); // match bates hotel
  130. Assert.Equal (strings.IndexOf ("bates hotel"), current = n.GetNextMatchingItem (current, ' ')); // match bates hotel
  131. }
  132. [Fact]
  133. public void Symbols ()
  134. {
  135. var strings = new string []{
  136. "$$",
  137. "$100.00",
  138. "$101.00",
  139. "$101.10",
  140. "$200.00",
  141. "appricot"
  142. };
  143. int current = 0;
  144. var n = new CollectionNavigator (strings);
  145. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  146. Assert.Equal ("a", n.SearchString);
  147. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  148. Assert.Equal ("$", n.SearchString);
  149. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '1'));
  150. Assert.Equal ("$1", n.SearchString);
  151. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '0'));
  152. Assert.Equal ("$10", n.SearchString);
  153. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '1'));
  154. Assert.Equal ("$101", n.SearchString);
  155. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '.'));
  156. Assert.Equal ("$101.", n.SearchString);
  157. // stay on the same item becuase still in timedelay
  158. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, 'a'));
  159. Assert.Equal ("$101.", n.SearchString);
  160. Thread.Sleep (n.TypingDelay + 100);
  161. // another '$' means searching for "$" again
  162. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
  163. Assert.Equal ("$", n.SearchString);
  164. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  165. Assert.Equal ("$$", n.SearchString);
  166. }
  167. [Fact]
  168. public void Delay ()
  169. {
  170. var strings = new string []{
  171. "$$",
  172. "$100.00",
  173. "$101.00",
  174. "$101.10",
  175. "$200.00",
  176. "appricot"
  177. };
  178. int current = 0;
  179. var n = new CollectionNavigator (strings);
  180. // No delay
  181. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  182. Assert.Equal ("a", n.SearchString);
  183. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  184. Assert.Equal ("$", n.SearchString);
  185. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  186. Assert.Equal ("$$", n.SearchString);
  187. // Delay
  188. Thread.Sleep (n.TypingDelay + 10);
  189. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  190. Assert.Equal ("a", n.SearchString);
  191. Thread.Sleep (n.TypingDelay + 10);
  192. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  193. Assert.Equal ("$", n.SearchString);
  194. Thread.Sleep (n.TypingDelay + 10);
  195. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '$'));
  196. Assert.Equal ("$", n.SearchString);
  197. Thread.Sleep (n.TypingDelay + 10);
  198. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '$'));
  199. Assert.Equal ("$", n.SearchString);
  200. Thread.Sleep (n.TypingDelay + 10);
  201. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
  202. Assert.Equal ("$", n.SearchString);
  203. Thread.Sleep (n.TypingDelay + 10);
  204. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '2')); // Shouldn't move
  205. Assert.Equal ("2", n.SearchString);
  206. }
  207. [Fact]
  208. public void MutliKeySearchPlusWrongKeyStays ()
  209. {
  210. var strings = new string []{
  211. "a",
  212. "c",
  213. "can",
  214. "candle",
  215. "candy",
  216. "yellow",
  217. "zebra"
  218. };
  219. int current = 0;
  220. var n = new CollectionNavigator (strings);
  221. // https://github.com/gui-cs/Terminal.Gui/pull/2132#issuecomment-1298425573
  222. // One thing that it currently does that is different from Explorer is that as soon as you hit a wrong key then it jumps to that index.
  223. // So if you type cand then z it jumps you to something beginning with z. In the same situation Windows Explorer beeps (not the best!)
  224. // but remains on candle.
  225. // We might be able to update the behaviour so that a 'wrong' keypress (z) within 500ms of a 'right' keypress ("can" + 'd') is
  226. // simply ignored (possibly ending the search process though). That would give a short delay for user to realise the thing
  227. // they typed doesn't exist and then start a new search (which would be possible 500ms after the last 'good' keypress).
  228. // This would only apply for 2+ character searches where theres been a successful 2+ character match right before.
  229. Assert.Equal (strings.IndexOf ("a"), current = n.GetNextMatchingItem (current, 'a'));
  230. Assert.Equal ("a", n.SearchString);
  231. Assert.Equal (strings.IndexOf ("c"), current = n.GetNextMatchingItem (current, 'c'));
  232. Assert.Equal ("c", n.SearchString);
  233. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'a'));
  234. Assert.Equal ("ca", n.SearchString);
  235. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'n'));
  236. Assert.Equal ("can", n.SearchString);
  237. Assert.Equal (strings.IndexOf ("candle"), current = n.GetNextMatchingItem (current, 'd'));
  238. Assert.Equal ("cand", n.SearchString);
  239. // Same as above, but with a 'wrong' key (z)
  240. Thread.Sleep (n.TypingDelay + 10);
  241. Assert.Equal (strings.IndexOf ("a"), current = n.GetNextMatchingItem (current, 'a'));
  242. Assert.Equal ("a", n.SearchString);
  243. Assert.Equal (strings.IndexOf ("c"), current = n.GetNextMatchingItem (current, 'c'));
  244. Assert.Equal ("c", n.SearchString);
  245. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'a'));
  246. Assert.Equal ("ca", n.SearchString);
  247. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'n'));
  248. Assert.Equal ("can", n.SearchString);
  249. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'z')); // Shouldn't move
  250. Assert.Equal ("can", n.SearchString); // Shouldn't change
  251. }
  252. [Fact]
  253. public void MinimizeMovement_False_ShouldMoveIfMultipleMatches ()
  254. {
  255. var strings = new string [] {
  256. "$$",
  257. "$100.00",
  258. "$101.00",
  259. "$101.10",
  260. "$200.00",
  261. "appricot",
  262. "c",
  263. "car",
  264. "cart",
  265. };
  266. int current = 0;
  267. var n = new CollectionNavigator (strings);
  268. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", false));
  269. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", false));
  270. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", false)); // back to top
  271. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", false));
  272. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$", false));
  273. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, "$", false));
  274. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$", false));
  275. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", false)); // back to top
  276. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, "a", false));
  277. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", false)); // back to top
  278. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$100.00", false));
  279. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$", false));
  280. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00", false));
  281. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2", false));
  282. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$200.00", false));
  283. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00", false));
  284. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2", false));
  285. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00", false));
  286. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2", false));
  287. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", false));
  288. Assert.Equal (strings.IndexOf ("cart"), current = n.GetNextMatchingItem (current, "car", false));
  289. Assert.Equal (-1, current = n.GetNextMatchingItem (current, "x", false));
  290. }
  291. [Fact]
  292. public void MinimizeMovement_True_ShouldStayOnCurrentIfMultipleMatches ()
  293. {
  294. var strings = new string [] {
  295. "$$",
  296. "$100.00",
  297. "$101.00",
  298. "$101.10",
  299. "$200.00",
  300. "appricot",
  301. "c",
  302. "car",
  303. "cart",
  304. };
  305. int current = 0;
  306. var n = new CollectionNavigator (strings);
  307. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true));
  308. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", true));
  309. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true)); // back to top
  310. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$1", true));
  311. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
  312. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
  313. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
  314. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
  315. Assert.Equal (-1, current = n.GetNextMatchingItem (current, "x", true));
  316. }
  317. }
  318. }