s0nspark-choice 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_helper')
3
+
4
+ require "choice"
5
+ require 'choice/option'
6
+
7
+ class TestOption < Test::Unit::TestCase
8
+ def setup
9
+ Choice.reset!
10
+ @option = Choice::Option.new
11
+ end
12
+
13
+ def test_desc
14
+ line_one = "This is a description."
15
+ line_two = "I can add many lines."
16
+ line_three = "There is no limit."
17
+
18
+ assert_equal false, @option.desc?
19
+
20
+ @option.desc line_one
21
+ @option.desc line_two
22
+
23
+ assert @option.desc?
24
+ assert_equal [line_one, line_two], @option.desc
25
+
26
+ @option.desc line_three
27
+ assert @option.desc?
28
+ assert_equal [line_one, line_two, line_three], @option.desc
29
+ end
30
+
31
+ def test_choice
32
+ short = "-p"
33
+
34
+ assert_equal false, @option.short?
35
+
36
+ @option.short short
37
+
38
+ assert @option.short?
39
+ assert_equal short, @option.short
40
+ end
41
+
42
+ def test_no_choice
43
+ default = 42
44
+
45
+ assert_raise(Choice::Option::ParseError) do
46
+ @option.defaut?
47
+ end
48
+
49
+ assert_raise(Choice::Option::ParseError) do
50
+ @option.defaut default
51
+ end
52
+
53
+ assert_raise(Choice::Option::ParseError) do
54
+ @option.defaut
55
+ end
56
+
57
+ end
58
+
59
+ def test_block_choice
60
+ assert_equal false, @option.action?
61
+
62
+ @option.action do
63
+ 1 + 1
64
+ end
65
+
66
+ assert @option.action?
67
+ assert_block(&@option.action)
68
+ end
69
+
70
+ def test_format
71
+ @option = Choice::Option.new do
72
+ validate /^\w+$/
73
+ end
74
+
75
+ assert_equal /^\w+$/, @option.validate
76
+
77
+ block = proc { |f| File.exists? f }
78
+ @option = Choice::Option.new do
79
+ validate &block
80
+ end
81
+
82
+ assert_equal block, @option.validate
83
+ end
84
+
85
+ def test_dsl
86
+ @option = Choice::Option.new do
87
+ short "-s"
88
+ long "--host=HOST"
89
+ cast String
90
+ desc "The hostname."
91
+ desc "(Alphanumeric only)"
92
+ filter do
93
+ 5 * 10
94
+ end
95
+ end
96
+
97
+ assert_equal "-s", @option.short
98
+ assert_equal "--host=HOST", @option.long
99
+ assert_equal String, @option.cast
100
+ assert_equal ["The hostname.", "(Alphanumeric only)"], @option.desc
101
+ assert_equal proc { 5 * 10 }.call, @option.filter.call
102
+ end
103
+
104
+ def test_to_a
105
+ desc = "This is your description."
106
+ short = "-t"
107
+ long = "--test=METHOD"
108
+ default = :to_a
109
+
110
+ @option.desc desc
111
+ @option.short short
112
+ @option.long long
113
+ @option.default default
114
+ @option.action { 1 + 1 }
115
+ array = @option.to_a
116
+
117
+ assert_equal Choice::Option, @option.class
118
+ assert_equal Array, array.class
119
+ assert array.include? [desc]
120
+ assert array.include? short
121
+ assert array.include? long
122
+ assert array.include? default
123
+ assert_equal proc { 1 + 1 }.call, array.select { |a| a.is_a? Proc }.first.call
124
+ end
125
+
126
+ def test_to_h
127
+ desc = "This is your description."
128
+ short = "-t"
129
+ long = "--test=METHOD"
130
+ cast = Integer
131
+
132
+ @option.desc desc
133
+ @option.short short
134
+ @option.long long
135
+ @option.cast cast
136
+ @option.filter { 2 + 2 }
137
+ hash = @option.to_h
138
+
139
+ assert_equal Choice::Option, @option.class
140
+ assert_equal Hash, hash.class
141
+ assert_equal [desc], hash['desc']
142
+ assert_equal short, hash['short']
143
+ assert_equal cast, hash['cast']
144
+ assert_equal proc { 2 + 2 }.call, hash['filter'].call
145
+ end
146
+ end
@@ -0,0 +1,374 @@
1
+
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'test_helper')
3
+
4
+ require 'choice/option'
5
+ require 'choice/parser'
6
+
7
+ class TestParser < Test::Unit::TestCase
8
+ def setup
9
+ @options = {}
10
+ end
11
+
12
+ def test_parse_options
13
+ @options['band'] = Choice::Option.new do
14
+ short '-b'
15
+ long '--band=BAND'
16
+ cast String
17
+ desc 'Your favorite band.'
18
+ end
19
+ @options['animal'] = Choice::Option.new do
20
+ short '-a'
21
+ long '--animal=ANIMAL'
22
+ cast String
23
+ desc 'Your favorite animal.'
24
+ end
25
+ band = 'Led Zeppelin'
26
+ animal = 'Reindeer'
27
+
28
+ args = ['-b', band, "--animal=#{animal}"]
29
+
30
+ choices = Choice::Parser.parse(@options, args)
31
+
32
+ assert_equal band, choices['band']
33
+ assert_equal animal, choices['animal']
34
+ end
35
+
36
+ def test_parse_no_options
37
+ assert_equal Hash.new, Choice::Parser.parse(nil, nil)
38
+ end
39
+
40
+ def test_parse_default
41
+ @options['soda'] = Choice::Option.new do
42
+ short '-s'
43
+ long '--soda=SODA'
44
+ default 'PibbJr'
45
+ end
46
+
47
+ args = []
48
+
49
+ choices = Choice::Parser.parse(@options, args)
50
+
51
+ assert_equal 'PibbJr', choices['soda']
52
+ end
53
+
54
+ def test_parse_options_with_filters
55
+ @options['host'] = Choice::Option.new do
56
+ short '-h'
57
+ filter do |opt|
58
+ opt.gsub!(/[^\w]/, '')
59
+ opt = opt.sub(/k/, 'c')
60
+ end
61
+ end
62
+ host = 'de.fun.kt'
63
+ args = ['-h', host]
64
+ choices = Choice::Parser.parse(@options, args)
65
+
66
+ assert_equal 'defunct', choices['host']
67
+ end
68
+
69
+ def test_casting
70
+ @options['port'] = Choice::Option.new do
71
+ short '-p'
72
+ cast Integer
73
+ end
74
+
75
+ port = '3000'
76
+ args = ['-p', port]
77
+ choices = Choice::Parser.parse(@options, args)
78
+
79
+ assert_equal port.to_i, choices['port']
80
+ end
81
+
82
+ def test_text_required
83
+ @options['name'] = Choice::Option.new do
84
+ short '-n'
85
+ long '--name=NAME'
86
+ end
87
+ @options['age'] = Choice::Option.new do
88
+ short '-a'
89
+ long 'age[=AGE]'
90
+ cast Integer
91
+ end
92
+
93
+ args = ['-n', '-a', '21']
94
+
95
+ assert_raise(Choice::Parser::ArgumentRequired) do
96
+ choices = Choice::Parser.parse(@options, args)
97
+ end
98
+ end
99
+
100
+ def test_text_optional
101
+ @options['color'] = Choice::Option.new do
102
+ short '-c'
103
+ long '--color[=COLOR]'
104
+ end
105
+
106
+ args = ['-c']
107
+ choices = Choice::Parser.parse(@options, args)
108
+
109
+ assert choices['color']
110
+
111
+ color = 'ladyblue'
112
+ args = ['-c', color]
113
+ choices = Choice::Parser.parse(@options, args)
114
+
115
+ assert_equal color, choices['color']
116
+ end
117
+
118
+ def test_text_optional_deprecated
119
+ @options['color'] = Choice::Option.new do
120
+ short '-c'
121
+ long '--color=[COLOR]'
122
+ end
123
+
124
+ args = ['-c']
125
+ choices = Choice::Parser.parse(@options, args)
126
+
127
+ assert choices['color']
128
+
129
+ color = 'ladyblue'
130
+ args = ['-c', color]
131
+ choices = Choice::Parser.parse(@options, args)
132
+
133
+ assert_equal color, choices['color']
134
+ end
135
+
136
+ def test_ignore_separator
137
+ options = []
138
+ options << ['keyboard', Choice::Option.new do
139
+ short '-k'
140
+ long '--keyboard=BOARD'
141
+ end]
142
+
143
+ options << ['mouse', Choice::Option.new do
144
+ short '-m'
145
+ long '--mouse=MOUSE'
146
+ end]
147
+
148
+ args = ['-m', 'onebutton']
149
+ choices = Choice::Parser.parse([options.first, '----', options.last], args)
150
+
151
+ assert choices['mouse']
152
+ assert_equal 1, choices.size
153
+ end
154
+
155
+ def test_long_as_switch
156
+ @options['chunky'] = Choice::Option.new do
157
+ short '-b'
158
+ long '--bacon'
159
+ end
160
+
161
+ args = ['--bacon']
162
+ choices = Choice::Parser.parse(@options, args)
163
+
164
+ assert choices['chunky']
165
+ end
166
+
167
+ def test_validate_regexp
168
+ @options['email'] = Choice::Option.new do
169
+ short '-e'
170
+ long '--email=EMAIL'
171
+ desc 'Your valid email addy.'
172
+ validate /^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]{2,4}$/i
173
+ end
174
+
175
+ email_bad = 'this will@neverwork'
176
+ email_good = 'chris@ozmm.org'
177
+
178
+ args = ['-e', email_bad]
179
+ assert_raise(Choice::Parser::ArgumentValidationFails) do
180
+ choices = Choice::Parser.parse(@options, args)
181
+ end
182
+
183
+ args = ['-e', email_good]
184
+ choices = Choice::Parser.parse(@options, args)
185
+
186
+ assert_equal email_good, choices['email']
187
+ end
188
+
189
+ def test_validate_block
190
+ @options['file'] = Choice::Option.new do
191
+ short '-f'
192
+ long '--file=FILE'
193
+ desc 'Your valid email addy.'
194
+ validate do |arg|
195
+ File.exists? arg
196
+ end
197
+ end
198
+
199
+ file_bad = 'not_a_file.rb'
200
+ file_good = __FILE__
201
+
202
+ args = ['-f', file_bad]
203
+ assert_raise(Choice::Parser::ArgumentValidationFails) do
204
+ choices = Choice::Parser.parse(@options, args)
205
+ end
206
+
207
+ args = ['-f', file_good]
208
+ choices = Choice::Parser.parse(@options, args)
209
+
210
+ assert_equal file_good, choices['file']
211
+ end
212
+
213
+ def test_unknown_argument
214
+ @options['cd'] = Choice::Option.new do
215
+ short '-c'
216
+ long '--cd=CD'
217
+ desc 'A CD you like.'
218
+ end
219
+
220
+ args = ['-c', 'BestOfYanni', '--grace']
221
+ assert_raise(Choice::Parser::UnknownOption) do
222
+ choices = Choice::Parser.parse(@options, args)
223
+ end
224
+ end
225
+
226
+ def test_valid
227
+ @options['suit'] = Choice::Option.new do
228
+ short '-s'
229
+ long '--suit=SUIT'
230
+ valid %w[club diamond spade heart]
231
+ desc "The suit of your card, sir."
232
+ end
233
+
234
+ suit_good = 'club'
235
+ suit_bad = 'joker'
236
+
237
+ args = ['-s', suit_bad]
238
+ assert_raise(Choice::Parser::InvalidArgument) do
239
+ choices = Choice::Parser.parse(@options, args)
240
+ end
241
+
242
+ args = ['-s', suit_good]
243
+ choices = Choice::Parser.parse(@options, args)
244
+
245
+ assert_equal suit_good, choices['suit']
246
+ end
247
+
248
+ def test_valid_needs_argument
249
+ @options['pants'] = Choice::Option.new do
250
+ short '-p'
251
+ long '--pants'
252
+ valid %w[jeans slacks trunks boxers]
253
+ desc "Your preferred type of pants."
254
+ end
255
+
256
+ args = ['-p']
257
+ assert_raise(Choice::Parser::ArgumentRequiredWithValid) do
258
+ choices = Choice::Parser.parse(@options, args)
259
+ end
260
+ end
261
+
262
+ def test_long_as_array
263
+ @options['medium'] = Choice::Option.new do
264
+ short '-m'
265
+ long '--medium=*MEDIUM'
266
+ desc "The medium(s) you like best."
267
+ end
268
+
269
+ mediums = %w[canvas stone steel]
270
+
271
+ args = ['-m', mediums.first, '-m', mediums[1], '-m', mediums.last]
272
+ choices = Choice::Parser.parse(@options, args)
273
+ assert_equal mediums, choices['medium']
274
+
275
+ args = ['-m', mediums.first, mediums[1], mediums.last]
276
+ choices = Choice::Parser.parse(@options, args)
277
+ assert_equal mediums, choices['medium']
278
+
279
+ args = ["--medium=#{mediums.first}", "--medium=#{mediums[1]}", "--medium=#{mediums.last}"]
280
+ choices = Choice::Parser.parse(@options, args)
281
+ assert_equal mediums, choices['medium']
282
+
283
+ args = ["--medium=#{mediums.first}", mediums[1], mediums.last]
284
+ choices = Choice::Parser.parse(@options, args)
285
+ assert_equal mediums, choices['medium']
286
+ end
287
+
288
+ def test_long_as_array_optional
289
+ @options['instruments'] = Choice::Option.new do
290
+ short '-i'
291
+ long '--instruments[=*INSTRUMENTS]'
292
+ desc "Do you like instruments? Which ones do you like best?"
293
+ end
294
+
295
+ instruments = %w[xylophone guitar piano]
296
+
297
+ args = ["--instruments=#{instruments.first}", "--instruments=#{instruments[1]}",
298
+ "--instruments=#{instruments.last}"]
299
+ choices = Choice::Parser.parse(@options, args)
300
+ assert_equal instruments, choices['instruments']
301
+
302
+ args = %w[--instruments]
303
+ choices = Choice::Parser.parse(@options, args)
304
+ assert_equal true, choices['instruments']
305
+ end
306
+
307
+ def test_long_as_array_with_valid
308
+ @options['suits'] = Choice::Option.new do
309
+ short '-s'
310
+ long '--suits=*SUITS'
311
+ valid %w[club diamond spade heart]
312
+ desc "The suits of your deck, sir."
313
+ end
314
+
315
+ suits = %w[spade heart]
316
+
317
+ args = ['-s', suits.first, suits.last]
318
+ choices = Choice::Parser.parse(@options, args)
319
+
320
+ assert_equal suits, choices['suits']
321
+
322
+ args = ['-s', suits.first, 'notasuit']
323
+ assert_raise(Choice::Parser::InvalidArgument) do
324
+ choices = Choice::Parser.parse(@options, args)
325
+ end
326
+ end
327
+
328
+ def test_long_with_spaces
329
+ @options['donut'] = Choice::Option.new do
330
+ short '-d'
331
+ long '--donut DONUT'
332
+ desc "Your favorite donut style."
333
+ end
334
+
335
+ donut = 'long-john'
336
+
337
+ args = ['--donut', donut]
338
+ choices = Choice::Parser.parse(@options, args)
339
+
340
+ assert_equal donut, choices['donut']
341
+ end
342
+
343
+ def test_optional_long_with_spaces
344
+ @options['donut'] = Choice::Option.new do
345
+ short '-d'
346
+ long '--donut [DONUT]'
347
+ desc "Your favorite donut style."
348
+ end
349
+
350
+ donut = 'chocolate'
351
+
352
+ args = ['--donut', donut]
353
+ choices = Choice::Parser.parse(@options, args)
354
+ assert_equal donut, choices['donut']
355
+
356
+ args = ['--donut']
357
+ choices = Choice::Parser.parse(@options, args)
358
+ assert_equal true, choices['donut']
359
+ end
360
+
361
+ def test_long_with_spaces_arrayed
362
+ @options['donuts'] = Choice::Option.new do
363
+ short '-d'
364
+ long '--donuts *DONUTS'
365
+ desc "Your favorite donut styles."
366
+ end
367
+
368
+ donuts = %w[glazed cream-filled]
369
+
370
+ args = ['--donuts', donuts.first, donuts.last]
371
+ choices = Choice::Parser.parse(@options, args)
372
+ assert_equal donuts, choices['donuts']
373
+ end
374
+ end