diff --git a/src/Spectre.Console/Prompts/List/ListPrompt.cs b/src/Spectre.Console/Prompts/List/ListPrompt.cs index c668502..ca0fa3f 100644 --- a/src/Spectre.Console/Prompts/List/ListPrompt.cs +++ b/src/Spectre.Console/Prompts/List/ListPrompt.cs @@ -42,6 +42,11 @@ internal sealed class ListPrompt } 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(nodes, converter, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize), wrapAround, selectionMode, skipUnselectableItems, searchEnabled); var hook = new ListPromptRenderHook(_console, () => BuildRenderable(state)); diff --git a/src/Tests/Spectre.Console.Tests/Unit/Prompts/MultiSelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/MultiSelectionPromptTests.cs index 405e154..43b1d5c 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/MultiSelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/MultiSelectionPromptTests.cs @@ -127,6 +127,23 @@ public sealed class MultiSelectionPromptTests // Then action.ShouldThrow(); } + + [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(); + + // When + Action action = () => prompt.Show(console); + + // Then + var exception = action.ShouldThrow(); + exception.Message.ShouldBe("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt."); + } } file sealed class CustomItem diff --git a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs index 9d3e7ac..2e066c5 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs @@ -114,6 +114,22 @@ public sealed class SelectionPromptTests // 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(); + + // When + Action action = () => prompt.Show(console); + + // Then + var exception = action.ShouldThrow(); + exception.Message.ShouldBe("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt."); + } } file sealed class CustomSelectionItem