rubycom 0.1.1

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,3 @@
1
+ module Rubycom
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubycom/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubycom"
8
+ spec.version = Rubycom::VERSION
9
+ spec.authors = ["Danny Purcell"]
10
+ spec.email = ["dpurcelljr@gmail.com"]
11
+ spec.description = %q{Allows command-line access for all singleton methods in an including class. Reads Yard style documentation for command line help output. Uses Yaml for parsing options. Allows the user to make a command-line tool by simply including Rubycom at the bottom.}
12
+ spec.summary = %q{Converts singleton methods to command-line functions upon inclusion.}
13
+ spec.homepage = "http://dannypurcell.github.io/Rubycom"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "test-unit"
23
+ spec.add_development_dependency "yard"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_dependency 'method_source'
26
+ end
@@ -0,0 +1,630 @@
1
+ require 'time'
2
+
3
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_module.rb"
4
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_composite.rb"
5
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_no_singleton.rb"
6
+
7
+ require 'test/unit'
8
+ #noinspection RubyInstanceMethodNamingConvention
9
+ class TestRubycom < Test::Unit::TestCase
10
+
11
+ def test_get_doc
12
+ method = UtilTestModule.public_method(:test_command_with_return)
13
+ result_hash = Rubycom.get_doc(method)
14
+ assert_equal('A test_command with a return argument'.gsub(/\n|\r|\s/, ''), result_hash[:desc].join("\n").gsub(/\n|\r|\s/, ''))
15
+ assert_equal(2, result_hash[:param].size)
16
+ assert_equal('[String] test_arg a test argument'.gsub(/\n|\r|\s/, ''), result_hash[:param][0].gsub(/\n|\r|\s/, ''))
17
+ assert_equal('[Integer] test_option_int an optional test argument which happens to be an Integer'.gsub(/\n|\r|\s/, ''), result_hash[:param][1].gsub(/\n|\r|\s/, ''))
18
+ assert_equal(2, result_hash[:return].size)
19
+ assert_equal('[Array] an array including both params if test_option_int != 1'.gsub(/\n|\r|\s/, ''), result_hash[:return][0].gsub(/\n|\r|\s/, ''))
20
+ assert_equal('[String] a the first param if test_option_int == 1'.gsub(/\n|\r|\s/, ''), result_hash[:return][1].gsub(/\n|\r|\s/, ''))
21
+ end
22
+
23
+ def test_get_command_usage
24
+ base = UtilTestModule
25
+ command_name = 'test_command_arg_false'
26
+ result = Rubycom.get_command_usage(base, command_name)
27
+ expected = <<-END.gsub(/^ {4}/, '')
28
+ Usage: test_command_arg_false [-test_flag=val]
29
+ Parameters:
30
+ [Boolean] test_flag a test Boolean argument
31
+ Returns:
32
+ [Boolean] the flag passed in
33
+ END
34
+ assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
35
+ end
36
+
37
+ def test_get_command_usage_nil_base
38
+ base = nil
39
+ command_name = 'test_command'
40
+ assert_raise(Rubycom::CLIError) { Rubycom.get_command_usage(base, command_name) }
41
+ end
42
+
43
+ def test_get_command_usage_nil_command
44
+ base = UtilTestModule
45
+ command_name = nil
46
+ result = Rubycom.get_command_usage(base, command_name)
47
+ assert_equal('No command specified.', result)
48
+ end
49
+
50
+ def test_get_command_usage_no_command
51
+ base = UtilTestModule
52
+ command_name = ''
53
+ result = Rubycom.get_command_usage(base, command_name)
54
+ assert_equal('No command specified.', result)
55
+ end
56
+
57
+ def test_get_command_usage_bad_base
58
+ base = ":asd"
59
+ command_name = 'test_command_with_options'
60
+ assert_raise(Rubycom::CLIError) { Rubycom.get_command_usage(base, command_name) }
61
+ end
62
+
63
+ def test_get_command_usage_invalid_command
64
+ base = UtilTestModule
65
+ command_name = '123asd!@#'
66
+ assert_raise(Rubycom::CLIError) { Rubycom.get_command_usage(base, command_name) }
67
+ end
68
+
69
+ def test_get_command_summary
70
+ base = UtilTestModule
71
+ command_name = 'test_command_with_options'
72
+ result = Rubycom.get_command_summary(base, command_name)
73
+ assert_equal("test_command_with_options - A test_command with an optional argument\n".gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
74
+ end
75
+
76
+ def test_get_command_summary_no_command
77
+ base = UtilTestModule
78
+ command_name = ''
79
+ result = Rubycom.get_command_summary(base, command_name)
80
+ assert_equal('No command specified.', result)
81
+ end
82
+
83
+ def test_get_command_summary_nil_base
84
+ base = nil
85
+ command_name = 'test_command_with_options'
86
+ assert_raise(Rubycom::CLIError) { Rubycom.get_command_summary(base, command_name) }
87
+ end
88
+
89
+ def test_get_command_summary_nil_command
90
+ base = UtilTestModule
91
+ command_name = nil
92
+ result = Rubycom.get_command_summary(base, command_name)
93
+ assert_equal('No command specified.', result)
94
+ end
95
+
96
+ def test_get_command_summary_wrong_base
97
+ base = UtilTestNoSingleton
98
+ command_name = 'test_command_with_options'
99
+ assert_raise(Rubycom::CLIError) { Rubycom.get_command_summary(base, command_name) }
100
+ end
101
+
102
+ def test_get_command_summary_bad_command
103
+ base = UtilTestModule
104
+ command_name = '!_fail_command_'
105
+ assert_raise(Rubycom::CLIError) { Rubycom.get_command_summary(base, command_name) }
106
+ end
107
+
108
+ def test_get_usage
109
+ base = UtilTestModule
110
+ result = Rubycom.get_usage(base)
111
+ expected = <<-END.gsub(/^ {4}/, '')
112
+ Usage:
113
+ UtilTestModule <command> [args]
114
+
115
+ Commands:
116
+ test_command - A basic test command
117
+ test_command_with_arg - A test_command with one arg
118
+ test_command_with_args - A test_command with two args
119
+ test_command_with_options - A test_command with an optional argument
120
+ test_command_all_options - A test_command with all optional arguments
121
+ test_command_options_arr - A test_command with an options array
122
+ test_command_with_return - A test_command with a return argument
123
+ test_command_arg_timestamp - A test_command with a Timestamp argument and an unnecessarily
124
+ long description which should overflow when
125
+ it tries to line up with other descriptions.
126
+ test_command_arg_false - A test_command with a Boolean argument
127
+ test_command_arg_arr - A test_command with an array argument
128
+ test_command_arg_hash - A test_command with an Hash argument
129
+
130
+ END
131
+ assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
132
+ end
133
+
134
+ def test_get_usage_nil_base
135
+ base = nil
136
+ result = Rubycom.get_usage(base)
137
+ assert_equal('', result)
138
+ end
139
+
140
+ def test_get_usage_no_singleton_base
141
+ base = UtilTestNoSingleton
142
+ result = Rubycom.get_usage(base)
143
+ assert_equal('', result)
144
+ end
145
+
146
+ def test_get_summary
147
+ base = UtilTestModule
148
+ result = Rubycom.get_summary(base)
149
+ expected = <<-END.gsub(/^ {4}/, '')
150
+ Commands:
151
+ test_command - A basic test command
152
+ test_command_with_arg - A test_command with one arg
153
+ test_command_with_args - A test_command with two args
154
+ test_command_with_options - A test_command with an optional argument
155
+ test_command_all_options - A test_command with all optional arguments
156
+ test_command_options_arr - A test_command with an options array
157
+ test_command_with_return - A test_command with a return argument
158
+ test_command_arg_timestamp - A test_command with a Timestamp argument and an unnecessarily
159
+ long description which should overflow when it tries to line up
160
+ with other descriptions.
161
+ test_command_arg_false - A test_command with a Boolean argument
162
+ test_command_arg_arr - A test_command with an array argument
163
+ test_command_arg_hash - A test_command with an Hash argument
164
+ END
165
+ assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
166
+ end
167
+
168
+ def test_get_summary_nil_base
169
+ base = nil
170
+ result = Rubycom.get_summary(base)
171
+ assert_equal('No Commands found for .', result)
172
+ end
173
+
174
+ def test_get_summary_no_singleton_base
175
+ base = UtilTestNoSingleton
176
+ result = Rubycom.get_summary(base)
177
+ assert_equal('No Commands found for UtilTestNoSingleton.', result)
178
+ end
179
+
180
+ def test_get_top_level_commands
181
+ test_command_list = [:test_command, :test_command_with_arg, :test_command_with_args, :test_command_with_options,
182
+ :test_command_all_options, :test_command_options_arr, :test_command_with_return,
183
+ :test_command_arg_timestamp, :test_command_arg_false, :test_command_arg_arr,
184
+ :test_command_arg_hash]
185
+ result_command_list = Rubycom.get_top_level_commands(UtilTestModule)
186
+ assert_equal(test_command_list.length, result_command_list.length)
187
+ test_command_list.each { |sym|
188
+ assert_includes(result_command_list, sym)
189
+ }
190
+ end
191
+
192
+ def test_get_top_level_commands_nil_base
193
+ test_command_list = []
194
+ result_command_list = Rubycom.get_top_level_commands(nil)
195
+ assert_equal(test_command_list.length, result_command_list.length)
196
+ test_command_list.each { |sym|
197
+ assert_includes(result_command_list, sym)
198
+ }
199
+ end
200
+
201
+ def test_get_commands_nil_base
202
+ test_command_list = []
203
+ result_command_list = Rubycom.get_commands(nil)
204
+ assert_equal(test_command_list.length, result_command_list.length)
205
+ test_command_list.each { |sym|
206
+ assert_includes(result_command_list, sym)
207
+ }
208
+ end
209
+
210
+ def test_get_top_level_commands_singleton_base
211
+ test_command_list = []
212
+ result_command_list = Rubycom.get_top_level_commands(UtilTestNoSingleton)
213
+ assert_equal(test_command_list.length, result_command_list.length)
214
+ test_command_list.each { |sym|
215
+ assert_includes(result_command_list, sym)
216
+ }
217
+ end
218
+
219
+ def test_parse_arg_string
220
+ test_arg = "test_arg"
221
+ result = Rubycom.parse_arg(test_arg)
222
+ expected = {arg: "test_arg"}
223
+ assert_equal(expected, result)
224
+ end
225
+
226
+ def test_parse_arg_fixnum
227
+ test_arg = "1234512415435"
228
+ result = Rubycom.parse_arg(test_arg)
229
+ expected = {arg: 1234512415435}
230
+ assert_equal(expected, result)
231
+ end
232
+
233
+ def test_parse_arg_float
234
+ test_arg = "12345.67890"
235
+ result = Rubycom.parse_arg(test_arg)
236
+ expected = {arg: 12345.67890}
237
+ assert_equal(expected, result)
238
+ end
239
+
240
+ def test_parse_arg_timestamp
241
+ time = Time.new
242
+ test_arg = time.to_s
243
+ result = Rubycom.parse_arg(test_arg)
244
+ expected = {arg: time}
245
+ assert_equal(expected[:arg].to_i, result[:arg].to_i)
246
+ end
247
+
248
+ def test_parse_arg_datetime
249
+ time = Time.new("2013-05-08 00:00:00 -0500")
250
+ date = Date.new(time.year, time.month, time.day)
251
+ test_arg = date.to_s
252
+ result = Rubycom.parse_arg(test_arg)
253
+ expected = {arg: date}
254
+ assert_equal(expected, result)
255
+ end
256
+
257
+ def test_parse_arg_array
258
+ test_arg = ["1", 2, "a", 'b']
259
+ result = Rubycom.parse_arg(test_arg.to_s)
260
+ expected = {arg: test_arg}
261
+ assert_equal(expected, result)
262
+ end
263
+
264
+ def test_parse_arg_hash
265
+ time = Time.new.to_s
266
+ test_arg = ":a: \"#{time}\""
267
+ result = Rubycom.parse_arg(test_arg.to_s)
268
+ expected = {arg: {a: time}}
269
+ assert_equal(expected, result)
270
+ end
271
+
272
+ def test_parse_arg_hash_group
273
+ test_arg = ":a: [1,2]\n:b: 1\n:c: test\n:d: 1.0\n:e: \"2013-05-08 23:21:52 -0500\"\n"
274
+ result = Rubycom.parse_arg(test_arg.to_s)
275
+ expected = {arg: {:a => [1, 2], :b => 1, :c => "test", :d => 1.0, :e => "2013-05-08 23:21:52 -0500"}}
276
+ assert_equal(expected, result)
277
+ end
278
+
279
+ def test_parse_arg_yaml
280
+ test_arg = {:a => ["1", 2, "a", 'b'], :b => 1, :c => "test", :d => "#{Time.now.to_s}"}
281
+ result = Rubycom.parse_arg(test_arg.to_yaml)
282
+ expected = {arg: test_arg}
283
+ assert_equal(expected, result)
284
+ end
285
+
286
+ def test_parse_arg_code
287
+ test_arg = 'def self.test_code_method; raise "Fail - test_parse_arg_code";end'
288
+ result = Rubycom.parse_arg(test_arg.to_s)
289
+ expected = {arg: 'def self.test_code_method; raise "Fail - test_parse_arg_code";end'}
290
+ assert_equal(expected, result)
291
+ end
292
+
293
+ def test_run
294
+ tst_out = ''
295
+
296
+ def tst_out.write(data)
297
+ self << data
298
+ end
299
+
300
+ o_stdout, $stdout = $stdout, tst_out
301
+ o_stderr, $stderr = $stderr, tst_out
302
+
303
+ base = UtilTestModule
304
+ args = ["test_command_with_arg", "HelloWorld"]
305
+ result = Rubycom.run(base, args)
306
+
307
+ expected = "test_arg=HelloWorld"
308
+ expected_out = expected
309
+ assert_equal(expected, result)
310
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
311
+ ensure
312
+ $stdout = o_stdout
313
+ $stderr = o_stderr
314
+ end
315
+
316
+ def test_run_help
317
+ tst_out = ''
318
+
319
+ def tst_out.write(data)
320
+ self << data
321
+ end
322
+
323
+ o_stdout, $stdout = $stdout, tst_out
324
+ o_stderr, $stderr = $stderr, tst_out
325
+
326
+ base = UtilTestModule
327
+ args = ["help"]
328
+ result = Rubycom.run(base, args)
329
+
330
+ expected = <<-END.gsub(/^ {4}/,'')
331
+ Usage:
332
+ UtilTestModule <command> [args]
333
+
334
+ Commands:
335
+ test_command - A basic test command
336
+ test_command_with_arg - A test_command with one arg
337
+ test_command_with_args - A test_command with two args
338
+ test_command_with_options - A test_command with an optional argument
339
+ test_command_all_options - A test_command with all optional arguments
340
+ test_command_options_arr - A test_command with an options array
341
+ test_command_with_return - A test_command with a return argument
342
+ test_command_arg_timestamp - A test_command with a Timestamp argument and an unnecessarily
343
+ long description which should overflow when
344
+ it tries to line up with other descriptions.
345
+ test_command_arg_false - A test_command with a Boolean argument
346
+ test_command_arg_arr - A test_command with an array argument
347
+ test_command_arg_hash - A test_command with an Hash argument
348
+ END
349
+ expected_out = expected
350
+ assert_equal(expected, result)
351
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
352
+ ensure
353
+ $stdout = o_stdout
354
+ $stderr = o_stderr
355
+ end
356
+
357
+ def test_run_nil_return
358
+ tst_out = ''
359
+
360
+ def tst_out.write(data)
361
+ self << data
362
+ end
363
+
364
+ o_stdout, $stdout = $stdout, tst_out
365
+ o_stderr, $stderr = $stderr, tst_out
366
+
367
+ base = UtilTestModule
368
+ args = ["test_command"]
369
+ result = Rubycom.run(base, args)
370
+
371
+ expected = nil
372
+ expected_out = "command test\n"
373
+ assert_equal(expected, result)
374
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
375
+ ensure
376
+ $stdout = o_stdout
377
+ $stderr = o_stderr
378
+ end
379
+
380
+ def test_run_hash_return
381
+ tst_out = ''
382
+
383
+ def tst_out.write(data)
384
+ self << data
385
+ end
386
+
387
+ o_stdout, $stdout = $stdout, tst_out
388
+ o_stderr, $stderr = $stderr, tst_out
389
+
390
+ base = UtilTestModule
391
+ time = Time.now.to_s
392
+ args = ["test_command_arg_timestamp", time]
393
+ result = Rubycom.run(base, args)
394
+
395
+ expected = {:test_time => Time.parse(time)}
396
+ expected_out = {test_time: Time.parse(time)}.to_yaml
397
+ assert_equal(expected, result)
398
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
399
+ ensure
400
+ $stdout = o_stdout
401
+ $stderr = o_stderr
402
+ end
403
+
404
+ def test_run_all_optional
405
+ tst_out = ''
406
+
407
+ def tst_out.write(data)
408
+ self << data
409
+ end
410
+
411
+ o_stdout, $stdout = $stdout, tst_out
412
+ o_stderr, $stderr = $stderr, tst_out
413
+
414
+ base = UtilTestModule
415
+ args = ["test_command_all_options"]
416
+ result = Rubycom.run(base, args)
417
+
418
+ e_test_arg = 'test_arg_default'
419
+ e_test_option = 'test_option_default'
420
+ expected = nil
421
+ expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
422
+ assert_equal(expected, result)
423
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
424
+ ensure
425
+ $stdout = o_stdout
426
+ $stderr = o_stderr
427
+ end
428
+
429
+ def test_run_all_opt_override_first
430
+ tst_out = ''
431
+
432
+ def tst_out.write(data)
433
+ self << data
434
+ end
435
+
436
+ o_stdout, $stdout = $stdout, tst_out
437
+ o_stderr, $stderr = $stderr, tst_out
438
+
439
+ base = UtilTestModule
440
+ args = ["test_command_all_options", "test_arg_modified"]
441
+ result = Rubycom.run(base, args)
442
+
443
+ e_test_arg = 'test_arg_modified'
444
+ e_test_option = 'test_option_default'
445
+ expected = nil
446
+ expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
447
+ assert_equal(expected, result)
448
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
449
+ ensure
450
+ $stdout = o_stdout
451
+ $stderr = o_stderr
452
+ end
453
+
454
+ def test_run_all_opt_override_first_alt
455
+ tst_out = ''
456
+
457
+ def tst_out.write(data)
458
+ self << data
459
+ end
460
+
461
+ o_stdout, $stdout = $stdout, tst_out
462
+ o_stderr, $stderr = $stderr, tst_out
463
+
464
+ base = UtilTestModule
465
+ args = ["test_command_all_options", "-test_arg=test_arg_modified"]
466
+ result = Rubycom.run(base, args)
467
+
468
+ e_test_arg = 'test_arg_modified'
469
+ e_test_option = 'test_option_default'
470
+ expected = nil
471
+ expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
472
+ assert_equal(expected, result)
473
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
474
+ ensure
475
+ $stdout = o_stdout
476
+ $stderr = o_stderr
477
+ end
478
+
479
+ def test_run_all_opt_override_second
480
+ tst_out = ''
481
+
482
+ def tst_out.write(data)
483
+ self << data
484
+ end
485
+
486
+ o_stdout, $stdout = $stdout, tst_out
487
+ o_stderr, $stderr = $stderr, tst_out
488
+
489
+ base = UtilTestModule
490
+ args = ["test_command_all_options", "-test_option=test_option_modified"]
491
+ result = Rubycom.run(base, args)
492
+
493
+ e_test_arg = 'test_arg_default'
494
+ e_test_option = 'test_option_modified'
495
+ expected = nil
496
+ expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
497
+ assert_equal(expected, result)
498
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
499
+ ensure
500
+ $stdout = o_stdout
501
+ $stderr = o_stderr
502
+ end
503
+
504
+ def test_run_all_opt_use_all_opt
505
+ tst_out = ''
506
+
507
+ def tst_out.write(data)
508
+ self << data
509
+ end
510
+
511
+ o_stdout, $stdout = $stdout, tst_out
512
+ o_stderr, $stderr = $stderr, tst_out
513
+
514
+ base = UtilTestModule
515
+ args = ["test_command_all_options", "-test_arg=test_arg_modified", "-test_option=test_option_modified"]
516
+ result = Rubycom.run(base, args)
517
+
518
+ e_test_arg = 'test_arg_modified'
519
+ e_test_option = 'test_option_modified'
520
+ expected = nil
521
+ expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
522
+ assert_equal(expected, result)
523
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
524
+ ensure
525
+ $stdout = o_stdout
526
+ $stderr = o_stderr
527
+ end
528
+
529
+ def test_run_all_opt_reverse
530
+ tst_out = ''
531
+
532
+ def tst_out.write(data)
533
+ self << data
534
+ end
535
+
536
+ o_stdout, $stdout = $stdout, tst_out
537
+ o_stderr, $stderr = $stderr, tst_out
538
+
539
+ base = UtilTestModule
540
+ args = ["test_command_all_options", "-test_option=test_option_modified", "-test_arg=test_arg_modified"]
541
+ result = Rubycom.run(base, args)
542
+
543
+ e_test_arg = 'test_arg_modified'
544
+ e_test_option = 'test_option_modified'
545
+ expected = nil
546
+ expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
547
+ assert_equal(expected, result)
548
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
549
+ ensure
550
+ $stdout = o_stdout
551
+ $stderr = o_stderr
552
+ end
553
+
554
+ def test_run_options_arr
555
+ tst_out = ''
556
+
557
+ def tst_out.write(data)
558
+ self << data
559
+ end
560
+
561
+ o_stdout, $stdout = $stdout, tst_out
562
+ o_stderr, $stderr = $stderr, tst_out
563
+
564
+ base = UtilTestModule
565
+ args = ["test_command_options_arr", "test_option1", "test_option2", 1.0, false]
566
+ result = Rubycom.run(base, args)
567
+
568
+ e_test_arg = 'test_option1'
569
+ e_test_options = ["test_option2", 1.0, false]
570
+ expected = nil
571
+ expected_out = "Output is test_option=#{e_test_arg},test_option_arr=#{e_test_options}\n"
572
+ assert_equal(expected, result)
573
+ assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
574
+ ensure
575
+ $stdout = o_stdout
576
+ $stderr = o_stderr
577
+ end
578
+
579
+ def test_run_missing_required_arg
580
+ tst_out = ''
581
+
582
+ def tst_out.puts(data)
583
+ self << data.to_s << "\n"
584
+ nil
585
+ end
586
+
587
+ def tst_out.write(data)
588
+ self << data
589
+ end
590
+
591
+ o_stdout, $stdout = $stdout, tst_out
592
+ o_stderr, $stderr = $stderr, tst_out
593
+
594
+ base = UtilTestModule
595
+ args = ["test_command_with_return", "-test_option_int=2"]
596
+ result = Rubycom.run(base, args)
597
+
598
+ expected = nil
599
+ expected_out = "No argument available for test_arg"
600
+ assert_equal(expected, result)
601
+ assert_equal(expected_out, tst_out.split(/\n|\r|\r\n/).first)
602
+ ensure
603
+ $stdout = o_stdout
604
+ $stderr = o_stderr
605
+ end
606
+
607
+ def test_run_composite
608
+ tst_out = ''
609
+
610
+ def tst_out.write(data)
611
+ self << data
612
+ end
613
+
614
+ o_stdout, $stdout = $stdout, tst_out
615
+ o_stderr, $stderr = $stderr, tst_out
616
+
617
+ base = UtilTestComposite
618
+ args = ["test_composite_command", "Hello Composite"]
619
+ result = Rubycom.run(base, args)
620
+
621
+ expected = "Hello Composite"
622
+ expected_out = "Hello Composite"
623
+ assert_equal(expected, result)
624
+ assert_equal(expected_out, tst_out.split(/\n|\r|\r\n/).first)
625
+ ensure
626
+ $stdout = o_stdout
627
+ $stderr = o_stderr
628
+ end
629
+
630
+ end