Skip to content

Commit 18cdb03

Browse files
Write a first test of getoptlong.rb
1 parent 0babcae commit 18cdb03

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

Rakefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
require "bundler/gem_tasks"
1+
require 'bundler/gem_tasks'
2+
require 'rake/testtask'
3+
4+
Rake::TestTask.new(:test) do |t|
5+
t.libs << 'test'
6+
t.libs << 'lib'
7+
t.test_files = FileList['test/test_*.rb']
8+
end
29

310
task :default => :test

test/test_getoptlong.rb

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
require 'test/unit'
2+
require 'getoptlong'
3+
4+
class TestGetoptLong < Test::Unit::TestCase
5+
6+
def verify(test_argv, expected_remaining_argv, expected_options)
7+
# Save ARGV and replace it with a test ARGV.
8+
argv_saved = ARGV
9+
ARGV.replace(test_argv)
10+
# Define options.
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]
15+
)
16+
opts.quiet = true
17+
# Gather options.
18+
actual_options = []
19+
opts.each do |opt, arg|
20+
actual_options << "#{opt}: #{arg}"
21+
end
22+
# Save remaining test ARGV and restore original ARGV.
23+
actual_remaining_argv = ARGV
24+
ARGV.replace(argv_saved)
25+
# Assert.
26+
assert_equal(expected_remaining_argv, actual_remaining_argv, 'ARGV')
27+
assert_equal(expected_options, actual_options, 'Options')
28+
end
29+
30+
def test_no_options
31+
expected_options = []
32+
expected_argv = %w[foo bar]
33+
argv = %w[foo bar]
34+
verify(argv, expected_argv, expected_options)
35+
end
36+
37+
def test_required_argument
38+
expected_options = [
39+
'--xxx: arg'
40+
]
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)
46+
end
47+
end
48+
49+
def test_required_argument_missing
50+
options = %w[--xxx --xx --x -x --aaa --aa --a -a]
51+
options.each do |option|
52+
argv = [option]
53+
e = assert_raise(GetoptLong::MissingArgument) do
54+
verify(argv, [], [])
55+
end
56+
assert_match('requires an argument', e.message)
57+
end
58+
end
59+
60+
def test_optional_argument
61+
expected_options = [
62+
'--yyy: arg'
63+
]
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)
69+
end
70+
end
71+
72+
def test_optional_argument_missing
73+
expected_options = [
74+
'--yyy: '
75+
]
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)
81+
end
82+
end
83+
84+
def test_no_argument
85+
expected_options = [
86+
'--zzz: '
87+
]
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)
93+
end
94+
end
95+
96+
def test_new_with_empty_array
97+
e = assert_raise(ArgumentError) do
98+
GetoptLong.new([])
99+
end
100+
assert_match(/no argument-flag/, e.message)
101+
end
102+
103+
def test_new_with_bad_array
104+
e = assert_raise(ArgumentError) do
105+
GetoptLong.new('foo')
106+
end
107+
assert_match(/option list contains non-Array argument/, e.message)
108+
end
109+
110+
def test_new_with_empty_subarray
111+
e = assert_raise(ArgumentError) do
112+
GetoptLong.new([[]])
113+
end
114+
assert_match(/no argument-flag/, e.message)
115+
end
116+
117+
def test_new_with_bad_subarray
118+
e = assert_raise(ArgumentError) do
119+
GetoptLong.new([1])
120+
end
121+
assert_match(/no option name/, e.message)
122+
end
123+
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
128+
arguments = [
129+
[invalid_option, '-v', GetoptLong::NO_ARGUMENT]
130+
]
131+
GetoptLong.new(*arguments)
132+
end
133+
assert_match(/invalid option/, e.message)
134+
end
135+
end
136+
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
141+
arguments = [
142+
['--verbose', invalid_alias, GetoptLong::NO_ARGUMENT]
143+
]
144+
GetoptLong.new(*arguments)
145+
end
146+
assert_match(/invalid option/, e.message)
147+
end
148+
end
149+
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
154+
arguments = [
155+
['--verbose', '-v', invalid_flag]
156+
]
157+
GetoptLong.new(*arguments)
158+
end
159+
assert_match(/no argument-flag/, e.message)
160+
end
161+
end
162+
163+
end

0 commit comments

Comments
 (0)