Files
Terminal.Gui/Tests/UnitTestsParallelizable/Text/CollectionNavigatorTests.cs
Tig a6258ed398 Updates IListDataSource.Render to rename the start parameter to viewportXOffset (#4392)
* Add comprehensive unit tests for WindowsKeyConverter

- Implement 118 parallelizable unit tests for WindowsKeyConverter

- Cover ToKey and ToKeyInfo methods with full bidirectional testing

- Test basic characters, modifiers, special keys, function keys

- Test VK_PACKET Unicode/IME input

- Test OEM keys, NumPad keys, and lock states

- Include round-trip conversion tests

- All tests passing successfully

Fixes #4389

* Rename `start` parameter to `viewportXOffset` for clarity

The `start` parameter in several methods and interfaces has been
renamed to `viewportXOffset` to better reflect its purpose as the
horizontal offset of the viewport during string rendering.

- Updated method signatures in `ListViewWithSelection` to use
  `viewportXOffset` instead of `start`, including default values.
- Modified the `RenderUstr` method in `ListViewWithSelection` to
  use `viewportXOffset` for calculating the starting index.
- Renamed the `start` parameter to `viewportXOffset` in the
  `IListDataSource` interface and updated its documentation.
- Replaced all occurrences of `start` with `viewportXOffset` in
  the `ListWrapper<T>` class, including method calls and logic.
- Updated the `RenderUstr` method in `ListWrapper<T>` to use
  `viewportXOffset` for substring calculations.
- Adjusted the test method in `ListViewTests.cs` to reflect the
  parameter name change.

These changes improve code readability and make the parameter's
role in rendering logic more explicit.

* Remove WindowsKeyConverterTests class that was added by mistake

* Modernized ListView and IListDataSource - Tons of new unit tests

Refactored `ListView` and `IListDataSource` to improve readability, maintainability, and functionality. Introduced `ListWrapper<T>` as a default implementation of `IListDataSource` for easier integration with standard collections.

Enhanced `ListView` with better handling of marking, selection, and scrolling. Replaced `viewportXOffset` with `viewportX` for horizontal scrolling. Added `EnsureSelectedItemVisible` to maintain visibility of the selected item.

Updated `IListDataSource` with detailed XML documentation and added `SuspendCollectionChangedEvent` for bulk updates. Improved null safety with nullable reference types.

Added comprehensive unit tests for `ListWrapper<T>` and `IListDataSource` to ensure robustness. Modernized the codebase with C# features like expression-bodied members and pattern matching. Fixed bugs related to `SelectedItem` validation and rendering artifacts.

* Improve index validation in ComboBox and ListView

Enhance robustness by adding stricter checks for valid indices
in ComboBox and ListView. Updated conditions in the
`_listview.SelectedItemChanged` event handler to ensure `e.Item`
is non-negative before accessing `_searchSet`. Modified the
`SetValue` method to use `e.Item` instead of `_listview.SelectedItem`.

In ListView, updated the `OnSelectedChanged` method to validate
that `SelectedItem` is non-negative (`>= 0`) before accessing
the `Source` list. These changes prevent potential out-of-range
errors and improve code safety.

* Refactor and enhance test coverage across modules

Refactored and added new tests to improve coverage, readability, and consistency across multiple test files. Key changes include:

- **ShortcutTests.cs**: Added tests for `BindKeyToApplication` and removed redundant tests.
- **SourcesManagerTests.cs**: Renamed `Update_*` tests to `Load_*` for clarity.
- **ArrangementTests.cs**: Reintroduced `MouseGrabHandler` tests, added `ViewArrangement` flag tests, and improved structure.
- **NeedsDrawTests.cs**: Replaced `Application.Screen.Size` with fixed dimensions for better isolation.
- **DimAutoTests.cs**: Updated layout tests to use fixed dimensions.
- **FrameTests.cs**: Standardized object initialization and validated frame behavior.
- **SubViewTests.cs**: Improved formatting and modernized event handling.
- **NumericUpDownTests.cs**: Decoupled layout tests from screen size.

General improvements:
- Enhanced formatting and removed redundant tests.
- Added comments for clarity.
- Introduced `ITestOutputHelper` for better debugging in `ArrangementTests`.

* Refactor to use nullable types for better null safety

Enabled nullable reference types across the codebase to improve null safety and prevent potential null reference issues. Refactored `SelectedItem` and related properties from `int` to `int?` to represent no selection with `null` instead of `-1`. Updated logic, event arguments, and method signatures to handle nullable values consistently.

Simplified object initialization using modern C# syntax and improved code readability with interpolated strings. Added null checks and early returns to prevent runtime errors. Enhanced error handling by throwing `ArgumentOutOfRangeException` for invalid values.

Updated tests to reflect the changes, replacing assertions for `-1` with `null` and ensuring proper handling of nullable values. Cleaned up redundant code and improved formatting for better maintainability.

* on` functionality has been deprecated, refactored, or removed from the `Shortcut` class.

* Refactor: Transition to instance-based architecture

Updated `Run-LocalCoverage.ps1` to increase `--blame-hang-timeout` from 10s to 60s. Improved null safety in `GuiTestContext` by adding null-conditional operators. Commented out problematic code in `SetupFakeApplicationAttribute.cs` to prevent test hangs.

Excluded `ViewBase` files from `UnitTests.Parallelizable.csproj` and removed redundant folder declarations. Simplified event handling in `IListDataSourceTests.cs` and updated `ListViewTests.cs` to use nullable reference types.

Enhanced documentation to emphasize the transition to an instance-based application architecture. Updated examples in `application.md`, `multitasking.md`, and `navigation.md` to reflect the use of `Application.Create()` and `View.App`. Clarified the obsolescence of the static `Application` class.

Revised table of contents in `toc.yml` to include new sections like "Application Deep Dive" and "Scheme Deep Dive." Added `dotnet-tools.json` for tool configuration.

These changes improve maintainability, testability, and alignment with modern C# practices.

* Refactor ListViewTests to use Terminal.Gui framework

The `ListViewTests` class has been refactored to replace the `AutoInitShutdown` attribute with explicit application lifecycle management using `IApplication` and `app.Init()` from the `Terminal.Gui` framework.

Key changes include:
- Rewriting tests to use `Terminal.Gui`'s application lifecycle.
- Adding a private `_output` field for logging test output via `ITestOutputHelper`.
- Updating `DriverAssert.AssertDriverContentsWithFrameAre` to include `app.Driver` for UI verification.
- Rewriting tests like `Clicking_On_Border_Is_Ignored`, `EnsureSelectedItemVisible_SelectedItem`, and others to align with the new framework.
- Adding explicit calls to `app.Shutdown()` for proper cleanup.
- Enabling nullable reference types with `#nullable enable`.
- Updating `using` directives and `namespace` to reflect the new structure.

These changes improve test maintainability, compatibility, and diagnostics.

* Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/UICatalogTop.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Views/ListWrapper.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Views/ListWrapper.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Updated the `SetMark` method to return `Source.IsMarked(SelectedItem.Value)` for consistency and removed an outdated comment questioning its correctness.

Enhanced the exception message in the `SelectedItem` property setter to provide clearer guidance when the value is out of range.

* Add comprehensive ListView behavior test coverage

Added multiple test methods to validate `ListView` behavior:
- `Vertical_ScrollBar_Hides_And_Shows_As_Needed`: Ensures the vertical scrollbar auto-hides/shows based on content height.
- `Mouse_Wheel_Scrolls`: Verifies vertical scrolling with the mouse wheel updates `TopItem`.
- `SelectedItem_With_Source_Null_Does_Nothing`: Confirms no exceptions occur when setting `SelectedItem` with a `null` source.
- `Horizontal_Scroll`: Tests horizontal scrolling, including programmatic and mouse wheel interactions, ensuring `LeftItem` updates correctly.
- `SetSourceAsync_SetsSource`: Validates the asynchronous `SetSourceAsync` method updates the source and item count.
- `AllowsMultipleSelection_Set_To_False_Unmarks_All_But_Selected`: Ensures disabling multiple selection unmarks all but the selected item.
- `Source_CollectionChanged_Remove`: Confirms `SelectedItem` and source count update correctly when items are removed from the source collection.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-19 20:39:34 -05:00

362 lines
17 KiB
C#

using Moq;
using Xunit.Abstractions;
namespace UnitTests_Parallelizable.TextTests;
public class CollectionNavigatorTests
{
private static readonly string [] simpleStrings =
{
"apricot", // 0
"arm", // 1
"bat", // 2
"batman", // 3
"candle" // 4
};
private readonly ITestOutputHelper _output;
public CollectionNavigatorTests (ITestOutputHelper output) { _output = output; }
[Fact]
public void AtSymbol ()
{
var strings = new [] { "apricot", "arm", "ta", "@bob", "@bb", "text", "egg", "candle" };
var n = new CollectionNavigator (strings);
Assert.Equal (3, n.GetNextMatchingItem (0, '@'));
Assert.Equal (3, n.GetNextMatchingItem (3, 'b'));
Assert.Equal (4, n.GetNextMatchingItem (3, 'b'));
}
[Fact]
public void Cycling ()
{
// cycling with 'b'
var n = new CollectionNavigator (simpleStrings);
Assert.Equal (2, n.GetNextMatchingItem (0, 'b'));
Assert.Equal (3, n.GetNextMatchingItem (2, 'b'));
// if 4 (candle) is selected it should loop back to bat
Assert.Equal (2, n.GetNextMatchingItem (4, 'b'));
// cycling with 'a'
n = new CollectionNavigator (simpleStrings);
Assert.Equal (0, n.GetNextMatchingItem (null, 'a'));
Assert.Equal (1, n.GetNextMatchingItem (0, 'a'));
// if 4 (candle) is selected it should loop back to apricot
Assert.Equal (0, n.GetNextMatchingItem (4, 'a'));
}
[Fact]
public void Delay ()
{
var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "apricot" };
int? current = 0;
var n = new CollectionNavigator (strings);
// No delay
Assert.Equal (strings.IndexOf ("apricot"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("a", n.SearchString);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$$", n.SearchString);
// Delay
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("apricot"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("a", n.SearchString);
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '2')); // Shouldn't move
Assert.Equal ("2", n.SearchString);
}
[Fact]
public void FullText ()
{
var strings = new [] { "apricot", "arm", "ta", "target", "text", "egg", "candle" };
var n = new CollectionNavigator (strings);
int? current = 0;
Assert.Equal (strings.IndexOf ("ta"), current = n.GetNextMatchingItem (current, 't'));
// should match "te" in "text"
Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'e'));
// still matches text
Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'x'));
// nothing starts texa so it should NOT jump to apricot
Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'a'));
Thread.Sleep (n.TypingDelay + 100);
// nothing starts "texa". Since were past timedelay we DO jump to apricot
Assert.Equal (strings.IndexOf ("apricot"), current = n.GetNextMatchingItem (current, 'a'));
}
[Theory]
[InlineData (KeyCode.A, true)]
[InlineData (KeyCode.Z, true)]
[InlineData (KeyCode.D0, true)]
[InlineData (KeyCode.A | KeyCode.ShiftMask, true)]
[InlineData (KeyCode.Z | KeyCode.ShiftMask, true)]
[InlineData (KeyCode.Space, true)]
[InlineData (KeyCode.Z | KeyCode.CtrlMask, false)]
[InlineData (KeyCode.Z | KeyCode.AltMask, false)]
[InlineData (KeyCode.F1, false)]
[InlineData (KeyCode.Delete, false)]
[InlineData (KeyCode.Esc, false)]
[InlineData (KeyCode.ShiftMask, false)]
public void IsCompatibleKey_Does_Not_Allow_Alt_And_Ctrl_Keys (KeyCode keyCode, bool compatible)
{
var m = new DefaultCollectionNavigatorMatcher ();
Assert.Equal (compatible, m.IsCompatibleKey (keyCode));
}
[Fact]
public void MinimizeMovement_False_ShouldMoveIfMultipleMatches ()
{
var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "apricot", "c", "car", "cart" };
int? current = 0;
var n = new CollectionNavigator (strings);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$"));
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$"));
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$")); // back to top
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$"));
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$"));
Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, "$"));
Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$"));
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$")); // back to top
Assert.Equal (strings.IndexOf ("apricot"), current = n.GetNextMatchingItem (current, "a"));
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$")); // back to top
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$100.00"));
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$"));
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00"));
Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2"));
Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$200.00"));
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00"));
Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2"));
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00"));
Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2"));
Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car"));
Assert.Equal (strings.IndexOf ("cart"), current = n.GetNextMatchingItem (current, "car"));
Assert.Null (n.GetNextMatchingItem (current, "x"));
}
[Fact]
public void MinimizeMovement_True_ShouldStayOnCurrentIfMultipleMatches ()
{
var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "apricot", "c", "car", "cart" };
int? current = 0;
var n = new CollectionNavigator (strings);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true));
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", true));
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true)); // back to top
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$1", true));
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
Assert.Null (n.GetNextMatchingItem (current, "x", true));
}
[Fact]
public void MutliKeySearchPlusWrongKeyStays ()
{
var strings = new [] { "a", "c", "can", "candle", "candy", "yellow", "zebra" };
int? current = 0;
var n = new CollectionNavigator (strings);
// https://github.com/gui-cs/Terminal.Gui/pull/2132#issuecomment-1298425573
// 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.
// 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!)
// but remains on candle.
// We might be able to update the behaviour so that a 'wrong' keypress (z) within 500ms of a 'right' keypress ("can" + 'd') is
// simply ignored (possibly ending the search process though). That would give a short delay for user to realise the thing
// they typed doesn't exist and then start a new search (which would be possible 500ms after the last 'good' keypress).
// This would only apply for 2+ character searches where theres been a successful 2+ character match right before.
Assert.Equal (strings.IndexOf ("a"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("a", n.SearchString);
Assert.Equal (strings.IndexOf ("c"), current = n.GetNextMatchingItem (current, 'c'));
Assert.Equal ("c", n.SearchString);
Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("ca", n.SearchString);
Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'n'));
Assert.Equal ("can", n.SearchString);
Assert.Equal (strings.IndexOf ("candle"), current = n.GetNextMatchingItem (current, 'd'));
Assert.Equal ("cand", n.SearchString);
// Same as above, but with a 'wrong' key (z)
Thread.Sleep (n.TypingDelay + 10);
Assert.Equal (strings.IndexOf ("a"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("a", n.SearchString);
Assert.Equal (strings.IndexOf ("c"), current = n.GetNextMatchingItem (current, 'c'));
Assert.Equal ("c", n.SearchString);
Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("ca", n.SearchString);
Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'n'));
Assert.Equal ("can", n.SearchString);
Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'z')); // Shouldn't move
Assert.Equal ("can", n.SearchString); // Shouldn't change
}
[Fact]
public void OutOfBoundsShouldBeIgnored ()
{
var n = new CollectionNavigator (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.GetNextMatchingItem (500, 'b'));
}
[Fact]
public void ShouldAcceptNull ()
{
var n = new CollectionNavigator (simpleStrings);
// Expect that index of null (i.e. no selection) should work correctly
// and select the first entry of the letter 'b'
Assert.Equal (2, n.GetNextMatchingItem (null, 'b'));
}
[Fact]
public void Symbols ()
{
var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "apricot" };
int? current = 0;
var n = new CollectionNavigator (strings);
Assert.Equal (strings.IndexOf ("apricot"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("a", n.SearchString);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '1'));
Assert.Equal ("$1", n.SearchString);
Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '0'));
Assert.Equal ("$10", n.SearchString);
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '1'));
Assert.Equal ("$101", n.SearchString);
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '.'));
Assert.Equal ("$101.", n.SearchString);
// stay on the same item becuase still in timedelay
Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, 'a'));
Assert.Equal ("$101.", n.SearchString);
Thread.Sleep (n.TypingDelay + 100);
// another '$' means searching for "$" again
Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$", n.SearchString);
Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
Assert.Equal ("$$", n.SearchString);
}
[Fact]
public void Unicode ()
{
var strings = new [] { "apricot", "arm", "ta", "丗丙业丞", "丗丙丛", "text", "egg", "candle" };
var n = new CollectionNavigator (strings);
int? current = 0;
Assert.Equal (strings.IndexOf ("丗丙业丞"), current = n.GetNextMatchingItem (current, '丗'));
// 丗丙业丞 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 (strings.IndexOf ("丗丙业丞"), current = n.GetNextMatchingItem (current, '丙'));
// No longer matches 丗丙业丞 and now only matches 丗丙丛
// so we should move to the new match
Assert.Equal (strings.IndexOf ("丗丙丛"), current = n.GetNextMatchingItem (current, '丛'));
// nothing starts "丗丙丛a". Since were still in the timedelay we do not jump to apricot
Assert.Equal (strings.IndexOf ("丗丙丛"), current = n.GetNextMatchingItem (current, 'a'));
Thread.Sleep (n.TypingDelay + 100);
// nothing starts "丗丙丛a". Since were past timedelay we DO jump to apricot
Assert.Equal (strings.IndexOf ("apricot"), current = n.GetNextMatchingItem (current, 'a'));
}
[Fact]
public void Word ()
{
var strings = new [] { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
int? current = 0;
var n = new CollectionNavigator (strings);
Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'b')); // match bat
Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'a')); // match bat
Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 't')); // match bat
Assert.Equal (
strings.IndexOf ("bates hotel"),
current = n.GetNextMatchingItem (current, 'e')
); // match bates hotel
Assert.Equal (
strings.IndexOf ("bates hotel"),
current = n.GetNextMatchingItem (current, 's')
); // match bates hotel
Assert.Equal (
strings.IndexOf ("bates hotel"),
current = n.GetNextMatchingItem (current, ' ')
); // match bates hotel
}
[Fact]
public void CustomMatcher_NeverMatches ()
{
var strings = new [] { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
int? current = 0;
var n = new CollectionNavigator (strings);
var matchNone = new Mock<ICollectionNavigatorMatcher> ();
matchNone.Setup (m => m.IsMatch (It.IsAny<string> (), It.IsAny<object> ()))
.Returns (false);
n.Matcher = matchNone.Object;
Assert.Equal (0, current = n.GetNextMatchingItem (current, 'b')); // no matches
Assert.Equal (0, current = n.GetNextMatchingItem (current, 'a')); // no matches
Assert.Equal (0, current = n.GetNextMatchingItem (current, 't')); // no matches
}
}