diff --git a/lib/tty/prompt/list.rb b/lib/tty/prompt/list.rb index 81f335b7..cab8b9c8 100644 --- a/lib/tty/prompt/list.rb +++ b/lib/tty/prompt/list.rb @@ -143,6 +143,10 @@ def setup_defaults # @api private def validate_defaults @default.each do |d| + if d.nil? || d.to_s.empty? + fail ConfigurationError, + "default index must be an integer in range (1 - #{@choices.size})" + end if d < 1 || d > @choices.size fail ConfigurationError, "default index `#{d}` out of range (1 - #{@choices.size})" diff --git a/spec/unit/select_spec.rb b/spec/unit/select_spec.rb index 4f87074e..424c9b7c 100644 --- a/spec/unit/select_spec.rb +++ b/spec/unit/select_spec.rb @@ -193,4 +193,26 @@ "[?] What size? \e[32mLarge\e[0m\n\e[?25h" ].join) end + + it "verifies default index format" do + prompt = TTY::TestPrompt.new + choices = %w(Large Medium Small) + prompt.input << "\r" + prompt.input.rewind + + expect { + prompt.select('What size?', choices, default: '') + }.to raise_error(TTY::Prompt::ConfigurationError, /in range \(1 - 3\)/) + end + + it "verifies default index range" do + prompt = TTY::TestPrompt.new + choices = %w(Large Medium Small) + prompt.input << "\r" + prompt.input.rewind + + expect { + prompt.select('What size?', choices, default: 10) + }.to raise_error(TTY::Prompt::ConfigurationError, /`10` out of range \(1 - 3\)/) + end end