Skip to content

Commit d5c5e87

Browse files
Add tests for previously untested code paths.
1 parent 94f4c8f commit d5c5e87

File tree

1 file changed

+128
-8
lines changed

1 file changed

+128
-8
lines changed

test/test_getoptlong.rb

Lines changed: 128 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33

44
class TestGetoptLong < Test::Unit::TestCase
55

6-
def getoptlong_new(argv)
7-
GetoptLong.new(
6+
def getoptlong_new(argv: [], env: nil, options: nil)
7+
options ||= [
88
['--xxx', '-x', '--aaa', '-a', GetoptLong::REQUIRED_ARGUMENT],
99
['--yyy', '-y', '--bbb', '-b', GetoptLong::OPTIONAL_ARGUMENT],
10-
['--zzz', '-z', '--ccc', '-c', GetoptLong::NO_ARGUMENT],
11-
argv:
12-
)
10+
['--zzz', '-z', '--ccc', '-c', GetoptLong::NO_ARGUMENT]
11+
]
12+
env ||= ENV
13+
14+
GetoptLong.new(*options, argv:, env:)
15+
.tap { |opts| opts.quiet = true }
1316
end
1417

15-
def verify(test_argv, expected_remaining_argv, expected_options)
18+
def verify(test_argv, expected_remaining_argv, expected_options, env = nil)
1619
# Define options.
17-
opts = getoptlong_new(test_argv)
18-
opts.quiet = true
20+
opts = getoptlong_new(argv: test_argv, env:)
21+
yield opts if block_given?
22+
1923
# Gather options.
2024
actual_options = []
2125
opts.each do |opt, arg|
@@ -160,4 +164,120 @@ def test_new_with_invalid_flag
160164
end
161165
end
162166

167+
def test_raise_ambiguous_option
168+
e = assert_raise(GetoptLong::AmbiguousOption) do
169+
options = [
170+
['--xxx', GetoptLong::REQUIRED_ARGUMENT],
171+
['--xxy', GetoptLong::NO_ARGUMENT]
172+
]
173+
getoptlong_new(argv: ['--xx'], options:).each { nil }
174+
end
175+
assert_match(/ambiguous/, e.message)
176+
end
177+
178+
def test_option_prefix
179+
assert_nothing_raised do
180+
options = [
181+
['--xxx', GetoptLong::NO_ARGUMENT],
182+
['--xxx-y', GetoptLong::NO_ARGUMENT],
183+
['--xx', GetoptLong::NO_ARGUMENT]
184+
]
185+
getoptlong_new(argv: ['--xx', '--xxx'], options:).each { nil }
186+
end
187+
end
188+
189+
def test_raise_needless_argument
190+
e = assert_raise(GetoptLong::NeedlessArgument) do
191+
options = [['--x', GetoptLong::NO_ARGUMENT]]
192+
getoptlong_new(argv: ['--x=z'], options:).each { nil }
193+
end
194+
assert_match(/doesn't allow an argument/, e.message)
195+
end
196+
197+
def test_raise_too_many_flags
198+
e = assert_raise(ArgumentError) do
199+
options = [
200+
['-y', GetoptLong::REQUIRED_ARGUMENT, GetoptLong::NO_ARGUMENT]
201+
]
202+
getoptlong_new(options:)
203+
end
204+
assert_match(/too many/, e.message)
205+
end
206+
207+
def test_raise_option_redefined
208+
e = assert_raise(ArgumentError) do
209+
options = [
210+
['--xxx', '-x', GetoptLong::REQUIRED_ARGUMENT],
211+
['--exclude', '-x', GetoptLong::NO_ARGUMENT]
212+
]
213+
getoptlong_new(options:)
214+
end
215+
assert_match(/redefined/, e.message)
216+
end
217+
218+
def test_set_ordering_raise
219+
e = assert_raise(ArgumentError) do
220+
getoptlong_new.ordering = 42
221+
end
222+
assert_match(/invalid ordering/, e.message)
223+
end
224+
225+
def test_raise_unrecognized_option
226+
e = assert_raise(GetoptLong::InvalidOption) do
227+
getoptlong_new(argv: ['--asdf']).each { nil }
228+
end
229+
assert_match(/unrecognized option/, e.message)
230+
end
231+
232+
def test_ordering
233+
GetoptLong::ORDERINGS.each do |order|
234+
argv = ['foo', '--xxx', 'arg', 'bar']
235+
236+
expected_options, expected_argv =
237+
case order
238+
when GetoptLong::REQUIRE_ORDER
239+
[[], %w[foo --xxx arg bar]]
240+
when GetoptLong::PERMUTE
241+
[['--xxx: arg'], %w[foo bar]]
242+
when GetoptLong::RETURN_IN_ORDER
243+
[[': foo', '--xxx: arg', ': bar'], []]
244+
end
245+
246+
verify(argv, expected_argv, expected_options) do |opts|
247+
opts.ordering = order
248+
end
249+
end
250+
end
251+
252+
def test_env_posixly_correct
253+
[{}, { 'POSIXLY_CORRECT' => '1' }].each do |order|
254+
argv = ['foo', '--xxx', 'arg', 'bar']
255+
256+
expected_options, expected_argv =
257+
case order
258+
when {}
259+
[['--xxx: arg'], %w[foo bar]]
260+
else
261+
[[], %w[foo --xxx arg bar]]
262+
end
263+
264+
verify(argv, expected_argv, expected_options, order)
265+
end
266+
end
267+
268+
def test_raise_invalid_option_with_single_hyphen
269+
options = [
270+
['-x', GetoptLong::NO_ARGUMENT],
271+
['--x', GetoptLong::NO_ARGUMENT]
272+
]
273+
argvs = [%w[-x-x foo], %w[-x- foo]]
274+
275+
argvs.each do |argv|
276+
e = assert_raise(GetoptLong::InvalidOption) do
277+
getoptlong_new(argv:, options:).each { nil }
278+
end
279+
assert_match(/invalid option/, e.message)
280+
end
281+
end
282+
163283
end

0 commit comments

Comments
 (0)