getopt 1.4.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,264 +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.4.2', Long::VERSION)
18
- end
19
-
20
- def test_constants
21
- assert_not_nil(BOOLEAN)
22
- assert_not_nil(OPTIONAL)
23
- assert_not_nil(REQUIRED)
24
- assert_not_nil(INCREMENT)
25
- end
26
-
27
- def test_getopts_basic
28
- assert_respond_to(Long, :getopts)
29
- assert_nothing_raised{
30
- Long.getopts(["--test"],["--help"],["--foo"])
31
- }
32
- assert_nothing_raised{
33
- Long.getopts(["--test", "-x"],["--help", "-y"],["--foo", "-z"])
34
- }
35
- assert_nothing_raised{
36
- Long.getopts(
37
- ["--test", "-x", BOOLEAN],
38
- ["--help", "-y", REQUIRED],
39
- ["--foo", "-z", OPTIONAL],
40
- ["--more", "-m", INCREMENT]
41
- )
42
- }
43
- assert_kind_of(Hash, Long.getopts("--test"))
44
- end
45
-
46
- def test_getopts_using_equals
47
- ARGV.push("--foo=hello","-b","world")
48
- assert_nothing_raised{
49
- @opts = Long.getopts(
50
- ["--foo", "-f", REQUIRED],
51
- ["--bar", "-b", OPTIONAL]
52
- )
53
- }
54
- assert_equal("hello", @opts["foo"])
55
- assert_equal("hello", @opts["f"])
56
- assert_equal("world", @opts["bar"])
57
- assert_equal("world", @opts["b"])
58
- end
59
-
60
- def test_getopts_long_embedded_hyphens
61
- ARGV.push('--foo-bar', 'hello', '--test1-test2-test3', 'world')
62
- assert_nothing_raised{
63
- @opts = Long.getopts(
64
- ['--foo-bar', '-f', REQUIRED],
65
- ['--test1-test2-test3', '-t', REQUIRED]
66
- )
67
- }
68
- assert_equal('hello', @opts['foo-bar'])
69
- assert_equal('hello', @opts['f'])
70
- assert_equal('world', @opts['test1-test2-test3'])
71
- assert_equal('world', @opts['t'])
72
- end
73
-
74
- def test_getopts_long_embedded_hyphens_using_equals_sign
75
- ARGV.push('--foo-bar=hello', '--test1-test2-test3=world')
76
- assert_nothing_raised{
77
- @opts = Long.getopts(
78
- ['--foo-bar', '-f', REQUIRED],
79
- ['--test1-test2-test3', '-t', REQUIRED]
80
- )
81
- }
82
- assert_equal('hello', @opts['foo-bar'])
83
- assert_equal('hello', @opts['f'])
84
- assert_equal('world', @opts['test1-test2-test3'])
85
- assert_equal('world', @opts['t'])
86
- end
87
-
88
- def test_getopts_short_switch_squished
89
- ARGV.push("-f", "hello", "-bworld")
90
- assert_nothing_raised{
91
- @opts = Long.getopts(
92
- ["--foo", "-f", REQUIRED],
93
- ["--bar", "-b", OPTIONAL]
94
- )
95
- }
96
- assert_equal("hello", @opts["f"])
97
- assert_equal("world", @opts["b"])
98
- end
99
-
100
- def test_getopts_increment_type
101
- ARGV.push("-m","-m")
102
- assert_nothing_raised{
103
- @opts = Long.getopts(["--more", "-m", INCREMENT])
104
- }
105
- assert_equal(2, @opts["more"])
106
- assert_equal(2, @opts["m"])
107
- end
108
-
109
- def test_switches_exist
110
- ARGV.push("--verbose","--test","--foo")
111
- assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
112
- assert_equal(true, @opts.has_key?("verbose"))
113
- assert_equal(true, @opts.has_key?("test"))
114
- assert_equal(true, @opts.has_key?("foo"))
115
- end
116
-
117
- def test_short_switch_synonyms
118
- ARGV.push("--verbose","--test","--foo")
119
- assert_nothing_raised{ @opts = Long.getopts("--verbose --test --foo") }
120
- assert_equal(true, @opts.has_key?("v"))
121
- assert_equal(true, @opts.has_key?("t"))
122
- assert_equal(true, @opts.has_key?("f"))
123
- end
124
-
125
- def test_short_switch_synonyms_with_explicit_types
126
- ARGV.push("--verbose", "--test", "hello", "--foo")
127
- assert_nothing_raised{
128
- @opts = Long.getopts(
129
- ["--verbose", BOOLEAN],
130
- ["--test", REQUIRED],
131
- ["--foo", BOOLEAN]
132
- )
133
- }
134
- assert(@opts.has_key?("v"))
135
- assert(@opts.has_key?("t"))
136
- assert(@opts.has_key?("f"))
137
- end
138
-
139
- def test_switches_with_required_arguments
140
- ARGV.push("--foo","1","--bar","hello")
141
- assert_nothing_raised{
142
- @opts = Long.getopts(
143
- ["--foo", "-f", REQUIRED],
144
- ["--bar", "-b", REQUIRED]
145
- )
146
- }
147
- assert_equal({"foo"=>"1", "bar"=>"hello", "f"=>"1", "b"=>"hello"}, @opts)
148
- end
149
-
150
- def test_compressed_switches
151
- ARGV.push("-fb")
152
- assert_nothing_raised{
153
- @opts = Long.getopts(
154
- ["--foo", "-f", BOOLEAN],
155
- ["--bar", "-b", BOOLEAN]
156
- )
157
- }
158
- assert_equal({"foo"=>true, "f"=>true, "b"=>true, "bar"=>true}, @opts)
159
- end
160
-
161
- def test_compress_switches_with_required_arg
162
- ARGV.push("-xf", "foo.txt")
163
- assert_nothing_raised{
164
- @opts = Long.getopts(
165
- ["--expand", "-x", BOOLEAN],
166
- ["--file", "-f", REQUIRED]
167
- )
168
- }
169
- assert_equal(
170
- {"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
171
- )
172
- end
173
-
174
- def test_compress_switches_with_compressed_required_arg
175
- ARGV.push("-xffoo.txt")
176
- assert_nothing_raised{
177
- @opts = Long.getopts(
178
- ["--expand", "-x", BOOLEAN],
179
- ["--file", "-f", REQUIRED]
180
- )
181
- }
182
- assert_equal(
183
- {"x"=>true, "expand"=>true, "f"=>"foo.txt", "file"=>"foo.txt"}, @opts
184
- )
185
- end
186
-
187
- def test_compress_switches_with_optional_arg_not_defined
188
- ARGV.push("-xf")
189
- assert_nothing_raised{
190
- @opts = Long.getopts(
191
- ["--expand", "-x", BOOLEAN],
192
- ["--file", "-f", OPTIONAL]
193
- )
194
- }
195
- assert_equal(
196
- {"x"=>true, "expand"=>true, "f"=>nil, "file"=>nil}, @opts
197
- )
198
- end
199
-
200
- def test_compress_switches_with_optional_arg
201
- ARGV.push("-xf", "boo.txt")
202
- assert_nothing_raised{
203
- @opts = Long.getopts(
204
- ["--expand", "-x", BOOLEAN],
205
- ["--file", "-f", OPTIONAL]
206
- )
207
- }
208
- assert_equal(
209
- {"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
210
- )
211
- end
212
-
213
- def test_compress_switches_with_compressed_optional_arg
214
- ARGV.push("-xfboo.txt")
215
- assert_nothing_raised{
216
- @opts = Long.getopts(
217
- ["--expand", "-x", BOOLEAN],
218
- ["--file", "-f", OPTIONAL]
219
- )
220
- }
221
- assert_equal(
222
- {"x"=>true, "expand"=>true, "f"=>"boo.txt", "file"=>"boo.txt"}, @opts
223
- )
224
- end
225
-
226
- def test_compressed_short_and_long_mixed
227
- ARGV.push("-xb", "--file", "boo.txt", "-v")
228
- assert_nothing_raised{
229
- @opts = Long.getopts(
230
- ["--expand", "-x", BOOLEAN],
231
- ["--verbose", "-v", BOOLEAN],
232
- ["--file", "-f", REQUIRED],
233
- ["--bar", "-b", OPTIONAL]
234
- )
235
- assert_equal(
236
- { "x"=>true, "expand"=>true,
237
- "v"=>true, "verbose"=>true,
238
- "f"=>"boo.txt", "file"=>"boo.txt",
239
- "b"=>nil, "bar"=>nil
240
- },
241
- @opts
242
- )
243
- }
244
- end
245
-
246
- def test_multiple_similar_long_switches_with_no_short_switches
247
- ARGV.push('--to','1','--too','2','--tooo','3')
248
- assert_nothing_raised{
249
- @opts = Long.getopts(
250
- ["--to", REQUIRED],
251
- ["--too", REQUIRED],
252
- ["--tooo", REQUIRED]
253
- )
254
- }
255
- assert_equal('1', @opts['to'])
256
- assert_equal('2', @opts['too'])
257
- assert_equal('3', @opts['tooo'])
258
- end
259
-
260
- def teardown
261
- @opts = nil
262
- ARGV.clear
263
- end
264
- 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
-
13
- def test_version
14
- assert_equal('1.4.2', Std::VERSION)
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