summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2024-02-09 12:59:43 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2024-02-09 19:58:19 +0900
commit2c6767b71ef5154f49e4aef7a236849a934e68fb (patch)
treeb1c5946b6272ffffbca1ef097c9621f8166d392a /test
parent5e12b757162970b317e2fdf2490b694b52125743 (diff)
[ruby/optparse] Respect default values in block parameters
Fix https://github.com/ruby/optparse/pull/55 https://github.com/ruby/optparse/commit/9d53e74aa4
Diffstat (limited to 'test')
-rw-r--r--test/optparse/test_acceptable.rb5
-rw-r--r--test/optparse/test_optarg.rb8
-rw-r--r--test/optparse/test_optparse.rb7
-rw-r--r--test/optparse/test_placearg.rb10
4 files changed, 30 insertions, 0 deletions
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb
index 12f5322726..c7ea2152fc 100644
--- a/test/optparse/test_acceptable.rb
+++ b/test/optparse/test_acceptable.rb
@@ -8,6 +8,7 @@ class TestOptionParserAcceptable < TestOptionParser
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
@opt.def_option("--float VAL", Float) { |v| @float = v }
@opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
+ @opt.def_option("--array VAL", Array) { |v| @array = v }
@opt.def_option("--decimal-integer VAL",
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
@@ -195,4 +196,8 @@ class TestOptionParserAcceptable < TestOptionParser
end
end
+ def test_array
+ assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
+ assert_equal(%w"a b c", @array)
+ end
end
diff --git a/test/optparse/test_optarg.rb b/test/optparse/test_optarg.rb
index 81127a8a37..f4882b0ea7 100644
--- a/test/optparse/test_optarg.rb
+++ b/test/optparse/test_optarg.rb
@@ -9,6 +9,7 @@ class TestOptionParserOptArg < TestOptionParser
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
+ @opt.def_option("--fallback[=VAL]") do |x = "fallback"| @flag = x end
@reopt = nil
end
@@ -57,4 +58,11 @@ class TestOptionParserOptArg < TestOptionParser
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
assert_equal("foo4", @flag)
end
+
+ def test_default_argument
+ assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
+ assert_equal("val1", @flag)
+ assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
+ assert_equal("fallback", @flag)
+ end
end
diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb
index be9bcb8425..3b9ccc756a 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -73,10 +73,17 @@ class TestOptionParser < Test::Unit::TestCase
@opt.def_option "-p", "--port=PORT", "port", Integer
@opt.def_option "-v", "--verbose" do @verbose = true end
@opt.def_option "-q", "--quiet" do @quiet = true end
+ @opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
result = {}
@opt.parse %w(--host localhost --port 8000 -v), into: result
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
assert_equal(true, @verbose)
+ result = {}
+ @opt.parse %w(--option -q), into: result
+ assert_equal({quiet: true, option: nil}, result)
+ result = {}
+ @opt.parse %w(--option OPTION -v), into: result
+ assert_equal({verbose: true, option: "OPTION"}, result)
end
def test_require_exact
diff --git a/test/optparse/test_placearg.rb b/test/optparse/test_placearg.rb
index ed0e4d3e6c..56b641b0b6 100644
--- a/test/optparse/test_placearg.rb
+++ b/test/optparse/test_placearg.rb
@@ -13,6 +13,7 @@ class TestOptionParserPlaceArg < TestOptionParser
@reopt = nil
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
+ @opt.def_option("--fallback [VAL]") do |x = "fallback"| @flag = x end
end
def test_short
@@ -73,4 +74,13 @@ class TestOptionParserPlaceArg < TestOptionParser
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
assert_equal(1, @topt)
end
+
+ def test_default_argument
+ assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
+ assert_equal("val1", @flag)
+ assert_equal(%w"", no_error {@opt.parse!(%w"--fallback val2")})
+ assert_equal("val2", @flag)
+ assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
+ assert_equal("fallback", @flag)
+ end
end