getopt 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
metadata.gz.sig CHANGED
Binary file
@@ -1,265 +0,0 @@
1
- #####################################################################
2
- # tc_getopt_long.rb
3
- #
4
- # Test suite for the getopt-long package. You should run this test
5
- # via the 'rake test' rake task.
6
- #####################################################################
7
- require 'test-unit'
8
- require 'getopt/long'
9
- include Getopt
10
-
11
- class TC_Getopt_Long < Test::Unit::TestCase
12
- def setup
13
- @opts = nil
14
- end
15
-
16
- def test_version
17
- assert_equal('1.5.1', Long::VERSION)
18
- assert_true(Long::VERSION.frozen?)
19
- end
20
-
21
- def test_constants
22
- assert_not_nil(BOOLEAN)
23
- assert_not_nil(OPTIONAL)
24
- assert_not_nil(REQUIRED)
25
- assert_not_nil(INCREMENT)
26
- end
27
-
28
- def test_getopts_basic
29
- assert_respond_to(Long, :getopts)
30
- assert_nothing_raised{
31
- Long.getopts(["--test"],["--help"],["--foo"])
32
- }
33
- assert_nothing_raised{
34
- Long.getopts(["--test", "-x"],["--help", "-y"],["--foo", "-z"])
35
- }
36
- assert_nothing_raised{
37
- Long.getopts(
38
- ["--test", "-x", BOOLEAN],
39
- ["--help", "-y", REQUIRED],
40
- ["--foo", "-z", OPTIONAL],
41
- ["--more", "-m", INCREMENT]
42
- )
43
- }
44
- assert_kind_of(Hash, Long.getopts("--test"))
45
- end
46
-
47
- def test_getopts_using_equals
48
- ARGV.push("--foo=hello","-b","world")
49
- assert_nothing_raised{
50
- @opts = Long.getopts(
51
- ["--foo", "-f", REQUIRED],
52
- ["--bar", "-b", OPTIONAL]
53
- )
54
- }
55
- assert_equal("hello", @opts["foo"])
56
- assert_equal("hello", @opts["f"])
57
- assert_equal("world", @opts["bar"])
58
- assert_equal("world", @opts["b"])
59
- end
60
-
61
- def test_getopts_long_embedded_hyphens
62
- ARGV.push('--foo-bar', 'hello', '--test1-test2-test3', 'world')
63
- assert_nothing_raised{
64
- @opts = Long.getopts(
65
- ['--foo-bar', '-f', REQUIRED],
66
- ['--test1-test2-test3', '-t', REQUIRED]
67
- )
68
- }
69
- assert_equal('hello', @opts['foo-bar'])
70
- assert_equal('hello', @opts['f'])
71
- assert_equal('world', @opts['test1-test2-test3'])
72
- assert_equal('world', @opts['t'])
73
- end
74
-
75
- def test_getopts_long_embedded_hyphens_using_equals_sign
76
- ARGV.push('--foo-bar=hello', '--test1-test2-test3=world')
77
- assert_nothing_raised{
78
- @opts = Long.getopts(
79
- ['--foo-bar', '-f', REQUIRED],
80
- ['--test1-test2-test3', '-t', REQUIRED]
81
- )
82
- }
83
- assert_equal('hello', @opts['foo-bar'])
84
- assert_equal('hello', @opts['f'])
85
- assert_equal('world', @opts['test1-test2-test3'])
86
- assert_equal('world', @opts['t'])
87
- end
88
-
89
- def test_getopts_short_switch_squished
90
- ARGV.push("-f", "hello", "-bworld")
91
- assert_nothing_raised{
92
- @opts = Long.getopts(
93
- ["--foo", "-f", REQUIRED],
94
- ["--bar", "-b", OPTIONAL]
95
- )
96
- }
97
- assert_equal("hello", @opts["f"])
98
- assert_equal("world", @opts["b"])
99
- end
100
-
101
- def test_getopts_increment_type
102
- ARGV.push("-m","-m")
103
- assert_nothing_raised{
104
- @opts = Long.getopts(["--more", "-m", INCREMENT])
105
- }
106
- assert_equal(2, @opts["more"])
107
- assert_equal(2, @opts["m"])
108
- end
109
-
110
- def test_switches_exist
111
- ARGV.push("--verbose","--test","--foo")
112
- assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
113
- assert_equal(true, @opts.has_key?("verbose"))
114
- assert_equal(true, @opts.has_key?("test"))
115
- assert_equal(true, @opts.has_key?("foo"))
116
- end
117
-
118
- def test_short_switch_synonyms
119
- ARGV.push("--verbose","--test","--foo")
120
- assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
121
- assert_equal(true, @opts.has_key?("v"))
122
- assert_equal(true, @opts.has_key?("t"))
123
- assert_equal(true, @opts.has_key?("f"))
124
- end
125
-
126
- def test_short_switch_synonyms_with_explicit_types
127
- ARGV.push("--verbose", "--test", "hello", "--foo")
128
- assert_nothing_raised{
129
- @opts = Long.getopts(
130
- ["--verbose", BOOLEAN],
131
- ["--test", REQUIRED],
132
- ["--foo", BOOLEAN]
133
- )
134
- }
135
- assert(@opts.has_key?("v"))
136
- assert(@opts.has_key?("t"))
137
- assert(@opts.has_key?("f"))
138
- end
139
-
140
- def test_switches_with_required_arguments
141
- ARGV.push("--foo","1","--bar","hello")
142
- assert_nothing_raised{
143
- @opts = Long.getopts(
144
- ["--foo", "-f", REQUIRED],
145
- ["--bar", "-b", REQUIRED]
146
- )
147
- }
148
- assert_equal({"foo"=>"1", "bar"=>"hello", "f"=>"1", "b"=>"hello"}, @opts)
149
- end
150
-
151
- def test_compressed_switches
152
- ARGV.push("-fb")
153
- assert_nothing_raised{
154
- @opts = Long.getopts(
155
- ["--foo", "-f", BOOLEAN],
156
- ["--bar", "-b", BOOLEAN]
157
- )
158
- }
159
- assert_equal({"foo"=>true, "f"=>true, "b"=>true, "bar"=>true}, @opts)
160
- end
161
-
162
- def test_compress_switches_with_required_arg
163
- ARGV.push("-xf", "foo.txt")
164
- assert_nothing_raised{
165
- @opts = Long.getopts(
166
- ["--expand", "-x", BOOLEAN],
167
- ["--file", "-f", REQUIRED]
168
- )
169
- }
170
- assert_equal(
171
- {"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
172
- )
173
- end
174
-
175
- def test_compress_switches_with_compressed_required_arg
176
- ARGV.push("-xffoo.txt")
177
- assert_nothing_raised{
178
- @opts = Long.getopts(
179
- ["--expand", "-x", BOOLEAN],
180
- ["--file", "-f", REQUIRED]
181
- )
182
- }
183
- assert_equal(
184
- {"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
185
- )
186
- end
187
-
188
- def test_compress_switches_with_optional_arg_not_defined
189
- ARGV.push("-xf")
190
- assert_nothing_raised{
191
- @opts = Long.getopts(
192
- ["--expand", "-x", BOOLEAN],
193
- ["--file", "-f", OPTIONAL]
194
- )
195
- }
196
- assert_equal(
197
- {"x"=>true, "expand"=>true, "f"=>nil, "file"=>nil}, @opts
198
- )
199
- end
200
-
201
- def test_compress_switches_with_optional_arg
202
- ARGV.push("-xf", "boo.txt")
203
- assert_nothing_raised{
204
- @opts = Long.getopts(
205
- ["--expand", "-x", BOOLEAN],
206
- ["--file", "-f", OPTIONAL]
207
- )
208
- }
209
- assert_equal(
210
- {"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
211
- )
212
- end
213
-
214
- def test_compress_switches_with_compressed_optional_arg
215
- ARGV.push("-xfboo.txt")
216
- assert_nothing_raised{
217
- @opts = Long.getopts(
218
- ["--expand", "-x", BOOLEAN],
219
- ["--file", "-f", OPTIONAL]
220
- )
221
- }
222
- assert_equal(
223
- {"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
224
- )
225
- end
226
-
227
- def test_compressed_short_and_long_mixed
228
- ARGV.push("-xb", "--file", "boo.txt", "-v")
229
- assert_nothing_raised{
230
- @opts = Long.getopts(
231
- ["--expand", "-x", BOOLEAN],
232
- ["--verbose", "-v", BOOLEAN],
233
- ["--file", "-f", REQUIRED],
234
- ["--bar", "-b", OPTIONAL]
235
- )
236
- assert_equal(
237
- { "x"=>true, "expand"=>true,
238
- "v"=>true, "verbose"=>true,
239
- "f"=>"boo.txt", "file"=>"boo.txt",
240
- "b"=>nil, "bar"=>nil
241
- },
242
- @opts
243
- )
244
- }
245
- end
246
-
247
- def test_multiple_similar_long_switches_with_no_short_switches
248
- ARGV.push('--to','1','--too','2','--tooo','3')
249
- assert_nothing_raised{
250
- @opts = Long.getopts(
251
- ["--to", REQUIRED],
252
- ["--too", REQUIRED],
253
- ["--tooo", REQUIRED]
254
- )
255
- }
256
- assert_equal('1', @opts['to'])
257
- assert_equal('2', @opts['too'])
258
- assert_equal('3', @opts['tooo'])
259
- end
260
-
261
- def teardown
262
- @opts = nil
263
- ARGV.clear
264
- end
265
- end
@@ -1,125 +0,0 @@
1
- ###################################################################
2
- # test_getopt_std.rb
3
- #
4
- # Test suite for the Getopt::Std class. You should run this test
5
- # via the 'rake test' task.
6
- ###################################################################
7
- require 'test-unit'
8
- require 'getopt/std'
9
- include Getopt
10
-
11
- class TC_Getopt_Std < Test::Unit::TestCase
12
- def test_version
13
- assert_equal('1.5.1', Std::VERSION)
14
- assert_true(Std::VERSION.frozen?)
15
- end
16
-
17
- def test_getopts_basic
18
- assert_respond_to(Std, :getopts)
19
- assert_nothing_raised{ Std.getopts("ID") }
20
- assert_kind_of(Hash, Std.getopts("ID"))
21
- end
22
-
23
- def test_getopts_separated_switches
24
- ARGV.push("-I", "-D")
25
- assert_equal({"I"=>true, "D"=>true}, Std.getopts("ID"))
26
- end
27
-
28
- # Inspired by RF bug #23477
29
- def test_getopts_arguments_that_match_switch_are_ok
30
- ARGV.push("-d", "d")
31
- assert_equal({"d" => "d"}, Std.getopts("d:"))
32
-
33
- ARGV.push("-d", "ad")
34
- assert_equal({"d" => "ad"}, Std.getopts("d:"))
35
-
36
- ARGV.push("-a", "ad")
37
- assert_equal({"a" => "ad"}, Std.getopts("d:a:"))
38
-
39
- ARGV.push("-a", "da")
40
- assert_equal({"a" => "da"}, Std.getopts("d:a:"))
41
-
42
- ARGV.push("-a", "d")
43
- assert_equal({"a" => "d"}, Std.getopts("d:a:"))
44
-
45
- ARGV.push("-a", "dad")
46
- assert_equal({"a" => "dad"}, Std.getopts("d:a:"))
47
-
48
- ARGV.push("-d", "d", "-a", "a")
49
- assert_equal({"d" => "d", "a" => "a"}, Std.getopts("d:a:"))
50
- end
51
-
52
- def test_getopts_joined_switches
53
- ARGV.push("-ID")
54
- assert_equal({"I"=>true, "D"=>true}, Std.getopts("ID"))
55
- end
56
-
57
- def test_getopts_separated_switches_with_mandatory_arg
58
- ARGV.push("-o", "hello", "-I", "-D")
59
- assert_equal({"o"=>"hello", "I"=>true, "D"=>true}, Std.getopts("o:ID"))
60
- end
61
-
62
- def test_getopts_joined_switches_with_mandatory_arg
63
- ARGV.push("-IDo", "hello")
64
- assert_equal({"o"=>"hello", "I"=>true, "D"=>true}, Std.getopts("o:ID"))
65
- end
66
-
67
- def test_getopts_no_args
68
- assert_nothing_raised{ Std.getopts("ID") }
69
- assert_equal({}, Std.getopts("ID"))
70
- assert_nil(Std.getopts("ID")["I"])
71
- assert_nil(Std.getopts("ID")["D"])
72
- end
73
-
74
- # If a switch that accepts an argument appears more than once, the values
75
- # are rolled into an array.
76
- def test_getopts_switch_repeated
77
- ARGV.push("-I", "-I", "-o", "hello", "-o", "world")
78
- assert_equal({"o" => ["hello","world"], "I"=>true}, Std.getopts("o:ID"))
79
- end
80
-
81
- # EXPECTED ERRORS
82
-
83
- def test_getopts_expected_errors_passing_switch_to_another_switch
84
- ARGV.push("-d", "-d")
85
- assert_raise(Getopt::Std::Error){ Std.getopts("d:a:") }
86
-
87
- ARGV.push("-d", "-a")
88
- assert_raise(Getopt::Std::Error){ Std.getopts("d:a:") }
89
-
90
- ARGV.push("-a", "-d")
91
- assert_raise(Getopt::Std::Error){ Std.getopts("d:a:") }
92
-
93
- ARGV.push("-d", "-d")
94
- assert_raise_message("cannot use switch '-d' as argument to another switch"){ Std.getopts("d:a:") }
95
- end
96
-
97
- def test_getopts_expected_errors_missing_arg
98
- ARGV.push("-ID")
99
- assert_raises(Std::Error){ Std.getopts("I:D") }
100
-
101
- ARGV.push("-ID")
102
- assert_raises(Std::Error){ Std.getopts("ID:") }
103
- end
104
-
105
- def test_getopts_expected_errors_extra_arg
106
- ARGV.push("-I", "-D", "-X")
107
- assert_raises(Std::Error){ Std.getopts("ID") }
108
-
109
- ARGV.push("-IDX")
110
- assert_raises(Std::Error){ Std.getopts("ID") }
111
-
112
- ARGV.push("-IDX")
113
- assert_raise_message("invalid option 'X'"){ Std.getopts("ID") }
114
- end
115
-
116
- def test_getopts_expected_errors_basic
117
- assert_raises(ArgumentError){ Std.getopts }
118
- assert_raises(NoMethodError){ Std.getopts(0) }
119
- assert_raises(NoMethodError){ Std.getopts(nil) }
120
- end
121
-
122
- def teardown
123
- ARGV.clear
124
- end
125
- end