4 class TestGetoptLong < Test::Unit::TestCase
6 def verify(test_argv, expected_remaining_argv, expected_options)
7 # Save ARGV and replace it with a test ARGV.
9 ARGV.replace(test_argv)
11 opts = GetoptLong.new(
12 ['--xxx', '-x', '--aaa', '-a', GetoptLong::REQUIRED_ARGUMENT],
13 ['--yyy', '-y', '--bbb', '-b', GetoptLong::OPTIONAL_ARGUMENT],
14 ['--zzz', '-z', '--ccc', '-c', GetoptLong::NO_ARGUMENT]
19 opts.each do |opt, arg|
20 actual_options << "#{opt}: #{arg}"
22 # Save remaining test ARGV and restore original ARGV.
23 actual_remaining_argv = ARGV.dup
24 ARGV.replace(argv_saved)
26 assert_equal(expected_remaining_argv, actual_remaining_argv, 'ARGV')
27 assert_equal(expected_options, actual_options, 'Options')
32 expected_argv = %w[foo bar]
34 verify(argv, expected_argv, expected_options)
37 def test_required_argument
41 expected_argv = %w[foo bar]
42 options = %w[--xxx --xx --x -x --aaa --aa --a -a]
43 options.each do |option|
44 argv = ['foo', option, 'arg', 'bar']
45 verify(argv, expected_argv, expected_options)
49 def test_required_argument_missing
50 options = %w[--xxx --xx --x -x --aaa --aa --a -a]
51 options.each do |option|
53 e = assert_raise(GetoptLong::MissingArgument) do
56 assert_match('requires an argument', e.message)
60 def test_optional_argument
64 expected_argv = %w[foo bar]
65 options = %w[--yyy --y --y -y --bbb --bb --b -b]
66 options.each do |option|
67 argv = ['foo', 'bar', option, 'arg']
68 verify(argv, expected_argv, expected_options)
72 def test_optional_argument_missing
76 expected_argv = %w[foo bar]
77 options = %w[--yyy --y --y -y --bbb --bb --b -b]
78 options.each do |option|
79 argv = ['foo', 'bar', option]
80 verify(argv, expected_argv, expected_options)
88 expected_argv = %w[foo bar]
89 options = %w[--zzz --zz --z -z --ccc --cc --c -c]
90 options.each do |option|
91 argv = ['foo', option, 'bar']
92 verify(argv, expected_argv, expected_options)
96 def test_new_with_empty_array
97 e = assert_raise(ArgumentError) do
100 assert_match(/no argument-flag/, e.message)
103 def test_new_with_bad_array
104 e = assert_raise(ArgumentError) do
105 GetoptLong.new('foo')
107 assert_match(/option list contains non-Array argument/, e.message)
110 def test_new_with_empty_subarray
111 e = assert_raise(ArgumentError) do
114 assert_match(/no argument-flag/, e.message)
117 def test_new_with_bad_subarray
118 e = assert_raise(ArgumentError) do
121 assert_match(/no option name/, e.message)
124 def test_new_with_invalid_option
125 invalid_options = %w[verbose -verbose -- +]
126 invalid_options.each do |invalid_option|
127 e = assert_raise(ArgumentError, invalid_option.to_s) do
129 [invalid_option, '-v', GetoptLong::NO_ARGUMENT]
131 GetoptLong.new(*arguments)
133 assert_match(/invalid option/, e.message)
137 def test_new_with_invalid_alias
138 invalid_aliases = %w[v - -- +]
139 invalid_aliases.each do |invalid_alias|
140 e = assert_raise(ArgumentError, invalid_alias.to_s) do
142 ['--verbose', invalid_alias, GetoptLong::NO_ARGUMENT]
144 GetoptLong.new(*arguments)
146 assert_match(/invalid option/, e.message)
150 def test_new_with_invalid_flag
151 invalid_flags = ['foo']
152 invalid_flags.each do |invalid_flag|
153 e = assert_raise(ArgumentError, invalid_flag.to_s) do
155 ['--verbose', '-v', invalid_flag]
157 GetoptLong.new(*arguments)
159 assert_match(/no argument-flag/, e.message)