Skip to content

Commit

Permalink
Fix Prompt#ask custom messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenezesds committed Mar 30, 2024
1 parent 2c2c44e commit c97541c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 29 deletions.
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
101 changes: 73 additions & 28 deletions spec/unit/question/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,53 @@

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[31m>>\e[0m Your answer is invalid (must match :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[1GWhat is your email? [email protected]\n",
"\e[2K\e[1G",
"\e[1A\e[2K\e[1G",
"What is your email? \e[[email protected]\e[0m\n"
].join)
"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 Your answer is invalid (must match :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[1GWhat is your email? [email protected]\n",
"\e[2K\e[1G",
"\e[1A\e[2K\e[1G",
"What is your email? \e[[email protected]\e[0m\n"
].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\n[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\n[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\n[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

0 comments on commit c97541c

Please sign in to comment.