Fix search bug in prompt related to custom item types

Closes #1626
This commit is contained in:
Patrik Svensson
2024-09-03 00:37:00 +02:00
committed by Patrik Svensson
parent 753894de94
commit fd69ad0b01
6 changed files with 70 additions and 7 deletions

View File

@ -3,7 +3,10 @@ namespace Spectre.Console.Tests.Unit;
public sealed class ListPromptStateTests
{
private ListPromptState<string> CreateListPromptState(int count, int pageSize, bool shouldWrap, bool searchEnabled)
=> new(Enumerable.Range(0, count).Select(i => new ListPromptItem<string>(i.ToString())).ToList(), pageSize, shouldWrap, SelectionMode.Independent, true, searchEnabled);
=> new(
Enumerable.Range(0, count).Select(i => new ListPromptItem<string>(i.ToString())).ToList(),
text => text,
pageSize, shouldWrap, SelectionMode.Independent, true, searchEnabled);
[Fact]
public void Should_Have_Start_Index_Zero()

View File

@ -410,4 +410,45 @@ public sealed class TextPromptTests
// Then
return Verifier.Verify(console.Output);
}
[Fact]
public void Should_Search_In_Remapped_Result()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.EmitAnsiSequences();
console.Input.PushText("2");
console.Input.PushKey(ConsoleKey.Enter);
var choices = new List<CustomSelectionItem>
{
new(33, "Item 1"),
new(34, "Item 2"),
};
var prompt = new SelectionPrompt<CustomSelectionItem>()
.Title("Select one")
.EnableSearch()
.UseConverter(o => o.Name)
.AddChoices(choices);
// When
var selection = prompt.Show(console);
// Then
selection.ShouldBe(choices[1]);
}
}
file sealed class CustomSelectionItem
{
public int Value { get; }
public string Name { get; }
public CustomSelectionItem(int value, string name)
{
Value = value;
Name = name ?? throw new ArgumentNullException(nameof(name));
}
}