Skip to content
Merged
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
11 changes: 10 additions & 1 deletion lib/thor/shell/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,25 @@ def ask_simply(statement, color, options)

def ask_filtered(statement, color, options)
answer_set = options[:limited_to]
case_insensitive = options.fetch(:case_insensitive, false)
correct_answer = nil
until correct_answer
answers = answer_set.join(", ")
answer = ask_simply("#{statement} [#{answers}]", color, options)
correct_answer = answer_set.include?(answer) ? answer : nil
correct_answer = answer_match(answer_set, answer, case_insensitive)
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
end
correct_answer
end

def answer_match(possibilities, answer, case_insensitive)
if case_insensitive
possibilities.detect{ |possibility| possibility.downcase == answer.downcase }
else
possibilities.detect{ |possibility| possibility == answer }
end
end

def merge(destination, content) #:nodoc:
require "tempfile"
Tempfile.open([File.basename(destination), File.extname(destination)], File.dirname(destination)) do |temp|
Expand Down
24 changes: 22 additions & 2 deletions spec/shell/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,39 @@ def shell
expect(shell.ask("What's your password?", :echo => false)).to eq("mysecretpass")
end

it "prints a message to the user with the available options and determines the correctness of the answer" do
it "prints a message to the user with the available options, expects case-sensitive matching, and determines the correctness of the answer" do
flavors = %w(strawberry chocolate vanilla)
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("chocolate")
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
end

it "prints a message to the user with the available options and reasks the question after an incorrect response" do
it "prints a message to the user with the available options, expects case-sensitive matching, and reasks the question after an incorrect response" do
flavors = %w(strawberry chocolate vanilla)
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("moose tracks", "chocolate")
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
end

it "prints a message to the user with the available options, expects case-sensitive matching, and reasks the question after a case-insensitive match" do
flavors = %w(strawberry chocolate vanilla)
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return("cHoCoLaTe", "chocolate")
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq("chocolate")
end

it "prints a message to the user with the available options, expects case-insensitive matching, and determines the correctness of the answer" do
flavors = %w(strawberry chocolate vanilla)
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors, :case_insensitive => true).and_return("CHOCOLATE")
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors, :case_insensitive => true)).to eq("chocolate")
end

it "prints a message to the user with the available options, expects case-insensitive matching, and reasks the question after an incorrect response" do
flavors = %w(strawberry chocolate vanilla)
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors, :case_insensitive => true).and_return("moose tracks", "chocolate")
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors, :case_insensitive => true)).to eq("chocolate")
end

it "prints a message to the user containing a default and sets the default if only enter is pressed" do
expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? (vanilla) ', :default => "vanilla").and_return("")
expect(shell.ask('What\'s your favorite Neopolitan flavor?', :default => "vanilla")).to eq("vanilla")
Expand Down