rubycom 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +162 -146
  3. data/Rakefile +12 -12
  4. data/lib/rubycom.rb +156 -226
  5. data/lib/rubycom/arg_parse.rb +252 -0
  6. data/lib/rubycom/command_interface.rb +97 -0
  7. data/lib/rubycom/completions.rb +62 -0
  8. data/lib/rubycom/error_handler.rb +15 -0
  9. data/lib/rubycom/executor.rb +23 -0
  10. data/lib/rubycom/helpers.rb +98 -0
  11. data/lib/rubycom/output_handler.rb +15 -0
  12. data/lib/rubycom/parameter_extract.rb +262 -0
  13. data/lib/rubycom/singleton_commands.rb +78 -0
  14. data/lib/rubycom/sources.rb +99 -0
  15. data/lib/rubycom/version.rb +1 -1
  16. data/lib/rubycom/yard_doc.rb +146 -0
  17. data/rubycom.gemspec +14 -16
  18. data/test/rubycom/arg_parse_test.rb +247 -0
  19. data/test/rubycom/command_interface_test.rb +293 -0
  20. data/test/rubycom/completions_test.rb +94 -0
  21. data/test/rubycom/error_handler_test.rb +72 -0
  22. data/test/rubycom/executor_test.rb +64 -0
  23. data/test/rubycom/helpers_test.rb +467 -0
  24. data/test/rubycom/output_handler_test.rb +76 -0
  25. data/test/rubycom/parameter_extract_test.rb +141 -0
  26. data/test/rubycom/rubycom_test.rb +290 -548
  27. data/test/rubycom/singleton_commands_test.rb +122 -0
  28. data/test/rubycom/sources_test.rb +59 -0
  29. data/test/rubycom/util_test_bin.rb +8 -0
  30. data/test/rubycom/util_test_composite.rb +23 -20
  31. data/test/rubycom/util_test_module.rb +142 -112
  32. data/test/rubycom/util_test_no_singleton.rb +2 -2
  33. data/test/rubycom/util_test_sub_module.rb +13 -0
  34. data/test/rubycom/yard_doc_test.rb +165 -0
  35. metadata +61 -24
  36. data/lib/rubycom/arguments.rb +0 -133
  37. data/lib/rubycom/commands.rb +0 -63
  38. data/lib/rubycom/documentation.rb +0 -212
  39. data/test/rubycom/arguments_test.rb +0 -289
  40. data/test/rubycom/commands_test.rb +0 -51
  41. data/test/rubycom/documentation_test.rb +0 -186
  42. data/test/rubycom/util_test_job.yaml +0 -21
  43. data/test/rubycom/utility_tester.rb +0 -17
@@ -1,63 +0,0 @@
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
@@ -1,212 +0,0 @@
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
- desc = self.get_module_doc(command_name)
35
- else
36
- raise CLIError, "Invalid command for #{base}, #{command_name}" unless base.public_methods.include?(command_name.to_sym)
37
- desc = self.get_doc(base.public_method(command_name.to_sym))[:desc].join("\n") rescue ""
38
- end
39
- (desc.nil?||desc=='nil'||desc.length==0) ? "#{command_name}\n" : self.get_formatted_summary(command_name, desc, separator)
40
- end
41
-
42
- # Arranges the given command_name and command_description with the separator in a standard format
43
- #
44
- # @param [String] command_name the command format
45
- # @param [String] command_description the description for the given command
46
- # @param [String] separator optional separator to use
47
- def self.get_formatted_summary(command_name, command_description, separator = ' - ')
48
- width = 95
49
- prefix = command_name.to_s.split(//).map { " " }.join + separator.split(//).map { " " }.join
50
- line_width = width - prefix.length
51
- description_msg = command_description.gsub(/(.{1,#{line_width}})(?: +|$)\n?|(.{#{line_width}})/, "#{prefix}"+'\1\2'+"\n")
52
- "#{command_name}#{separator}#{description_msg.lstrip}"
53
- end
54
-
55
- # Retrieves the usage description for the given Module with a list of command methods
56
- #
57
- # @param [Module] base the module which invoked 'include Rubycom'
58
- # @return [String] the usage description for the module with a list of command methods
59
- def self.get_usage(base)
60
- return '' if base.nil? || !base.respond_to?(:included_modules)
61
- return '' if Commands.get_top_level_commands(base).size == 0
62
- "Usage:\n #{base} <command> [args]\n\n" << self.get_summary(base)
63
- end
64
-
65
- # Retrieves the usage description for the given command_name
66
- #
67
- # @param [Module] base the module which invoked 'include Rubycom'
68
- # @param [String] command_name the command to retrieve usage for
69
- # @param [Array] args the remaining args other than the command_name, used of sub-command look-ups
70
- # @return [String] the detailed usage description for the given command_name
71
- def self.get_command_usage(base, command_name, args=[])
72
- raise CLIError, "Can not get usage for #{command_name} with base: #{base||"nil"}" if base.nil? || !base.respond_to?(:included_modules)
73
- return 'No command specified.' if command_name.nil? || command_name.length == 0
74
- if base.included_modules.map { |mod| mod.name.to_sym }.include?(command_name.to_sym)
75
- if args.empty?
76
- self.get_usage(eval(command_name.to_s))
77
- else
78
- self.get_command_usage(eval(command_name.to_s), args[0], args[1..-1])
79
- end
80
- else
81
- raise CLIError, "Invalid command for #{base}, #{command_name}" unless base.public_methods.include?(command_name.to_sym)
82
- m = base.public_method(command_name.to_sym)
83
- method_doc = self.get_doc(m) || {}
84
-
85
- msg = "Usage: #{m.name}#{self.get_param_usage(m)}\n"
86
- msg << "Parameters:\n " unless m.parameters.empty?
87
- msg << "#{method_doc[:param].join("\n ")}\n" unless method_doc[:param].nil?
88
- msg << "Returns:\n " unless method_doc[:return].nil?
89
- msg << "#{method_doc[:return].join("\n ")}\n" unless method_doc[:return].nil?
90
- msg
91
- end
92
- end
93
-
94
- # Discovers the given Method's parameters and uses them to build a formatted usage string
95
- #
96
- # @param [Method] method the Method object to generate usage for
97
- def self.get_param_usage(method)
98
- return "" if method.parameters.nil? || method.parameters.empty?
99
- Arguments.get_param_definitions(method).group_by { |_, hsh|
100
- hsh[:type]
101
- }.map { |key, val_arr|
102
- vals = Hash[*val_arr.flatten]
103
- {
104
- key => if key == :opt
105
- vals.map { |param, val_hsh| "-#{param.to_s}=#{val_hsh[:default]}" }
106
- elsif key == :req
107
- vals.keys.map { |param| " <#{param.to_s}>" }
108
- else
109
- vals.keys.map { |param| " [&#{param.to_s}]" }
110
- end
111
- }
112
- }.reduce(&:merge).map { |type, param_arr|
113
- if type == :opt
114
- " [#{param_arr.join("|")}]"
115
- else
116
- param_arr.join
117
- end
118
- }.join
119
- end
120
-
121
- # Retrieves the given module's documentation from it's source code
122
- #
123
- # @param [String] base_name
124
- # @return [String] the documentation text from the top of the specified module
125
- def self.get_module_doc(base_name)
126
- begin
127
- mod_const = Kernel.const_get(base_name.to_sym)
128
- File.read(mod_const.public_method(mod_const.singleton_methods().first).source_location.first).split(//).reduce("") { |str, c|
129
- unless str.gsub("\n", '').gsub(/\s+/, '').include?("module#{mod_const}")
130
- str << c
131
- end
132
- str
133
- }.split("\n").select { |line| line.strip.match(/^#/) && !line.strip.match(/^#!/) }.map { |line| line.strip.gsub(/^#+/, '') }.join("\n")
134
- rescue
135
- ""
136
- end
137
- end
138
-
139
- # Retrieves the given method's documentation from it's source code
140
- #
141
- # @param [Method] method the Method who's documentation should be retrieved
142
- # @return [Hash] a Hash representing the given Method's documentation, documentation parsed as follows:
143
- # :desc = the first general method comment lines,
144
- # :word = each @word comment (i.e.- a line starting with @param will be saved as :param => ["line"])
145
- def self.get_doc(method)
146
- method.comment.split("\n").map { |line|
147
- line.gsub(/#\s*/, '') }.group_by { |doc|
148
- if doc.match(/^@\w+/).nil?
149
- :desc
150
- else
151
- doc.match(/^@\w+/).to_s.gsub('@', '').to_sym
152
- end
153
- }.map { |key, val|
154
- Hash[key, val.map { |val_line| val_line.gsub(/^@\w+/, '').lstrip }.select { |line| line != '' }]
155
- }.reduce(&:merge)
156
- end
157
-
158
- # @return [String] the usage message for Rubycom's default commands
159
- def self.get_default_commands_usage()
160
- <<-END.gsub(/^ {6}/,'')
161
-
162
- Default Commands:
163
- help - prints this help page
164
- job - run a job file
165
- register_completions - setup bash tab completion
166
- tab_complete - print a list of possible matches for a given word
167
- END
168
- end
169
-
170
- # @return [String] the usage message for Rubycom's job runner
171
- def self.get_job_usage(base)
172
- <<-END.gsub(/^ {6}/,'')
173
- Usage: #{base} job <job_path>
174
- Parameters:
175
- [String] job_path the path to the job yaml to be run
176
- Details:
177
- A job yaml is any yaml file which follows the format below.
178
- * The env: key shown below is optional
179
- * The steps: key must contain a set of numbered steps as shown
180
- * The desc: key shown below is an optional context for the job's log output
181
- * Other than cmd: there may be as many keys in a numbered command as you choose
182
- ---
183
- env:
184
- test_msg: Hello World
185
- test_arg: 123
186
- working_dir: ./test
187
- steps:
188
- 1:
189
- desc: Run test command with environment variable
190
- test_context: Some more log context
191
- cmd: ruby env[working_dir]/test.rb test_command env[test_msg]
192
- 2:
193
- cmd: ls env[working_dir]
194
- END
195
- end
196
-
197
- def self.get_register_completions_usage(base)
198
- <<-END.gsub(/^ {6}/,'')
199
- Usage: #{base} register_completions
200
- END
201
- end
202
-
203
- def self.get_tab_complete_usage(base)
204
- <<-END.gsub(/^ {6}/,'')
205
- Usage: #{base} tab_complete <word>
206
- Parameters:
207
- [String] word the word or partial word to find matches for
208
- END
209
- end
210
-
211
- end
212
- end
@@ -1,289 +0,0 @@
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
- def test_parse_args_no_params
149
- test_method = UtilTestModule.public_method(:test_command)
150
- params = Rubycom::Arguments.get_param_definitions(test_method)
151
- arguments = []
152
- expected = nil
153
- result = Rubycom::Arguments.resolve(params, arguments)
154
- assert_equal(expected, result)
155
- end
156
-
157
- def test_parse_args_one_param
158
- test_method = UtilTestModule.public_method(:test_command_with_arg)
159
- params = Rubycom::Arguments.get_param_definitions(test_method)
160
- arguments = ["abc"]
161
- expected = {:test_arg=>"abc"}
162
- result = Rubycom::Arguments.resolve(params, arguments)
163
- assert_equal(expected, result)
164
- end
165
-
166
- def test_parse_args_one_param_array
167
- test_method = UtilTestModule.public_method(:test_command_with_arg)
168
- params = Rubycom::Arguments.get_param_definitions(test_method)
169
- arguments = ["[a,b,c,1,2,3]"]
170
- expected = {:test_arg=>['a','b','c',1,2,3]}
171
- result = Rubycom::Arguments.resolve(params, arguments)
172
- assert_equal(expected, result)
173
- end
174
-
175
- def test_parse_args_one_param_hash
176
- test_method = UtilTestModule.public_method(:test_command_with_arg)
177
- params = Rubycom::Arguments.get_param_definitions(test_method)
178
- arguments = ["{abc: 123}"]
179
- expected = {:test_arg=>{"abc"=>123}}
180
- result = Rubycom::Arguments.resolve(params, arguments)
181
- assert_equal(expected, result)
182
- end
183
-
184
- def test_parse_args_too_many_args
185
- test_method = UtilTestModule.public_method(:test_command)
186
- params = Rubycom::Arguments.get_param_definitions(test_method)
187
- arguments = ["123"]
188
- expected = nil
189
- result = nil
190
- assert_raise Rubycom::CLIError do
191
- result = Rubycom::Arguments.resolve(params, arguments)
192
- end
193
- assert_equal(expected, result)
194
- end
195
-
196
- def test_parse_args_too_few_args
197
- test_method = UtilTestModule.public_method(:test_command_with_arg)
198
- params = Rubycom::Arguments.get_param_definitions(test_method)
199
- arguments = []
200
- expected = nil
201
- result = nil
202
- assert_raise Rubycom::CLIError do
203
- result = Rubycom::Arguments.resolve(params, arguments)
204
- end
205
- assert_equal(expected, result)
206
- end
207
-
208
- def test_parse_args_with_options
209
- test_method = UtilTestModule.public_method(:test_command_with_options)
210
- params = Rubycom::Arguments.get_param_definitions(test_method)
211
- arguments = [";asd"]
212
- expected = {:test_arg=>";asd", :test_option=>"option_default"}
213
- result = Rubycom::Arguments.resolve(params, arguments)
214
- assert_equal(expected, result)
215
- end
216
-
217
- def test_parse_args_with_options_filled
218
- test_method = UtilTestModule.public_method(:test_command_with_options)
219
- params = Rubycom::Arguments.get_param_definitions(test_method)
220
- arguments = ["[1,2,3]","--test_option=abc"]
221
- expected = {:test_arg=>[1, 2, 3], :test_option=>"abc"}
222
- result = Rubycom::Arguments.resolve(params, arguments)
223
- assert_equal(expected, result)
224
- end
225
-
226
- def test_parse_args_all_options
227
- test_method = UtilTestModule.public_method(:test_command_all_options)
228
- params = Rubycom::Arguments.get_param_definitions(test_method)
229
- arguments = []
230
- expected = {:test_arg=>"test_arg_default", :test_option=>"test_option_default"}
231
- result = Rubycom::Arguments.resolve(params, arguments)
232
- assert_equal(expected, result)
233
- end
234
-
235
- def test_parse_args_all_opts_args_entered
236
- test_method = UtilTestModule.public_method(:test_command_all_options)
237
- params = Rubycom::Arguments.get_param_definitions(test_method)
238
- arguments = ["[1,2,3]","abc"]
239
- expected = {:test_arg=>[1, 2, 3], :test_option=>"abc"}
240
- result = Rubycom::Arguments.resolve(params, arguments)
241
- assert_equal(expected, result)
242
- end
243
-
244
- def test_parse_args_all_opts_filled
245
- test_method = UtilTestModule.public_method(:test_command_all_options)
246
- params = Rubycom::Arguments.get_param_definitions(test_method)
247
- arguments = ["-test_arg='123asd'","--test_option=[b,c,d]"]
248
- expected = {:test_arg=>"123asd", :test_option=>["b","c","d"]}
249
- result = Rubycom::Arguments.resolve(params, arguments)
250
- assert_equal(expected, result)
251
- end
252
-
253
- def test_parse_args_arr_option
254
- test_method = UtilTestModule.public_method(:test_command_arg_arr)
255
- params = Rubycom::Arguments.get_param_definitions(test_method)
256
- arguments = ["--test_arr=['123','abc']"]
257
- expected = {:test_arr=>["123","abc"]}
258
- result = Rubycom::Arguments.resolve(params, arguments)
259
- assert_equal(expected, result)
260
- end
261
-
262
- def test_parse_args_options_arr
263
- test_method = UtilTestModule.public_method(:test_command_options_arr)
264
- params = Rubycom::Arguments.get_param_definitions(test_method)
265
- arguments = [",a2","1","2","3","a","b","c"]
266
- expected = {:test_option=>",a2", :test_options=>[1, 2, 3, "a", "b", "c"]}
267
- result = Rubycom::Arguments.resolve(params, arguments)
268
- assert_equal(expected, result)
269
- end
270
-
271
- def test_parse_args_options_arr_with_arr
272
- test_method = UtilTestModule.public_method(:test_command_options_arr)
273
- params = Rubycom::Arguments.get_param_definitions(test_method)
274
- arguments = [",a2","[1,2,3,a,b,c]"]
275
- expected = {:test_option=>",a2", :test_options=>[[1, 2, 3, "a", "b", "c"]]}
276
- result = Rubycom::Arguments.resolve(params, arguments)
277
- assert_equal(expected, result)
278
- end
279
-
280
- def test_parse_args_hash
281
- test_method = UtilTestModule.public_method(:test_command_arg_hash)
282
- params = Rubycom::Arguments.get_param_definitions(test_method)
283
- arguments = ["{testing: 'arg1', 123: 'abc', abc: 123}"]
284
- expected = {:test_hash=>{"testing"=>"arg1", 123=>"abc", "abc"=>123}}
285
- result = Rubycom::Arguments.resolve(params, arguments)
286
- assert_equal(expected, result)
287
- end
288
-
289
- end