123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using Terminal.Gui;
- using Xunit;
- namespace Terminal.Gui.Core {
- public class SearchCollectionNavigatorTests {
- static string [] simpleStrings = new string []{
- "appricot", // 0
- "arm", // 1
- "bat", // 2
- "batman", // 3
- "candle" // 4
- };
- [Fact]
- public void TestSearchCollectionNavigator_ShouldAcceptNegativeOne ()
- {
- var n = new SearchCollectionNavigator (simpleStrings);
-
- // Expect that index of -1 (i.e. no selection) should work correctly
- // and select the first entry of the letter 'b'
- Assert.Equal (2, n.CalculateNewIndex (-1, 'b'));
- }
- [Fact]
- public void TestSearchCollectionNavigator_OutOfBoundsShouldBeIgnored()
- {
- var n = new SearchCollectionNavigator (simpleStrings);
- // Expect saying that index 500 is the current selection should not cause
- // error and just be ignored (treated as no selection)
- Assert.Equal (2, n.CalculateNewIndex (500, 'b'));
- }
- [Fact]
- public void TestSearchCollectionNavigator_Cycling ()
- {
- var n = new SearchCollectionNavigator (simpleStrings);
- Assert.Equal (2, n.CalculateNewIndex ( 0, 'b'));
- Assert.Equal (3, n.CalculateNewIndex ( 2, 'b'));
- // if 4 (candle) is selected it should loop back to bat
- Assert.Equal (2, n.CalculateNewIndex ( 4, 'b'));
- }
- [Fact]
- public void TestSearchCollectionNavigator_ToSearchText ()
- {
- var strings = new string []{
- "appricot",
- "arm",
- "bat",
- "batman",
- "bbfish",
- "candle"
- };
- var n = new SearchCollectionNavigator (strings);
- Assert.Equal (2, n.CalculateNewIndex (0, 'b'));
- Assert.Equal (4, n.CalculateNewIndex (2, 'b'));
- // another 'b' means searching for "bbb" which does not exist
- // so we go back to looking for "b" as a fresh key strike
- Assert.Equal (4, n.CalculateNewIndex (2, 'b'));
- }
- [Fact]
- public void TestSearchCollectionNavigator_FullText ()
- {
- var strings = new string []{
- "appricot",
- "arm",
- "ta",
- "target",
- "text",
- "egg",
- "candle"
- };
- var n = new SearchCollectionNavigator (strings);
- Assert.Equal (2, n.CalculateNewIndex (0, 't'));
- // should match "te" in "text"
- Assert.Equal (4, n.CalculateNewIndex (2, 'e'));
- // still matches text
- Assert.Equal (4, n.CalculateNewIndex (4, 'x'));
- // nothing starts texa so it jumps to a for appricot
- Assert.Equal (0, n.CalculateNewIndex (4, 'a'));
- }
- [Fact]
- public void TestSearchCollectionNavigator_Unicode ()
- {
- var strings = new string []{
- "appricot",
- "arm",
- "ta",
- "丗丙业丞",
- "丗丙丛",
- "text",
- "egg",
- "candle"
- };
- var n = new SearchCollectionNavigator (strings);
- Assert.Equal (3, n.CalculateNewIndex (0, '丗'));
- // 丗丙业丞 is as good a match as 丗丙丛
- // so when doing multi character searches we should
- // prefer to stay on the same index unless we invalidate
- // our typed text
- Assert.Equal (3, n.CalculateNewIndex (3, '丙'));
- // No longer matches 丗丙业丞 and now only matches 丗丙丛
- // so we should move to the new match
- Assert.Equal (4, n.CalculateNewIndex (3, '丛'));
- // nothing starts "丗丙丛a" so it jumps to a for appricot
- Assert.Equal (0, n.CalculateNewIndex (4, 'a'));
- }
- [Fact]
- public void TestSearchCollectionNavigator_AtSymbol ()
- {
- var strings = new string []{
- "appricot",
- "arm",
- "ta",
- "@bob",
- "@bb",
- "text",
- "egg",
- "candle"
- };
- var n = new SearchCollectionNavigator (strings);
- Assert.Equal (3, n.CalculateNewIndex (0, '@'));
- Assert.Equal (3, n.CalculateNewIndex (3, 'b'));
- Assert.Equal (4, n.CalculateNewIndex (3, 'b'));
- }
- }
- }
|