Skip to content

Commit

Permalink
Fix issue #11 setting default value for yes?/no? prompts.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Mar 25, 2016
1 parent 041731c commit 6c3a52d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/tty/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ def invoke_select(object, question, *args, &block)
#
# @api public
def yes?(message, *args, &block)
options = Utils.extract_options!(args)
options.merge!(default: true)
defaults = { suffix: 'Y/n', default: true }
options = Utils.extract_options!(args)
options.merge!(defaults.reject { |k, _| options.key?(k) })

question = ConfirmQuestion.new(self, options)
question.call(message, &block)
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/yes_no_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
].join)
end

it 'changes default' do
prompt.input << "\n"
prompt.input.rewind
expect(prompt.yes?("Are you a human?", default: false)).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[1000D\e[K\e[1A",
"\e[1000D\e[K",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end

it "customizes question through options" do
prompt.input << "\r"
prompt.input.rewind
Expand Down Expand Up @@ -112,6 +124,18 @@
].join)
end

it 'changes default' do
prompt.input << "\r"
prompt.input.rewind
expect(prompt.no?("Are you a human?", default: true)).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[1000D\e[K\e[1A",
"\e[1000D\e[K",
"Are you a human? \e[32mYes\e[0m\n"
].join)
end

it "customizes question through DSL" do
prompt.input << "agree\r"
prompt.input.rewind
Expand Down

0 comments on commit 6c3a52d

Please sign in to comment.