cmd 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (14) hide show
  1. data/AUTHORS +1 -0
  2. data/CHANGELOG +5 -0
  3. data/INSTALL +11 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README +411 -0
  6. data/Rakefile +141 -0
  7. data/THANKS +12 -0
  8. data/TODO +124 -0
  9. data/example/calc.rb +86 -0
  10. data/example/phonebook.rb +69 -0
  11. data/lib/cmd.rb +557 -0
  12. data/setup.rb +1360 -0
  13. data/test/tc_cmd.rb +284 -0
  14. metadata +67 -0
@@ -0,0 +1,284 @@
1
+ require File.dirname(__FILE__) + '/../lib/cmd'
2
+ require 'test/unit'
3
+ require 'stringio'
4
+
5
+ class TCmdException < StandardError
6
+ end
7
+
8
+ class CustomException < StandardError
9
+ end
10
+
11
+ class AnotherCustomException < StandardError
12
+ end
13
+
14
+ class Cmd
15
+ def in
16
+ @stdin
17
+ end
18
+
19
+ def out
20
+ @stdout
21
+ end
22
+ end
23
+
24
+ class TCmdBasic < Cmd
25
+ end
26
+
27
+ class TCmdWithCommands < Cmd
28
+
29
+ prompt_with ENV['USER']
30
+
31
+ handle CustomException, :handle_custom_exception
32
+ handle AnotherCustomException, 'Handled another custom exception'
33
+ handle ZeroDivisionError, 'Division by zero'
34
+
35
+ def do_exit_violently
36
+ raise TCmdException
37
+ end
38
+
39
+ def do_trigger_custom_exception
40
+ raise CustomException
41
+ end
42
+
43
+ def do_trigger_another_custom_exception
44
+ raise AnotherCustomException
45
+ end
46
+
47
+ def do_trigger_zero_division_error
48
+ 1 / 0
49
+ end
50
+
51
+ def do_simple
52
+ write 'Ran simple command'
53
+ end
54
+
55
+ def do_shell_type
56
+ end
57
+
58
+ protected
59
+
60
+ def handle_custom_exception
61
+ write 'Handled custom exception'
62
+ end
63
+
64
+ def handle_exception(exception)
65
+ write 'Exception was raised'
66
+ end
67
+
68
+ def empty_line
69
+ write 'Empty line was entered'
70
+ end
71
+
72
+ end
73
+
74
+ class TCmdLifecycle < Cmd
75
+
76
+ def methods_ran
77
+ (@methods_ran ||= []).sort!
78
+ @methods_ran
79
+ end
80
+
81
+ protected
82
+
83
+ def setup; methods_ran << 'setup' end
84
+ def preloop; methods_ran << 'preloop' end
85
+ def postloop; methods_ran << 'postloop' end
86
+
87
+ def precmd(line)
88
+ methods_ran << 'precmd'
89
+ line
90
+ end
91
+
92
+ def postcmd(line)
93
+ methods_ran << 'postcmd'
94
+ line
95
+ end
96
+
97
+ end
98
+
99
+ class TC_Cmd < Test::Unit::TestCase
100
+ def setup
101
+ @basic = setup_mock_cmd(TCmdBasic)
102
+ @cmd = setup_mock_cmd(TCmdWithCommands)
103
+ @lifecycle = setup_mock_cmd(TCmdLifecycle)
104
+ end
105
+
106
+ def test_simple_command
107
+ run_command 'simple'
108
+ assert_stdout_equal "Ran simple command\n"
109
+ end
110
+
111
+ def test_custom_exception
112
+ run_command 'trigger_custom_exception'
113
+ assert_stdout_equal "Handled custom exception\n"
114
+ end
115
+
116
+ def test_another_custom_exception
117
+ run_command 'trigger_another_custom_exception'
118
+ assert_stdout_equal "Handled another custom exception\n"
119
+ end
120
+
121
+ def test_zero_division_error
122
+ run_command 'trigger_zero_division_error'
123
+ assert_stdout_equal "Division by zero\n"
124
+ end
125
+
126
+ def test_prompt
127
+ assert_equal ENV['USER'], @cmd.send(:prompt)
128
+ end
129
+
130
+ def test_abbrev_command_lookup
131
+ run_command 'si'
132
+ assert_stdout_equal "Ran simple command\n"
133
+ end
134
+
135
+ def test_abbrev_command_ambiguous_lookup
136
+ run_command 's'
137
+ assert_stdout_equal "No such command 's'\n"
138
+ end
139
+
140
+ def test_empty_line
141
+ run_command ' '
142
+ assert_stdout_equal "Empty line was entered\n"
143
+ end
144
+
145
+ def test_lifecycle_callbacks
146
+ run_command 'exit', @lifecycle
147
+ assert_equal %w(postcmd postloop precmd preloop setup),
148
+ @lifecycle.methods_ran
149
+ end
150
+
151
+ def test_command_list
152
+ assert_equal %w(exit help shell), @basic.send(:command_list).sort
153
+ end
154
+
155
+ def test_subcommand_list
156
+ assert_equal %w(exit_violently shell_type), @cmd.send(:subcommand_list).sort
157
+ end
158
+
159
+ def test_complete
160
+ assert_equal %w(exit help shell), @basic.send(:complete, '').sort
161
+ assert_equal %w(help), @basic.send(:complete, 'h')
162
+ assert_equal [], @basic.send(:complete, 'asdf')
163
+ end
164
+
165
+ def test_has_subcommands?
166
+ assert ! @cmd.send(:has_subcommands?, 'help')
167
+ assert ! @basic.send(:has_subcommands?, 'help')
168
+ assert @cmd.send(:has_subcommands?, 'exit')
169
+ assert @cmd.send(:has_subcommands?, '!')
170
+ end
171
+
172
+ def test_subcommand_lookup
173
+ assert_equal [], @basic.send(:subcommands, 'help')
174
+ assert_equal %w(exit_violently), @cmd.send(:subcommands, 'exit')
175
+ assert_equal %w(shell_type), @cmd.send(:subcommands, '!')
176
+ end
177
+
178
+ def test_documented_commands_list
179
+ assert_equal %w(exit help shell), @basic.send(:documented_commands).sort
180
+ end
181
+
182
+ def test_handle_exception
183
+ run_command 'exit violently'
184
+ assert_stdout_equal "Exception was raised\n"
185
+ end
186
+
187
+ def test_undocummented_commands_list
188
+ assert ! @basic.send(:undocumented_commands?)
189
+ assert_equal [], @basic.send(:undocumented_commands)
190
+ end
191
+
192
+ def test_translate_shortcut
193
+ assert_equal 'help', @basic.send(:translate_shortcut, 'help')
194
+ assert_equal 'help', @basic.send(:translate_shortcut, '?')
195
+ end
196
+
197
+ def test_has_shortcuts?
198
+ assert @cmd.send(:has_shortcuts?, 'help')
199
+ assert !@cmd.send(:has_shortcuts?, 'exit')
200
+ end
201
+
202
+ def test_command_shortcuts
203
+ assert_equal ['?'], @basic.send(:command_shortcuts, 'help')
204
+ end
205
+
206
+ def test_find_subcommand_in_args
207
+ assert_equal 'help_me', @basic.send(:find_subcommand_in_args,
208
+ %w(help_you help_me help_him),
209
+ %w(help me help you))
210
+ assert_equal 'help_me', @basic.send(:find_subcommand_in_args,
211
+ %w(help_you help_me help_him),
212
+ %w(help me))
213
+ assert_equal nil, @basic.send(:find_subcommand_in_args,
214
+ %w(help_you help_me help_him),
215
+ %w(help her))
216
+ end
217
+
218
+ def test_command_missing
219
+ run_command '??'
220
+ assert_stdout_equal "No such command '??'\n"
221
+ end
222
+
223
+ def test_no_help
224
+ run_command '? flarp'
225
+ assert_stdout_equal "No help for command 'flarp'\n"
226
+ end
227
+
228
+ def test_current_command
229
+ run_command 'help'
230
+ assert_equal 'help', @cmd.send(:current_command)
231
+ end
232
+
233
+ def test_current_command_with_shortcut
234
+ run_command '?'
235
+ assert_equal 'help', @cmd.send(:current_command)
236
+ end
237
+
238
+ def test_help_on_single_command
239
+ @basic.send(:execute_line, '? ?')
240
+ actual = @basic.out.string
241
+ @basic.out.rewind
242
+ @basic.send(:print_help, 'help')
243
+ expected = @basic.out.string
244
+ assert_equal expected, actual
245
+
246
+ @basic.send(:execute_line, 'help not-a-command')
247
+ actual = @basic.out.string
248
+ @basic.out.rewind
249
+ @basic.send(:no_help, 'not-a-command')
250
+ expected = @basic.out.string
251
+ assert_equal expected, actual
252
+ end
253
+
254
+ def test_parse_line
255
+ assert_equal ['help', nil], @basic.send(:parse_line, 'help')
256
+ assert_equal ['help', nil], @basic.send(:parse_line, 'help ')
257
+ assert_equal ['help', 'shell'], @basic.send(:parse_line, 'help shell')
258
+ assert_equal ['shell_type', nil], @cmd.send(:parse_line, 'shell type')
259
+ assert_equal ['shell_type', 'zsh'], @cmd.send(:parse_line, 'shell type zsh')
260
+ end
261
+
262
+ protected
263
+
264
+ def run_command(line, obj = @cmd)
265
+ obj.in << line
266
+ obj.in.rewind
267
+ obj.run
268
+ obj.out.pos = obj.send(:prompt).size
269
+ end
270
+
271
+ def assert_stdout_equal(line, obj = @cmd)
272
+ assert_equal line, obj.out.read
273
+ end
274
+
275
+ def setup_mock_cmd(klass)
276
+ obj = klass.new
277
+ obj.stdin = StringIO.new
278
+ obj.stdout = StringIO.new
279
+ obj.send :stoploop
280
+ obj.turn_off_readline
281
+ obj
282
+ end
283
+
284
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: cmd
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.7.0
7
+ date: 2005-04-10
8
+ summary: A generic class to build line-oriented command interpreters.
9
+ require_paths:
10
+ - lib
11
+ email: marcel@vernix.org
12
+ homepage: http://cmd.rubyforge.org
13
+ rubyforge_project: cmd
14
+ description: A generic class to build line-oriented command interpreters.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Marcel Molina Jr.
29
+ files:
30
+ - test
31
+ - lib
32
+ - Rakefile
33
+ - setup.rb
34
+ - AUTHORS
35
+ - TODO
36
+ - INSTALL
37
+ - example
38
+ - CHANGELOG
39
+ - THANKS
40
+ - MIT-LICENSE
41
+ - download
42
+ - README
43
+ - lib/cmd.rb
44
+ - test/tc_cmd.rb
45
+ - example/phonebook.rb
46
+ - example/calc.rb
47
+ test_files:
48
+ - test/tc_cmd.rb
49
+ rdoc_options:
50
+ - "--main"
51
+ - README
52
+ - "--title"
53
+ - cmd
54
+ - "--line-numbers"
55
+ - "--inline-source"
56
+ extra_rdoc_files:
57
+ - AUTHORS
58
+ - CHANGELOG
59
+ - INSTALL
60
+ - README
61
+ - THANKS
62
+ - TODO
63
+ - lib/cmd.rb
64
+ executables: []
65
+ extensions: []
66
+ requirements: []
67
+ dependencies: []