rubycom 0.2.5 → 0.3.0
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.
- 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
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'method_source'
|
3
|
+
|
4
|
+
module Rubycom
|
5
|
+
module Arguments
|
6
|
+
|
7
|
+
# Parses the given arguments and matches them to the given parameters
|
8
|
+
#
|
9
|
+
# @param [Hash] parameters a Hash representing the parameters to match.
|
10
|
+
# Entries should match :param_name => { type: :req||:opt||:rest,
|
11
|
+
# def:(source_definition),
|
12
|
+
# default:(default_value || :nil_rubycom_required_param)
|
13
|
+
# }
|
14
|
+
# @param [Array] arguments an Array of Strings representing the arguments to be parsed
|
15
|
+
# @return [Hash] a Hash mapping the defined parameters to their matching argument values
|
16
|
+
def self.parse_arguments(parameters={}, arguments=[])
|
17
|
+
raise CLIError, 'parameters may not be nil' if parameters.nil?
|
18
|
+
raise CLIError, 'arguments may not be nil' if arguments.nil?
|
19
|
+
sorted_args = arguments.map { |arg|
|
20
|
+
self.parse_arg(arg)
|
21
|
+
}.group_by { |hsh|
|
22
|
+
hsh.keys.first
|
23
|
+
}.map { |key, arr|
|
24
|
+
(key == :rubycom_non_opt_arg) ? Hash[key, arr.map { |hsh| hsh.values }.flatten(1)] : Hash[key, arr.map { |hsh| hsh.values.first }.reduce(&:merge)]
|
25
|
+
}.reduce(&:merge) || {}
|
26
|
+
|
27
|
+
sorted_arg_count = sorted_args.map { |key, val| val }.flatten(1).length
|
28
|
+
types = parameters.values.group_by { |hsh| hsh[:type] }.map { |type, defs_arr| Hash[type, defs_arr.length] }.reduce(&:merge) || {}
|
29
|
+
raise CLIError, "Wrong number of arguments. Expected at least #{types[:req]}, received #{sorted_arg_count}" if sorted_arg_count < (types[:req]||0)
|
30
|
+
raise CLIError, "Wrong number of arguments. Expected at most #{(types[:req]||0) + (types[:opt]||0)}, received #{sorted_arg_count}" if types[:rest].nil? && (sorted_arg_count > ((types[:req]||0) + (types[:opt]||0)))
|
31
|
+
|
32
|
+
parameters.map { |param_sym, def_hash|
|
33
|
+
if def_hash[:type] == :req
|
34
|
+
raise CLIError, "No argument available for #{param_sym}" if sorted_args[:rubycom_non_opt_arg].nil? || sorted_args[:rubycom_non_opt_arg].length == 0
|
35
|
+
Hash[param_sym, sorted_args[:rubycom_non_opt_arg].shift]
|
36
|
+
elsif def_hash[:type] == :opt
|
37
|
+
if sorted_args[param_sym].nil?
|
38
|
+
arg = (sorted_args[:rubycom_non_opt_arg].nil? || sorted_args[:rubycom_non_opt_arg].empty?) ? parameters[param_sym][:default] : sorted_args[:rubycom_non_opt_arg].shift
|
39
|
+
else
|
40
|
+
arg = sorted_args[param_sym]
|
41
|
+
end
|
42
|
+
Hash[param_sym, arg]
|
43
|
+
elsif def_hash[:type] == :rest
|
44
|
+
ret = Hash[param_sym, ((!sorted_args[param_sym].nil?) ? sorted_args[param_sym] : sorted_args[:rubycom_non_opt_arg])]
|
45
|
+
sorted_args[:rubycom_non_opt_arg] = []
|
46
|
+
ret
|
47
|
+
end
|
48
|
+
}.reduce(&:merge)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Uses YAML.load to parse the given String
|
52
|
+
#
|
53
|
+
# @param [String] arg a String representing the argument to be parsed
|
54
|
+
# @return [Object] the result of parsing the given arg with YAML.load
|
55
|
+
def self.parse_arg(arg)
|
56
|
+
return Hash[:rubycom_non_opt_arg, nil] if arg.nil?
|
57
|
+
if arg.is_a?(String) && ((arg.match(/^[-]{3,}\w+/) != nil) || ((arg.match(/^[-]{1,}\w+/) == nil) && (arg.match(/^\w+=/) != nil)))
|
58
|
+
raise CLIError, "Improper option specification, options must start with one or two dashes. Received: #{arg}"
|
59
|
+
elsif arg.is_a?(String) && arg.match(/^(-|--)\w+[=|\s]{1}/) != nil
|
60
|
+
k, v = arg.partition(/^(-|--)\w+[=|\s]{1}/).select { |part|
|
61
|
+
!part.empty?
|
62
|
+
}.each_with_index.map { |part, index|
|
63
|
+
index == 0 ? part.chomp('=').gsub(/^--/, '').gsub(/^-/, '').strip.to_sym : (YAML.load(part) rescue "#{part}")
|
64
|
+
}
|
65
|
+
Hash[k, v]
|
66
|
+
else
|
67
|
+
begin
|
68
|
+
parsed_arg = YAML.load("#{arg}")
|
69
|
+
rescue Exception
|
70
|
+
parsed_arg = "#{arg}"
|
71
|
+
end
|
72
|
+
Hash[:rubycom_non_opt_arg, parsed_arg]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Builds a hash mapping parameter names (as symbols) to their
|
77
|
+
# :type (:req,:opt,:rest), :def (source_definition), :default (default_value || :nil_rubycom_required_param)
|
78
|
+
# for each parameter defined by the given method.
|
79
|
+
#
|
80
|
+
# @param [Method] method the Method who's parameter hash should be built
|
81
|
+
# @return [Hash] a Hash representing the given Method's parameters
|
82
|
+
def self.get_param_definitions(method)
|
83
|
+
raise CLIError, 'method must be an instance of the Method class' unless method.class == Method
|
84
|
+
method.parameters.map { |param|
|
85
|
+
param[1].to_s
|
86
|
+
}.map { |param_name|
|
87
|
+
{param_name.to_sym => method.source.split("\n").select { |line| line.include?(param_name) }.first}
|
88
|
+
}.map { |param_hash|
|
89
|
+
param_def = param_hash.flatten[1].gsub(/(def\s+self\.#{method.name.to_s}|def\s+#{method.name.to_s})/, '').lstrip.chomp.chomp(')').reverse.chomp('(').reverse.split(',').map { |param_n| param_n.lstrip }.select { |candidate| candidate.include?(param_hash.flatten[0].to_s) }.first
|
90
|
+
Hash[
|
91
|
+
param_hash.flatten[0],
|
92
|
+
Hash[
|
93
|
+
:def, param_def,
|
94
|
+
:type, method.parameters.select { |arr| arr[1] == param_hash.flatten[0] }.flatten[0],
|
95
|
+
:default, (param_def.include?('=') ? YAML.load(param_def.split('=')[1..-1].join('=')) : :nil_rubycom_required_param)
|
96
|
+
]
|
97
|
+
]
|
98
|
+
}.reduce(&:merge) || {}
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Rubycom
|
2
|
+
module Commands
|
3
|
+
|
4
|
+
# Retrieves the singleton methods in the given base and included Modules
|
5
|
+
#
|
6
|
+
# @param [Module] base the module which invoked 'include Rubycom'
|
7
|
+
# @param [Boolean] all if true recursively search for included modules' commands, if false return only top level commands
|
8
|
+
# @return [Hash] a Hash of Symbols representing the command methods in the given base and it's included modules (if all=true)
|
9
|
+
def self.get_commands(base, all=true)
|
10
|
+
return {} if base.nil? || !base.respond_to?(:singleton_methods) || !base.respond_to?(:included_modules)
|
11
|
+
{
|
12
|
+
base.name.to_sym => {
|
13
|
+
commands: base.singleton_methods(true).select { |sym| ![:included, :extended].include?(sym) },
|
14
|
+
inclusions: base.included_modules.select { |mod|
|
15
|
+
![:Rubycom].include?(mod.name.to_sym)
|
16
|
+
}.map { |mod|
|
17
|
+
all ? self.get_commands(mod) : mod.name.to_sym
|
18
|
+
}
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
# Discovers the commands specified in the given base without considering the commands contained in sub-modules
|
24
|
+
#
|
25
|
+
# @param [Module] base the base Module to search
|
26
|
+
# @return [Array] a list of command name symbols which are defined in the given Module
|
27
|
+
def self.get_top_level_commands(base)
|
28
|
+
return {} if base.nil? || !base.respond_to?(:singleton_methods) || !base.respond_to?(:included_modules)
|
29
|
+
excluded_commands = [:included, :extended]
|
30
|
+
excluded_modules = [:Rubycom]
|
31
|
+
base.singleton_methods(true).select { |sym| !excluded_commands.include?(sym) } +
|
32
|
+
base.included_modules.select { |mod| !excluded_modules.include?(mod.name.to_sym) }.map { |mod| mod.name.to_sym }.flatten
|
33
|
+
end
|
34
|
+
|
35
|
+
# Discovers the commands specified in the given base and included Modules
|
36
|
+
#
|
37
|
+
# @param [Module] base the base Module to search
|
38
|
+
# @return [Hash] a set of command name symbols mapped to containing Modules
|
39
|
+
def self.index_commands(base)
|
40
|
+
excluded_commands = [:included, :extended]
|
41
|
+
excluded_modules = [:Rubycom]
|
42
|
+
Hash[base.singleton_methods(true).select { |sym| !excluded_commands.include?(sym) }.map { |sym|
|
43
|
+
[sym, base]
|
44
|
+
}].merge(
|
45
|
+
base.included_modules.select { |mod| !excluded_modules.include?(mod.name.to_sym) }.map { |mod|
|
46
|
+
self.index_commands(mod)
|
47
|
+
}.reduce(&:merge) || {}
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Looks up the commands which will be available on the given base Module and returns the longest command name
|
52
|
+
# Used in arranging the command list format
|
53
|
+
#
|
54
|
+
# @param [Module] base the base Module to look up
|
55
|
+
# @return [String] the longest command name which will show in a list of commands for the given base Module
|
56
|
+
def self.get_longest_command_name(base)
|
57
|
+
return '' if base.nil?
|
58
|
+
self.get_commands(base, false).map { |_, mod_hash|
|
59
|
+
mod_hash[:commands] + mod_hash[:inclusions].flatten }.flatten.max_by(&:size) or ''
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/commands.rb"
|
2
|
+
|
3
|
+
module Rubycom
|
4
|
+
module Documentation
|
5
|
+
|
6
|
+
# Retrieves the summary for each command method in the given Module
|
7
|
+
#
|
8
|
+
# @param [Module] base the module which invoked 'include Rubycom'
|
9
|
+
# @return [String] the summary for each command method in the given Module
|
10
|
+
def self.get_summary(base)
|
11
|
+
Commands.get_top_level_commands(base).each_with_index.map { |sym, index|
|
12
|
+
"#{"Commands:\n" if index == 0}" << self.get_command_summary(base, sym, self.get_separator(sym, Commands.get_longest_command_name(base).length))
|
13
|
+
}.reduce(:+) or "No Commands found for #{base}."
|
14
|
+
end
|
15
|
+
|
16
|
+
# Creates a separator with the appropriate spacing to line up a command/description pair in a command list
|
17
|
+
#
|
18
|
+
# @param [Symbol] sym
|
19
|
+
# @param [Integer] spacer_length
|
20
|
+
# @return [String] a spaced separator String for use in a command/description list
|
21
|
+
def self.get_separator(sym, spacer_length=0)
|
22
|
+
[].unshift(" " * (spacer_length - sym.to_s.length)).join << " - "
|
23
|
+
end
|
24
|
+
|
25
|
+
# Retrieves the summary for the given command_name
|
26
|
+
#
|
27
|
+
# @param [Module] base the module which invoked 'include Rubycom'
|
28
|
+
# @param [String] command_name the command to retrieve usage for
|
29
|
+
# @return [String] a summary of the given command_name
|
30
|
+
def self.get_command_summary(base, command_name, separator = ' - ')
|
31
|
+
raise CLIError, "Can not get usage for #{command_name} with base: #{base||"nil"}" if base.nil? || !base.respond_to?(:included_modules)
|
32
|
+
return 'No command specified.' if command_name.nil? || command_name.length == 0
|
33
|
+
if base.included_modules.map { |mod| mod.name.to_sym }.include?(command_name.to_sym)
|
34
|
+
begin
|
35
|
+
mod_const = Kernel.const_get(command_name.to_sym)
|
36
|
+
desc = File.read(mod_const.public_method(mod_const.singleton_methods().first).source_location.first).split(//).reduce("") { |str, c|
|
37
|
+
unless str.gsub("\n", '').gsub(/\s+/, '').include?("module#{mod_const}")
|
38
|
+
str << c
|
39
|
+
end
|
40
|
+
str
|
41
|
+
}.split("\n").select { |line| line.strip.match(/^#/) && !line.strip.match(/^#!/) }.map { |line| line.strip.gsub(/^#+/, '') }.join("\n")
|
42
|
+
rescue
|
43
|
+
desc = ""
|
44
|
+
end
|
45
|
+
else
|
46
|
+
raise CLIError, "Invalid command for #{base}, #{command_name}" unless base.public_methods.include?(command_name.to_sym)
|
47
|
+
desc = self.get_doc(base.public_method(command_name.to_sym))[:desc].join("\n") rescue ""
|
48
|
+
end
|
49
|
+
(desc.nil?||desc=='nil'||desc.length==0) ? "#{command_name}\n" : self.get_formatted_summary(command_name, desc, separator)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Arranges the given command_name and command_description with the separator in a standard format
|
53
|
+
#
|
54
|
+
# @param [String] command_name the command format
|
55
|
+
# @param [String] command_description the description for the given command
|
56
|
+
# @param [String] separator optional separator to use
|
57
|
+
def self.get_formatted_summary(command_name, command_description, separator = ' - ')
|
58
|
+
width = 95
|
59
|
+
prefix = command_name.to_s.split(//).map { " " }.join + separator.split(//).map { " " }.join
|
60
|
+
line_width = width - prefix.length
|
61
|
+
description_msg = command_description.gsub(/(.{1,#{line_width}})(?: +|$)\n?|(.{#{line_width}})/, "#{prefix}"+'\1\2'+"\n")
|
62
|
+
"#{command_name}#{separator}#{description_msg.lstrip}"
|
63
|
+
end
|
64
|
+
|
65
|
+
# Retrieves the usage description for the given Module with a list of command methods
|
66
|
+
#
|
67
|
+
# @param [Module] base the module which invoked 'include Rubycom'
|
68
|
+
# @return [String] the usage description for the module with a list of command methods
|
69
|
+
def self.get_usage(base)
|
70
|
+
return '' if base.nil? || !base.respond_to?(:included_modules)
|
71
|
+
return '' if Commands.get_top_level_commands(base).size == 0
|
72
|
+
"Usage:\n #{base} <command> [args]\n\n" << self.get_summary(base)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Retrieves the usage description for the given command_name
|
76
|
+
#
|
77
|
+
# @param [Module] base the module which invoked 'include Rubycom'
|
78
|
+
# @param [String] command_name the command to retrieve usage for
|
79
|
+
# @param [Array] args the remaining args other than the command_name, used of sub-command look-ups
|
80
|
+
# @return [String] the detailed usage description for the given command_name
|
81
|
+
def self.get_command_usage(base, command_name, args=[])
|
82
|
+
raise CLIError, "Can not get usage for #{command_name} with base: #{base||"nil"}" if base.nil? || !base.respond_to?(:included_modules)
|
83
|
+
return 'No command specified.' if command_name.nil? || command_name.length == 0
|
84
|
+
if base.included_modules.map { |mod| mod.name.to_sym }.include?(command_name.to_sym)
|
85
|
+
if args.empty?
|
86
|
+
self.get_usage(eval(command_name.to_s))
|
87
|
+
else
|
88
|
+
self.get_command_usage(eval(command_name.to_s), args[0], args[1..-1])
|
89
|
+
end
|
90
|
+
else
|
91
|
+
raise CLIError, "Invalid command for #{base}, #{command_name}" unless base.public_methods.include?(command_name.to_sym)
|
92
|
+
m = base.public_method(command_name.to_sym)
|
93
|
+
method_doc = self.get_doc(m) || {}
|
94
|
+
|
95
|
+
msg = "Usage: #{m.name}#{self.get_param_usage(m)}\n"
|
96
|
+
msg << "Parameters:\n " unless m.parameters.empty?
|
97
|
+
msg << "#{method_doc[:param].join("\n ")}\n" unless method_doc[:param].nil?
|
98
|
+
msg << "Returns:\n " unless method_doc[:return].nil?
|
99
|
+
msg << "#{method_doc[:return].join("\n ")}\n" unless method_doc[:return].nil?
|
100
|
+
msg
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Discovers the given Method's parameters and uses them to build a formatted usage string
|
105
|
+
#
|
106
|
+
# @param [Method] method the Method object to generate usage for
|
107
|
+
def self.get_param_usage(method)
|
108
|
+
return "" if method.parameters.nil? || method.parameters.empty?
|
109
|
+
Arguments.get_param_definitions(method).group_by { |_, hsh|
|
110
|
+
hsh[:type]
|
111
|
+
}.map { |key, val_arr|
|
112
|
+
vals = Hash[*val_arr.flatten]
|
113
|
+
{
|
114
|
+
key => if key == :opt
|
115
|
+
vals.map { |param, val_hsh| "-#{param.to_s}=#{val_hsh[:default]}" }
|
116
|
+
elsif key == :req
|
117
|
+
vals.keys.map { |param| " <#{param.to_s}>" }
|
118
|
+
else
|
119
|
+
vals.keys.map { |param| " [&#{param.to_s}]" }
|
120
|
+
end
|
121
|
+
}
|
122
|
+
}.reduce(&:merge).map { |type, param_arr|
|
123
|
+
if type == :opt
|
124
|
+
" [#{param_arr.join("|")}]"
|
125
|
+
else
|
126
|
+
param_arr.join
|
127
|
+
end
|
128
|
+
}.join
|
129
|
+
end
|
130
|
+
|
131
|
+
# Retrieves the given method's documentation from it's source code
|
132
|
+
#
|
133
|
+
# @param [Method] method the Method who's documentation should be retrieved
|
134
|
+
# @return [Hash] a Hash representing the given Method's documentation, documentation parsed as follows:
|
135
|
+
# :desc = the first general method comment lines,
|
136
|
+
# :word = each @word comment (i.e.- a line starting with @param will be saved as :param => ["line"])
|
137
|
+
def self.get_doc(method)
|
138
|
+
method.comment.split("\n").map { |line|
|
139
|
+
line.gsub(/#\s*/, '') }.group_by { |doc|
|
140
|
+
if doc.match(/^@\w+/).nil?
|
141
|
+
:desc
|
142
|
+
else
|
143
|
+
doc.match(/^@\w+/).to_s.gsub('@', '').to_sym
|
144
|
+
end
|
145
|
+
}.map { |key, val|
|
146
|
+
Hash[key, val.map { |val_line| val_line.gsub(/^@\w+/, '').lstrip }.select { |line| line != '' }]
|
147
|
+
}.reduce(&:merge)
|
148
|
+
end
|
149
|
+
|
150
|
+
# @return [String] the usage message for Rubycom's default commands
|
151
|
+
def self.get_default_commands_usage()
|
152
|
+
<<-END.gsub(/^ {6}/,'')
|
153
|
+
|
154
|
+
Default Commands:
|
155
|
+
help - prints this help page
|
156
|
+
job - run a job file
|
157
|
+
register_completions - setup bash tab completion
|
158
|
+
tab_complete - print a list of possible matches for a given word
|
159
|
+
END
|
160
|
+
end
|
161
|
+
|
162
|
+
# @return [String] the usage message for Rubycom's job runner
|
163
|
+
def self.get_job_usage(base)
|
164
|
+
<<-END.gsub(/^ {6}/,'')
|
165
|
+
Usage: #{base} job <job_path>
|
166
|
+
Parameters:
|
167
|
+
[String] job_path the path to the job yaml to be run
|
168
|
+
Details:
|
169
|
+
A job yaml is any yaml file which follows the format below.
|
170
|
+
* The env: key shown below is optional
|
171
|
+
* The steps: key must contain a set of numbered steps as shown
|
172
|
+
* The desc: key shown below is an optional context for the job's log output
|
173
|
+
* Other than cmd: there may be as many keys in a numbered command as you choose
|
174
|
+
---
|
175
|
+
env:
|
176
|
+
test_msg: Hello World
|
177
|
+
test_arg: 123
|
178
|
+
working_dir: ./test
|
179
|
+
steps:
|
180
|
+
1:
|
181
|
+
desc: Run test command with environment variable
|
182
|
+
test_context: Some more log context
|
183
|
+
cmd: ruby env[working_dir]/test.rb test_command env[test_msg]
|
184
|
+
2:
|
185
|
+
cmd: ls env[working_dir]
|
186
|
+
END
|
187
|
+
end
|
188
|
+
|
189
|
+
def self.get_register_completions_usage(base)
|
190
|
+
<<-END.gsub(/^ {6}/,'')
|
191
|
+
Usage: #{base} register_completions
|
192
|
+
END
|
193
|
+
end
|
194
|
+
|
195
|
+
def self.get_tab_complete_usage(base)
|
196
|
+
<<-END.gsub(/^ {6}/,'')
|
197
|
+
Usage: #{base} tab_complete <word>
|
198
|
+
Parameters:
|
199
|
+
[String] word the word or partial word to find matches for
|
200
|
+
END
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
end
|
data/lib/rubycom/version.rb
CHANGED
@@ -0,0 +1,148 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/../../lib/rubycom/arguments.rb"
|
2
|
+
|
3
|
+
require "#{File.dirname(__FILE__)}/util_test_module.rb"
|
4
|
+
require "#{File.dirname(__FILE__)}/util_test_composite.rb"
|
5
|
+
require "#{File.dirname(__FILE__)}/util_test_no_singleton.rb"
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
|
9
|
+
class ArgumentsTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_parse_arg_string
|
12
|
+
test_arg = "test_arg"
|
13
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
14
|
+
expected = {rubycom_non_opt_arg: "test_arg"}
|
15
|
+
assert_equal(expected, result)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_parse_arg_fixnum
|
19
|
+
test_arg = "1234512415435"
|
20
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
21
|
+
expected = {rubycom_non_opt_arg: 1234512415435}
|
22
|
+
assert_equal(expected, result)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_parse_arg_float
|
26
|
+
test_arg = "12345.67890"
|
27
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
28
|
+
expected = {rubycom_non_opt_arg: 12345.67890}
|
29
|
+
assert_equal(expected, result)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_parse_arg_timestamp
|
33
|
+
time = Time.new
|
34
|
+
test_arg = time.to_s
|
35
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
36
|
+
expected = {rubycom_non_opt_arg: time}
|
37
|
+
assert_equal(expected[:rubycom_non_opt_arg].to_i, result[:rubycom_non_opt_arg].to_i)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_parse_arg_datetime
|
41
|
+
time = Time.new("2013-05-08 00:00:00 -0500")
|
42
|
+
date = Date.new(time.year, time.month, time.day)
|
43
|
+
test_arg = date.to_s
|
44
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
45
|
+
expected = {rubycom_non_opt_arg: date}
|
46
|
+
assert_equal(expected, result)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_parse_arg_array
|
50
|
+
test_arg = ["1", 2, "a", 'b']
|
51
|
+
result = Rubycom::Arguments.parse_arg(test_arg.to_s)
|
52
|
+
expected = {rubycom_non_opt_arg: test_arg}
|
53
|
+
assert_equal(expected, result)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_parse_arg_hash
|
57
|
+
time = Time.new.to_s
|
58
|
+
test_arg = ":a: \"#{time}\""
|
59
|
+
result = Rubycom::Arguments.parse_arg(test_arg.to_s)
|
60
|
+
expected = {rubycom_non_opt_arg: {a: time}}
|
61
|
+
assert_equal(expected, result)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_parse_arg_hash_group
|
65
|
+
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"
|
66
|
+
result = Rubycom::Arguments.parse_arg(test_arg.to_s)
|
67
|
+
expected = {rubycom_non_opt_arg: {:a => [1, 2], :b => 1, :c => "test", :d => 1.0, :e => "2013-05-08 23:21:52 -0500"}}
|
68
|
+
assert_equal(expected, result)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_parse_arg_yaml
|
72
|
+
test_arg = {:a => ["1", 2, "a", 'b'], :b => 1, :c => "test", :d => "#{Time.now.to_s}"}
|
73
|
+
result = Rubycom::Arguments.parse_arg(test_arg.to_yaml)
|
74
|
+
expected = {rubycom_non_opt_arg: test_arg}
|
75
|
+
assert_equal(expected, result)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_parse_arg_code
|
79
|
+
test_arg = 'def self.test_code_method; raise "Fail - test_parse_arg_code";end'
|
80
|
+
result = Rubycom::Arguments.parse_arg(test_arg.to_s)
|
81
|
+
expected = {rubycom_non_opt_arg: 'def self.test_code_method; raise "Fail - test_parse_arg_code";end'}
|
82
|
+
assert_equal(expected, result)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_parse_opt_string_eq
|
86
|
+
test_arg = "-test_arg=\"test\""
|
87
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
88
|
+
expected = {test_arg: "test"}
|
89
|
+
assert_equal(expected, result)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_parse_opt_string_sp
|
93
|
+
test_arg = "-test_arg \"test\""
|
94
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
95
|
+
expected = {test_arg: "test"}
|
96
|
+
assert_equal(expected, result)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_parse_opt_long_string_eq
|
100
|
+
test_arg = "--test_arg=\"test\""
|
101
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
102
|
+
expected = {test_arg: "test"}
|
103
|
+
assert_equal(expected, result)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_parse_opt_long_string_sp
|
107
|
+
test_arg = "--test_arg \"test\""
|
108
|
+
result = Rubycom::Arguments.parse_arg(test_arg)
|
109
|
+
expected = {test_arg: "test"}
|
110
|
+
assert_equal(expected, result)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_get_param_definitions
|
114
|
+
test_method = UtilTestModule.public_method('test_command_with_args')
|
115
|
+
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}}
|
116
|
+
result = Rubycom::Arguments.get_param_definitions(test_method)
|
117
|
+
assert_equal(expected, result)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_get_param_definitions_no_args
|
121
|
+
test_method = UtilTestModule.public_method('test_command')
|
122
|
+
expected = {}
|
123
|
+
result = Rubycom::Arguments.get_param_definitions(test_method)
|
124
|
+
assert_equal(expected, result)
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_get_param_definitions_arr_param
|
128
|
+
test_method = UtilTestModule.public_method('test_command_options_arr')
|
129
|
+
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}}
|
130
|
+
result = Rubycom::Arguments.get_param_definitions(test_method)
|
131
|
+
assert_equal(expected, result)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_get_param_definitions_all_options
|
135
|
+
test_method = UtilTestModule.public_method('test_command_all_options')
|
136
|
+
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"}}
|
137
|
+
result = Rubycom::Arguments.get_param_definitions(test_method)
|
138
|
+
assert_equal(expected, result)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_get_param_definitions_mixed
|
142
|
+
test_method = UtilTestModule.public_method('test_command_with_options')
|
143
|
+
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"}}
|
144
|
+
result = Rubycom::Arguments.get_param_definitions(test_method)
|
145
|
+
assert_equal(expected, result)
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|