diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2016-02-19 12:48:47 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2016-02-19 12:48:47 -0300 |
commit | 70297a61ab7be27deb15e6b25bc18358b4c679fe (patch) | |
tree | ed38a17a0fbb974a357795ee337a50fa608d0c88 | |
parent | a3166e918311f3caf24fba89b76179c62c3ccbd7 (diff) | |
parent | 5f0a9447c34f5fa56ede6aaf98859cf3664157f9 (diff) | |
download | highline-70297a61ab7be27deb15e6b25bc18358b4c679fe.tar.gz |
Merge pull request #189 from kevinoid/fix-ask-checking
Fix agree validation to only accept "yes" or "no"
-rwxr-xr-x | lib/highline.rb | 2 | ||||
-rwxr-xr-x | test/test_highline.rb | 56 |
2 files changed, 49 insertions, 9 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..a75c143 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -34,15 +34,55 @@ class TestHighLine < Minitest::Test @terminal = HighLine.new(@input, @output) end - def test_agree - @input << "y\nyes\nYES\nHell no!\nNo\n" - @input.rewind + def test_agree_valid_yes_answers + valid_yes_answers = %w{ y yes Y YES } - assert_equal(true, @terminal.agree("Yes or no? ")) - assert_equal(true, @terminal.agree("Yes or no? ")) - assert_equal(true, @terminal.agree("Yes or no? ")) - assert_equal(false, @terminal.agree("Yes or no? ")) - + valid_yes_answers.each do |user_input| + @input << "#{user_input}\n" + @input.rewind + + assert_equal true, @terminal.agree("Yes or no? ") + assert_equal "Yes or no? ", @output.string + + @input.truncate(@input.rewind) + @output.truncate(@output.rewind) + end + end + + def test_agree_valid_no_answers + valid_no_answers = %w{ n no N NO } + + valid_no_answers.each do |user_input| + @input << "#{user_input}\n" + @input.rewind + + assert_equal false, @terminal.agree("Yes or no? ") + assert_equal "Yes or no? ", @output.string + + @input.truncate(@input.rewind) + @output.truncate(@output.rewind) + end + end + + def test_agree_invalid_answers + invalid_answers = [ "ye", "yuk", "nope", "Oh yes", "Oh no", "Hell no!"] + + invalid_answers.each do |user_input| + # Each invalid answer, should be followed by a 'y' (as the question is reasked) + @input << "#{user_input}\ny\n" + @input.rewind + + assert_equal true, @terminal.agree("Yes or no? ") + + # It reasks the question if the answer is invalid + assert_equal "Yes or no? Please enter \"yes\" or \"no\".\nYes or no? ", @output.string + + @input.truncate(@input.rewind) + @output.truncate(@output.rewind) + end + end + + def test_agree_with_getc @input.truncate(@input.rewind) @input << "yellow" @input.rewind |