Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Spectre.Console/Prompts/List/ListPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public async Task<ListPromptState<T>> Show(
}

var nodes = tree.Traverse().ToList();
if (nodes.Count == 0)
{
throw new InvalidOperationException("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt.");
}

var state = new ListPromptState<T>(nodes, converter, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize), wrapAround, selectionMode, skipUnselectableItems, searchEnabled);
var hook = new ListPromptRenderHook<T>(_console, () => BuildRenderable(state));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ public void Should_Throw_When_Getting_Parents_Of_Non_Existing_Node()
// Then
action.ShouldThrow<ArgumentOutOfRangeException>();
}

[Fact] public void Should_Throw_Meaningful_Exception_For_Empty_Prompt()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Spacebar);

var prompt = new MultiSelectionPrompt<string>();

// When
Action action = () => prompt.Show(console);

// Then
var exception = action.ShouldThrow<InvalidOperationException>();
exception.Message.ShouldBe("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt.");
}
}

file sealed class CustomItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ public void Should_Search_In_Remapped_Result()
// Then
selection.ShouldBe(choices[1]);
}

[Fact] public void Should_Throw_Meaningful_Exception_For_Empty_Prompt()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;

var prompt = new SelectionPrompt<string>();

// When
Action action = () => prompt.Show(console);

// Then
var exception = action.ShouldThrow<InvalidOperationException>();
exception.Message.ShouldBe("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt.");
}
}

file sealed class CustomSelectionItem
Expand Down