diff options
Diffstat (limited to 'test/optparse')
| -rw-r--r-- | test/optparse/test_load.rb | 61 | ||||
| -rw-r--r-- | test/optparse/test_optparse.rb | 12 | ||||
| -rw-r--r-- | test/optparse/test_switch.rb | 50 |
3 files changed, 116 insertions, 7 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 d50203bb63..ff334009a6 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -216,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_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 |
