summaryrefslogtreecommitdiff
path: root/test/optparse
diff options
context:
space:
mode:
Diffstat (limited to 'test/optparse')
-rw-r--r--test/optparse/test_load.rb61
-rw-r--r--test/optparse/test_optparse.rb17
-rw-r--r--test/optparse/test_placearg.rb25
-rw-r--r--test/optparse/test_switch.rb50
4 files changed, 143 insertions, 10 deletions
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 7f35cb4a8a..ff334009a6 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -184,10 +184,9 @@ class TestOptionParser < Test::Unit::TestCase
File.open(File.join(dir, "options.rb"), "w") do |f|
f.puts "#{<<~"begin;"}\n#{<<~'end;'}"
begin;
- stdout = STDOUT.dup
+ stdout = $stdout.dup
def stdout.tty?; true; end
- Object.__send__(:remove_const, :STDOUT)
- STDOUT = stdout
+ $stdout = stdout
ARGV.options do |opt|
end;
100.times {|i| f.puts " opt.on('--opt-#{i}') {}"}
@@ -217,4 +216,16 @@ class TestOptionParser < Test::Unit::TestCase
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