Skip to content

Commit

Permalink
Fix #ask issue handling default option when nil or empty string #12
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Mar 28, 2016
1 parent 4fec300 commit c0db392
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/tty/prompt/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def render_question
header
elsif @done
header += @prompt.decorate("#{@input}", @color)
elsif default?
elsif default? && !Utils.blank?(@default)
header += @prompt.decorate("(#{default})", :bright_black) + ' '
end
@prompt.print(header)
Expand Down
29 changes: 29 additions & 0 deletions spec/unit/ask_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8

RSpec.describe TTY::Prompt, '#ask' do

subject(:prompt) { TTY::TestPrompt.new }

it 'asks question' do
Expand Down Expand Up @@ -54,4 +55,32 @@
"What is your name? \e[32mPiotr\e[0m\n"
].join)
end

it "permits empty default parameter" do
prompt.input << "\r"
prompt.input.rewind

answer = prompt.ask("What is your name?", default: '')
expect(answer).to eq('')
expect(prompt.output.string).to eq([
"What is your name? ",
"\e[1000D\e[K\e[1A",
"\e[1000D\e[K",
"What is your name? \n"
].join)
end

it "permits nil default parameter" do
prompt.input << "\r"
prompt.input.rewind

answer = prompt.ask("What is your name?", default: nil)
expect(answer).to eq(nil)
expect(prompt.output.string).to eq([
"What is your name? ",
"\e[1000D\e[K\e[1A",
"\e[1000D\e[K",
"What is your name? \n"
].join)
end
end

0 comments on commit c0db392

Please sign in to comment.