summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Locke <kevin@kevinlocke.name>2016-02-18 22:02:17 -0800
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2016-02-19 05:29:30 -0300
commit2deec4c0df50b1c0ac502713cca6d6cf22d2b551 (patch)
tree2eab26e6fc100f91ac74fba8cd765bd25de3a39e
parenta3166e918311f3caf24fba89b76179c62c3ccbd7 (diff)
downloadhighline-2deec4c0df50b1c0ac502713cca6d6cf22d2b551.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 775a4ac..70734cb 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? "))