Skip to content

Commit a120bad

Browse files
committed
multilist: fix min validation when both min and max specified
1 parent 5152fb5 commit a120bad

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/tty/prompt/multi_list.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def max(value)
4545
def keyenter(*)
4646
valid = true
4747
valid = @min <= @selected.size if @min
48-
valid = @selected.size <= @max if @max
48+
valid &= @selected.size <= @max if @max
4949

5050
super if valid
5151
end

spec/unit/multi_select_spec.rb

+34-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ def output_helper(prompt, choices, active, selected, options = {})
1717
out = []
1818
out << "\e[?25l" if init
1919
out << prompt << " "
20-
out << "(min. #{options[:min]}) " if options[:min]
21-
out << "(max. #{options[:max]}) " if options[:max]
20+
if options[:min] && options[:max]
21+
out << "(min. #{options[:min]}, max. #{options[:max]}) "
22+
else
23+
out << "(min. #{options[:min]}) " if options[:min]
24+
out << "(max. #{options[:max]}) " if options[:max]
25+
end
2226
out << selected.join(", ")
2327
out << " " if (init || hint) && !selected.empty?
2428
out << "\e[90m(#{hint})\e[0m" if hint
@@ -905,4 +909,32 @@ def exit_message(prompt, choices)
905909
expect(prompt.output.string).to eq(expected_output)
906910
end
907911
end
912+
913+
context "with :min and :max" do
914+
let(:choices) { %w[A B C] }
915+
let(:keydown) { "j" }
916+
let(:spacebar) { " " }
917+
let(:enter) { "\r" }
918+
before do
919+
prompt.on(:keypress) { |e| prompt.trigger(:keydown) if e.value == keydown }
920+
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)
938+
end
939+
end
908940
end

0 commit comments

Comments
 (0)