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
@@ -0,0 +1,122 @@
1
+ require "#{File.dirname(__FILE__)}/../../lib/rubycom/singleton_commands.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 SingletonCommandsTest < Test::Unit::TestCase
10
+
11
+ def test_discover_command_command_run
12
+ test_mod = UtilTestComposite
13
+ test_args = {:args=>["UtilTestModule", "test_command_with_return", "testing_argument"],:opts=>{"test_option_int"=>10}}
14
+ result = Rubycom::SingletonCommands.discover_command(test_mod, test_args)
15
+ expected = UtilTestModule.public_method(:test_command_with_return)
16
+ assert_equal(expected, result)
17
+ end
18
+
19
+ def test_discover_commands_nil
20
+ test_mod = UtilTestComposite
21
+ test_args = nil
22
+ result = nil
23
+ assert_raise(ArgumentError) { Rubycom::SingletonCommands.discover_commands(test_mod, test_args) }
24
+ expected = nil
25
+ assert_equal(expected, result)
26
+ end
27
+
28
+ def test_discover_commands_nil_args
29
+ test_mod = UtilTestComposite
30
+ test_args = {}
31
+ result = Rubycom::SingletonCommands.discover_commands(test_mod, test_args)
32
+ expected = [
33
+ UtilTestComposite
34
+ ]
35
+ assert_equal(expected, result)
36
+ end
37
+
38
+ def test_discover_commands_empty
39
+ test_mod = UtilTestComposite
40
+ test_args = {:args => []}
41
+ result = Rubycom::SingletonCommands.discover_commands(test_mod, test_args)
42
+ expected = [
43
+ UtilTestComposite
44
+ ]
45
+ assert_equal(expected, result)
46
+ end
47
+
48
+ def test_discover_commands_command_before_module
49
+ test_mod = UtilTestComposite
50
+ test_args = {:args => ['test_command', 'UtilTestModule', 'test_extra_arg']}
51
+ result = Rubycom::SingletonCommands.discover_commands(test_mod, test_args)
52
+ expected = [
53
+ UtilTestComposite,
54
+ "test_command",
55
+ "UtilTestModule",
56
+ "test_extra_arg"
57
+ ]
58
+ assert_equal(expected, result)
59
+ end
60
+
61
+ def test_discover_commands_no_module_match
62
+ test_mod = UtilTestComposite
63
+ test_args = {:args => ['test_arg', 'test_command', 'test_extra_arg']}
64
+ result = Rubycom::SingletonCommands.discover_commands(test_mod, test_args)
65
+ expected = [
66
+ UtilTestComposite,
67
+ "test_arg",
68
+ "test_command",
69
+ "test_extra_arg"
70
+ ]
71
+ assert_equal(expected, result)
72
+ end
73
+
74
+ def test_discover_commands_no_method_match
75
+ test_mod = UtilTestComposite
76
+ test_args = {:args => ['UtilTestModule', 'test_arg', 'test_extra_arg']}
77
+ result = Rubycom::SingletonCommands.discover_commands(test_mod, test_args)
78
+ expected = [
79
+ UtilTestComposite,
80
+ UtilTestModule,
81
+ "test_arg",
82
+ "test_extra_arg"
83
+ ]
84
+ assert_equal(expected, result)
85
+ end
86
+
87
+ def test_discover_commands_extra_arg
88
+ test_mod = UtilTestComposite
89
+ test_args = {:args => ['UtilTestModule', 'test_command', 'test_extra_arg']}
90
+ result = Rubycom::SingletonCommands.discover_commands(test_mod, test_args)
91
+ expected = [
92
+ UtilTestComposite,
93
+ UtilTestModule,
94
+ UtilTestModule.public_method(:test_command),
95
+ "test_extra_arg"
96
+ ]
97
+ assert_equal(expected, result)
98
+ end
99
+
100
+ def test_get_commands
101
+ test_mod = UtilTestComposite
102
+ result = Rubycom::SingletonCommands.get_commands(test_mod)
103
+ result.each{|k, v|
104
+ assert(k.class == Symbol, "each key should be a symbol")
105
+ UtilTestComposite.included_modules.include?(k)
106
+ assert([Hash, Symbol].include?(v.class), "each value should be a Hash or a Symbol")
107
+ assert(v == :method, "if the returned value is not a hash then it should be the symbol :method") if v.class == Symbol
108
+ }
109
+ end
110
+
111
+ def test_get_commands_all_false
112
+ test_mod = UtilTestComposite
113
+ result_keys = Rubycom::SingletonCommands.get_commands(test_mod, false)[:UtilTestComposite].keys.map{|key|key.to_s}
114
+ test_mod.singleton_methods(false).each{|meth|
115
+ assert(result_keys.include?(meth.to_s), "get_commands result keys: #{result_keys} should include base module singleton method #{meth.to_s}")
116
+ }
117
+ test_mod.included_modules.reject{|mod|mod.to_s == "Rubycom"}.each{|mod|
118
+ assert(result_keys.include?(mod.to_s), "get_commands result keys: #{result_keys} should include base included module #{mod.to_s}")
119
+ }
120
+ end
121
+
122
+ end
@@ -0,0 +1,59 @@
1
+ require "#{File.dirname(__FILE__)}/../../lib/rubycom/sources.rb"
2
+
3
+ require "#{File.dirname(__FILE__)}/util_test_bin.rb"
4
+ require "#{File.dirname(__FILE__)}/util_test_module.rb"
5
+ require "#{File.dirname(__FILE__)}/util_test_composite.rb"
6
+ require "#{File.dirname(__FILE__)}/util_test_no_singleton.rb"
7
+
8
+ require 'test/unit'
9
+
10
+ class SourcesTest < Test::Unit::TestCase
11
+
12
+ def test_source_commands
13
+ test_commands = [
14
+ UtilTestComposite,
15
+ UtilTestModule,
16
+ UtilTestModule.public_method(:test_command),
17
+ "test_extra_arg"
18
+ ]
19
+ result = Rubycom::Sources.source_commands(test_commands)
20
+ assert(result.size > 0, "should be at least one result")
21
+ result.each{|res|
22
+ assert(res.class == Hash, "each result should be a Hash")
23
+ assert(res.size == 2, "each result should have two keys")
24
+ assert(res.has_key?(:command), "each result should have a :command key")
25
+ assert(res.has_key?(:source), "each result should have a :source key")
26
+ }
27
+ assert(result.select{|res|res[:command] == UtilTestComposite}.size == 1, "result should have a UtilTestComposite command")
28
+ assert(result.select{|res|res[:command] == UtilTestModule}.size == 1, "result should have a UtilTestComposite command")
29
+ assert(result.select{|res|res[:command] == UtilTestModule.public_method(:test_command)}.size == 1, "result should have a test_command method command")
30
+ assert(result.select{|res|res[:command] == "test_extra_arg"}.size == 1, "result should have a test_extra_arg String command")
31
+ assert_equal(result.select{|res|res[:command] == UtilTestComposite}.first[:source].gsub("\n",''), (File.read("#{File.dirname(__FILE__)}/util_test_composite.rb")+File.read("#{File.dirname(__FILE__)}/util_test_sub_module.rb")+File.read("#{File.dirname(__FILE__)}/util_test_bin.rb")).gsub("\n",''))
32
+ assert_equal(result.select{|res|res[:command] == UtilTestModule}.first[:source].gsub("\n",''), (File.read("#{File.dirname(__FILE__)}/util_test_module.rb")).gsub("\n",''))
33
+ assert_equal(result.select{|res|res[:command] == UtilTestModule.public_method(:test_command)}.first[:source].gsub("\n",''), "# A basic test command\n def self.test_command\n puts 'command test'\n end\n".gsub("\n",''))
34
+ assert_equal(result.select{|res|res[:command] == "test_extra_arg"}.first[:source].gsub("\n",''), "test_extra_arg")
35
+ end
36
+
37
+ def test_source_command_run
38
+ test_command = UtilTestModule.public_method(:test_command_with_return)
39
+ result = Rubycom::Sources.source_command(test_command)
40
+ expected = "# A test_command with a return argument\n#\n# @param [String] test_arg a test argument\n# @param [Integer] test_option_int an optional test argument which happens to be an Integer\n# @return [Array] an array including both params if test_option_int != 1\n# @return [String] the first param if test_option_int == 1\n def self.test_command_with_return(test_arg, test_option_int=1)\n ret = [test_arg, test_option_int]\n if test_option_int == 1\n ret = test_arg\n end\n ret\n end\n"
41
+ assert_equal(expected, result)
42
+ end
43
+
44
+ def test_source_command_run_module
45
+ test_command = UtilTestModule
46
+ result = Rubycom::Sources.source_command(test_command)
47
+ expected = File.read("#{File.dirname(__FILE__)}/util_test_module.rb")
48
+ assert_equal(expected, result)
49
+ end
50
+
51
+ def test_source_command_run_composite
52
+ test_command = UtilTestComposite
53
+ result = Rubycom::Sources.source_command(test_command)
54
+ bin_file = File.read("#{File.dirname(__FILE__)}/util_test_bin.rb")
55
+ lib_file = File.read("#{File.dirname(__FILE__)}/util_test_composite.rb")
56
+ assert(result.include?(bin_file),"source for composite module #{test_command} must include bin_file")
57
+ assert(result.include?(lib_file),"source for composite module #{test_command} must include lib_file")
58
+ end
59
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require "#{File.expand_path(File.dirname(__FILE__))}/../../lib/rubycom.rb"
3
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_composite.rb"
4
+
5
+ # Test library entry point. Acts as the runnable for UtilTestComposite.rb
6
+ module UtilTestComposite
7
+ include Rubycom
8
+ end
@@ -1,20 +1,23 @@
1
- #!/usr/bin/env ruby
2
- require "#{File.expand_path(File.dirname(__FILE__))}/../../lib/rubycom.rb"
3
- require "#{File.expand_path(File.dirname(__FILE__))}/util_test_module.rb"
4
- require "#{File.expand_path(File.dirname(__FILE__))}/util_test_no_singleton.rb"
5
-
6
- module UtilTestComposite
7
- include UtilTestModule
8
-
9
- include UtilTestNoSingleton
10
-
11
- # A test_command in a composite console
12
- #
13
- # @param [String] test_arg a test argument
14
- # @return [String] the test arg
15
- def self.test_composite_command(test_arg)
16
- test_arg
17
- end
18
-
19
- include Rubycom
20
- end
1
+ #!/usr/bin/env ruby
2
+ require "#{File.expand_path(File.dirname(__FILE__))}/../../lib/rubycom.rb"
3
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_module.rb"
4
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_no_singleton.rb"
5
+
6
+ # Composite module for testing
7
+ module UtilTestComposite
8
+ include UtilTestModule
9
+
10
+ include UtilTestNoSingleton
11
+
12
+ require "#{File.expand_path(File.dirname(__FILE__))}/util_test_sub_module.rb"
13
+ include UtilTestSubModule
14
+
15
+ # A test_command in a composite console
16
+ #
17
+ # @param [String] test_arg a test argument
18
+ # @return [String] the test arg
19
+ def self.test_composite_command(test_arg)
20
+ test_arg
21
+ end
22
+
23
+ end
@@ -1,112 +1,142 @@
1
- require "#{File.expand_path(File.dirname(__FILE__))}/../../lib/rubycom.rb"
2
- # A command module used for testing
3
- module UtilTestModule
4
-
5
- # A test non-command method
6
- def non_command
7
- puts 'fail'
8
- end
9
-
10
- # A basic test command
11
- def self.test_command
12
- puts 'command test'
13
- end
14
-
15
- def self.test_command_no_docs
16
- puts 'command test'
17
- end
18
-
19
- # A test_command with one arg
20
- #
21
- # @param [String] test_arg a test argument
22
- def self.test_command_with_arg(test_arg)
23
- "test_arg=#{test_arg}"
24
- end
25
-
26
- # A test_command with an arg named arg
27
- #
28
- # @param [String] arg a test argument whose parameter name is arg
29
- def self.test_command_arg_named_arg(arg)
30
- "arg=#{arg}"
31
- end
32
-
33
- # A test_command with two args
34
- # @param [String] test_arg a test argument
35
- # @param [String] another_test_arg another test argument
36
- def self.test_command_with_args(test_arg, another_test_arg)
37
- puts "test_arg=#{test_arg},another_test_arg=#{another_test_arg}"
38
- end
39
-
40
- # A test_command with an optional argument
41
- # @param [String] test_arg a test argument
42
- # @param [String] test_option an optional test argument
43
- def self.test_command_with_options(test_arg, test_option='option_default')
44
- puts "test_arg=#{test_arg},test_option=#{test_option}"
45
- end
46
-
47
- # A test_command with all optional arguments
48
- # @param [String] test_arg an optional test argument
49
- # @param [String] test_option another optional test argument
50
- def self.test_command_all_options(test_arg='test_arg_default', test_option='test_option_default')
51
- puts "Output is test_arg=#{test_arg},test_option=#{test_option}"
52
- end
53
-
54
- # A test_command with an options array
55
- # @param [String] test_option an optional test argument
56
- # @param [Array] test_options an optional array of arguments
57
- def self.test_command_options_arr (
58
- test_option="test_option_default",
59
- *test_options
60
- )
61
- puts "Output is test_option=#{test_option},test_option_arr=#{test_options}"
62
- end
63
-
64
- # A test_command with a return argument
65
- #
66
- # @param [String] test_arg a test argument
67
- # @param [Integer] test_option_int an optional test argument which happens to be an Integer
68
- # @return [Array] an array including both params if test_option_int != 1
69
- # @return [String] a the first param if test_option_int == 1
70
- def self.test_command_with_return(test_arg, test_option_int=1)
71
- ret = [test_arg, test_option_int]
72
- if test_option_int == 1
73
- ret = test_arg
74
- end
75
- ret
76
- end
77
-
78
- # A test_command with a Timestamp argument and an unnecessarily long description which should overflow when
79
- # it tries to line up with other descriptions.
80
- # @param [Timestamp] test_time a test Timestamp argument
81
- # @return [Hash] a hash including the given argument
82
- def self.test_command_arg_timestamp(test_time)
83
- {test_time: test_time}
84
- end
85
-
86
- # A test_command with a Boolean argument
87
- # @param [Boolean] test_flag a test Boolean argument
88
- # @return [Boolean] the flag passed in
89
- def self.test_command_arg_false(test_flag=false)
90
- test_flag
91
- end
92
-
93
- # A test_command with an array argument
94
- #
95
- # @param [Array] test_arr an Array test argument
96
- def self.test_command_arg_arr(test_arr=[])
97
- test_arr
98
- end
99
-
100
- # A test_command with an Hash argument
101
- # @param [Hash] test_hash a Hash test argument
102
- def self.test_command_arg_hash(test_hash={})
103
- test_hash
104
- end
105
-
106
- # A test_command with several mixed options
107
- def self.test_command_mixed_options(test_arg, test_arr=[], test_opt='test_opt_arg', test_hsh={}, test_bool=true, *test_rest)
108
- "test_arg=#{test_arg} test_arr=#{test_arr} test_opt=#{test_opt} test_hsh=#{test_hsh} test_bool=#{test_bool} test_rest=#{test_rest}"
109
- end
110
-
111
- include Rubycom
112
- end
1
+ require "#{File.expand_path(File.dirname(__FILE__))}/../../lib/rubycom.rb"
2
+ # A command module used for testing
3
+ #
4
+ #This module contains most of the test case input methods.
5
+ module UtilTestModule
6
+
7
+ # A test non-command method
8
+ def non_command
9
+ puts 'fail'
10
+ end
11
+
12
+ # A basic test command
13
+ def self.test_command
14
+ puts 'command test'
15
+ end
16
+
17
+ def self.test_command_no_docs
18
+ puts 'command test'
19
+ end
20
+
21
+ # A test_command with one arg
22
+ #
23
+ # @param [String] test_arg a test argument
24
+ def self.test_command_with_arg(test_arg)
25
+ "test_arg=#{test_arg}"
26
+ end
27
+
28
+ # A test_command with an arg named arg
29
+ #
30
+ # @param [String] arg a test argument whose parameter name is arg
31
+ def self.test_command_arg_named_arg(arg)
32
+ "arg=#{arg}"
33
+ end
34
+
35
+ # A test_command with two args
36
+ # @param [String] test_arg a test argument
37
+ # @param [String] another_test_arg another test argument
38
+ def self.test_command_with_args(test_arg, another_test_arg)
39
+ puts "test_arg=#{test_arg},another_test_arg=#{another_test_arg}"
40
+ end
41
+
42
+ # A test_command with an optional argument
43
+ # @param [String] test_arg a test argument
44
+ # @param [String] test_option an optional test argument
45
+ def self.test_command_with_options(test_arg, test_option='option_default')
46
+ puts "test_arg=#{test_arg},test_option=#{test_option}"
47
+ end
48
+
49
+ # A test_command with all optional arguments
50
+ # @param [String] test_arg an optional test argument
51
+ # @param [String] test_option another optional test argument
52
+ def self.test_command_all_options(test_arg='test_arg_default', test_option='test_option_default')
53
+ puts "Output is test_arg=#{test_arg},test_option=#{test_option}"
54
+ end
55
+
56
+ # A test_command with a nil optional argument
57
+ # @param [String] test_arg a test argument
58
+ # @param [String] test_option an optional test argument with a nil default value
59
+ # @return [String] a message including the value and class of each parameter
60
+ def self.test_command_nil_option(test_arg, test_option=nil)
61
+ "test_arg=#{test_arg}, test_arg.class=#{test_arg.class}, test_option=#{test_option}, test_option.class=#{test_option.class}"
62
+ end
63
+
64
+ # A test_command with an options array
65
+ # @param [String] test_option an optional test argument
66
+ # @param [Array] test_options an optional array of arguments
67
+ def self.test_command_options_arr (
68
+ test_option='test_option_default',
69
+ *test_options
70
+ )
71
+ puts "Output is test_option=#{test_option},test_option_arr=#{test_options}"
72
+ end
73
+
74
+ # A test_command with a return argument
75
+ #
76
+ # @param [String] test_arg a test argument
77
+ # @param [Integer] test_option_int an optional test argument which happens to be an Integer
78
+ # @return [Array] an array including both params if test_option_int != 1
79
+ # @return [String] the first param if test_option_int == 1
80
+ def self.test_command_with_return(test_arg, test_option_int=1)
81
+ ret = [test_arg, test_option_int]
82
+ if test_option_int == 1
83
+ ret = test_arg
84
+ end
85
+ ret
86
+ end
87
+
88
+ # A test_command with a Timestamp argument and an unnecessarily long description which should overflow when
89
+ # it tries to line up with other descriptions.
90
+ #
91
+ # some more stuff
92
+ #
93
+ # @param [Timestamp] test_time a test Timestamp argument
94
+ # @return [Hash] a hash including the given argument
95
+ def self.test_command_arg_timestamp(test_time)
96
+ {:test_time => test_time}
97
+ end
98
+
99
+ # A test_command with a Boolean argument
100
+ # @param [Boolean] test_flag a test Boolean argument
101
+ # @return [Boolean] the flag passed in
102
+ def self.test_command_arg_false(test_flag=false)
103
+ test_flag
104
+ end
105
+
106
+ # A test_command with an array argument
107
+ #
108
+ # @param [Array] test_arr an Array test argument
109
+ def self.test_command_arg_arr(test_arr=[])
110
+ test_arr
111
+ end
112
+
113
+ # A test_command with an Hash argument
114
+ # @param [Hash] test_hash a Hash test argument
115
+ def self.test_command_arg_hash(test_hash={})
116
+ test_hash
117
+ end
118
+
119
+ # A test_command with several mixed options
120
+ #
121
+ # @param [String] test_arg
122
+ # @param [Array] test_arr
123
+ # @param [String] test_opt
124
+ # @param [Fixnum] test_opt
125
+ def self.test_command_mixed_options(test_arg, test_arr=[], test_opt='test_opt_arg', test_hsh={}, test_bool=true, *test_rest)
126
+ "test_arg=#{test_arg} test_arr=#{test_arr} test_opt=#{test_opt} test_hsh=#{test_hsh} test_bool=#{test_bool} test_rest=#{test_rest}"
127
+ end
128
+
129
+ # A test_command with several mixed options with varying names
130
+ #
131
+ # @param [Object] arg_test anything
132
+ # @param [Array] arr an array of things
133
+ # @param [String] opt an optional string
134
+ # @param [Hash] hsh a hash representing some test keys and values
135
+ # @param [TrueClass|FalseClass] bool a true or false
136
+ # @param [Array] rest_test everything else
137
+ def self.test_command_mixed_names(arg_test, arr=[], opt='test_opt_arg', hsh={}, bool=true, *rest_test)
138
+ "arg_test=#{arg_test} arr=#{arr} opt=#{opt} hsh=#{hsh} bool=#{bool} rest_test=#{rest_test}"
139
+ end
140
+
141
+ include Rubycom
142
+ end