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 79d8573
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -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
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
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? p@m.c",
"\e[2K\e[1GWhat is your email? p@m.co",
"\e[2K\e[1GWhat is your email? p@m.com",
"\e[2K\e[1G",
"\e[1A\e[2K\e[1G",
"What is your email? \e[32mp@m.com\e[0m\n"
].join }

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

expect(answer).to eq("p@m.com")
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\np@m.com"
prompt.input.rewind

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

expect(answer).to eq("p@m.com")
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\np@m.com"
prompt.input.rewind

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

expect(answer).to eq("p@m.com")
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 79d8573

Please sign in to comment.