Skip to content

Commit

Permalink
[Resolves #91] Invalid characters on suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbaltazar committed Oct 31, 2018
1 parent 68c7538 commit aa26130
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/tty/prompt/confirm_question.rb
Expand Up @@ -122,7 +122,9 @@ def create_suffix
# @api private
def conversion
proc { |input|
regex = Regexp.new(Regexp.escape("^#{positive}|#{positive[0]}$/i"))
r1 = Regexp.escape(positive)
r2 = Regexp.escape(positive[0])
regex = Regexp.new("^#{r1}|#{r2}$", true)
!input.match(regex).nil?
}
end
Expand Down
31 changes: 16 additions & 15 deletions spec/unit/yes_no_spec.rb
Expand Up @@ -95,6 +95,22 @@
].join)
end

it "accepts regex conflicting characters as suffix" do
prompt.input << "]\n"
prompt.input.rewind
result = prompt.yes?("Are you a human? Use [ for yes and ] for no") do |q|
q.suffix '[/]'
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? Use [ for yes and ] for no \e[90m([/])\e[0m ",
"\e[2K\e[1GAre you a human? Use [ for yes and ] for no \e[90m([/])\e[0m ]",
"\e[2K\e[1GAre you a human? Use [ for yes and ] for no \e[90m([/])\e[0m ]\n",
"\e[1A\e[2K\e[1G",
"Are you a human? Use [ for yes and ] for no \e[32m]\e[0m\n"
].join)
end

it "customizes question through options" do
prompt.input << "\r"
prompt.input.rewind
Expand Down Expand Up @@ -135,21 +151,6 @@
"Are you a human? \e[32mDisagree\e[0m\n"
].join)
end

it "accepts regex conflicting characters as suffix" do
prompt.input << "]\n"
prompt.input.rewind
result = prompt.yes?("Are you a human? Use [ for yes and ] for no") do |q|
q.suffix '[/]'
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? Use [ for yes and ] for no \e[90m([/])\e[0m ",
"\e[2K\e[1GAre you a human? Use [ for yes and ] for no \e[90m([/])\e[0m [",
"\e[2K\e[1GAre you a human? Use [ for yes and ] for no \e[90m([/])\e[0m [\n",
"Are you a human? Use [ for yes and ] for no \e[32m[\e[0m\n"
].join)
end
end

context '#no?' do
Expand Down

0 comments on commit aa26130

Please sign in to comment.