diff options
author | Kevin Locke <kevin@kevinlocke.name> | 2016-02-18 22:02:17 -0800 |
---|---|---|
committer | Kevin Locke <kevin@kevinlocke.name> | 2016-02-18 22:02:17 -0800 |
commit | 6bba9b61465cc4f3b899c6fd2838ec39bd5c7d95 (patch) | |
tree | cea7a7fce6306a87e2bbef559e57e017d770a939 | |
parent | 446ceb184dbfffbd83bd25edb15bcee3e630b3e4 (diff) | |
download | highline-6bba9b61465cc4f3b899c6fd2838ec39bd5c7d95.tar.gz |
Fix agree validation to only accept "yes" or "no"
Although the documentation for agree states that it only accepts
yes/no/y/n case-insensitively, the Regexp allowed any string which
started with /y/i or ended with /no?/i and interpreted any starting with
'y' as true. Fix the Regexp to properly exclude the start/end markers
from the alternation and add a test value to confirm it works.
Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
-rwxr-xr-x | lib/highline.rb | 2 | ||||
-rwxr-xr-x | test/test_highline.rb | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/lib/highline.rb b/lib/highline.rb index ba409b6..ff6da99 100755 --- a/lib/highline.rb +++ b/lib/highline.rb @@ -190,7 +190,7 @@ class HighLine # @see Question#character def agree( yes_or_no_question, character = nil ) ask(yes_or_no_question, lambda { |yn| yn.downcase[0] == ?y}) do |q| - q.validate = /\Ay(?:es)?|no?\Z/i + q.validate = /\A(?:y(?:es)?|no?)\Z/i q.responses[:not_valid] = 'Please enter "yes" or "no".' q.responses[:ask_on_error] = :question q.character = character diff --git a/test/test_highline.rb b/test/test_highline.rb index a5101c1..7c95557 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -35,7 +35,7 @@ class TestHighLine < Minitest::Test end def test_agree - @input << "y\nyes\nYES\nHell no!\nNo\n" + @input << "y\nyes\nYES\nyuk\nHell no!\nNo\n" @input.rewind assert_equal(true, @terminal.agree("Yes or no? ")) |