diff options
Diffstat (limited to 'test/optparse')
| -rw-r--r-- | test/optparse/test_acceptable.rb | 59 | ||||
| -rw-r--r-- | test/optparse/test_autoconf.rb | 14 | ||||
| -rw-r--r-- | test/optparse/test_bash_completion.rb | 10 | ||||
| -rw-r--r-- | test/optparse/test_cclass.rb | 2 | ||||
| -rw-r--r-- | test/optparse/test_did_you_mean.rb | 40 | ||||
| -rw-r--r-- | test/optparse/test_getopts.rb | 5 | ||||
| -rw-r--r-- | test/optparse/test_kwargs.rb | 38 | ||||
| -rw-r--r-- | test/optparse/test_noarg.rb | 28 | ||||
| -rw-r--r-- | test/optparse/test_optarg.rb | 16 | ||||
| -rw-r--r-- | test/optparse/test_optparse.rb | 44 | ||||
| -rw-r--r-- | test/optparse/test_placearg.rb | 16 | ||||
| -rw-r--r-- | test/optparse/test_reqarg.rb | 28 | ||||
| -rw-r--r-- | test/optparse/test_summary.rb | 14 | ||||
| -rw-r--r-- | test/optparse/test_zsh_completion.rb | 5 |
14 files changed, 265 insertions, 54 deletions
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb index 6ec619ef8c..12f5322726 100644 --- a/test/optparse/test_acceptable.rb +++ b/test/optparse/test_acceptable.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: false require_relative 'test_optparse' -class TestOptionParser::Acceptable < TestOptionParser +class TestOptionParserAcceptable < TestOptionParser def setup super @@ -32,19 +33,19 @@ class TestOptionParser::Acceptable < TestOptionParser assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0x3")}) assert_equal(3, @integer) - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--integer 0b") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--integer 09") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--integer 0x") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--integer 1234xyz") end end @@ -65,11 +66,11 @@ class TestOptionParser::Acceptable < TestOptionParser assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E-2")}) assert_in_epsilon(0.01, @float) - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--float 0e") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--float 1.234xyz") end end @@ -84,18 +85,21 @@ class TestOptionParser::Acceptable < TestOptionParser 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_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--numeric 1/") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--numeric 12/34xyz") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--numeric 12x/34yz") end end @@ -107,21 +111,21 @@ class TestOptionParser::Acceptable < TestOptionParser 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 + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 010")}) + assert_equal(10, @decimal_integer) - e = assert_raises(OptionParser::InvalidArgument) do - @opt.parse!(%w"--decimal-integer 09") - end + assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 09")}) + assert_equal(9, @decimal_integer) - assert_equal("invalid argument: --decimal-integer 09", e.message) + assert_raise(OptionParser::InvalidArgument) do + @opt.parse!(%w"--decimal-integer 0b1") + end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--decimal-integer x") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--decimal-integer 1234xyz") end end @@ -142,19 +146,19 @@ class TestOptionParser::Acceptable < TestOptionParser assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 011")}) assert_equal(9, @octal_integer) - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--octal-integer 09") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--octal-integer 0b1") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--octal-integer x") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--octal-integer 01234xyz") end end @@ -172,24 +176,23 @@ class TestOptionParser::Acceptable < TestOptionParser assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1E2")}) assert_in_delta(100.0, @decimal_numeric) - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--decimal-numeric 0b1") end - e = assert_raises(OptionParser::InvalidArgument) do + e = assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--decimal-numeric 09") end assert_equal("invalid argument: --decimal-numeric 09", e.message) - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--decimal-integer 1234xyz") end - assert_raises(OptionParser::InvalidArgument) do + assert_raise(OptionParser::InvalidArgument) do @opt.parse!(%w"--decimal-integer 12.34xyz") end end end - diff --git a/test/optparse/test_autoconf.rb b/test/optparse/test_autoconf.rb index cb9c938609..ec87744633 100644 --- a/test/optparse/test_autoconf.rb +++ b/test/optparse/test_autoconf.rb @@ -1,9 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'optparse/ac' -class TestOptionParser < Test::Unit::TestCase; end - -class TestOptionParser::AutoConf < Test::Unit::TestCase +class TestOptionParserAutoConf < Test::Unit::TestCase def setup @opt = OptionParser::AC.new @foo = @bar = self.class @@ -13,7 +12,7 @@ class TestOptionParser::AutoConf < Test::Unit::TestCase end class DummyOutput < String - alias write << + alias write concat end def no_error(*args) $stderr, stderr = DummyOutput.new, $stderr @@ -31,6 +30,13 @@ class TestOptionParser::AutoConf < Test::Unit::TestCase 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) diff --git a/test/optparse/test_bash_completion.rb b/test/optparse/test_bash_completion.rb index baeb6d9882..60c82f7136 100644 --- a/test/optparse/test_bash_completion.rb +++ b/test/optparse/test_bash_completion.rb @@ -1,9 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'optparse' -class TestOptionParser < Test::Unit::TestCase -end -class TestOptionParser::BashCompletion < Test::Unit::TestCase +class TestOptionParserBashCompletion < Test::Unit::TestCase def setup @opt = OptionParser.new @opt.define("-z", "zzz") {} @@ -39,4 +38,9 @@ class TestOptionParser::BashCompletion < Test::Unit::TestCase 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 index ac46f46bb2..0ded61f60e 100644 --- a/test/optparse/test_cclass.rb +++ b/test/optparse/test_cclass.rb @@ -1,7 +1,7 @@ # frozen_string_literal: false require_relative 'test_optparse' -class TestOptionParser::CClass < TestOptionParser +class TestOptionParserCClass < TestOptionParser def test_no_argument flags = [] @opt.def_option("-[a-z]") {|x| flags << x} diff --git a/test/optparse/test_did_you_mean.rb b/test/optparse/test_did_you_mean.rb new file mode 100644 index 0000000000..d1514b73eb --- /dev/null +++ b/test/optparse/test_did_you_mean.rb @@ -0,0 +1,40 @@ +# 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 + ::DidYouMean.formatter = ::DidYouMean::Formatter + 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 ae22f68184..7d9160f7af 100644 --- a/test/optparse/test_getopts.rb +++ b/test/optparse/test_getopts.rb @@ -1,9 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'optparse' -class TestOptionParser < Test::Unit::TestCase -end -class TestOptionParser::Getopts < Test::Unit::TestCase +class TestOptionParserGetopts < Test::Unit::TestCase def setup @opt = OptionParser.new 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_noarg.rb b/test/optparse/test_noarg.rb index 3e6ed42f91..a53399afc2 100644 --- a/test/optparse/test_noarg.rb +++ b/test/optparse/test_noarg.rb @@ -1,8 +1,15 @@ +# frozen_string_literal: false require_relative 'test_optparse' -module TestOptionParser::NoArg +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 + 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} @@ -54,4 +61,19 @@ module TestOptionParser::NoArg 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 3114b80fc5..81127a8a37 100644 --- a/test/optparse/test_optarg.rb +++ b/test/optparse/test_optarg.rb @@ -1,11 +1,14 @@ +# 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 @reopt = nil end @@ -43,4 +46,15 @@ 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 end diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb index e85a2ef586..5f5ea183b0 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' @@ -8,7 +9,7 @@ class TestOptionParser < Test::Unit::TestCase end class DummyOutput < String - alias write << + alias write concat end def assert_no_error(*args) $stderr, stderr = DummyOutput.new, $stderr @@ -63,4 +64,45 @@ class TestOptionParser < Test::Unit::TestCase assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/n")}) assert_equal(/foo/n, @reopt) 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 + result = {} + @opt.parse %w(--host localhost --port 8000 -v), into: result + assert_equal({host: "localhost", port: 8000, verbose: true}, result) + assert_equal(true, @verbose) + 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 -F -Ffoo).each do |arg| + result = {} + @opt.parse([arg, 'foo'], 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))} + 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 end diff --git a/test/optparse/test_placearg.rb b/test/optparse/test_placearg.rb index 0bbd1a007e..94cfb0e819 100644 --- a/test/optparse/test_placearg.rb +++ b/test/optparse/test_placearg.rb @@ -1,6 +1,7 @@ +# 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} @@ -10,6 +11,8 @@ class TestOptionParser::PlaceArg < TestOptionParser @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 end def test_short @@ -47,6 +50,17 @@ class TestOptionParser::PlaceArg < TestOptionParser 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) diff --git a/test/optparse/test_reqarg.rb b/test/optparse/test_reqarg.rb index 397da4a593..d5686d13aa 100644 --- a/test/optparse/test_reqarg.rb +++ b/test/optparse/test_reqarg.rb @@ -1,8 +1,15 @@ +# frozen_string_literal: false require_relative 'test_optparse' -module TestOptionParser::ReqArg +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 + end + class Def1 < TestOptionParser - include ReqArg + include TestOptionParserReqArg def setup super @opt.def_option("-xVAL") {|x| @flag = x} @@ -12,21 +19,21 @@ module TestOptionParser::ReqArg 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} @@ -63,6 +70,17 @@ module TestOptionParser::ReqArg 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 + class TestOptionParser::WithPattern < TestOptionParser def test_pattern pat = num = nil diff --git a/test/optparse/test_summary.rb b/test/optparse/test_summary.rb index 54fd194bbd..6b36ce3c76 100644 --- a/test/optparse/test_summary.rb +++ b/test/optparse/test_summary.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: false require_relative 'test_optparse' -class TestOptionParser::SummaryTest < TestOptionParser +class TestOptionParserSummaryTest < TestOptionParser def test_short_clash r = nil o = OptionParser.new do |opts| @@ -43,4 +44,15 @@ class TestOptionParser::SummaryTest < TestOptionParser 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 end diff --git a/test/optparse/test_zsh_completion.rb b/test/optparse/test_zsh_completion.rb index 7e5ba71384..76f0a73f32 100644 --- a/test/optparse/test_zsh_completion.rb +++ b/test/optparse/test_zsh_completion.rb @@ -1,9 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'optparse' -class TestOptionParser < Test::Unit::TestCase -end -class TestOptionParser::BashCompletion < Test::Unit::TestCase +class TestOptionParserZshCompletion < Test::Unit::TestCase def setup @opt = OptionParser.new @opt.define("-z", "zzz") {} |
