summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Locke <kevin@kevinlocke.name>2016-02-18 22:02:17 -0800
committerKevin Locke <kevin@kevinlocke.name>2016-02-18 22:02:17 -0800
commit6bba9b61465cc4f3b899c6fd2838ec39bd5c7d95 (patch)
treecea7a7fce6306a87e2bbef559e57e017d770a939
parent446ceb184dbfffbd83bd25edb15bcee3e630b3e4 (diff)
downloadhighline-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-xlib/highline.rb2
-rwxr-xr-xtest/test_highline.rb2
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? "))