diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e8e38..f7a439e 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/lib/tty/prompt.rb b/lib/tty/prompt.rb index 98cbc0a..8bcdb54 100644 --- a/lib/tty/prompt.rb +++ b/lib/tty/prompt.rb @@ -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 diff --git a/spec/unit/question/validate_spec.rb b/spec/unit/question/validate_spec.rb index 0e994cc..2420d94 100644 --- a/spec/unit/question/validate_spec.rb +++ b/spec/unit/question/validate_spec.rb @@ -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 @@ -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? ",