pry 0.10.2-i386-mingw32 → 1.0.0.pre1-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. data/.document +2 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +21 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG +503 -0
  6. data/CONTRIBUTORS +55 -0
  7. data/Gemfile +9 -0
  8. data/Guardfile +62 -0
  9. data/LICENSE +2 -2
  10. data/{README.md → README.markdown} +31 -37
  11. data/Rakefile +144 -0
  12. data/TODO +117 -0
  13. data/lib/pry.rb +146 -33
  14. data/lib/pry/cli.rb +13 -35
  15. data/lib/pry/code.rb +63 -24
  16. data/lib/pry/code/loc.rb +2 -2
  17. data/lib/pry/code_object.rb +21 -40
  18. data/lib/pry/command.rb +6 -9
  19. data/lib/pry/command_set.rb +37 -80
  20. data/lib/pry/commands.rb +1 -1
  21. data/lib/pry/commands/amend_line.rb +1 -1
  22. data/lib/pry/commands/bang.rb +1 -1
  23. data/lib/pry/commands/cat.rb +2 -11
  24. data/lib/pry/commands/cat/abstract_formatter.rb +1 -1
  25. data/lib/pry/commands/cat/exception_formatter.rb +7 -6
  26. data/lib/pry/commands/cat/file_formatter.rb +32 -15
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  28. data/lib/pry/commands/cd.rb +3 -14
  29. data/lib/pry/commands/code_collector.rb +4 -4
  30. data/lib/pry/commands/easter_eggs.rb +3 -3
  31. data/lib/pry/commands/edit.rb +22 -10
  32. data/lib/pry/commands/edit/exception_patcher.rb +1 -1
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +2 -0
  34. data/lib/pry/{method/patcher.rb → commands/edit/method_patcher.rb} +37 -40
  35. data/lib/pry/commands/find_method.rb +22 -16
  36. data/lib/pry/commands/gem_install.rb +2 -5
  37. data/lib/pry/commands/gem_open.rb +1 -1
  38. data/lib/pry/commands/gist.rb +11 -10
  39. data/lib/pry/commands/help.rb +14 -14
  40. data/lib/pry/commands/hist.rb +5 -24
  41. data/lib/pry/commands/ls.rb +287 -56
  42. data/lib/pry/commands/play.rb +10 -44
  43. data/lib/pry/commands/pry_backtrace.rb +2 -1
  44. data/lib/pry/commands/raise_up.rb +1 -1
  45. data/lib/pry/commands/reload_code.rb +15 -31
  46. data/lib/pry/commands/ri.rb +3 -7
  47. data/lib/pry/commands/shell_command.rb +12 -17
  48. data/lib/pry/commands/shell_mode.rb +2 -2
  49. data/lib/pry/commands/show_doc.rb +0 -5
  50. data/lib/pry/commands/show_info.rb +10 -11
  51. data/lib/pry/commands/show_source.rb +3 -15
  52. data/lib/pry/commands/simple_prompt.rb +1 -1
  53. data/lib/pry/commands/toggle_color.rb +4 -8
  54. data/lib/pry/commands/whereami.rb +10 -18
  55. data/lib/pry/completion.rb +293 -0
  56. data/lib/pry/config.rb +233 -20
  57. data/lib/pry/core_extensions.rb +19 -29
  58. data/lib/pry/custom_completions.rb +6 -0
  59. data/lib/pry/editor.rb +103 -109
  60. data/lib/pry/helpers/base_helpers.rb +109 -22
  61. data/lib/pry/helpers/command_helpers.rb +8 -10
  62. data/lib/pry/helpers/documentation_helpers.rb +2 -1
  63. data/lib/pry/helpers/text.rb +5 -4
  64. data/lib/pry/history.rb +10 -21
  65. data/lib/pry/history_array.rb +0 -5
  66. data/lib/pry/hooks.rb +29 -9
  67. data/lib/pry/indent.rb +10 -5
  68. data/lib/pry/method.rb +86 -81
  69. data/lib/pry/method/weird_method_locator.rb +2 -4
  70. data/lib/pry/module_candidate.rb +14 -5
  71. data/lib/pry/pager.rb +48 -193
  72. data/lib/pry/plugins.rb +2 -2
  73. data/lib/pry/pry_class.rb +193 -104
  74. data/lib/pry/pry_instance.rb +154 -152
  75. data/lib/pry/rbx_method.rb +13 -0
  76. data/lib/pry/rbx_path.rb +1 -1
  77. data/lib/pry/repl.rb +14 -17
  78. data/lib/pry/repl_file_loader.rb +3 -8
  79. data/lib/pry/rubygem.rb +3 -3
  80. data/lib/pry/terminal.rb +3 -4
  81. data/lib/pry/test/helper.rb +11 -6
  82. data/lib/pry/version.rb +1 -1
  83. data/lib/pry/wrapped_module.rb +56 -49
  84. data/man/pry.1 +195 -0
  85. data/man/pry.1.html +204 -0
  86. data/man/pry.1.ronn +141 -0
  87. data/pry.gemspec +31 -0
  88. data/spec/Procfile +3 -0
  89. data/spec/cli_spec.rb +78 -0
  90. data/spec/code_object_spec.rb +277 -0
  91. data/spec/code_spec.rb +219 -0
  92. data/spec/command_helpers_spec.rb +29 -0
  93. data/spec/command_integration_spec.rb +562 -0
  94. data/spec/command_set_spec.rb +627 -0
  95. data/spec/command_spec.rb +821 -0
  96. data/spec/commands/amend_line_spec.rb +247 -0
  97. data/spec/commands/bang_spec.rb +18 -0
  98. data/spec/commands/cat_spec.rb +164 -0
  99. data/spec/commands/cd_spec.rb +250 -0
  100. data/spec/commands/disable_pry_spec.rb +25 -0
  101. data/spec/commands/edit_spec.rb +725 -0
  102. data/spec/commands/exit_all_spec.rb +27 -0
  103. data/spec/commands/exit_program_spec.rb +19 -0
  104. data/spec/commands/exit_spec.rb +28 -0
  105. data/spec/commands/find_method_spec.rb +70 -0
  106. data/spec/commands/gem_list_spec.rb +26 -0
  107. data/spec/commands/gist_spec.rb +79 -0
  108. data/spec/commands/help_spec.rb +56 -0
  109. data/spec/commands/hist_spec.rb +172 -0
  110. data/spec/commands/jump_to_spec.rb +15 -0
  111. data/spec/commands/ls_spec.rb +189 -0
  112. data/spec/commands/play_spec.rb +136 -0
  113. data/spec/commands/raise_up_spec.rb +56 -0
  114. data/spec/commands/save_file_spec.rb +177 -0
  115. data/spec/commands/show_doc_spec.rb +488 -0
  116. data/spec/commands/show_input_spec.rb +17 -0
  117. data/spec/commands/show_source_spec.rb +760 -0
  118. data/spec/commands/whereami_spec.rb +203 -0
  119. data/spec/completion_spec.rb +221 -0
  120. data/spec/control_d_handler_spec.rb +62 -0
  121. data/spec/documentation_helper_spec.rb +73 -0
  122. data/spec/editor_spec.rb +79 -0
  123. data/spec/exception_whitelist_spec.rb +21 -0
  124. data/spec/fixtures/candidate_helper1.rb +11 -0
  125. data/spec/fixtures/candidate_helper2.rb +8 -0
  126. data/spec/fixtures/example.erb +5 -0
  127. data/spec/fixtures/example_nesting.rb +33 -0
  128. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  129. data/spec/fixtures/testlinkrc +2 -0
  130. data/spec/fixtures/testrc +2 -0
  131. data/spec/fixtures/testrcbad +2 -0
  132. data/spec/fixtures/whereami_helper.rb +6 -0
  133. data/spec/helper.rb +35 -0
  134. data/spec/helpers/bacon.rb +86 -0
  135. data/spec/helpers/mock_pry.rb +44 -0
  136. data/spec/helpers/repl_tester.rb +112 -0
  137. data/spec/helpers/table_spec.rb +105 -0
  138. data/spec/history_array_spec.rb +67 -0
  139. data/spec/hooks_spec.rb +522 -0
  140. data/spec/indent_spec.rb +301 -0
  141. data/spec/method_spec.rb +482 -0
  142. data/spec/prompt_spec.rb +61 -0
  143. data/spec/pry_defaults_spec.rb +420 -0
  144. data/spec/pry_history_spec.rb +69 -0
  145. data/spec/pry_output_spec.rb +95 -0
  146. data/spec/pry_repl_spec.rb +86 -0
  147. data/spec/pry_spec.rb +394 -0
  148. data/spec/pryrc_spec.rb +97 -0
  149. data/spec/run_command_spec.rb +25 -0
  150. data/spec/sticky_locals_spec.rb +147 -0
  151. data/spec/syntax_checking_spec.rb +81 -0
  152. data/spec/wrapped_module_spec.rb +261 -0
  153. data/wiki/Customizing-pry.md +397 -0
  154. data/wiki/Home.md +4 -0
  155. metadata +272 -61
  156. checksums.yaml +0 -7
  157. data/CHANGELOG.md +0 -714
  158. data/lib/pry/code/code_file.rb +0 -103
  159. data/lib/pry/color_printer.rb +0 -55
  160. data/lib/pry/commands/change_inspector.rb +0 -27
  161. data/lib/pry/commands/change_prompt.rb +0 -26
  162. data/lib/pry/commands/list_inspectors.rb +0 -35
  163. data/lib/pry/commands/list_prompts.rb +0 -35
  164. data/lib/pry/commands/ls/constants.rb +0 -47
  165. data/lib/pry/commands/ls/formatter.rb +0 -49
  166. data/lib/pry/commands/ls/globals.rb +0 -48
  167. data/lib/pry/commands/ls/grep.rb +0 -21
  168. data/lib/pry/commands/ls/instance_vars.rb +0 -39
  169. data/lib/pry/commands/ls/interrogatable.rb +0 -18
  170. data/lib/pry/commands/ls/jruby_hacks.rb +0 -49
  171. data/lib/pry/commands/ls/local_names.rb +0 -35
  172. data/lib/pry/commands/ls/local_vars.rb +0 -39
  173. data/lib/pry/commands/ls/ls_entity.rb +0 -70
  174. data/lib/pry/commands/ls/methods.rb +0 -57
  175. data/lib/pry/commands/ls/methods_helper.rb +0 -46
  176. data/lib/pry/commands/ls/self_methods.rb +0 -32
  177. data/lib/pry/commands/watch_expression.rb +0 -105
  178. data/lib/pry/commands/watch_expression/expression.rb +0 -38
  179. data/lib/pry/config/behavior.rb +0 -139
  180. data/lib/pry/config/convenience.rb +0 -25
  181. data/lib/pry/config/default.rb +0 -161
  182. data/lib/pry/exceptions.rb +0 -78
  183. data/lib/pry/input_completer.rb +0 -242
  184. data/lib/pry/input_lock.rb +0 -132
  185. data/lib/pry/inspector.rb +0 -27
  186. data/lib/pry/last_exception.rb +0 -61
  187. data/lib/pry/object_path.rb +0 -82
  188. data/lib/pry/output.rb +0 -50
  189. data/lib/pry/prompt.rb +0 -26
@@ -0,0 +1,627 @@
1
+ require 'helper'
2
+
3
+ describe Pry::CommandSet do
4
+ before do
5
+ @set = Pry::CommandSet.new do
6
+ import Pry::Commands
7
+ end
8
+
9
+ @ctx = {
10
+ :target => binding,
11
+ :command_set => @set
12
+ }
13
+ end
14
+
15
+ it 'should call the block used for the command when it is called' do
16
+ run = false
17
+ @set.command 'foo' do
18
+ run = true
19
+ end
20
+
21
+ @set.run_command @ctx, 'foo'
22
+ run.should == true
23
+ end
24
+
25
+ it 'should pass arguments of the command to the block' do
26
+ @set.command 'foo' do |*args|
27
+ args.should == [1, 2, 3]
28
+ end
29
+
30
+ @set.run_command @ctx, 'foo', 1, 2, 3
31
+ end
32
+
33
+ it 'should use the first argument as context' do
34
+ ctx = @ctx
35
+
36
+ @set.command 'foo' do
37
+ self.context.should == ctx
38
+ end
39
+
40
+ @set.run_command @ctx, 'foo'
41
+ end
42
+
43
+ it 'should raise an error when calling an undefined command' do
44
+ @set.command('foo') {}
45
+ lambda {
46
+ @set.run_command @ctx, 'bar'
47
+ }.should.raise(Pry::NoCommandError)
48
+ end
49
+
50
+ it 'should be able to remove its own commands' do
51
+ @set.command('foo') {}
52
+ @set.delete 'foo'
53
+
54
+ lambda {
55
+ @set.run_command @ctx, 'foo'
56
+ }.should.raise(Pry::NoCommandError)
57
+ end
58
+
59
+ it 'should be able to remove its own commands, by listing name' do
60
+ @set.command(/^foo1/, 'desc', :listing => 'foo') {}
61
+ @set.delete 'foo'
62
+
63
+ lambda {
64
+ @set.run_command @ctx, /^foo1/
65
+ }.should.raise(Pry::NoCommandError)
66
+ end
67
+
68
+ it 'should be able to import some commands from other sets' do
69
+ run = false
70
+
71
+ other_set = Pry::CommandSet.new do
72
+ command('foo') { run = true }
73
+ command('bar') {}
74
+ end
75
+
76
+ @set.import_from(other_set, 'foo')
77
+
78
+ @set.run_command @ctx, 'foo'
79
+ run.should == true
80
+
81
+ lambda {
82
+ @set.run_command @ctx, 'bar'
83
+ }.should.raise(Pry::NoCommandError)
84
+ end
85
+
86
+ it 'should return command set after import' do
87
+ run = false
88
+
89
+ other_set = Pry::CommandSet.new do
90
+ command('foo') { run = true }
91
+ command('bar') {}
92
+ end
93
+
94
+ @set.import(other_set).should == @set
95
+ end
96
+
97
+ it 'should return command set after import_from' do
98
+ run = false
99
+
100
+ other_set = Pry::CommandSet.new do
101
+ command('foo') { run = true }
102
+ command('bar') {}
103
+ end
104
+
105
+ @set.import_from(other_set, 'foo').should == @set
106
+ end
107
+
108
+ it 'should be able to import some commands from other sets using listing name' do
109
+ run = false
110
+
111
+ other_set = Pry::CommandSet.new do
112
+ command(/^foo1/, 'desc', :listing => 'foo') { run = true }
113
+ end
114
+
115
+ @set.import_from(other_set, 'foo')
116
+
117
+ @set.run_command @ctx, /^foo1/
118
+ run.should == true
119
+ end
120
+
121
+ it 'should be able to import a whole set' do
122
+ run = []
123
+
124
+ other_set = Pry::CommandSet.new do
125
+ command('foo') { run << true }
126
+ command('bar') { run << true }
127
+ end
128
+
129
+ @set.import other_set
130
+
131
+ @set.run_command @ctx, 'foo'
132
+ @set.run_command @ctx, 'bar'
133
+ run.should == [true, true]
134
+ end
135
+
136
+ it 'should be able to import sets at creation' do
137
+ run = false
138
+ @set.command('foo') { run = true }
139
+
140
+ Pry::CommandSet.new(@set).run_command @ctx, 'foo'
141
+ run.should == true
142
+ end
143
+
144
+ it 'should set the descriptions of commands' do
145
+ @set.command('foo', 'some stuff') {}
146
+ @set.commands['foo'].description.should == 'some stuff'
147
+ end
148
+
149
+ describe "aliases" do
150
+ it 'should be able to alias command' do
151
+ run = false
152
+ @set.command('foo', 'stuff') { run = true }
153
+
154
+ @set.alias_command 'bar', 'foo'
155
+ @set.commands['bar'].match.should == 'bar'
156
+ @set.commands['bar'].description.should == 'Alias for `foo`'
157
+
158
+ @set.run_command @ctx, 'bar'
159
+ run.should == true
160
+ end
161
+
162
+ it 'should inherit options from original command' do
163
+ run = false
164
+ @set.command('foo', 'stuff', :shellwords => true, :interpolate => false) { run = true }
165
+
166
+ @set.alias_command 'bar', 'foo'
167
+ @set.commands['bar'].options[:shellwords].should == @set.commands['foo'].options[:shellwords]
168
+ @set.commands['bar'].options[:interpolate].should == @set.commands['foo'].options[:interpolate]
169
+
170
+ # however some options should not be inherited
171
+ @set.commands['bar'].options[:listing].should.not == @set.commands['foo'].options[:listing]
172
+ @set.commands['bar'].options[:listing].should == "bar"
173
+ end
174
+
175
+ it 'should be able to specify alias\'s description when aliasing' do
176
+ run = false
177
+ @set.command('foo', 'stuff') { run = true }
178
+
179
+ @set.alias_command 'bar', 'foo', :desc => "tobina"
180
+ @set.commands['bar'].match.should == 'bar'
181
+ @set.commands['bar'].description.should == "tobina"
182
+
183
+ @set.run_command @ctx, 'bar'
184
+ run.should == true
185
+ end
186
+
187
+ it "should be able to alias a command by its invocation line" do
188
+ run = false
189
+ @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
190
+
191
+ @set.alias_command 'bar', 'foo1'
192
+ @set.commands['bar'].match.should == 'bar'
193
+ @set.commands['bar'].description.should == 'Alias for `foo1`'
194
+
195
+ @set.run_command @ctx, 'bar'
196
+ run.should == true
197
+ end
198
+
199
+ it "should be able to specify options when creating alias" do
200
+ run = false
201
+ @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
202
+
203
+ @set.alias_command /^b.r/, 'foo1', :listing => "bar"
204
+ @set.commands[/^b.r/].options[:listing].should == "bar"
205
+ end
206
+
207
+ it "should set description to default if description parameter is nil" do
208
+ run = false
209
+ @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
210
+
211
+ @set.alias_command "bar", 'foo1'
212
+ @set.commands["bar"].description.should == "Alias for `foo1`"
213
+ end
214
+ end
215
+
216
+ it 'should be able to change the descriptions of commands' do
217
+ @set.command('foo', 'bar') {}
218
+ @set.desc 'foo', 'baz'
219
+
220
+ @set.commands['foo'].description.should == 'baz'
221
+ end
222
+
223
+ it 'should get the descriptions of commands' do
224
+ @set.command('foo', 'bar') {}
225
+ @set.desc('foo').should == 'bar'
226
+ end
227
+
228
+ it 'should get the descriptions of commands, by listing' do
229
+ @set.command(/^foo1/, 'bar', :listing => 'foo') {}
230
+ @set.desc('foo').should == 'bar'
231
+ end
232
+
233
+ it 'should return Pry::Command::VOID_VALUE for commands by default' do
234
+ @set.command('foo') { 3 }
235
+ @set.run_command(@ctx, 'foo').should == Pry::Command::VOID_VALUE
236
+ end
237
+
238
+ it 'should be able to keep return values' do
239
+ @set.command('foo', '', :keep_retval => true) { 3 }
240
+ @set.run_command(@ctx, 'foo').should == 3
241
+ end
242
+
243
+ it 'should be able to keep return values, even if return value is nil' do
244
+ @set.command('foo', '', :keep_retval => true) { nil }
245
+ @set.run_command(@ctx, 'foo').should == nil
246
+ end
247
+
248
+ it 'should be able to have its own helpers' do
249
+ @set.command('foo') do
250
+ should.respond_to :my_helper
251
+ end
252
+
253
+ @set.helpers do
254
+ def my_helper; end
255
+ end
256
+
257
+ @set.run_command(@ctx, 'foo')
258
+ Pry::Command.subclass('foo', '', {}, Module.new).new({:target => binding}).should.not.respond_to :my_helper
259
+ end
260
+
261
+ it 'should not recreate a new helper module when helpers is called' do
262
+ @set.command('foo') do
263
+ should.respond_to :my_helper
264
+ should.respond_to :my_other_helper
265
+ end
266
+
267
+ @set.helpers do
268
+ def my_helper; end
269
+ end
270
+
271
+ @set.helpers do
272
+ def my_other_helper; end
273
+ end
274
+
275
+ @set.run_command(@ctx, 'foo')
276
+ end
277
+
278
+ it 'should import helpers from imported sets' do
279
+ imported_set = Pry::CommandSet.new do
280
+ helpers do
281
+ def imported_helper_method; end
282
+ end
283
+ end
284
+
285
+ @set.import imported_set
286
+ @set.command('foo') { should.respond_to :imported_helper_method }
287
+ @set.run_command(@ctx, 'foo')
288
+ end
289
+
290
+ it 'should import helpers even if only some commands are imported' do
291
+ imported_set = Pry::CommandSet.new do
292
+ helpers do
293
+ def imported_helper_method; end
294
+ end
295
+
296
+ command('bar') {}
297
+ end
298
+
299
+ @set.import_from imported_set, 'bar'
300
+ @set.command('foo') { should.respond_to :imported_helper_method }
301
+ @set.run_command(@ctx, 'foo')
302
+ end
303
+
304
+ it 'should provide a :listing for a command that defaults to its name' do
305
+ @set.command 'foo', "" do;end
306
+ @set.commands['foo'].options[:listing].should == 'foo'
307
+ end
308
+
309
+ it 'should provide a :listing for a command that differs from its name' do
310
+ @set.command 'foo', "", :listing => 'bar' do;end
311
+ @set.commands['foo'].options[:listing].should == 'bar'
312
+ end
313
+
314
+ it "should provide a 'help' command" do
315
+ @ctx[:command_set] = @set
316
+ @ctx[:output] = StringIO.new
317
+
318
+ lambda {
319
+ @set.run_command(@ctx, 'help')
320
+ }.should.not.raise
321
+ end
322
+
323
+
324
+ describe "renaming a command" do
325
+ it 'should be able to rename and run a command' do
326
+ run = false
327
+ @set.command('foo') { run = true }
328
+ @set.rename_command('bar', 'foo')
329
+ @set.run_command(@ctx, 'bar')
330
+ run.should == true
331
+ end
332
+
333
+ it 'should accept listing name when renaming a command' do
334
+ run = false
335
+ @set.command('foo', "", :listing => 'love') { run = true }
336
+ @set.rename_command('bar', 'love')
337
+ @set.run_command(@ctx, 'bar')
338
+ run.should == true
339
+ end
340
+
341
+ it 'should raise exception trying to rename non-existent command' do
342
+ lambda { @set.rename_command('bar', 'foo') }.should.raise ArgumentError
343
+ end
344
+
345
+ it 'should make old command name inaccessible' do
346
+ @set.command('foo') { }
347
+ @set.rename_command('bar', 'foo')
348
+ lambda { @set.run_command(@ctx, 'foo') }.should.raise Pry::NoCommandError
349
+ end
350
+
351
+ it 'should be able to pass in options when renaming command' do
352
+ desc = "hello"
353
+ listing = "bing"
354
+ @set.command('foo') { }
355
+ @set.rename_command('bar', 'foo', :description => desc, :listing => listing, :keep_retval => true)
356
+ @set.commands['bar'].description.should == desc
357
+ @set.commands['bar'].options[:listing].should == listing
358
+ @set.commands['bar'].options[:keep_retval].should == true
359
+ end
360
+ end
361
+
362
+ describe "command decorators - before_command and after_command" do
363
+ describe "before_command" do
364
+ it 'should be called before the original command' do
365
+ foo = []
366
+ @set.command('foo') { foo << 1 }
367
+ @set.before_command('foo') { foo << 2 }
368
+ @set.run_command(@ctx, 'foo')
369
+
370
+ foo.should == [2, 1]
371
+ end
372
+
373
+ it 'should be called before the original command, using listing name' do
374
+ foo = []
375
+ @set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
376
+ @set.before_command('foo') { foo << 2 }
377
+ @set.run_command(@ctx, /^foo1/)
378
+
379
+ foo.should == [2, 1]
380
+ end
381
+
382
+ it 'should share the context with the original command' do
383
+ @ctx[:target] = "test target string".__binding__
384
+ before_val = nil
385
+ orig_val = nil
386
+ @set.command('foo') { orig_val = target }
387
+ @set.before_command('foo') { before_val = target }
388
+ @set.run_command(@ctx, 'foo')
389
+
390
+ before_val.should == @ctx[:target]
391
+ orig_val.should == @ctx[:target]
392
+ end
393
+
394
+ it 'should work when applied multiple times' do
395
+ foo = []
396
+ @set.command('foo') { foo << 1 }
397
+ @set.before_command('foo') { foo << 2 }
398
+ @set.before_command('foo') { foo << 3 }
399
+ @set.before_command('foo') { foo << 4 }
400
+ @set.run_command(@ctx, 'foo')
401
+
402
+ foo.should == [4, 3, 2, 1]
403
+ end
404
+
405
+ end
406
+
407
+ describe "after_command" do
408
+ it 'should be called after the original command' do
409
+ foo = []
410
+ @set.command('foo') { foo << 1 }
411
+ @set.after_command('foo') { foo << 2 }
412
+ @set.run_command(@ctx, 'foo')
413
+
414
+ foo.should == [1, 2]
415
+ end
416
+
417
+ it 'should be called after the original command, using listing name' do
418
+ foo = []
419
+ @set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
420
+ @set.after_command('foo') { foo << 2 }
421
+ @set.run_command(@ctx, /^foo1/)
422
+
423
+ foo.should == [1, 2]
424
+ end
425
+
426
+ it 'should share the context with the original command' do
427
+ @ctx[:target] = "test target string".__binding__
428
+ after_val = nil
429
+ orig_val = nil
430
+ @set.command('foo') { orig_val = target }
431
+ @set.after_command('foo') { after_val = target }
432
+ @set.run_command(@ctx, 'foo')
433
+
434
+ after_val.should == @ctx[:target]
435
+ orig_val.should == @ctx[:target]
436
+ end
437
+
438
+ it 'should determine the return value for the command' do
439
+ @set.command('foo', 'bar', :keep_retval => true) { 1 }
440
+ @set.after_command('foo') { 2 }
441
+ @set.run_command(@ctx, 'foo').should == 2
442
+ end
443
+
444
+ it 'should work when applied multiple times' do
445
+ foo = []
446
+ @set.command('foo') { foo << 1 }
447
+ @set.after_command('foo') { foo << 2 }
448
+ @set.after_command('foo') { foo << 3 }
449
+ @set.after_command('foo') { foo << 4 }
450
+ @set.run_command(@ctx, 'foo')
451
+
452
+ foo.should == [1, 2, 3, 4]
453
+ end
454
+ end
455
+
456
+ describe "before_command and after_command" do
457
+ it 'should work when combining both before_command and after_command' do
458
+ foo = []
459
+ @set.command('foo') { foo << 1 }
460
+ @set.after_command('foo') { foo << 2 }
461
+ @set.before_command('foo') { foo << 3 }
462
+ @set.run_command(@ctx, 'foo')
463
+
464
+ foo.should == [3, 1, 2]
465
+ end
466
+
467
+ end
468
+
469
+ end
470
+
471
+ describe 'find_command' do
472
+ it 'should find commands with the right string' do
473
+ cmd = @set.command('rincewind'){ }
474
+ @set.find_command('rincewind').should == cmd
475
+ end
476
+
477
+ it 'should not find commands with spaces before' do
478
+ cmd = @set.command('luggage'){ }
479
+ @set.find_command(' luggage').should == nil
480
+ end
481
+
482
+ it 'should find commands with arguments after' do
483
+ cmd = @set.command('vetinari'){ }
484
+ @set.find_command('vetinari --knock 3').should == cmd
485
+ end
486
+
487
+ it 'should find commands with names containing spaces' do
488
+ cmd = @set.command('nobby nobbs'){ }
489
+ @set.find_command('nobby nobbs --steal petty-cash').should == cmd
490
+ end
491
+
492
+ it 'should find command defined by regex' do
493
+ cmd = @set.command(/(capt|captain) vimes/i){ }
494
+ @set.find_command('Capt Vimes').should == cmd
495
+ end
496
+
497
+ it 'should find commands defined by regex with arguments' do
498
+ cmd = @set.command(/(cpl|corporal) Carrot/i){ }
499
+ @set.find_command('cpl carrot --write-home').should == cmd
500
+ end
501
+
502
+ it 'should not find commands by listing' do
503
+ cmd = @set.command(/werewol(f|ve)s?/, 'only once a month', :listing => "angua"){ }
504
+ @set.find_command('angua').should == nil
505
+ end
506
+
507
+ it 'should not find commands without command_prefix' do
508
+ Pry.config.command_prefix = '%'
509
+ cmd = @set.command('detritus'){ }
510
+ @set.find_command('detritus').should == nil
511
+ Pry.config.command_prefix = ''
512
+ end
513
+
514
+ it "should find commands that don't use the prefix" do
515
+ Pry.config.command_prefix = '%'
516
+ cmd = @set.command('colon', 'Sergeant Fred', :use_prefix => false){ }
517
+ @set.find_command('colon').should == cmd
518
+ Pry.config.command_prefix = ''
519
+ end
520
+
521
+ it "should find the command that has the longest match" do
522
+ cmd = @set.command(/\.(.*)/){ }
523
+ cmd2 = @set.command(/\.\|\|(.*)/){ }
524
+ @set.find_command('.||').should == cmd2
525
+ end
526
+
527
+ it "should find the command that has the longest name" do
528
+ cmd = @set.command(/\.(.*)/){ }
529
+ cmd2 = @set.command('.||'){ }
530
+ @set.find_command('.||').should == cmd2
531
+ end
532
+ end
533
+
534
+ describe '.valid_command?' do
535
+ it 'should be true for commands that can be found' do
536
+ cmd = @set.command('archchancellor')
537
+ @set.valid_command?('archchancellor of_the?(:University)').should == true
538
+ end
539
+
540
+ it 'should be false for commands that can\'' do
541
+ @set.valid_command?('def monkey(ape)').should == false
542
+ end
543
+
544
+ it 'should not cause argument interpolation' do
545
+ cmd = @set.command('hello')
546
+ lambda {
547
+ @set.valid_command?('hello #{raise "futz"}')
548
+ }.should.not.raise
549
+ end
550
+ end
551
+
552
+ describe '.process_line' do
553
+
554
+ it 'should return Result.new(false) if there is no matching command' do
555
+ result = @set.process_line('1 + 42')
556
+ result.command?.should == false
557
+ result.void_command?.should == false
558
+ result.retval.should == nil
559
+ end
560
+
561
+ it 'should return Result.new(true, VOID) if the command is not keep_retval' do
562
+ @set.create_command('mrs-cake') do
563
+ def process; 42; end
564
+ end
565
+
566
+ result = @set.process_line('mrs-cake')
567
+ result.command?.should == true
568
+ result.void_command?.should == true
569
+ result.retval.should == Pry::Command::VOID_VALUE
570
+ end
571
+
572
+ it 'should return Result.new(true, retval) if the command is keep_retval' do
573
+ @set.create_command('magrat', 'the maiden', :keep_retval => true) do
574
+ def process; 42; end
575
+ end
576
+
577
+ result = @set.process_line('magrat')
578
+ result.command?.should == true
579
+ result.void_command?.should == false
580
+ result.retval.should == 42
581
+ end
582
+
583
+ it 'should pass through context' do
584
+ ctx = {
585
+ :eval_string => "bloomers",
586
+ :pry_instance => Object.new,
587
+ :output => StringIO.new,
588
+ :target => binding
589
+ }
590
+ @set.create_command('agnes') do
591
+ define_method(:process) do
592
+ eval_string.should == ctx[:eval_string]
593
+ output.should == ctx[:output]
594
+ target.should == ctx[:target]
595
+ _pry_.should == ctx[:pry_instance]
596
+ end
597
+ end
598
+
599
+ @set.process_line('agnes', ctx)
600
+ end
601
+
602
+ it 'should add command_set to context' do
603
+ set = @set
604
+ @set.create_command(/nann+y ogg+/) do
605
+ define_method(:process) do
606
+ command_set.should == set
607
+ end
608
+ end
609
+
610
+ @set.process_line('nannnnnny oggggg')
611
+ end
612
+ end
613
+
614
+ if defined?(Bond)
615
+ describe '.complete' do
616
+ it "should list all command names" do
617
+ @set.create_command('susan'){ }
618
+ @set.complete('sus').should.include 'susan '
619
+ end
620
+
621
+ it "should delegate to commands" do
622
+ @set.create_command('susan'){ def complete(search); ['--foo']; end }
623
+ @set.complete('susan ').should == ['--foo']
624
+ end
625
+ end
626
+ end
627
+ end