summaryrefslogtreecommitdiff
path: root/test/optparse
diff options
context:
space:
mode:
Diffstat (limited to 'test/optparse')
-rw-r--r--test/optparse/test_acceptable.rb2
-rw-r--r--test/optparse/test_load.rb61
-rw-r--r--test/optparse/test_optparse.rb93
-rw-r--r--test/optparse/test_placearg.rb25
-rw-r--r--test/optparse/test_switch.rb50
5 files changed, 224 insertions, 7 deletions
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb
index c7ea2152fc..8b578effde 100644
--- a/test/optparse/test_acceptable.rb
+++ b/test/optparse/test_acceptable.rb
@@ -199,5 +199,7 @@ class TestOptionParserAcceptable < TestOptionParser
def test_array
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
assert_equal(%w"a b c", @array)
+ assert_equal(%w"", no_error {@opt.parse!(%w"--array a")})
+ assert_equal(%w"a", @array)
end
end
diff --git a/test/optparse/test_load.rb b/test/optparse/test_load.rb
index 0ebe855682..f664cfbf72 100644
--- a/test/optparse/test_load.rb
+++ b/test/optparse/test_load.rb
@@ -31,7 +31,13 @@ class TestOptionParserLoad < Test::Unit::TestCase
assert_equal({test: result}, into)
end
+ def assert_load_nothing
+ assert !new_parser.load
+ assert_nil @result
+ end
+
def setup_options(env, dir, suffix = nil)
+ env.update({'HOME'=>@tmpdir})
optdir = File.join(@tmpdir, dir)
FileUtils.mkdir_p(optdir)
file = File.join(optdir, [@basename, suffix].join(""))
@@ -41,7 +47,7 @@ class TestOptionParserLoad < Test::Unit::TestCase
begin
yield dir, optdir
ensure
- File.unlink(file)
+ File.unlink(file) rescue nil
Dir.rmdir(optdir) rescue nil
end
else
@@ -50,7 +56,7 @@ class TestOptionParserLoad < Test::Unit::TestCase
end
def setup_options_home(&block)
- setup_options({'HOME'=>@tmpdir}, ".options", &block)
+ setup_options({}, ".options", &block)
end
def setup_options_xdg_config_home(&block)
@@ -58,7 +64,7 @@ class TestOptionParserLoad < Test::Unit::TestCase
end
def setup_options_home_config(&block)
- setup_options({'HOME'=>@tmpdir}, ".config", ".options", &block)
+ setup_options({}, ".config", ".options", &block)
end
def setup_options_xdg_config_dirs(&block)
@@ -66,7 +72,11 @@ class TestOptionParserLoad < Test::Unit::TestCase
end
def setup_options_home_config_settings(&block)
- setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block)
+ setup_options({}, "config/settings", ".options", &block)
+ end
+
+ def setup_options_home_options(envname, &block)
+ setup_options({envname => '~/options'}, "options", ".options", &block)
end
def test_load_home_options
@@ -91,7 +101,7 @@ class TestOptionParserLoad < Test::Unit::TestCase
end
def test_load_xdg_config_home
- result, = setup_options_xdg_config_home
+ result, dir = setup_options_xdg_config_home
assert_load(result)
setup_options_home_config do
@@ -105,6 +115,11 @@ class TestOptionParserLoad < Test::Unit::TestCase
setup_options_home_config_settings do
assert_load(result)
end
+
+ File.unlink("#{dir}/#{@basename}.options")
+ setup_options_home_config do
+ assert_load_nothing
+ end
end
def test_load_home_config
@@ -118,6 +133,11 @@ class TestOptionParserLoad < Test::Unit::TestCase
setup_options_home_config_settings do
assert_load(result)
end
+
+ setup_options_xdg_config_home do |_, dir|
+ File.unlink("#{dir}/#{@basename}.options")
+ assert_load_nothing
+ end
end
def test_load_xdg_config_dirs
@@ -135,7 +155,34 @@ class TestOptionParserLoad < Test::Unit::TestCase
end
def test_load_nothing
- assert !new_parser.load
- assert_nil @result
+ setup_options({}, "") do
+ assert_load_nothing
+ end
+ end
+
+ def test_not_expand_path_basename
+ basename = @basename
+ @basename = "~"
+ $test_optparse_basename = "/" + @basename
+ alias $test_optparse_prog $0
+ alias $0 $test_optparse_basename
+ setup_options({'HOME'=>@tmpdir+"/~options"}, "", "options") do
+ assert_load_nothing
+ end
+ ensure
+ alias $0 $test_optparse_prog
+ @basename = basename
+ end
+
+ def test_not_expand_path_xdg_config_home
+ setup_options_home_options('XDG_CONFIG_HOME') do
+ assert_load_nothing
+ end
+ end
+
+ def test_not_expand_path_xdg_config_dirs
+ setup_options_home_options('XDG_CONFIG_DIRS') do
+ assert_load_nothing
+ end
end
end
diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb
index 3b9ccc756a..ff334009a6 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -74,6 +74,7 @@ class TestOptionParser < Test::Unit::TestCase
@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
+ @opt.def_option "-a", "--array [VAL]", Array do |val| val end
result = {}
@opt.parse %w(--host localhost --port 8000 -v), into: result
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
@@ -84,6 +85,18 @@ class TestOptionParser < Test::Unit::TestCase
result = {}
@opt.parse %w(--option OPTION -v), into: result
assert_equal({verbose: true, option: "OPTION"}, result)
+ result = {}
+ @opt.parse %w(-a b,c,d), into: result
+ assert_equal({array: %w(b c d)}, result)
+ result = {}
+ @opt.parse %w(--array b,c,d), into: result
+ assert_equal({array: %w(b c d)}, result)
+ result = {}
+ @opt.parse %w(-a b), into: result
+ assert_equal({array: %w(b)}, result)
+ result = {}
+ @opt.parse %w(--array b), into: result
+ assert_equal({array: %w(b)}, result)
end
def test_require_exact
@@ -116,6 +129,35 @@ class TestOptionParser < Test::Unit::TestCase
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
@@ -135,4 +177,55 @@ 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
+ $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
+
+ def test_program_name
+ program = $0
+ $0 = "rdbg3.5"
+ assert_equal "rdbg3.5", OptionParser.new.program_name
+ RbConfig::CONFIG["EXECUTABLE_EXTS"]&.split(" ") do |ext|
+ $0 = "rdbg3.5" + ext
+ assert_equal "rdbg3.5", OptionParser.new.program_name
+ end
+ ensure
+ $0 = program
+ end
end
diff --git a/test/optparse/test_placearg.rb b/test/optparse/test_placearg.rb
index a8a11e676b..d5be5a66fb 100644
--- a/test/optparse/test_placearg.rb
+++ b/test/optparse/test_placearg.rb
@@ -7,6 +7,10 @@ class TestOptionParserPlaceArg < TestOptionParser
@opt.def_option("-x [VAL]") {|x| @flag = x}
@opt.def_option("--option [VAL]") {|x| @flag = x}
@opt.def_option("-T [level]", /^[0-4]$/, Integer) {|x| @topt = x}
+ @opt.def_option("--enum [VAL]", [:Alpha, :Bravo, :Charlie]) {|x| @enum = x}
+ @opt.def_option("--enumval [VAL]", [[:Alpha, 1], [:Bravo, 2], [:Charlie, 3]]) {|x| @enum = x}
+ @opt.def_option("--integer [VAL]", Integer, [1, 2, 3]) {|x| @integer = x}
+ @opt.def_option("--range [VAL]", Integer, 1..3) {|x| @range = x}
@topt = nil
@opt.def_option("-n") {}
@opt.def_option("--regexp [REGEXP]", Regexp) {|x| @reopt = x}
@@ -93,4 +97,25 @@ class TestOptionParserPlaceArg < TestOptionParser
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
assert_equal(nil, @flag)
end
+
+ def test_enum
+ assert_equal([], no_error {@opt.parse!(%w"--enum=A")})
+ assert_equal(:Alpha, @enum)
+ end
+
+ def test_enum_pair
+ assert_equal([], no_error {@opt.parse!(%w"--enumval=A")})
+ assert_equal(1, @enum)
+ end
+
+ def test_enum_conversion
+ assert_equal([], no_error {@opt.parse!(%w"--integer=1")})
+ assert_equal(1, @integer)
+ end
+
+ def test_enum_range
+ assert_equal([], no_error {@opt.parse!(%w"--range=1")})
+ assert_equal(1, @range)
+ assert_raise(OptionParser::InvalidArgument) {@opt.parse!(%w"--range=4")}
+ end
end
diff --git a/test/optparse/test_switch.rb b/test/optparse/test_switch.rb
new file mode 100644
index 0000000000..b06f4e310b
--- /dev/null
+++ b/test/optparse/test_switch.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: false
+
+require 'test/unit'
+require 'optparse'
+
+
+class TestOptionParserSwitch < Test::Unit::TestCase
+
+ def setup
+ @parser = OptionParser.new
+ end
+
+ def assert_invalidarg_error(msg, &block)
+ exc = assert_raise(OptionParser::InvalidArgument) do
+ yield
+ end
+ assert_equal "invalid argument: #{msg}", exc.message
+ end
+
+ def test_make_switch__enum_array
+ p = @parser
+ p.on("--enum=<val>", ["aa", "bb", "cc"])
+ p.permute(["--enum=bb"], into: (opts={}))
+ assert_equal({:enum=>"bb"}, opts)
+ assert_invalidarg_error("--enum=dd") do
+ p.permute(["--enum=dd"], into: (opts={}))
+ end
+ end
+
+ def test_make_switch__enum_hash
+ p = @parser
+ p.on("--hash=<val>", {"aa"=>"AA", "bb"=>"BB"})
+ p.permute(["--hash=bb"], into: (opts={}))
+ assert_equal({:hash=>"BB"}, opts)
+ assert_invalidarg_error("--hash=dd") do
+ p.permute(["--hash=dd"], into: (opts={}))
+ end
+ end
+
+ def test_make_switch__enum_set
+ p = @parser
+ p.on("--set=<val>", Set.new(["aa", "bb", "cc"]))
+ p.permute(["--set=bb"], into: (opts={}))
+ assert_equal({:set=>"bb"}, opts)
+ assert_invalidarg_error("--set=dd") do
+ p.permute(["--set=dd"], into: (opts={}))
+ end
+ end
+
+end