diff --git a/test/Spectre.Console.Tests/Unit/Prompts/ListPromptStateTests.cs b/test/Spectre.Console.Tests/Unit/Prompts/ListPromptStateTests.cs index e0b4fa0f1..cda4f3755 100644 --- a/test/Spectre.Console.Tests/Unit/Prompts/ListPromptStateTests.cs +++ b/test/Spectre.Console.Tests/Unit/Prompts/ListPromptStateTests.cs @@ -2,14 +2,14 @@ namespace Spectre.Console.Tests.Unit; public sealed class ListPromptStateTests { - private ListPromptState CreateListPromptState(int count, int pageSize, bool shouldWrap) - => new(Enumerable.Repeat(new ListPromptItem(string.Empty), count).ToList(), pageSize, shouldWrap, SelectionMode.Independent, true, false); + private ListPromptState CreateListPromptState(int count, int pageSize, bool shouldWrap, bool searchEnabled) + => new(Enumerable.Range(0, count).Select(i => new ListPromptItem(i.ToString())).ToList(), pageSize, shouldWrap, SelectionMode.Independent, true, searchEnabled); [Fact] public void Should_Have_Start_Index_Zero() { // Given - var state = CreateListPromptState(100, 10, false); + var state = CreateListPromptState(100, 10, false, false); // When /* noop */ @@ -24,7 +24,7 @@ public void Should_Have_Start_Index_Zero() public void Should_Increase_Index(bool wrap) { // Given - var state = CreateListPromptState(100, 10, wrap); + var state = CreateListPromptState(100, 10, wrap, false); var index = state.Index; // When @@ -40,7 +40,7 @@ public void Should_Increase_Index(bool wrap) public void Should_Go_To_End(bool wrap) { // Given - var state = CreateListPromptState(100, 10, wrap); + var state = CreateListPromptState(100, 10, wrap, false); // When state.Update(ConsoleKey.End.ToConsoleKeyInfo()); @@ -53,7 +53,7 @@ public void Should_Go_To_End(bool wrap) public void Should_Clamp_Index_If_No_Wrap() { // Given - var state = CreateListPromptState(100, 10, false); + var state = CreateListPromptState(100, 10, false, false); state.Update(ConsoleKey.End.ToConsoleKeyInfo()); // When @@ -67,7 +67,7 @@ public void Should_Clamp_Index_If_No_Wrap() public void Should_Wrap_Index_If_Wrap() { // Given - var state = CreateListPromptState(100, 10, true); + var state = CreateListPromptState(100, 10, true, false); state.Update(ConsoleKey.End.ToConsoleKeyInfo()); // When @@ -81,7 +81,7 @@ public void Should_Wrap_Index_If_Wrap() public void Should_Wrap_Index_If_Wrap_And_Down() { // Given - var state = CreateListPromptState(100, 10, true); + var state = CreateListPromptState(100, 10, true, false); // When state.Update(ConsoleKey.UpArrow.ToConsoleKeyInfo()); @@ -94,7 +94,7 @@ public void Should_Wrap_Index_If_Wrap_And_Down() public void Should_Wrap_Index_If_Wrap_And_Page_Up() { // Given - var state = CreateListPromptState(10, 100, true); + var state = CreateListPromptState(10, 100, true, false); // When state.Update(ConsoleKey.PageUp.ToConsoleKeyInfo()); @@ -107,7 +107,7 @@ public void Should_Wrap_Index_If_Wrap_And_Page_Up() public void Should_Wrap_Index_If_Wrap_And_Offset_And_Page_Down() { // Given - var state = CreateListPromptState(10, 100, true); + var state = CreateListPromptState(10, 100, true, false); state.Update(ConsoleKey.End.ToConsoleKeyInfo()); state.Update(ConsoleKey.UpArrow.ToConsoleKeyInfo()); @@ -117,4 +117,31 @@ public void Should_Wrap_Index_If_Wrap_And_Offset_And_Page_Down() // Then state.Index.ShouldBe(8); } + + [Fact] + public void Should_Jump_To_First_Matching_Item_When_Searching() + { + // Given + var state = CreateListPromptState(10, 100, true, true); + + // When + state.Update(ConsoleKey.D3.ToConsoleKeyInfo()); + + // Then + state.Index.ShouldBe(3); + } + + [Fact] + public void Should_Jump_Back_To_First_Item_When_Clearing_Search_Term() + { + // Given + var state = CreateListPromptState(10, 100, true, true); + + // When + state.Update(ConsoleKey.D3.ToConsoleKeyInfo()); + state.Update(ConsoleKey.Backspace.ToConsoleKeyInfo()); + + // Then + state.Index.ShouldBe(0); + } } diff --git a/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 18851432c..e01a50ba8 100644 --- a/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -20,4 +20,48 @@ public void Should_Not_Throw_When_Selecting_An_Item_With_Escaped_Markup() // Then console.Output.ShouldContain(@"[red]This text will never be red[/]"); } + + [Fact] + public void Should_Select_The_First_Leaf_Item() + { + // Given + var console = new TestConsole(); + console.Profile.Capabilities.Interactive = true; + console.Input.PushKey(ConsoleKey.Enter); + + // When + var prompt = new SelectionPrompt() + .Title("Select one") + .Mode(SelectionMode.Leaf) + .AddChoiceGroup("Group one", "A", "B") + .AddChoiceGroup("Group two", "C", "D"); + var selection = prompt.Show(console); + + // Then + selection.ShouldBe("A"); + } + + [Fact] + public void Should_Select_The_Last_Leaf_Item_When_Wrapping_Around() + { + // Given + var console = new TestConsole(); + console.Profile.Capabilities.Interactive = true; + console.Input.PushKey(ConsoleKey.UpArrow); + console.Input.PushKey(ConsoleKey.Enter); + + // When + var prompt = new SelectionPrompt() + .Title("Select one") + .Mode(SelectionMode.Leaf) + .WrapAround() + .AddChoiceGroup("Group one", "A", "B") + .AddChoiceGroup("Group two", "C", "D"); + var selection = prompt.Show(console); + + // Then + selection.ShouldBe("D"); + } + + }