Skip to content

Commit a515be0

Browse files
committed
multilist: ensure min <= max
1 parent 0b3c437 commit a515be0

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/tty/prompt/multi_list.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class MultiList < List
1818
# @api public
1919
def initialize(prompt, **options)
2020
super
21+
check_minmax(options[:min], options[:max])
2122
@selected = SelectedChoices.new
2223
@help = options[:help]
2324
@echo = options.fetch(:echo, true)
@@ -29,13 +30,15 @@ def initialize(prompt, **options)
2930
#
3031
# @api public
3132
def min(value)
33+
check_minmax(value, @max)
3234
@min = value
3335
end
3436

3537
# Set a maximum number of choices
3638
#
3739
# @api public
3840
def max(value)
41+
check_minmax(@min, value)
3942
@max = value
4043
end
4144

@@ -136,6 +139,17 @@ def minmax_help
136139
"(%s) " % [help.join(", ")]
137140
end
138141

142+
# Check that min is not greater than max
143+
#
144+
# @raise [ConfigurationError]
145+
#
146+
# @api_private
147+
def check_minmax(min, max)
148+
return unless min && max && min > max
149+
150+
raise ConfigurationError, "min must not be greater than max"
151+
end
152+
139153
# Build a default help text
140154
#
141155
# @return [String]

spec/unit/multi_select_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ def exit_message(prompt, choices)
933933

934934
expect(prompt.output.string).to eq(expected_output)
935935
end
936+
936937
it "raises an error message when the min is greater than max" do
937938
choices = %w[A B C]
938939
expect {

0 commit comments

Comments
 (0)