diff options
Diffstat (limited to 'test/optparse')
| -rw-r--r-- | test/optparse/test_acceptable.rb | 205 | ||||
| -rw-r--r-- | test/optparse/test_autoconf.rb | 69 | ||||
| -rw-r--r-- | test/optparse/test_bash_completion.rb | 46 | ||||
| -rw-r--r-- | test/optparse/test_cclass.rb | 18 | ||||
| -rw-r--r-- | test/optparse/test_did_you_mean.rb | 48 | ||||
| -rw-r--r-- | test/optparse/test_getopts.rb | 18 | ||||
| -rw-r--r-- | test/optparse/test_kwargs.rb | 38 | ||||
| -rw-r--r-- | test/optparse/test_load.rb | 188 | ||||
| -rw-r--r-- | test/optparse/test_noarg.rb | 42 | ||||
| -rw-r--r-- | test/optparse/test_optarg.rb | 36 | ||||
| -rw-r--r-- | test/optparse/test_optparse.rb | 187 | ||||
| -rw-r--r-- | test/optparse/test_placearg.rb | 80 | ||||
| -rw-r--r-- | test/optparse/test_reqarg.rb | 56 | ||||
| -rw-r--r-- | test/optparse/test_summary.rb | 81 | ||||
| -rw-r--r-- | test/optparse/test_switch.rb | 50 | ||||
| -rw-r--r-- | test/optparse/test_zsh_completion.rb | 21 |
16 files changed, 1159 insertions, 24 deletions
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb new file mode 100644 index 0000000000..8b578effde --- /dev/null +++ b/test/optparse/test_acceptable.rb @@ -0,0 +1,205 @@ +# frozen_string_literal: false +require_relative 'test_optparse' + +class TestOptionParserAcceptable < TestOptionParser + + def setup + super + @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 } + @opt.def_option("--octal-integer VAL", + OptionParser::OctalInteger) { |i| @octal_integer = i } + @opt.def_option("--decimal-numeric VAL", + OptionParser::DecimalNumeric) { |i| @decimal_numeric = i } + end + + def test_integer + assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0")}) + assert_equal(0, @integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0b10")}) + assert_equal(2, @integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--integer 077")}) + assert_equal(63, @integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--integer 10")}) + assert_equal(10, @integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0x3")}) + assert_equal(3, @integer) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 0b") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 09") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 0x") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 1234xyz") + end + end + + def test_float + assert_equal(%w"", no_error {@opt.parse!(%w"--float 0")}) + assert_in_epsilon(0.0, @float) + + assert_equal(%w"", no_error {@opt.parse!(%w"--float 0.0")}) + assert_in_epsilon(0.0, @float) + + assert_equal(%w"", no_error {@opt.parse!(%w"--float 1.2")}) + assert_in_epsilon(1.2, @float) + + assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E2")}) + assert_in_epsilon(100, @float) + + assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E-2")}) + assert_in_epsilon(0.01, @float) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--float 0e") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--float 1.234xyz") + end + end + + def test_numeric + assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 0")}) + assert_equal(0, @numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 0/1")}) + assert_equal(0, @numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1/2")}) + assert_equal(Rational(1, 2), @numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 010")}) + assert_equal(8, @numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1.2/2.3")}) + assert_equal(Rational(12, 23), @numeric) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--numeric 1/") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--numeric 12/34xyz") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--numeric 12x/34yz") + end + end + + def test_decimal_integer + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 0")}) + assert_equal(0, @decimal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 10")}) + assert_equal(10, @decimal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 010")}) + assert_equal(10, @decimal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 09")}) + assert_equal(9, @decimal_integer) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 0b1") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer x") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 1234xyz") + end + end + + def test_octal_integer + assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 0")}) + assert_equal(0, @octal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 6")}) + assert_equal(6, @octal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 07")}) + assert_equal(7, @octal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 10")}) + assert_equal(8, @octal_integer) + + assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 011")}) + assert_equal(9, @octal_integer) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer 09") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer 0b1") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer x") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer 01234xyz") + end + end + + def test_decimal_numeric + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 0")}) + assert_equal(0, @decimal_numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 01")}) + assert_equal(1, @decimal_numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1.2")}) + assert_in_delta(1.2, @decimal_numeric) + + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1E2")}) + assert_in_delta(100.0, @decimal_numeric) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-numeric 0b1") + end + + e = assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-numeric 09") + end + + assert_equal("invalid argument: --decimal-numeric 09", e.message) + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 1234xyz") + end + + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 12.34xyz") + end + end + + 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_autoconf.rb b/test/optparse/test_autoconf.rb new file mode 100644 index 0000000000..ec87744633 --- /dev/null +++ b/test/optparse/test_autoconf.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: false +require 'test/unit' +require 'optparse/ac' + +class TestOptionParserAutoConf < Test::Unit::TestCase + def setup + @opt = OptionParser::AC.new + @foo = @bar = self.class + @opt.ac_arg_enable("foo", "foo option") {|x| @foo = x} + @opt.ac_arg_disable("bar", "bar option") {|x| @bar = x} + @opt.ac_arg_with("zot", "zot option") {|x| @zot = x} + end + + class DummyOutput < String + alias write concat + end + def no_error(*args) + $stderr, stderr = DummyOutput.new, $stderr + assert_nothing_raised(*args) {return yield} + ensure + stderr, $stderr = $stderr, stderr + $!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $! + assert_empty(stderr) + end + + def test_enable + @opt.parse!(%w"--enable-foo") + assert_equal(true, @foo) + @opt.parse!(%w"--enable-bar") + assert_equal(true, @bar) + end + + def test_enable_value + @opt.parse!(%w"--enable-foo=A") + assert_equal("A", @foo) + @opt.parse!(%w"--enable-bar=B") + assert_equal("B", @bar) + end + + def test_disable + @opt.parse!(%w"--disable-foo") + assert_equal(false, @foo) + @opt.parse!(%w"--disable-bar") + assert_equal(false, @bar) + end + + def test_with + @opt.parse!(%w"--with-zot=foobar") + assert_equal("foobar", @zot) + @opt.parse!(%w"--without-zot") + assert_nil(@zot) + end + + def test_without + @opt.parse!(%w"--without-zot") + assert_nil(@zot) + assert_raise(OptionParser::NeedlessArgument) {@opt.parse!(%w"--without-zot=foobar")} + end + + def test_help + help = @opt.help + assert_match(/--enable-foo/, help) + assert_match(/--disable-bar/, help) + assert_match(/--with-zot/, help) + assert_not_match(/--disable-foo/, help) + assert_not_match(/--enable-bar/, help) + assert_not_match(/--without/, help) + end +end diff --git a/test/optparse/test_bash_completion.rb b/test/optparse/test_bash_completion.rb new file mode 100644 index 0000000000..60c82f7136 --- /dev/null +++ b/test/optparse/test_bash_completion.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: false +require 'test/unit' +require 'optparse' + +class TestOptionParserBashCompletion < Test::Unit::TestCase + def setup + @opt = OptionParser.new + @opt.define("-z", "zzz") {} + @opt.define("--foo") {} + @opt.define("--bar=BAR") {} + @opt.define("--for=TYPE", [:hello, :help, :zot]) {} + end + + def test_empty + assert_equal([], @opt.candidate("")) + end + + def test_one_hyphen + assert_equal(%w[-z --foo --bar= --for=], @opt.candidate("-")) + end + + def test_two_hyphen + assert_equal(%w[--foo --bar= --for=], @opt.candidate("--")) + end + + def test_long_f + assert_equal(%w[--foo --for=], @opt.candidate("--f")) + end + + def test_long_for_option + assert_equal(%w[--for=], @opt.candidate("--for")) + end + + def test_long_for_option_args + assert_equal(%w[hello help zot], @opt.candidate("--for=")) + end + + def test_long_for_option_complete + assert_equal(%w[hello help], @opt.candidate("--for=h")) + end + + def test_case_sensitive + @opt.define("-Z") {} + assert_equal(%w[-z], @opt.candidate("-z")) + end +end diff --git a/test/optparse/test_cclass.rb b/test/optparse/test_cclass.rb new file mode 100644 index 0000000000..0ded61f60e --- /dev/null +++ b/test/optparse/test_cclass.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: false +require_relative 'test_optparse' + +class TestOptionParserCClass < TestOptionParser + def test_no_argument + flags = [] + @opt.def_option("-[a-z]") {|x| flags << x} + no_error {@opt.parse!(%w"-a")} + assert_equal(%w"a", flags) + end + + def test_required_argument + flags = [] + @opt.def_option("-[a-z]X") {|x| flags << x} + no_error {@opt.parse!(%w"-a")} + assert_equal(%w"a", flags) + end +end diff --git a/test/optparse/test_did_you_mean.rb b/test/optparse/test_did_you_mean.rb new file mode 100644 index 0000000000..3c923922ec --- /dev/null +++ b/test/optparse/test_did_you_mean.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: false +require_relative 'test_optparse' +begin + require "did_you_mean" +rescue LoadError + return +end + +class TestOptionParserDidYouMean < TestOptionParser + def setup + super + @opt.def_option("--foo", Integer) { |v| @foo = v } + @opt.def_option("--bar", Integer) { |v| @bar = v } + @opt.def_option("--baz", Integer) { |v| @baz = v } + @formatter = ::DidYouMean.formatter + if ::DidYouMean.const_defined?(:Formatter) + ::DidYouMean.formatter = ::DidYouMean::Formatter + else + case @formatter + when ::DidYouMean::PlainFormatter + else + ::DidYouMean.formatter = ::DidYouMean::PlainFormatter.new + end + end + end + + def teardown + ::DidYouMean.formatter = @formatter + end + + def test_no_suggestion + assert_raise_with_message(OptionParser::InvalidOption, "invalid option: --cuz") do + @opt.permute!(%w"--cuz") + end + end + + def test_plain + assert_raise_with_message(OptionParser::InvalidOption, /invalid option: --baa\nDid you mean\?\s+bar\s+baz\Z/) do + @opt.permute!(%w"--baa") + end + end + + def test_ambiguous + assert_raise_with_message(OptionParser::AmbiguousOption, /ambiguous option: --ba\nDid you mean\?\s+bar\s+baz\Z/) do + @opt.permute!(%w"--ba") + end + end +end diff --git a/test/optparse/test_getopts.rb b/test/optparse/test_getopts.rb index fdefe5f3e5..4a0ae284e7 100644 --- a/test/optparse/test_getopts.rb +++ b/test/optparse/test_getopts.rb @@ -1,4 +1,6 @@ +# frozen_string_literal: false require 'test/unit' +require 'optparse' class TestOptionParserGetopts < Test::Unit::TestCase def setup @@ -9,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_kwargs.rb b/test/optparse/test_kwargs.rb new file mode 100644 index 0000000000..2e826bfd12 --- /dev/null +++ b/test/optparse/test_kwargs.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: false +require 'test/unit' +require 'optparse' +require 'optparse/kwargs' + +class TestOptionParserKwArg < Test::Unit::TestCase + class K + def initialize(host:, port: 8080) + @host = host + @port = port + end + end + + class DummyOutput < String + alias write concat + end + def assert_no_error(*args) + $stderr, stderr = DummyOutput.new, $stderr + assert_nothing_raised(*args) {return yield} + ensure + stderr, $stderr = $stderr, stderr + $!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $! + assert_empty(stderr) + end + alias no_error assert_no_error + + def test_kwarg + opt = OptionParser.new + options = opt.define_by_keywords({}, K.instance_method(:initialize), + port: [Integer]) + assert_raise(OptionParser::MissingArgument) {opt.parse!(%w"--host")} + assert_nothing_raised {opt.parse!(%w"--host=localhost")} + assert_equal("localhost", options[:host]) + assert_nothing_raised {opt.parse!(%w"--port")} + assert_nothing_raised {opt.parse!(%w"--port=80")} + assert_equal(80, options[:port]) + end +end diff --git a/test/optparse/test_load.rb b/test/optparse/test_load.rb new file mode 100644 index 0000000000..f664cfbf72 --- /dev/null +++ b/test/optparse/test_load.rb @@ -0,0 +1,188 @@ +# 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 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("")) + File.write(file, "--test=#{dir}") + ENV.update(env) + if block_given? + begin + yield dir, optdir + ensure + File.unlink(file) rescue nil + Dir.rmdir(optdir) rescue nil + end + else + return dir, optdir + end + end + + def setup_options_home(&block) + setup_options({}, ".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({}, ".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({}, "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 + 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, dir = 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 + + File.unlink("#{dir}/#{@basename}.options") + setup_options_home_config do + assert_load_nothing + 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 + + setup_options_xdg_config_home do |_, dir| + File.unlink("#{dir}/#{@basename}.options") + assert_load_nothing + 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 + 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_noarg.rb b/test/optparse/test_noarg.rb index 28c469093d..a53399afc2 100644 --- a/test/optparse/test_noarg.rb +++ b/test/optparse/test_noarg.rb @@ -1,8 +1,15 @@ -require 'test_optparse' +# frozen_string_literal: false +require_relative 'test_optparse' + +module TestOptionParserNoArg + def setup + super + @opt.def_option "--with_underscore" do |x| @flag = x end + @opt.def_option "--with-hyphen" do |x| @flag = x end + end -module TestOptionParser::NoArg class Def1 < TestOptionParser - include NoArg + include TestOptionParserNoArg def setup super @opt.def_option("-x") {|x| @flag = x} @@ -10,7 +17,7 @@ module TestOptionParser::NoArg end end class Def2 < TestOptionParser - include NoArg + include TestOptionParserNoArg def setup super @opt.def_option("-x", "--option") {|x| @flag = x} @@ -18,7 +25,7 @@ module TestOptionParser::NoArg end def test_short - assert_raises(OptionParser::InvalidOption) {@opt.parse!(%w"-xq")} + assert_raise(OptionParser::InvalidOption) {@opt.parse!(%w"-xq")} assert_equal(%w"", no_error {@opt.parse!(%w"-x")}) assert_equal(true, @flag) @flag = nil @@ -27,11 +34,11 @@ module TestOptionParser::NoArg end def test_abbrev - assert_raises(OptionParser::InvalidOption) {@opt.parse!(%w"-oq")} + assert_raise(OptionParser::InvalidOption) {@opt.parse!(%w"-oq")} assert_equal(%w"", no_error {@opt.parse!(%w"-o")}) assert_equal(true, @flag) @flag = nil - assert_raises(OptionParser::InvalidOption) {@opt.parse!(%w"-O")} + assert_raise(OptionParser::InvalidOption) {@opt.parse!(%w"-O")} assert_nil(@flag) @flag = nil assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")}) @@ -39,7 +46,7 @@ module TestOptionParser::NoArg end def test_long - assert_raises(OptionParser::NeedlessArgument) {@opt.parse!(%w"--option=x")} + assert_raise(OptionParser::NeedlessArgument) {@opt.parse!(%w"--option=x")} assert_equal(%w"", no_error {@opt.parse!(%w"--opt")}) assert_equal(true, @flag) @flag = nil @@ -49,9 +56,24 @@ module TestOptionParser::NoArg def test_ambiguous @opt.def_option("--open") {|x|} - assert_raises(OptionParser::AmbiguousOption) {@opt.parse!(%w"--op")} - assert_raises(OptionParser::AmbiguousOption) {@opt.parse!(%w"-o")} + assert_raise(OptionParser::AmbiguousOption) {@opt.parse!(%w"--op")} + assert_raise(OptionParser::AmbiguousOption) {@opt.parse!(%w"-o")} assert_equal(%w"", no_error {@opt.parse!(%w"--opt")}) assert_equal(true, @flag) end + + def test_hyphenize + @flag = nil + assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore")}) + assert_equal(true, @flag) + @flag = nil + assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore")}) + assert_equal(true, @flag) + @flag = nil + assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen")}) + assert_equal(true, @flag) + @flag = nil + assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen")}) + assert_equal(true, @flag) + end end diff --git a/test/optparse/test_optarg.rb b/test/optparse/test_optarg.rb index 49cb1b93ec..f94460527f 100644 --- a/test/optparse/test_optarg.rb +++ b/test/optparse/test_optarg.rb @@ -1,10 +1,17 @@ -require 'test_optparse' +# frozen_string_literal: false +require_relative 'test_optparse' -class TestOptionParser::OptArg < TestOptionParser +class TestOptionParserOptArg < TestOptionParser def setup super @opt.def_option("-x[VAL]") {|x| @flag = x} @opt.def_option("--option[=VAL]") {|x| @flag = x} + @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 def test_short @@ -41,4 +48,29 @@ class TestOptionParser::OptArg < TestOptionParser assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")}) assert_equal(nil, @flag) end + + def test_hyphenize + assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore=foo1")}) + assert_equal("foo1", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore=foo2")}) + assert_equal("foo2", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen=foo3")}) + assert_equal("foo3", @flag) + 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 9c73399f5b..ff334009a6 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require 'test/unit' require 'optparse' @@ -6,9 +7,19 @@ class TestOptionParser < Test::Unit::TestCase @opt = OptionParser.new @flag = self.class # cannot set by option end - def no_error(*args) + + class DummyOutput < String + alias write concat + end + def assert_no_error(*args) + $stderr, stderr = DummyOutput.new, $stderr assert_nothing_raised(*args) {return yield} + ensure + stderr, $stderr = $stderr, stderr + $!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $! + assert_empty(stderr) end + alias no_error assert_no_error def test_permute assert_equal(%w"", no_error {@opt.permute!(%w"")}) @@ -43,4 +54,178 @@ class TestOptionParser < Test::Unit::TestCase assert_equal(%w"foo -- --help bar", no_error {@opt.order!(%w"foo -- --help bar")}) assert_equal(self.class, @flag) end + + def test_regexp + return unless defined?(@reopt) + assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/")}) + assert_equal(/foo/, @reopt) + assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/i")}) + 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 + @opt.def_option "-h", "--host=HOST", "hostname" + @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 + @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) + 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) + 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 + @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 + + @opt.require_exact = true + [%w(--zrs foo), %w(--zrs=foo), %w(-F foo), %w(-Ffoo)].each do |args| + result = {} + @opt.parse(args, into: result) + assert_equal({zrs: 'foo'}, result) + end + + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--zr foo))} + assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--z foo))} + 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 + @opt.def_option(/^[^-]/) do |arg| + assert(false, "Never gets called") + end + 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 f4fd249b7f..d5be5a66fb 100644 --- a/test/optparse/test_placearg.rb +++ b/test/optparse/test_placearg.rb @@ -1,16 +1,31 @@ -require 'test_optparse' +# frozen_string_literal: false +require_relative 'test_optparse' -class TestOptionParser::PlaceArg < TestOptionParser +class TestOptionParserPlaceArg < TestOptionParser def setup super @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} + @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) @@ -23,6 +38,8 @@ class TestOptionParser::PlaceArg < 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) @@ -35,6 +52,8 @@ class TestOptionParser::PlaceArg < 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")}) @@ -42,4 +61,61 @@ class TestOptionParser::PlaceArg < TestOptionParser assert_equal(%w"", no_error {@opt.parse!(%w"--opt bar")}) assert_equal("bar", @flag) end + + def test_hyphenize + assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore=foo1")}) + assert_equal("foo1", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore=foo2")}) + assert_equal("foo2", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen=foo3")}) + assert_equal("foo3", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")}) + assert_equal("foo4", @flag) + end + + def test_conv + assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T te.rb")}) + assert_nil(@topt) + 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 + + 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_reqarg.rb b/test/optparse/test_reqarg.rb index 0999e5e603..31d4fef417 100644 --- a/test/optparse/test_reqarg.rb +++ b/test/optparse/test_reqarg.rb @@ -1,30 +1,40 @@ -require 'test_optparse' +# frozen_string_literal: false +require_relative 'test_optparse' + +module TestOptionParserReqArg + def setup + 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 -module TestOptionParser::ReqArg class Def1 < TestOptionParser - include ReqArg + include TestOptionParserReqArg def setup super @opt.def_option("-xVAL") {|x| @flag = x} @opt.def_option("--option=VAL") {|x| @flag = x} + @opt.def_option("--regexp=REGEXP", Regexp) {|x| @reopt = x} + @reopt = nil end end class Def2 < TestOptionParser - include ReqArg + include TestOptionParserReqArg def setup super @opt.def_option("-x", "--option=VAL") {|x| @flag = x} end end class Def3 < TestOptionParser - include ReqArg + include TestOptionParserReqArg def setup super @opt.def_option("--option=VAL", "-x") {|x| @flag = x} end end class Def4 < TestOptionParser - include ReqArg + include TestOptionParserReqArg def setup super @opt.def_option("-xVAL", "--option=VAL") {|x| @flag = x} @@ -32,7 +42,7 @@ module TestOptionParser::ReqArg end def test_short - assert_raises(OptionParser::MissingArgument) {@opt.parse!(%w"-x")} + assert_raise(OptionParser::MissingArgument) {@opt.parse!(%w"-x")} assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")}) assert_equal("foo", @flag) assert_equal(%w"", no_error {@opt.parse!(%w"-xbar")}) @@ -42,7 +52,7 @@ module TestOptionParser::ReqArg end def test_abbrev - assert_raises(OptionParser::MissingArgument) {@opt.parse!(%w"-o")} + assert_raise(OptionParser::MissingArgument) {@opt.parse!(%w"-o")} assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")}) assert_equal("foo", @flag) assert_equal(%w"", no_error {@opt.parse!(%w"-obar")}) @@ -52,7 +62,7 @@ module TestOptionParser::ReqArg end def test_long - assert_raises(OptionParser::MissingArgument) {@opt.parse!(%w"--opt")} + assert_raise(OptionParser::MissingArgument) {@opt.parse!(%w"--opt")} assert_equal(%w"", no_error {@opt.parse!(%w"--opt foo")}) assert_equal("foo", @flag) assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")}) @@ -60,4 +70,32 @@ module TestOptionParser::ReqArg assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")}) assert_equal("foo", @flag) end + + def test_hyphenize + assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore foo1")}) + assert_equal("foo1", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore foo2")}) + assert_equal("foo2", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen foo3")}) + assert_equal("foo3", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen foo4")}) + 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 + @opt.def_option("--pattern=VAL", /(\w+)(?:\s*:\s*(\w+))?/) {|x, y, z| pat = [x, y, z]} + @opt.def_option("-T NUM", /\A[1-4]\z/) {|n| num = n} + no_error {@opt.parse!(%w"--pattern=key:val")} + assert_equal(%w"key:val key val", pat, '[ruby-list:45645]') + no_error {@opt.parse!(%w"-T 4")} + assert_equal("4", num, '[ruby-dev:37514]') + end + end end diff --git a/test/optparse/test_summary.rb b/test/optparse/test_summary.rb new file mode 100644 index 0000000000..b5dcb3524e --- /dev/null +++ b/test/optparse/test_summary.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: false +require_relative 'test_optparse' + +class TestOptionParserSummaryTest < TestOptionParser + def test_short_clash + r = nil + o = OptionParser.new do |opts| + opts.on("-f", "--first-option", "description 1", "description 2"){r = "first-option"} + opts.on("-t", "--test-option"){r = "test-option"} + opts.on("-t", "--another-test-option"){r = "another-test-option"} + opts.separator "this is\nseparator" + opts.on("-l", "--last-option"){r = "last-option"} + end + s = o.summarize + o.parse("-t") + assert_match(/--#{r}/, s.grep(/^\s*-t,/)[0]) + assert_match(/first-option/, s[0]) + assert_match(/description 1/, s[0]) + assert_match(/description 2/, s[1]) + assert_match(/last-option/, s[-1]) + end + + def test_banner + o = OptionParser.new("foo bar") + assert_equal("foo bar", o.banner) + end + + def test_banner_from_progname + o = OptionParser.new + o.program_name = "foobar" + assert_equal("Usage: foobar [options]\n", o.help) + end + + def test_summary + o = OptionParser.new("foo\nbar") + assert_equal("foo\nbar\n", o.to_s) + assert_equal(["foo\n", "bar"], o.to_a) + end + + def test_summary_containing_space + # test for r35467. OptionParser#to_a shouldn't split str by spaces. + bug6348 = '[ruby-dev:45568]' + o = OptionParser.new("foo bar") + assert_equal("foo bar\n", o.to_s, bug6348) + assert_equal(["foo bar"], o.to_a, bug6348) + end + + def test_ver + o = OptionParser.new("foo bar") + o.program_name = "foo" + assert_warning('') {assert_nil(o.version)} + assert_warning('') {assert_nil(o.release)} + o.version = [0, 1] + assert_equal "foo 0.1", o.ver + 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 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 diff --git a/test/optparse/test_zsh_completion.rb b/test/optparse/test_zsh_completion.rb new file mode 100644 index 0000000000..76f0a73f32 --- /dev/null +++ b/test/optparse/test_zsh_completion.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: false +require 'test/unit' +require 'optparse' + +class TestOptionParserZshCompletion < Test::Unit::TestCase + def setup + @opt = OptionParser.new + @opt.define("-z", "zzz") {} + @opt.define("--foo") {} + @opt.define("--bar=BAR") {} + @opt.define("--for=TYPE", [:hello, :help, :zot]) {} + end + + def test_compsys + compsys = @opt.compsys("", "zshcompsys") + assert_match(/\"-z\[zzz\]\"/, compsys) + assert_match(/\"--foo\[\]\"/, compsys) + assert_match(/\"--bar\[\]\"/, compsys) + assert_match(/\"--for\[\]\"/, compsys) + end +end |
