Skip to content

Commit

Permalink
Change to ensure list default parameter is valid in reference to issue
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Mar 28, 2016
1 parent 1938f7f commit 4fec300
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/tty/prompt/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4fec300

Please sign in to comment.