Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min and max validation in TTY::Prompt::MultiList #1

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change log

## [Unreleased]
* Add validation to ensure min is less than or equal to max in #multi_select

## [v0.23.1] - 2021-04-17

### Changed
Expand Down
18 changes: 17 additions & 1 deletion lib/tty/prompt/multi_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ def initialize(prompt, **options)
@echo = options.fetch(:echo, true)
@min = options[:min]
@max = options[:max]
validate_min_max
end

# Set a minimum number of choices
#
# @api public
def min(value)
@min = value
validate_min_max
end

# Set a maximum number of choices
#
# @api public
def max(value)
@max = value
validate_min_max
end

# Callback fired when enter/return key is pressed
Expand All @@ -45,7 +48,7 @@ def max(value)
def keyenter(*)
valid = true
valid = @min <= @selected.size if @min
valid = @selected.size <= @max if @max
valid &= @selected.size <= @max if @max

super if valid
end
Expand Down Expand Up @@ -219,6 +222,19 @@ def render_menu

output.join
end

# Validate min and max requirements are within bounds
#
# @raise [ConfigurationError]
# raised when min is greater than max
# or when min is less than available choices
#
# @api private
def validate_min_max
return unless @min && @max && @min > @max

raise ConfigurationError, "min cannot be greater than max"
end
end # MultiList
end # Prompt
end # TTY
46 changes: 44 additions & 2 deletions spec/unit/multi_select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ def output_helper(prompt, choices, active, selected, options = {})
out = []
out << "\e[?25l" if init
out << prompt << " "
out << "(min. #{options[:min]}) " if options[:min]
out << "(max. #{options[:max]}) " if options[:max]
if options[:min] || options[:max]
minmax_help = []
minmax_help << "min. #{options[:min]}" if options[:min]
minmax_help << "max. #{options[:max]}" if options[:max]
out << "(#{minmax_help.join(', ')}) "
end
out << selected.join(", ")
out << " " if (init || hint) && !selected.empty?
out << "\e[90m(#{hint})\e[0m" if hint
Expand Down Expand Up @@ -905,4 +909,42 @@ def exit_message(prompt, choices)
expect(prompt.output.string).to eq(expected_output)
end
end

context "with :min and :max" do
it "raises error when min is greater than max" do
choices = %w[A B C]
prompt.input << " " << "\r"
prompt.input.rewind

expect {
prompt.multi_select("What letter?", choices, min: 3, max: 2)
}.to raise_error(TTY::Prompt::ConfigurationError,
/min cannot be greater than max/)
end

it "requires number of choices" do
choices = %w[A B C D]
prompt.on(:keypress) { |e|
prompt.trigger(:keyup) if e.value == "k"
prompt.trigger(:keydown) if e.value == "j"
}

prompt.input << " " << "\r" << "j" << " " << "\r"
prompt.input.rewind

value = prompt.multi_select("Select letters?", choices, min: 2, max: 3)
expect(value).to eq(%w[A B])

expected_output =
output_helper("Select letters?", choices, "A", [], init: true, min: 2, max: 3,
hint: "Press #{up_down} arrow to move, Space to select and Enter to finish") +
output_helper("Select letters?", choices, "A", %w[A], min: 2, max: 3) +
output_helper("Select letters?", choices, "A", %w[A], min: 2, max: 3) +
output_helper("Select letters?", choices, "B", %w[A], min: 2, max: 3) +
output_helper("Select letters?", choices, "B", %w[A B], min: 2, max: 3) +
exit_message("Select letters?", %w[A B])

expect(prompt.output.string).to eq(expected_output)
end
end
end