ivanvc-choice 0.1.3.1

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