rubycom 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +4 -0
- data/Rakefile +18 -9
- data/lib/rubycom.rb +119 -331
- data/lib/rubycom/arguments.rb +102 -0
- data/lib/rubycom/commands.rb +63 -0
- data/lib/rubycom/documentation.rb +204 -0
- data/lib/rubycom/version.rb +1 -1
- data/test/rubycom/arguments_test.rb +148 -0
- data/test/rubycom/commands_test.rb +51 -0
- data/test/rubycom/documentation_test.rb +186 -0
- data/test/rubycom/rubycom_test.rb +527 -0
- data/test/rubycom/util_test_composite.rb +1 -0
- metadata +13 -4
- data/test/rubycom/test_rubycom.rb +0 -762
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubycom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Purcell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,9 +96,15 @@ files:
|
|
96
96
|
- README.md
|
97
97
|
- Rakefile
|
98
98
|
- lib/rubycom.rb
|
99
|
+
- lib/rubycom/arguments.rb
|
100
|
+
- lib/rubycom/commands.rb
|
101
|
+
- lib/rubycom/documentation.rb
|
99
102
|
- lib/rubycom/version.rb
|
100
103
|
- rubycom.gemspec
|
101
|
-
- test/rubycom/
|
104
|
+
- test/rubycom/arguments_test.rb
|
105
|
+
- test/rubycom/commands_test.rb
|
106
|
+
- test/rubycom/documentation_test.rb
|
107
|
+
- test/rubycom/rubycom_test.rb
|
102
108
|
- test/rubycom/util_test_composite.rb
|
103
109
|
- test/rubycom/util_test_job.yaml
|
104
110
|
- test/rubycom/util_test_module.rb
|
@@ -129,7 +135,10 @@ signing_key:
|
|
129
135
|
specification_version: 4
|
130
136
|
summary: Converts singleton methods to command-line functions upon inclusion.
|
131
137
|
test_files:
|
132
|
-
- test/rubycom/
|
138
|
+
- test/rubycom/arguments_test.rb
|
139
|
+
- test/rubycom/commands_test.rb
|
140
|
+
- test/rubycom/documentation_test.rb
|
141
|
+
- test/rubycom/rubycom_test.rb
|
133
142
|
- test/rubycom/util_test_composite.rb
|
134
143
|
- test/rubycom/util_test_job.yaml
|
135
144
|
- test/rubycom/util_test_module.rb
|
@@ -1,762 +0,0 @@
|
|
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
|
-
expected = "test_command_with_options - A test_command with an optional argument\n"
|
73
|
-
result = Rubycom.get_command_summary(base, command_name)
|
74
|
-
assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_get_command_summary_no_command
|
78
|
-
base = UtilTestModule
|
79
|
-
command_name = ''
|
80
|
-
result = Rubycom.get_command_summary(base, command_name)
|
81
|
-
assert_equal('No command specified.', result)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_get_command_summary_nil_base
|
85
|
-
base = nil
|
86
|
-
command_name = 'test_command_with_options'
|
87
|
-
assert_raise(Rubycom::CLIError) { Rubycom.get_command_summary(base, command_name) }
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_get_command_summary_nil_command
|
91
|
-
base = UtilTestModule
|
92
|
-
command_name = nil
|
93
|
-
result = Rubycom.get_command_summary(base, command_name)
|
94
|
-
assert_equal('No command specified.', result)
|
95
|
-
end
|
96
|
-
|
97
|
-
def test_get_command_summary_wrong_base
|
98
|
-
base = UtilTestNoSingleton
|
99
|
-
command_name = 'test_command_with_options'
|
100
|
-
assert_raise(Rubycom::CLIError) { Rubycom.get_command_summary(base, command_name) }
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_get_command_summary_bad_command
|
104
|
-
base = UtilTestModule
|
105
|
-
command_name = '!_fail_command_'
|
106
|
-
assert_raise(Rubycom::CLIError) { Rubycom.get_command_summary(base, command_name) }
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_get_usage
|
110
|
-
base = UtilTestModule
|
111
|
-
result = Rubycom.get_usage(base)
|
112
|
-
expected = <<-END.gsub(/^ {4}/, '')
|
113
|
-
Usage:
|
114
|
-
UtilTestModule <command> [args]
|
115
|
-
|
116
|
-
Commands:
|
117
|
-
test_command - A basic test command
|
118
|
-
test_command_no_docs
|
119
|
-
test_command_with_arg - A test_command with one arg
|
120
|
-
test_command_arg_named_arg - A test_command with an arg named arg
|
121
|
-
test_command_with_args - A test_command with two args
|
122
|
-
test_command_with_options - A test_command with an optional argument
|
123
|
-
test_command_all_options - A test_command with all optional arguments
|
124
|
-
test_command_options_arr - A test_command with an options array
|
125
|
-
test_command_with_return - A test_command with a return argument
|
126
|
-
test_command_arg_timestamp - A test_command with a Timestamp argument and an unnecessarily
|
127
|
-
long description which should overflow when
|
128
|
-
it tries to line up with other descriptions.
|
129
|
-
test_command_arg_false - A test_command with a Boolean argument
|
130
|
-
test_command_arg_arr - A test_command with an array argument
|
131
|
-
test_command_arg_hash - A test_command with an Hash argument
|
132
|
-
test_command_mixed_options - A test_command with several mixed options
|
133
|
-
|
134
|
-
END
|
135
|
-
assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_get_usage_nil_base
|
139
|
-
base = nil
|
140
|
-
result = Rubycom.get_usage(base)
|
141
|
-
assert_equal('', result)
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_get_usage_no_singleton_base
|
145
|
-
base = UtilTestNoSingleton
|
146
|
-
result = Rubycom.get_usage(base)
|
147
|
-
assert_equal('', result)
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_get_summary
|
151
|
-
base = UtilTestModule
|
152
|
-
result = Rubycom.get_summary(base)
|
153
|
-
expected = <<-END.gsub(/^ {4}/, '')
|
154
|
-
Commands:
|
155
|
-
test_command - A basic test command
|
156
|
-
test_command_no_docs
|
157
|
-
test_command_with_arg - A test_command with one arg
|
158
|
-
test_command_arg_named_arg - A test_command with an arg named arg
|
159
|
-
test_command_with_args - A test_command with two args
|
160
|
-
test_command_with_options - A test_command with an optional argument
|
161
|
-
test_command_all_options - A test_command with all optional arguments
|
162
|
-
test_command_options_arr - A test_command with an options array
|
163
|
-
test_command_with_return - A test_command with a return argument
|
164
|
-
test_command_arg_timestamp - A test_command with a Timestamp argument and an unnecessarily
|
165
|
-
long description which should overflow when
|
166
|
-
it tries to line up with other descriptions.
|
167
|
-
test_command_arg_false - A test_command with a Boolean argument
|
168
|
-
test_command_arg_arr - A test_command with an array argument
|
169
|
-
test_command_arg_hash - A test_command with an Hash argument
|
170
|
-
test_command_mixed_options - A test_command with several mixed options
|
171
|
-
END
|
172
|
-
assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_get_summary_nil_base
|
176
|
-
base = nil
|
177
|
-
result = Rubycom.get_summary(base)
|
178
|
-
assert_equal('No Commands found for .', result)
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_get_summary_no_singleton_base
|
182
|
-
base = UtilTestNoSingleton
|
183
|
-
result = Rubycom.get_summary(base)
|
184
|
-
assert_equal('No Commands found for UtilTestNoSingleton.', result)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_get_top_level_commands
|
188
|
-
test_command_list = [:test_command, :test_command_no_docs, :test_command_with_arg, :test_command_arg_named_arg,
|
189
|
-
:test_command_with_args, :test_command_with_options, :test_command_all_options,
|
190
|
-
:test_command_options_arr, :test_command_with_return, :test_command_arg_timestamp,
|
191
|
-
:test_command_arg_false, :test_command_arg_arr, :test_command_arg_hash,
|
192
|
-
:test_command_mixed_options]
|
193
|
-
result_command_list = Rubycom.get_top_level_commands(UtilTestModule)
|
194
|
-
assert_equal(test_command_list.length, result_command_list.length)
|
195
|
-
test_command_list.each { |sym|
|
196
|
-
assert_includes(result_command_list, sym)
|
197
|
-
}
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_get_top_level_commands_nil_base
|
201
|
-
test_command_list = []
|
202
|
-
result_command_list = Rubycom.get_top_level_commands(nil)
|
203
|
-
assert_equal(test_command_list.length, result_command_list.length)
|
204
|
-
test_command_list.each { |sym|
|
205
|
-
assert_includes(result_command_list, sym)
|
206
|
-
}
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_get_commands_nil_base
|
210
|
-
test_command_list = []
|
211
|
-
result_command_list = Rubycom.get_commands(nil)
|
212
|
-
assert_equal(test_command_list.length, result_command_list.length)
|
213
|
-
test_command_list.each { |sym|
|
214
|
-
assert_includes(result_command_list, sym)
|
215
|
-
}
|
216
|
-
end
|
217
|
-
|
218
|
-
def test_get_top_level_commands_singleton_base
|
219
|
-
test_command_list = []
|
220
|
-
result_command_list = Rubycom.get_top_level_commands(UtilTestNoSingleton)
|
221
|
-
assert_equal(test_command_list.length, result_command_list.length)
|
222
|
-
test_command_list.each { |sym|
|
223
|
-
assert_includes(result_command_list, sym)
|
224
|
-
}
|
225
|
-
end
|
226
|
-
|
227
|
-
def test_parse_arg_string
|
228
|
-
test_arg = "test_arg"
|
229
|
-
result = Rubycom.parse_arg(test_arg)
|
230
|
-
expected = {rubycom_non_opt_arg: "test_arg"}
|
231
|
-
assert_equal(expected, result)
|
232
|
-
end
|
233
|
-
|
234
|
-
def test_parse_arg_fixnum
|
235
|
-
test_arg = "1234512415435"
|
236
|
-
result = Rubycom.parse_arg(test_arg)
|
237
|
-
expected = {rubycom_non_opt_arg: 1234512415435}
|
238
|
-
assert_equal(expected, result)
|
239
|
-
end
|
240
|
-
|
241
|
-
def test_parse_arg_float
|
242
|
-
test_arg = "12345.67890"
|
243
|
-
result = Rubycom.parse_arg(test_arg)
|
244
|
-
expected = {rubycom_non_opt_arg: 12345.67890}
|
245
|
-
assert_equal(expected, result)
|
246
|
-
end
|
247
|
-
|
248
|
-
def test_parse_arg_timestamp
|
249
|
-
time = Time.new
|
250
|
-
test_arg = time.to_s
|
251
|
-
result = Rubycom.parse_arg(test_arg)
|
252
|
-
expected = {rubycom_non_opt_arg: time}
|
253
|
-
assert_equal(expected[:rubycom_non_opt_arg].to_i, result[:rubycom_non_opt_arg].to_i)
|
254
|
-
end
|
255
|
-
|
256
|
-
def test_parse_arg_datetime
|
257
|
-
time = Time.new("2013-05-08 00:00:00 -0500")
|
258
|
-
date = Date.new(time.year, time.month, time.day)
|
259
|
-
test_arg = date.to_s
|
260
|
-
result = Rubycom.parse_arg(test_arg)
|
261
|
-
expected = {rubycom_non_opt_arg: date}
|
262
|
-
assert_equal(expected, result)
|
263
|
-
end
|
264
|
-
|
265
|
-
def test_parse_arg_array
|
266
|
-
test_arg = ["1", 2, "a", 'b']
|
267
|
-
result = Rubycom.parse_arg(test_arg.to_s)
|
268
|
-
expected = {rubycom_non_opt_arg: test_arg}
|
269
|
-
assert_equal(expected, result)
|
270
|
-
end
|
271
|
-
|
272
|
-
def test_parse_arg_hash
|
273
|
-
time = Time.new.to_s
|
274
|
-
test_arg = ":a: \"#{time}\""
|
275
|
-
result = Rubycom.parse_arg(test_arg.to_s)
|
276
|
-
expected = {rubycom_non_opt_arg: {a: time}}
|
277
|
-
assert_equal(expected, result)
|
278
|
-
end
|
279
|
-
|
280
|
-
def test_parse_arg_hash_group
|
281
|
-
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"
|
282
|
-
result = Rubycom.parse_arg(test_arg.to_s)
|
283
|
-
expected = {rubycom_non_opt_arg: {:a => [1, 2], :b => 1, :c => "test", :d => 1.0, :e => "2013-05-08 23:21:52 -0500"}}
|
284
|
-
assert_equal(expected, result)
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_parse_arg_yaml
|
288
|
-
test_arg = {:a => ["1", 2, "a", 'b'], :b => 1, :c => "test", :d => "#{Time.now.to_s}"}
|
289
|
-
result = Rubycom.parse_arg(test_arg.to_yaml)
|
290
|
-
expected = {rubycom_non_opt_arg: test_arg}
|
291
|
-
assert_equal(expected, result)
|
292
|
-
end
|
293
|
-
|
294
|
-
def test_parse_arg_code
|
295
|
-
test_arg = 'def self.test_code_method; raise "Fail - test_parse_arg_code";end'
|
296
|
-
result = Rubycom.parse_arg(test_arg.to_s)
|
297
|
-
expected = {rubycom_non_opt_arg: 'def self.test_code_method; raise "Fail - test_parse_arg_code";end'}
|
298
|
-
assert_equal(expected, result)
|
299
|
-
end
|
300
|
-
|
301
|
-
def test_parse_opt_string_eq
|
302
|
-
test_arg = "-test_arg=\"test\""
|
303
|
-
result = Rubycom.parse_arg(test_arg)
|
304
|
-
expected = {test_arg: "test"}
|
305
|
-
assert_equal(expected, result)
|
306
|
-
end
|
307
|
-
|
308
|
-
def test_parse_opt_string_sp
|
309
|
-
test_arg = "-test_arg \"test\""
|
310
|
-
result = Rubycom.parse_arg(test_arg)
|
311
|
-
expected = {test_arg: "test"}
|
312
|
-
assert_equal(expected, result)
|
313
|
-
end
|
314
|
-
|
315
|
-
def test_parse_opt_long_string_eq
|
316
|
-
test_arg = "--test_arg=\"test\""
|
317
|
-
result = Rubycom.parse_arg(test_arg)
|
318
|
-
expected = {test_arg: "test"}
|
319
|
-
assert_equal(expected, result)
|
320
|
-
end
|
321
|
-
|
322
|
-
def test_parse_opt_long_string_sp
|
323
|
-
test_arg = "--test_arg \"test\""
|
324
|
-
result = Rubycom.parse_arg(test_arg)
|
325
|
-
expected = {test_arg: "test"}
|
326
|
-
assert_equal(expected, result)
|
327
|
-
end
|
328
|
-
|
329
|
-
def test_get_param_definitions
|
330
|
-
test_method = UtilTestModule.public_method('test_command_with_args')
|
331
|
-
expected = {:test_arg=>{:def=>"test_arg", :type=>:req, :default=>:nil_rubycom_required_param}, :another_test_arg=>{:def=>"another_test_arg", :type=>:req, :default=>:nil_rubycom_required_param}}
|
332
|
-
result = Rubycom.get_param_definitions(test_method)
|
333
|
-
assert_equal(expected, result)
|
334
|
-
end
|
335
|
-
|
336
|
-
def test_get_param_definitions_no_args
|
337
|
-
test_method = UtilTestModule.public_method('test_command')
|
338
|
-
expected = {}
|
339
|
-
result = Rubycom.get_param_definitions(test_method)
|
340
|
-
assert_equal(expected, result)
|
341
|
-
end
|
342
|
-
|
343
|
-
def test_get_param_definitions_arr_param
|
344
|
-
test_method = UtilTestModule.public_method('test_command_options_arr')
|
345
|
-
expected = {:test_option=>{:def=>"test_option=\"test_option_default\"", :type=>:opt, :default=>"test_option_default"}, :test_options=>{:def=>"*test_options", :type=>:rest, :default=>:nil_rubycom_required_param}}
|
346
|
-
result = Rubycom.get_param_definitions(test_method)
|
347
|
-
assert_equal(expected, result)
|
348
|
-
end
|
349
|
-
|
350
|
-
def test_get_param_definitions_all_options
|
351
|
-
test_method = UtilTestModule.public_method('test_command_all_options')
|
352
|
-
expected = {:test_arg=>{:def=>"test_arg='test_arg_default'", :type=>:opt, :default=>"test_arg_default"}, :test_option=>{:def=>"test_option='test_option_default'", :type=>:opt, :default=>"test_option_default"}}
|
353
|
-
result = Rubycom.get_param_definitions(test_method)
|
354
|
-
assert_equal(expected, result)
|
355
|
-
end
|
356
|
-
|
357
|
-
def test_get_param_definitions_mixed
|
358
|
-
test_method = UtilTestModule.public_method('test_command_with_options')
|
359
|
-
expected = {:test_arg=>{:def=>"test_arg", :type=>:req, :default=>:nil_rubycom_required_param}, :test_option=>{:def=>"test_option='option_default'", :type=>:opt, :default=>"option_default"}}
|
360
|
-
result = Rubycom.get_param_definitions(test_method)
|
361
|
-
assert_equal(expected, result)
|
362
|
-
end
|
363
|
-
|
364
|
-
def test_run
|
365
|
-
tst_out = ''
|
366
|
-
|
367
|
-
def tst_out.write(data)
|
368
|
-
self << data
|
369
|
-
end
|
370
|
-
|
371
|
-
o_stdout, $stdout = $stdout, tst_out
|
372
|
-
o_stderr, $stderr = $stderr, tst_out
|
373
|
-
|
374
|
-
base = UtilTestModule
|
375
|
-
args = ["test_command_with_arg", "HelloWorld"]
|
376
|
-
result = Rubycom.run(base, args)
|
377
|
-
|
378
|
-
expected = "test_arg=HelloWorld"
|
379
|
-
expected_out = expected
|
380
|
-
assert_equal(expected, result)
|
381
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
382
|
-
ensure
|
383
|
-
$stdout = o_stdout
|
384
|
-
$stderr = o_stderr
|
385
|
-
end
|
386
|
-
|
387
|
-
def test_run_help
|
388
|
-
tst_out = ''
|
389
|
-
|
390
|
-
def tst_out.write(data)
|
391
|
-
self << data
|
392
|
-
end
|
393
|
-
|
394
|
-
o_stdout, $stdout = $stdout, tst_out
|
395
|
-
o_stderr, $stderr = $stderr, tst_out
|
396
|
-
|
397
|
-
base = UtilTestModule
|
398
|
-
args = ["help"]
|
399
|
-
result = Rubycom.run(base, args)
|
400
|
-
|
401
|
-
expected = <<-END.gsub(/^ {4}/,'')
|
402
|
-
Usage:
|
403
|
-
UtilTestModule <command> [args]
|
404
|
-
|
405
|
-
Commands:
|
406
|
-
test_command - A basic test command
|
407
|
-
test_command_no_docs
|
408
|
-
test_command_with_arg - A test_command with one arg
|
409
|
-
test_command_arg_named_arg - A test_command with an arg named arg
|
410
|
-
test_command_with_args - A test_command with two args
|
411
|
-
test_command_with_options - A test_command with an optional argument
|
412
|
-
test_command_all_options - A test_command with all optional arguments
|
413
|
-
test_command_options_arr - A test_command with an options array
|
414
|
-
test_command_with_return - A test_command with a return argument
|
415
|
-
test_command_arg_timestamp - A test_command with a Timestamp argument and an unnecessarily
|
416
|
-
long description which should overflow when
|
417
|
-
it tries to line up with other descriptions.
|
418
|
-
test_command_arg_false - A test_command with a Boolean argument
|
419
|
-
test_command_arg_arr - A test_command with an array argument
|
420
|
-
test_command_arg_hash - A test_command with an Hash argument
|
421
|
-
test_command_mixed_options - A test_command with several mixed options
|
422
|
-
END
|
423
|
-
expected_out = expected
|
424
|
-
assert_equal(expected.gsub(/\n|\r|\s/, ''), result.gsub(/\n|\r|\s/, ''))
|
425
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
426
|
-
ensure
|
427
|
-
$stdout = o_stdout
|
428
|
-
$stderr = o_stderr
|
429
|
-
end
|
430
|
-
|
431
|
-
def test_run_nil_return
|
432
|
-
tst_out = ''
|
433
|
-
|
434
|
-
def tst_out.write(data)
|
435
|
-
self << data
|
436
|
-
end
|
437
|
-
|
438
|
-
o_stdout, $stdout = $stdout, tst_out
|
439
|
-
o_stderr, $stderr = $stderr, tst_out
|
440
|
-
|
441
|
-
base = UtilTestModule
|
442
|
-
args = ["test_command"]
|
443
|
-
result = Rubycom.run(base, args)
|
444
|
-
|
445
|
-
expected = nil
|
446
|
-
expected_out = "command test\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_hash_return
|
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
|
-
time = Time.now.to_s
|
466
|
-
args = ["test_command_arg_timestamp", time]
|
467
|
-
result = Rubycom.run(base, args)
|
468
|
-
|
469
|
-
expected = {:test_time => Time.parse(time)}
|
470
|
-
expected_out = {test_time: Time.parse(time)}.to_yaml
|
471
|
-
assert_equal(expected, result)
|
472
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
473
|
-
ensure
|
474
|
-
$stdout = o_stdout
|
475
|
-
$stderr = o_stderr
|
476
|
-
end
|
477
|
-
|
478
|
-
def test_run_all_optional
|
479
|
-
tst_out = ''
|
480
|
-
|
481
|
-
def tst_out.write(data)
|
482
|
-
self << data
|
483
|
-
end
|
484
|
-
|
485
|
-
o_stdout, $stdout = $stdout, tst_out
|
486
|
-
o_stderr, $stderr = $stderr, tst_out
|
487
|
-
|
488
|
-
base = UtilTestModule
|
489
|
-
args = ["test_command_all_options"]
|
490
|
-
result = Rubycom.run(base, args)
|
491
|
-
|
492
|
-
e_test_arg = 'test_arg_default'
|
493
|
-
e_test_option = 'test_option_default'
|
494
|
-
expected = nil
|
495
|
-
expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
|
496
|
-
assert_equal(expected, result)
|
497
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
498
|
-
ensure
|
499
|
-
$stdout = o_stdout
|
500
|
-
$stderr = o_stderr
|
501
|
-
end
|
502
|
-
|
503
|
-
def test_run_all_opt_override_first
|
504
|
-
tst_out = ''
|
505
|
-
|
506
|
-
def tst_out.write(data)
|
507
|
-
self << data
|
508
|
-
end
|
509
|
-
|
510
|
-
o_stdout, $stdout = $stdout, tst_out
|
511
|
-
o_stderr, $stderr = $stderr, tst_out
|
512
|
-
|
513
|
-
base = UtilTestModule
|
514
|
-
args = ["test_command_all_options", "test_arg_modified"]
|
515
|
-
result = Rubycom.run(base, args)
|
516
|
-
|
517
|
-
e_test_arg = 'test_arg_modified'
|
518
|
-
e_test_option = 'test_option_default'
|
519
|
-
expected = nil
|
520
|
-
expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
|
521
|
-
assert_equal(expected, result)
|
522
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
523
|
-
ensure
|
524
|
-
$stdout = o_stdout
|
525
|
-
$stderr = o_stderr
|
526
|
-
end
|
527
|
-
|
528
|
-
def test_run_all_opt_override_first_alt
|
529
|
-
tst_out = ''
|
530
|
-
|
531
|
-
def tst_out.write(data)
|
532
|
-
self << data
|
533
|
-
end
|
534
|
-
|
535
|
-
o_stdout, $stdout = $stdout, tst_out
|
536
|
-
o_stderr, $stderr = $stderr, tst_out
|
537
|
-
|
538
|
-
base = UtilTestModule
|
539
|
-
args = ["test_command_all_options", "-test_arg=test_arg_modified"]
|
540
|
-
result = Rubycom.run(base, args)
|
541
|
-
|
542
|
-
e_test_arg = 'test_arg_modified'
|
543
|
-
e_test_option = 'test_option_default'
|
544
|
-
expected = nil
|
545
|
-
expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
|
546
|
-
assert_equal(expected, result)
|
547
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
548
|
-
ensure
|
549
|
-
$stdout = o_stdout
|
550
|
-
$stderr = o_stderr
|
551
|
-
end
|
552
|
-
|
553
|
-
def test_run_all_opt_override_second
|
554
|
-
tst_out = ''
|
555
|
-
|
556
|
-
def tst_out.write(data)
|
557
|
-
self << data
|
558
|
-
end
|
559
|
-
|
560
|
-
o_stdout, $stdout = $stdout, tst_out
|
561
|
-
o_stderr, $stderr = $stderr, tst_out
|
562
|
-
|
563
|
-
base = UtilTestModule
|
564
|
-
args = ["test_command_all_options", "-test_option=test_option_modified"]
|
565
|
-
result = Rubycom.run(base, args)
|
566
|
-
|
567
|
-
e_test_arg = 'test_arg_default'
|
568
|
-
e_test_option = 'test_option_modified'
|
569
|
-
expected = nil
|
570
|
-
expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
|
571
|
-
assert_equal(expected, result)
|
572
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
573
|
-
ensure
|
574
|
-
$stdout = o_stdout
|
575
|
-
$stderr = o_stderr
|
576
|
-
end
|
577
|
-
|
578
|
-
def test_run_all_opt_use_all_opt
|
579
|
-
tst_out = ''
|
580
|
-
|
581
|
-
def tst_out.write(data)
|
582
|
-
self << data
|
583
|
-
end
|
584
|
-
|
585
|
-
o_stdout, $stdout = $stdout, tst_out
|
586
|
-
o_stderr, $stderr = $stderr, tst_out
|
587
|
-
|
588
|
-
base = UtilTestModule
|
589
|
-
args = ["test_command_all_options", "-test_arg=test_arg_modified", "-test_option=test_option_modified"]
|
590
|
-
result = Rubycom.run(base, args)
|
591
|
-
|
592
|
-
e_test_arg = 'test_arg_modified'
|
593
|
-
e_test_option = 'test_option_modified'
|
594
|
-
expected = nil
|
595
|
-
expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
|
596
|
-
assert_equal(expected, result)
|
597
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
598
|
-
ensure
|
599
|
-
$stdout = o_stdout
|
600
|
-
$stderr = o_stderr
|
601
|
-
end
|
602
|
-
|
603
|
-
def test_run_all_opt_reverse
|
604
|
-
tst_out = ''
|
605
|
-
|
606
|
-
def tst_out.write(data)
|
607
|
-
self << data
|
608
|
-
end
|
609
|
-
|
610
|
-
o_stdout, $stdout = $stdout, tst_out
|
611
|
-
o_stderr, $stderr = $stderr, tst_out
|
612
|
-
|
613
|
-
base = UtilTestModule
|
614
|
-
args = ["test_command_all_options", "-test_option=test_option_modified", "-test_arg=test_arg_modified"]
|
615
|
-
result = Rubycom.run(base, args)
|
616
|
-
|
617
|
-
e_test_arg = 'test_arg_modified'
|
618
|
-
e_test_option = 'test_option_modified'
|
619
|
-
expected = nil
|
620
|
-
expected_out = "Output is test_arg=#{e_test_arg},test_option=#{e_test_option}\n"
|
621
|
-
assert_equal(expected, result)
|
622
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
623
|
-
ensure
|
624
|
-
$stdout = o_stdout
|
625
|
-
$stderr = o_stderr
|
626
|
-
end
|
627
|
-
|
628
|
-
def test_run_options_arr
|
629
|
-
tst_out = ''
|
630
|
-
|
631
|
-
def tst_out.write(data)
|
632
|
-
self << data
|
633
|
-
end
|
634
|
-
|
635
|
-
o_stdout, $stdout = $stdout, tst_out
|
636
|
-
o_stderr, $stderr = $stderr, tst_out
|
637
|
-
|
638
|
-
base = UtilTestModule
|
639
|
-
args = ["test_command_options_arr", "test_option1", "test_option2", 1.0, false]
|
640
|
-
result = Rubycom.run(base, args)
|
641
|
-
|
642
|
-
e_test_arg = 'test_option1'
|
643
|
-
e_test_options = ["test_option2", 1.0, false]
|
644
|
-
expected = nil
|
645
|
-
expected_out = "Output is test_option=#{e_test_arg},test_option_arr=#{e_test_options}\n"
|
646
|
-
assert_equal(expected, result)
|
647
|
-
assert_equal(expected_out.gsub(/\n|\r|\s/, ''), tst_out.gsub(/\n|\r|\s/, ''))
|
648
|
-
ensure
|
649
|
-
$stdout = o_stdout
|
650
|
-
$stderr = o_stderr
|
651
|
-
end
|
652
|
-
|
653
|
-
def test_run_missing_required_arg
|
654
|
-
tst_out = ''
|
655
|
-
|
656
|
-
def tst_out.puts(data)
|
657
|
-
self << data.to_s << "\n"
|
658
|
-
nil
|
659
|
-
end
|
660
|
-
|
661
|
-
def tst_out.write(data)
|
662
|
-
self << data
|
663
|
-
end
|
664
|
-
|
665
|
-
o_stdout, $stdout = $stdout, tst_out
|
666
|
-
o_stderr, $stderr = $stderr, tst_out
|
667
|
-
|
668
|
-
base = UtilTestModule
|
669
|
-
args = ["test_command_with_return", "-test_option_int=2"]
|
670
|
-
result = Rubycom.run(base, args)
|
671
|
-
|
672
|
-
expected = nil
|
673
|
-
expected_out = "No argument available for test_arg\n"
|
674
|
-
|
675
|
-
assert_equal(expected, result)
|
676
|
-
assert_equal(expected_out, tst_out.lines.first)
|
677
|
-
Rubycom.get_command_usage(base,args[0],args[1..-1]).each_line{|expected_line|
|
678
|
-
assert_equal(true, tst_out.lines.include?(expected_line))
|
679
|
-
}
|
680
|
-
ensure
|
681
|
-
$stdout = o_stdout
|
682
|
-
$stderr = o_stderr
|
683
|
-
end
|
684
|
-
|
685
|
-
def test_run_composite
|
686
|
-
tst_out = ''
|
687
|
-
|
688
|
-
def tst_out.write(data)
|
689
|
-
self << data
|
690
|
-
end
|
691
|
-
|
692
|
-
o_stdout, $stdout = $stdout, tst_out
|
693
|
-
o_stderr, $stderr = $stderr, tst_out
|
694
|
-
|
695
|
-
base = UtilTestComposite
|
696
|
-
args = ["test_composite_command", "Hello Composite"]
|
697
|
-
result = Rubycom.run(base, args)
|
698
|
-
|
699
|
-
expected = "Hello Composite"
|
700
|
-
expected_out = "Hello Composite"
|
701
|
-
assert_equal(expected, result)
|
702
|
-
assert_equal(expected_out, tst_out.split(/\n|\r|\r\n/).first)
|
703
|
-
ensure
|
704
|
-
$stdout = o_stdout
|
705
|
-
$stderr = o_stderr
|
706
|
-
end
|
707
|
-
|
708
|
-
def test_full_run_mixed_args
|
709
|
-
mod = "util_test_module.rb"
|
710
|
-
command = "test_command_mixed_options"
|
711
|
-
args = "testing_arg \"[test1, test2]\" -test_opt='testing_option' \"{a: 'test_hsh_arg'}\" -test_bool=true some other args"
|
712
|
-
expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=true test_rest=["some", "other", "args"]'+"\n"
|
713
|
-
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
714
|
-
assert_equal(expected, result)
|
715
|
-
end
|
716
|
-
|
717
|
-
def test_full_run_mixed_args_solid_arr
|
718
|
-
mod = "util_test_module.rb"
|
719
|
-
command = "test_command_mixed_options"
|
720
|
-
args = "testing_arg [test1,test2] -test_opt='testing_option' \"{a: 'test_hsh_arg'}\" some other args"
|
721
|
-
expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=some test_rest=["other", "args"]'+"\n"
|
722
|
-
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
723
|
-
assert_equal(expected, result)
|
724
|
-
end
|
725
|
-
|
726
|
-
def test_full_run_mixed_args_quoted_solid_arr
|
727
|
-
mod = "util_test_module.rb"
|
728
|
-
command = "test_command_mixed_options"
|
729
|
-
args = 'testing_arg "[test1,test2]" -test_opt="testing_option" "{a: "test_hsh_arg"}" -test_bool=false some other args'
|
730
|
-
expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=false test_rest=["some", "other", "args"]'+"\n"
|
731
|
-
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
732
|
-
assert_equal(expected, result)
|
733
|
-
end
|
734
|
-
|
735
|
-
def test_full_run_mixed_args_odd_sp
|
736
|
-
mod = "util_test_module.rb"
|
737
|
-
command = "test_command_mixed_options"
|
738
|
-
args = 'testing_arg "[ test1 , test2 ]" -test_opt="testing_option" "{ a: "test_hsh_arg" }" -test_bool=false some other args'
|
739
|
-
expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=false test_rest=["some", "other", "args"]'+"\n"
|
740
|
-
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
741
|
-
assert_equal(expected, result)
|
742
|
-
end
|
743
|
-
|
744
|
-
def test_full_run_mixed_args_hash_rocket
|
745
|
-
mod = "util_test_module.rb"
|
746
|
-
command = "test_command_mixed_options"
|
747
|
-
args = 'testing_arg "[ test1 , test2 ]" -test_opt="testing_option" "{ :a => "test_hsh_arg" }" false some other args'
|
748
|
-
expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={ :a => test_hsh_arg } test_bool=false test_rest=["some", "other", "args"]'+"\n"
|
749
|
-
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
750
|
-
assert_equal(expected, result)
|
751
|
-
end
|
752
|
-
|
753
|
-
def test_full_run_mixed_args_no_rest
|
754
|
-
mod = "util_test_module.rb"
|
755
|
-
command = "test_command_mixed_options"
|
756
|
-
args = "testing_arg \"[test1, test2]\" -test_opt='testing_option' \"{a: 'test_hsh_arg'}\""
|
757
|
-
expected = 'test_arg=testing_arg test_arr=["test1", "test2"] test_opt=testing_option test_hsh={"a"=>"test_hsh_arg"} test_bool=true test_rest=[]'+"\n"
|
758
|
-
result = %x(ruby #{File.expand_path(File.dirname(__FILE__))}/#{mod} #{command} #{args})
|
759
|
-
assert_equal(expected, result)
|
760
|
-
end
|
761
|
-
|
762
|
-
end
|