Skip to content

Commit 30d47b3

Browse files
committed
multilist: ensure min <= max
1 parent a120bad commit 30d47b3

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

lib/tty/prompt/multi_list.rb

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def initialize(prompt, **options)
2323
@echo = options.fetch(:echo, true)
2424
@min = options[:min]
2525
@max = options[:max]
26+
error = "min must not be greater than max"
27+
raise ConfigurationError, error if @min && @max && @min > @max
2628
end
2729

2830
# Set a minimum number of choices

spec/unit/multi_select_spec.rb

+33-18
Original file line numberDiff line numberDiff line change
@@ -916,25 +916,40 @@ def exit_message(prompt, choices)
916916
let(:spacebar) { " " }
917917
let(:enter) { "\r" }
918918
before do
919-
prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == keydown }
919+
prompt.on(:keypress) do |e|
920+
prompt.trigger(:keydown) if e.value == keydown
921+
end
920922
end
921-
it "requires a min number of choices" do
922-
prompt.input << spacebar << enter << keydown << spacebar << enter
923-
prompt.input.rewind
924-
925-
value = prompt.multi_select("What letter?", choices, min: 2, max: 2, per_page: choices.count)
926-
expect(value).to eq(%w[A B])
927-
928-
expected_output =
929-
output_helper("What letter?", choices, "A", [], init: true, min: 2, max: 2,
930-
hint: "Press #{up_down} arrow to move, Space to select and Enter to finish") +
931-
output_helper("What letter?", choices, "A", %w[A], min: 2, max: 2) +
932-
output_helper("What letter?", choices, "A", %w[A], min: 2, max: 2) +
933-
output_helper("What letter?", choices, "B", %w[A], min: 2, max: 2) +
934-
output_helper("What letter?", choices, "B", %w[A B], min: 2, max: 2) +
935-
exit_message("What letter?", %w[A B])
936-
937-
expect(prompt.output.string).to eq(expected_output)
923+
context "with min <= max" do
924+
let(:min) { 2 }
925+
let(:max) { 2 }
926+
it "requires a min number of choices" do
927+
prompt.input << spacebar << enter << keydown << spacebar << enter
928+
prompt.input.rewind
929+
930+
value = prompt.multi_select("What letter?", choices, min: min, max: max, per_page: choices.count)
931+
expect(value).to eq(%w[A B])
932+
933+
expected_output =
934+
output_helper("What letter?", choices, "A", [], init: true, min: min, max: max,
935+
hint: "Press #{up_down} arrow to move, Space to select and Enter to finish") +
936+
output_helper("What letter?", choices, "A", %w[A], min: min, max: max) +
937+
output_helper("What letter?", choices, "A", %w[A], min: min, max: max) +
938+
output_helper("What letter?", choices, "B", %w[A], min: min, max: max) +
939+
output_helper("What letter?", choices, "B", %w[A B], min: min, max: max) +
940+
exit_message("What letter?", %w[A B])
941+
942+
expect(prompt.output.string).to eq(expected_output)
943+
end
944+
end
945+
context "with min > max" do
946+
let(:min) { 3 }
947+
let(:max) { 2 }
948+
it "raises an error message" do
949+
options = { min: min, max: max, per_page: choices.count }
950+
error_msg = "min must not be greater than max"
951+
expect { prompt.multi_select("What letter?", choices, options) }.to raise_error(TTY::Prompt::ConfigurationError, error_msg)
952+
end
938953
end
939954
end
940955
end

0 commit comments

Comments
 (0)