summaryrefslogtreecommitdiff
path: root/test/optparse/test_switch.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/optparse/test_switch.rb')
-rw-r--r--test/optparse/test_switch.rb50
1 files changed, 50 insertions, 0 deletions
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