mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
[ruby/optparse] Enhance to support 'Set' object as an enum
(https://github.com/ruby/optparse/pull/76)
* Enhance to support 'Set' object as an enum
* Add test script for '#make_swithc()'
---------
3869000e98
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
parent
41242560b6
commit
9fd793e0bd
2 changed files with 52 additions and 1 deletions
|
@ -7,6 +7,7 @@
|
|||
#
|
||||
# See OptionParser for documentation.
|
||||
#
|
||||
require 'set' unless defined?(Set)
|
||||
|
||||
#--
|
||||
# == Developer Documentation (not for RDoc output)
|
||||
|
@ -1500,7 +1501,7 @@ XXX
|
|||
case o
|
||||
when Proc, Method
|
||||
block = notwice(o, block, 'block')
|
||||
when Array, Hash
|
||||
when Array, Hash, Set
|
||||
if Array === o
|
||||
o, v = o.partition {|v,| Completion.completable?(v)}
|
||||
values = notwice(v, values, 'values') unless v.empty?
|
||||
|
|
50
test/optparse/test_switch.rb
Normal file
50
test/optparse/test_switch.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue