diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/test/optparse |
Fresh start
Diffstat (limited to 'jni/ruby/test/optparse')
-rw-r--r-- | jni/ruby/test/optparse/test_acceptable.rb | 195 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_autoconf.rb | 63 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_bash_completion.rb | 42 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_getopts.rb | 34 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_noarg.rb | 57 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_optarg.rb | 46 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_optparse.rb | 66 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_placearg.rb | 56 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_reqarg.rb | 77 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_summary.rb | 46 | ||||
-rw-r--r-- | jni/ruby/test/optparse/test_zsh_completion.rb | 22 |
11 files changed, 704 insertions, 0 deletions
diff --git a/jni/ruby/test/optparse/test_acceptable.rb b/jni/ruby/test/optparse/test_acceptable.rb new file mode 100644 index 0000000..6ec619e --- /dev/null +++ b/jni/ruby/test/optparse/test_acceptable.rb @@ -0,0 +1,195 @@ +require_relative 'test_optparse' + +class TestOptionParser::Acceptable < 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("--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_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 0b") + end + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 09") + end + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--integer 0x") + end + + assert_raises(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_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--float 0e") + end + + assert_raises(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 1.2/2.3")}) + assert_equal(Rational(12, 23), @numeric) + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--numeric 1/") + end + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--numeric 12/34xyz") + end + + assert_raises(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_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 0b1") + end + + e = assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 09") + end + + assert_equal("invalid argument: --decimal-integer 09", e.message) + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer x") + end + + assert_raises(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_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer 09") + end + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer 0b1") + end + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--octal-integer x") + end + + assert_raises(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_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-numeric 0b1") + end + + e = assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-numeric 09") + end + + assert_equal("invalid argument: --decimal-numeric 09", e.message) + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 1234xyz") + end + + assert_raises(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 12.34xyz") + end + end + +end + diff --git a/jni/ruby/test/optparse/test_autoconf.rb b/jni/ruby/test/optparse/test_autoconf.rb new file mode 100644 index 0000000..cb9c938 --- /dev/null +++ b/jni/ruby/test/optparse/test_autoconf.rb @@ -0,0 +1,63 @@ +require 'test/unit' +require 'optparse/ac' + +class TestOptionParser < Test::Unit::TestCase; end + +class TestOptionParser::AutoConf < 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 << + 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_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/jni/ruby/test/optparse/test_bash_completion.rb b/jni/ruby/test/optparse/test_bash_completion.rb new file mode 100644 index 0000000..baeb6d9 --- /dev/null +++ b/jni/ruby/test/optparse/test_bash_completion.rb @@ -0,0 +1,42 @@ +require 'test/unit' +require 'optparse' + +class TestOptionParser < Test::Unit::TestCase +end +class TestOptionParser::BashCompletion < 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 +end diff --git a/jni/ruby/test/optparse/test_getopts.rb b/jni/ruby/test/optparse/test_getopts.rb new file mode 100644 index 0000000..ae22f68 --- /dev/null +++ b/jni/ruby/test/optparse/test_getopts.rb @@ -0,0 +1,34 @@ +require 'test/unit' +require 'optparse' + +class TestOptionParser < Test::Unit::TestCase +end +class TestOptionParser::Getopts < Test::Unit::TestCase + def setup + @opt = OptionParser.new + end + + def test_short_noarg + o = @opt.getopts(%w[-a], "ab") + 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']) + end + + def test_long_noarg + o = @opt.getopts(%w[--foo], "", "foo", "bar") + 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']) + end +end diff --git a/jni/ruby/test/optparse/test_noarg.rb b/jni/ruby/test/optparse/test_noarg.rb new file mode 100644 index 0000000..3e6ed42 --- /dev/null +++ b/jni/ruby/test/optparse/test_noarg.rb @@ -0,0 +1,57 @@ +require_relative 'test_optparse' + +module TestOptionParser::NoArg + class Def1 < TestOptionParser + include NoArg + def setup + super + @opt.def_option("-x") {|x| @flag = x} + @opt.def_option("--option") {|x| @flag = x} + end + end + class Def2 < TestOptionParser + include NoArg + def setup + super + @opt.def_option("-x", "--option") {|x| @flag = x} + end + end + + def test_short + assert_raise(OptionParser::InvalidOption) {@opt.parse!(%w"-xq")} + assert_equal(%w"", no_error {@opt.parse!(%w"-x")}) + assert_equal(true, @flag) + @flag = nil + assert_equal(%w"foo", no_error {@opt.parse!(%w"-x foo")}) + assert_equal(true, @flag) + end + + def test_abbrev + assert_raise(OptionParser::InvalidOption) {@opt.parse!(%w"-oq")} + assert_equal(%w"", no_error {@opt.parse!(%w"-o")}) + assert_equal(true, @flag) + @flag = nil + assert_raise(OptionParser::InvalidOption) {@opt.parse!(%w"-O")} + assert_nil(@flag) + @flag = nil + assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")}) + assert_equal(true, @flag) + end + + def test_long + assert_raise(OptionParser::NeedlessArgument) {@opt.parse!(%w"--option=x")} + assert_equal(%w"", no_error {@opt.parse!(%w"--opt")}) + assert_equal(true, @flag) + @flag = nil + assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")}) + assert_equal(true, @flag) + end + + def test_ambiguous + @opt.def_option("--open") {|x|} + 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 +end diff --git a/jni/ruby/test/optparse/test_optarg.rb b/jni/ruby/test/optparse/test_optarg.rb new file mode 100644 index 0000000..3114b80 --- /dev/null +++ b/jni/ruby/test/optparse/test_optarg.rb @@ -0,0 +1,46 @@ +require_relative 'test_optparse' + +class TestOptionParser::OptArg < 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} + @reopt = nil + end + + def test_short + assert_equal(%w"", no_error {@opt.parse!(%w"-x")}) + assert_equal(nil, @flag) + @flag = false + assert_equal(%w"foo", no_error {@opt.parse!(%w"-x foo")}) + assert_equal(nil, @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-xfoo")}) + assert_equal("foo", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-x=")}) + assert_equal("=", @flag) + end + + def test_abbrev + assert_equal(%w"", no_error {@opt.parse!(%w"-o")}) + assert_equal(nil, @flag) + @flag = false + assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")}) + assert_equal(nil, @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-ofoo")}) + assert_equal("foo", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-o=")}) + assert_equal("=", @flag) + end + + def test_long + assert_equal(%w"", no_error {@opt.parse!(%w"--opt")}) + assert_equal(nil, @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")}) + assert_equal("foo", @flag) + assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")}) + assert_equal(nil, @flag) + end +end diff --git a/jni/ruby/test/optparse/test_optparse.rb b/jni/ruby/test/optparse/test_optparse.rb new file mode 100644 index 0000000..e85a2ef --- /dev/null +++ b/jni/ruby/test/optparse/test_optparse.rb @@ -0,0 +1,66 @@ +require 'test/unit' +require 'optparse' + +class TestOptionParser < Test::Unit::TestCase + def setup + @opt = OptionParser.new + @flag = self.class # cannot set by option + end + + class DummyOutput < String + alias write << + 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"")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo bar", no_error {@opt.permute!(%w"foo bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"- foo bar", no_error {@opt.permute!(%w"- foo bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo bar", no_error {@opt.permute!(%w"-- foo bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo - bar", no_error {@opt.permute!(%w"foo - bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo bar", no_error {@opt.permute!(%w"foo -- bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo --help bar", no_error {@opt.permute!(%w"foo -- --help bar")}) + assert_equal(self.class, @flag) + end + + def test_order + assert_equal(%w"", no_error {@opt.order!(%w"")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo bar", no_error {@opt.order!(%w"foo bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"- foo bar", no_error {@opt.order!(%w"- foo bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo bar", no_error {@opt.permute!(%w"-- foo bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo - bar", no_error {@opt.order!(%w"foo - bar")}) + assert_equal(self.class, @flag) + assert_equal(%w"foo -- bar", no_error {@opt.order!(%w"foo -- bar")}) + assert_equal(self.class, @flag) + 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) + end +end diff --git a/jni/ruby/test/optparse/test_placearg.rb b/jni/ruby/test/optparse/test_placearg.rb new file mode 100644 index 0000000..0bbd1a0 --- /dev/null +++ b/jni/ruby/test/optparse/test_placearg.rb @@ -0,0 +1,56 @@ +require_relative 'test_optparse' + +class TestOptionParser::PlaceArg < 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} + @topt = nil + @opt.def_option("-n") {} + @opt.def_option("--regexp [REGEXP]", Regexp) {|x| @reopt = x} + @reopt = nil + end + + def test_short + assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")}) + assert_equal(nil, @flag) + @flag = false + assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")}) + assert_equal("foo", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-xbar")}) + assert_equal("bar", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-x=")}) + assert_equal("=", @flag) + end + + def test_abbrev + assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")}) + assert_equal(nil, @flag) + @flag = false + assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")}) + assert_equal("foo", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-obar")}) + assert_equal("bar", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-o=")}) + assert_equal("=", @flag) + end + + def test_long + assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")}) + assert_equal(nil, @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")}) + assert_equal("foo", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--opt bar")}) + assert_equal("bar", @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 +end diff --git a/jni/ruby/test/optparse/test_reqarg.rb b/jni/ruby/test/optparse/test_reqarg.rb new file mode 100644 index 0000000..397da4a --- /dev/null +++ b/jni/ruby/test/optparse/test_reqarg.rb @@ -0,0 +1,77 @@ +require_relative 'test_optparse' + +module TestOptionParser::ReqArg + class Def1 < TestOptionParser + include ReqArg + 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 + def setup + super + @opt.def_option("-x", "--option=VAL") {|x| @flag = x} + end + end + class Def3 < TestOptionParser + include ReqArg + def setup + super + @opt.def_option("--option=VAL", "-x") {|x| @flag = x} + end + end + class Def4 < TestOptionParser + include ReqArg + def setup + super + @opt.def_option("-xVAL", "--option=VAL") {|x| @flag = x} + end + end + + def test_short + 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")}) + assert_equal("bar", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-x=")}) + assert_equal("=", @flag) + end + + def test_abbrev + 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")}) + assert_equal("bar", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"-o=")}) + assert_equal("=", @flag) + end + + def test_long + 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")}) + assert_equal("", @flag) + assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")}) + assert_equal("foo", @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/jni/ruby/test/optparse/test_summary.rb b/jni/ruby/test/optparse/test_summary.rb new file mode 100644 index 0000000..54fd194 --- /dev/null +++ b/jni/ruby/test/optparse/test_summary.rb @@ -0,0 +1,46 @@ +require_relative 'test_optparse' + +class TestOptionParser::SummaryTest < 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 +end diff --git a/jni/ruby/test/optparse/test_zsh_completion.rb b/jni/ruby/test/optparse/test_zsh_completion.rb new file mode 100644 index 0000000..7e5ba71 --- /dev/null +++ b/jni/ruby/test/optparse/test_zsh_completion.rb @@ -0,0 +1,22 @@ +require 'test/unit' +require 'optparse' + +class TestOptionParser < Test::Unit::TestCase +end +class TestOptionParser::BashCompletion < 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 |