Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Enhance to support 'Set' object as an enum #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
# See OptionParser for documentation.
#
require 'set' unless defined?(Set)

#--
# == Developer Documentation (not for RDoc output)
Expand Down Expand Up @@ -1494,7 +1495,7 @@ def make_switch(opts, block = nil)
case o
when Proc, Method
block = notwice(o, block, 'block')
when Array, Hash
when Array, Hash, Set
case pattern
when CompletingHash
when nil
Expand Down
50 changes: 50 additions & 0 deletions test/optparse/test_switch.rb
Original file line number Diff line number Diff line change
@@ -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