diff options
Diffstat (limited to 'test/optparse')
-rw-r--r-- | test/optparse/test_acceptable.rb | 5 | ||||
-rw-r--r-- | test/optparse/test_getopts.rb | 16 | ||||
-rw-r--r-- | test/optparse/test_load.rb | 141 | ||||
-rw-r--r-- | test/optparse/test_optarg.rb | 16 | ||||
-rw-r--r-- | test/optparse/test_optparse.rb | 103 | ||||
-rw-r--r-- | test/optparse/test_placearg.rb | 26 | ||||
-rw-r--r-- | test/optparse/test_reqarg.rb | 6 | ||||
-rw-r--r-- | test/optparse/test_summary.rb | 23 |
8 files changed, 334 insertions, 2 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_getopts.rb b/test/optparse/test_getopts.rb index 7d9160f7af..4a0ae284e7 100644 --- a/test/optparse/test_getopts.rb +++ b/test/optparse/test_getopts.rb @@ -11,23 +11,39 @@ class TestOptionParserGetopts < Test::Unit::TestCase o = @opt.getopts(%w[-a], "ab") assert_equal(true, o['a']) assert_equal(false, o['b']) + + o = @opt.getopts(%w[-a], "ab", symbolize_names: true) + assert_equal(true, o[:a]) + assert_equal(false, o[:b]) end def test_short_arg o = @opt.getopts(%w[-a1], "a:b:") assert_equal("1", o['a']) assert_equal(nil, o['b']) + + o = @opt.getopts(%w[-a1], "a:b:", symbolize_names: true) + assert_equal("1", o[:a]) + assert_equal(nil, o[:b]) end def test_long_noarg o = @opt.getopts(%w[--foo], "", "foo", "bar") assert_equal(true, o['foo']) assert_equal(false, o['bar']) + + o = @opt.getopts(%w[--foo], "", "foo", "bar", symbolize_names: true) + assert_equal(true, o[:foo]) + assert_equal(false, o[:bar]) end def test_long_arg o = @opt.getopts(%w[--bar ZOT], "", "foo:FOO", "bar:BAR") assert_equal("FOO", o['foo']) assert_equal("ZOT", o['bar']) + + o = @opt.getopts(%w[--bar ZOT], "", "foo:FOO", "bar:BAR", symbolize_names: true) + assert_equal("FOO", o[:foo]) + assert_equal("ZOT", o[:bar]) end end diff --git a/test/optparse/test_load.rb b/test/optparse/test_load.rb new file mode 100644 index 0000000000..0ebe855682 --- /dev/null +++ b/test/optparse/test_load.rb @@ -0,0 +1,141 @@ +# frozen_string_literal: false +require 'test/unit' +require 'optparse' +require 'tmpdir' + +class TestOptionParserLoad < Test::Unit::TestCase + def setup + @tmpdir = Dir.mktmpdir("optparse_test-") + @basename = File.basename($0, '.*') + @envs = %w[HOME XDG_CONFIG_HOME XDG_CONFIG_DIRS].each_with_object({}) do |v, h| + h[v] = ENV.delete(v) + end + end + + def teardown + ENV.update(@envs) + FileUtils.rm_rf(@tmpdir) + end + + def new_parser + @result = nil + OptionParser.new do |opt| + opt.on("--test=arg") {|v| @result = v} + end + end + + def assert_load(result) + assert new_parser.load + assert_equal(result, @result) + assert new_parser.load(into: into = {}) + assert_equal({test: result}, into) + end + + def setup_options(env, dir, suffix = nil) + optdir = File.join(@tmpdir, dir) + FileUtils.mkdir_p(optdir) + file = File.join(optdir, [@basename, suffix].join("")) + File.write(file, "--test=#{dir}") + ENV.update(env) + if block_given? + begin + yield dir, optdir + ensure + File.unlink(file) + Dir.rmdir(optdir) rescue nil + end + else + return dir, optdir + end + end + + def setup_options_home(&block) + setup_options({'HOME'=>@tmpdir}, ".options", &block) + end + + def setup_options_xdg_config_home(&block) + setup_options({'XDG_CONFIG_HOME'=>@tmpdir+"/xdg"}, "xdg", ".options", &block) + end + + def setup_options_home_config(&block) + setup_options({'HOME'=>@tmpdir}, ".config", ".options", &block) + end + + def setup_options_xdg_config_dirs(&block) + setup_options({'XDG_CONFIG_DIRS'=>@tmpdir+"/xdgconf"}, "xdgconf", ".options", &block) + end + + def setup_options_home_config_settings(&block) + setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block) + end + + def test_load_home_options + result, = setup_options_home + assert_load(result) + + setup_options_xdg_config_home do + assert_load(result) + end + + setup_options_home_config do + assert_load(result) + end + + setup_options_xdg_config_dirs do + assert_load(result) + end + + setup_options_home_config_settings do + assert_load(result) + end + end + + def test_load_xdg_config_home + result, = setup_options_xdg_config_home + assert_load(result) + + setup_options_home_config do + assert_load(result) + end + + setup_options_xdg_config_dirs do + assert_load(result) + end + + setup_options_home_config_settings do + assert_load(result) + end + end + + def test_load_home_config + result, = setup_options_home_config + assert_load(result) + + setup_options_xdg_config_dirs do + assert_load(result) + end + + setup_options_home_config_settings do + assert_load(result) + end + end + + def test_load_xdg_config_dirs + result, = setup_options_xdg_config_dirs + assert_load(result) + + setup_options_home_config_settings do + assert_load(result) + end + end + + def test_load_home_config_settings + result, = setup_options_home_config_settings + assert_load(result) + end + + def test_load_nothing + assert !new_parser.load + assert_nil @result + end +end diff --git a/test/optparse/test_optarg.rb b/test/optparse/test_optarg.rb index 81127a8a37..f94460527f 100644 --- a/test/optparse/test_optarg.rb +++ b/test/optparse/test_optarg.rb @@ -9,6 +9,8 @@ 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 + @opt.def_option("--lambda[=VAL]", &->(x) {@flag = x}) @reopt = nil end @@ -57,4 +59,18 @@ 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 + + def test_lambda + assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")}) + assert_equal("lambda1", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")}) + assert_equal(nil, @flag) + end end diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb index 5f5ea183b0..8d09e0f28b 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -63,6 +63,9 @@ class TestOptionParser < Test::Unit::TestCase assert_equal(/foo/i, @reopt) assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/n")}) assert_equal(/foo/n, @reopt) + assert_equal(%w"", no_error {@opt.parse!(%W"--regexp=/\u{3042}/s")}) + assert_equal(Encoding::Windows_31J, @reopt.encoding) + assert_equal("\x82\xa0".force_encoding(Encoding::Windows_31J), @reopt.source) end def test_into @@ -70,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 @@ -85,9 +95,9 @@ class TestOptionParser < Test::Unit::TestCase end @opt.require_exact = true - %w(--zrs -F -Ffoo).each do |arg| + [%w(--zrs foo), %w(--zrs=foo), %w(-F foo), %w(-Ffoo)].each do |args| result = {} - @opt.parse([arg, 'foo'], into: result) + @opt.parse(args, into: result) assert_equal({zrs: 'foo'}, result) end @@ -96,6 +106,55 @@ class TestOptionParser < Test::Unit::TestCase assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zrs foo))} assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zr foo))} assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo))} + + @opt.def_option('-f', '--[no-]foo', 'foo') {|arg| @foo = arg} + @opt.parse(%w[-f]) + assert_equal(true, @foo) + @opt.parse(%w[--foo]) + assert_equal(true, @foo) + @opt.parse(%w[--no-foo]) + assert_equal(false, @foo) + end + + def test_exact_option + @opt.def_option('-F', '--zrs=IRS', 'zrs') + %w(--zrs --zr --z -zfoo -z -F -Ffoo).each do |arg| + result = {} + @opt.parse([arg, 'foo'], into: result) + assert_equal({zrs: 'foo'}, result) + end + + [%w(--zrs foo), %w(--zrs=foo), %w(-F foo), %w(-Ffoo)].each do |args| + result = {} + @opt.parse(args, into: result, exact: true) + assert_equal({zrs: 'foo'}, result) + end + + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--zr foo), exact: true)} + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--z foo), exact: true)} + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zrs foo), exact: true)} + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-zr foo), exact: true)} + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-z foo), exact: true)} + + @opt.def_option('-f', '--[no-]foo', 'foo') {|arg| @foo = arg} + @opt.parse(%w[-f], exact: true) + assert_equal(true, @foo) + @opt.parse(%w[--foo], exact: true) + assert_equal(true, @foo) + @opt.parse(%w[--no-foo], exact: true) + assert_equal(false, @foo) + end + + def test_raise_unknown + @opt.def_option('--my-foo [ARG]') {|arg| @foo = arg} + assert @opt.raise_unknown + + @opt.raise_unknown = false + assert_equal(%w[--my-bar], @opt.parse(%w[--my-foo --my-bar])) + assert_nil(@foo) + + assert_equal(%w[--my-bar], @opt.parse(%w[--my-foo x --my-bar])) + assert_equal("x", @foo) end def test_nonopt_pattern @@ -105,4 +164,44 @@ class TestOptionParser < Test::Unit::TestCase e = assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-t))} assert_equal(["-t"], e.args) end + + def test_help_pager + require 'tmpdir' + Dir.mktmpdir do |dir| + File.open(File.join(dir, "options.rb"), "w") do |f| + f.puts "#{<<~"begin;"}\n#{<<~'end;'}" + begin; + stdout = STDOUT.dup + def stdout.tty?; true; end + Object.__send__(:remove_const, :STDOUT) + STDOUT = stdout + ARGV.options do |opt| + end; + 100.times {|i| f.puts " opt.on('--opt-#{i}') {}"} + f.puts "#{<<~"begin;"}\n#{<<~'end;'}" + begin; + opt.parse! + end + end; + end + + optparse = $".find {|path| path.end_with?("/optparse.rb")} + args = ["-r#{optparse}", "options.rb", "--help"] + cmd = File.join(dir, "pager.cmd") + if RbConfig::CONFIG["EXECUTABLE_EXTS"]&.include?(".cmd") + command = "@echo off" + else # if File.executable?("/bin/sh") + # TruffleRuby just calls `posix_spawnp` and no fallback to `/bin/sh`. + command = "#!/bin/sh\n" + end + + [ + [{"RUBY_PAGER"=>cmd, "PAGER"=>"echo ng"}, "Executing RUBY_PAGER"], + [{"RUBY_PAGER"=>nil, "PAGER"=>cmd}, "Executing PAGER"], + ].each do |env, expected| + File.write(cmd, "#{command}\n" "echo #{expected}\n", perm: 0o700) + assert_in_out_err([env, *args], "", [expected], chdir: dir) + end + end + end end diff --git a/test/optparse/test_placearg.rb b/test/optparse/test_placearg.rb index 94cfb0e819..a8a11e676b 100644 --- a/test/optparse/test_placearg.rb +++ b/test/optparse/test_placearg.rb @@ -13,11 +13,15 @@ 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 + @opt.def_option("--lambda [VAL]", &->(x) {@flag = x}) end def test_short assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")}) assert_equal(nil, @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-x -")}) + assert_equal("-", @flag) @flag = false assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")}) assert_equal("foo", @flag) @@ -30,6 +34,8 @@ class TestOptionParserPlaceArg < TestOptionParser def test_abbrev assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")}) assert_equal(nil, @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-o -")}) + assert_equal("-", @flag) @flag = false assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")}) assert_equal("foo", @flag) @@ -42,6 +48,8 @@ class TestOptionParserPlaceArg < TestOptionParser def test_long assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")}) assert_equal(nil, @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--opt -")}) + assert_equal("-", @flag) assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")}) assert_equal("", @flag) assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")}) @@ -67,4 +75,22 @@ 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 + + def test_lambda + assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")}) + assert_equal("lambda1", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--lambda lambda2")}) + assert_equal("lambda2", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")}) + assert_equal(nil, @flag) + end end diff --git a/test/optparse/test_reqarg.rb b/test/optparse/test_reqarg.rb index d5686d13aa..31d4fef417 100644 --- a/test/optparse/test_reqarg.rb +++ b/test/optparse/test_reqarg.rb @@ -6,6 +6,7 @@ module TestOptionParserReqArg super @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("--lambda=VAL", &->(x) {@flag = x}) end class Def1 < TestOptionParser @@ -81,6 +82,11 @@ module TestOptionParserReqArg assert_equal("foo4", @flag) end + def test_lambda + assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")}) + assert_equal("lambda1", @flag) + end + class TestOptionParser::WithPattern < TestOptionParser def test_pattern pat = num = nil diff --git a/test/optparse/test_summary.rb b/test/optparse/test_summary.rb index 6b36ce3c76..b5dcb3524e 100644 --- a/test/optparse/test_summary.rb +++ b/test/optparse/test_summary.rb @@ -55,4 +55,27 @@ class TestOptionParserSummaryTest < TestOptionParser o.release = "rel" assert_equal "foo 0.1 (rel)", o.ver end + + # https://github.com/ruby/optparse/issues/37 + def test_very_long_without_short + o = OptionParser.new do |opts| + # This causes TypeError + opts.on('', '--long-long-option-param-without-short', "Error desc") { options[:long_long_option_param_without_short] = true } + opts.on('', '--long-option-param', "Long desc") { options[:long_option_param_without_short] = true } + opts.on('-a', '--long-long-option-param-with-short', "Normal description") { options[:long_long_option_param_with_short] = true } + + opts.on('', '--long-long-option-param-without-short-but-with-desc', 'Description of the long long param') { options[:long_long_option_param_without_short_but_with_desc] = true } + end + + s = o.summarize + + assert_match(/^\s*--long-long-option-param-without-short$/, s[0]) + assert_match(/^\s*Error desc$/, s[1]) + assert_match(/^\s*--long-option-param\s+Long desc$/, s[2]) + assert_match(/^\s*-a\s+Normal description$/, s[3]) + assert_match(/^\s*--long-long-option-param-with-short$/, s[4]) + + assert_match(/^\s*--long-long-option-param-without-short-but-with-desc$/, s[5]) + assert_match(/^\s*Description of the long long param$/, s[6]) + end end |