Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Prompt#messages to default on Questions initialization #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## master

### Fixed
* Fix Prompt#ask not having custom messages set from key arguments

## [v0.23.1] - 2021-04-17

### Changed
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def decorate(string, *colors)
#
# @api public
def invoke_question(object, message, **options, &block)
options[:messages] = self.class.messages
options[:messages] = self.class.messages.merge(options[:messages] || {})
question = object.new(self, **options)
question.(message, &block)
end
Expand Down
57 changes: 51 additions & 6 deletions spec/unit/question/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@
].join)
end

let(:expected_custom_validation_message) { [
"What is your email? ",
"\e[2K\e[1GWhat is your email? w",
"\e[2K\e[1GWhat is your email? wr",
"\e[2K\e[1GWhat is your email? wro",
"\e[2K\e[1GWhat is your email? wron",
"\e[2K\e[1GWhat is your email? wrong",
"\e[2K\e[1GWhat is your email? wrong\n",
"\e[31m>>\e[0m Not an email!\e[1A",
"\e[2K\e[1G",
"What is your email? ",
"\e[2K\e[1GWhat is your email? p",
"\e[2K\e[1GWhat is your email? p@",
"\e[2K\e[1GWhat is your email? p@m",
"\e[2K\e[1GWhat is your email? p@m.",
"\e[2K\e[1GWhat is your email? [email protected]",
"\e[2K\e[1GWhat is your email? [email protected]",
"\e[2K\e[1GWhat is your email? [email protected]",
"\e[2K\e[1G",
"\e[1A\e[2K\e[1G",
"What is your email? \e[[email protected]\e[0m\n"
].join }

it "provides custom error message for wrong input" do
prompt.input << "wrong\[email protected]"
prompt.input.rewind
Expand All @@ -95,15 +118,37 @@
q.messages[:valid?] = "Not an email!"
end

expect(answer).to eq("[email protected]")
expect(prompt.output.string).to eq(expected_custom_validation_message)
end

it "provides custom error message for wrong input by keyword parameter instead of passing by block" do
prompt.input << "wrong\[email protected]"
prompt.input.rewind

answer = prompt.ask("What is your email?", validate: :email,
messages: { valid?: 'Not an email!' })

expect(answer).to eq("[email protected]")
expect(prompt.output.string).to eq(expected_custom_validation_message)
end

it "keeps the default messages after adding only one custom message" do
prompt.input << "\np\[email protected]"
prompt.input.rewind

answer = prompt.ask("What is your email?", required: true, validate: :email,
messages: { valid?: 'Not an email!' })

expect(answer).to eq("[email protected]")
expect(prompt.output.string).to eq([
"What is your email? ",
"\e[2K\e[1GWhat is your email? w",
"\e[2K\e[1GWhat is your email? wr",
"\e[2K\e[1GWhat is your email? wro",
"\e[2K\e[1GWhat is your email? wron",
"\e[2K\e[1GWhat is your email? wrong",
"\e[2K\e[1GWhat is your email? wrong\n",
"\e[2K\e[1GWhat is your email? \n",
"\e[31m>>\e[0m Value must be provided\e[1A",
"\e[2K\e[1G",
"What is your email? ",
"\e[2K\e[1GWhat is your email? p",
"\e[2K\e[1GWhat is your email? p\n",
"\e[31m>>\e[0m Not an email!\e[1A",
"\e[2K\e[1G",
"What is your email? ",
Expand Down