summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormmihira <mmihira.w@gmail.com>2016-07-09 13:11:20 +1000
committermmihira <mmihira.w@gmail.com>2016-07-09 13:11:20 +1000
commit4298414b4032752574db9bb59aac86498feb2ecc (patch)
treeb60747cf2b3cc1d1c450e0fd8d7f57e437350ed5
parent7d9c50ec81baa304a5144f19898505c7f3b9ae04 (diff)
downloadhighline-4298414b4032752574db9bb59aac86498feb2ecc.tar.gz
Tests for Proc as confirm attribute. Update docs
-rw-r--r--Changelog.md2
-rwxr-xr-xlib/highline/question.rb14
-rwxr-xr-xtest/test_highline.rb18
3 files changed, 29 insertions, 5 deletions
diff --git a/Changelog.md b/Changelog.md
index 8e3fab3..f36ad1f 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,6 +2,8 @@
Below is a complete listing of changes for each revision of HighLine.
+* PR #201 - Confirm in question now accepts Proc
+
* PR #197 - Some HighLine::Menu improvements
* Move Menu::MenuItem to Menu::Item with its own file
* Some small refactorings
diff --git a/lib/highline/question.rb b/lib/highline/question.rb
index 07d6954..c3a217e 100755
--- a/lib/highline/question.rb
+++ b/lib/highline/question.rb
@@ -147,11 +147,15 @@ class HighLine
attr_accessor :in
#
# Asks a yes or no confirmation question, to ensure a user knows what
- # they have just agreed to. If set to +true+ the question will be,
- # "Are you sure? " Any other true value for this attribute is assumed
- # to be the question to ask. When +false+ or +nil+ (the default),
- # answers are not confirmed.
- #
+ # they have just agreed to. The confirm attribute can be set to :
+ # +true+ : In this case the question will be, "Are you sure?".
+ # Proc : The Proc is yielded the answer given. The Proc must
+ # output a string which is then used as the confirm
+ # question.
+ # String : The String must use ERB syntax. The String is
+ # evaluated with with access to question and answer and
+ # is then used as the confirm question.
+ # When set to +false+ or +nil+ (the default), answers are not confirmed.
attr_accessor :confirm
#
# When set, the user will be prompted for multiple answers which will
diff --git a/test/test_highline.rb b/test/test_highline.rb
index a75c143..9cdfeb4 100755
--- a/test/test_highline.rb
+++ b/test/test_highline.rb
@@ -579,6 +579,24 @@ class TestHighLine < Minitest::Test
assert_equal( "Enter a filename: " +
"Are you sure you want to overwrite junk.txt? ",
@output.string )
+
+ @input.truncate(@input.rewind)
+ @input << "junk.txt\nyes\nsave.txt\nn\n"
+ @input.rewind
+ @output.truncate(@output.rewind)
+
+ scoped_variable = { "junk.txt" => '20mb' }
+ answer = @terminal.ask("Enter a filename: ") do |q|
+ q.confirm = Proc.new do |answer|
+ "Are you sure you want to overwrite #{answer} with size " +
+ "of #{scoped_variable[answer]}? "
+ end
+ end
+ assert_equal("junk.txt", answer)
+ assert_equal( "Enter a filename: " +
+ "Are you sure you want to overwrite junk.txt " +
+ "with size of 20mb? ",
+ @output.string )
end
def test_generic_confirm_with_true