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

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,821 @@
1
+ require 'helper'
2
+
3
+ describe "Pry::Command" do
4
+
5
+ before do
6
+ @set = Pry::CommandSet.new
7
+ @set.import Pry::Commands
8
+ end
9
+
10
+ describe 'call_safely' do
11
+
12
+ it 'should display a message if gems are missing' do
13
+ cmd = @set.create_command "ford-prefect", "From a planet near Beetlegeuse", :requires_gem => %w(ghijkl) do
14
+ #
15
+ end
16
+
17
+ mock_command(cmd, %w(hello world)).output.should =~ /install-command ford-prefect/
18
+ end
19
+
20
+ it 'should abort early if arguments are required' do
21
+ cmd = @set.create_command 'arthur-dent', "Doesn't understand Thursdays", :argument_required => true do
22
+ #
23
+ end
24
+
25
+ lambda {
26
+ mock_command(cmd, %w())
27
+ }.should.raise(Pry::CommandError)
28
+ end
29
+
30
+ it 'should return VOID without keep_retval' do
31
+ cmd = @set.create_command 'zaphod-beeblebrox', "Likes pan-Galactic Gargle Blasters" do
32
+ def process
33
+ 3
34
+ end
35
+ end
36
+
37
+ mock_command(cmd).return.should == Pry::Command::VOID_VALUE
38
+ end
39
+
40
+ it 'should return the return value with keep_retval' do
41
+ cmd = @set.create_command 'tricia-mcmillian', "a.k.a Trillian", :keep_retval => true do
42
+ def process
43
+ 5
44
+ end
45
+ end
46
+
47
+ mock_command(cmd).return.should == 5
48
+ end
49
+
50
+ it 'should call hooks in the right order' do
51
+ cmd = @set.create_command 'marvin', "Pained by the diodes in his left side" do
52
+ def process
53
+ output.puts 3 + args[0].to_i
54
+ end
55
+ end
56
+
57
+ @set.before_command 'marvin' do |i|
58
+ output.puts 2 + i.to_i
59
+ end
60
+ @set.before_command 'marvin' do |i|
61
+ output.puts 1 + i.to_i
62
+ end
63
+
64
+ @set.after_command 'marvin' do |i|
65
+ output.puts 4 + i.to_i
66
+ end
67
+
68
+ @set.after_command 'marvin' do |i|
69
+ output.puts 5 + i.to_i
70
+ end
71
+
72
+ mock_command(cmd, %w(2)).output.should == "3\n4\n5\n6\n7\n"
73
+ end
74
+
75
+ # TODO: This strikes me as rather silly...
76
+ it 'should return the value from the last hook with keep_retval' do
77
+ cmd = @set.create_command 'slartibartfast', "Designs Fjords", :keep_retval => true do
78
+ def process
79
+ 22
80
+ end
81
+ end
82
+
83
+ @set.after_command 'slartibartfast' do
84
+ 10
85
+ end
86
+
87
+ mock_command(cmd).return.should == 10
88
+ end
89
+ end
90
+
91
+ describe 'help' do
92
+ it 'should default to the description for blocky commands' do
93
+ @set.command 'oolon-colluphid', "Raving Atheist" do
94
+ #
95
+ end
96
+
97
+ mock_command(@set.commands['help'], %w(oolon-colluphid), :command_set => @set).output.should =~ /Raving Atheist/
98
+ end
99
+
100
+ it 'should use slop to generate the help for classy commands' do
101
+ @set.create_command 'eddie', "The ship-board computer" do
102
+ def options(opt)
103
+ opt.banner "Over-cheerful, and makes a ticking noise."
104
+ end
105
+ end
106
+
107
+ mock_command(@set.commands['help'], %w(eddie), :command_set => @set).output.should =~ /Over-cheerful/
108
+ end
109
+
110
+ it 'should provide --help for classy commands' do
111
+ cmd = @set.create_command 'agrajag', "Killed many times by Arthur" do
112
+ def options(opt)
113
+ opt.on :r, :retaliate, "Try to get Arthur back"
114
+ end
115
+ end
116
+
117
+ mock_command(cmd, %w(--help)).output.should =~ /--retaliate/
118
+ end
119
+
120
+ it 'should provide a -h for classy commands' do
121
+ cmd = @set.create_command 'zarniwoop', "On an intergalactic cruise, in his office." do
122
+ def options(opt)
123
+ opt.on :e, :escape, "Help zaphod escape the Total Perspective Vortex"
124
+ end
125
+ end
126
+
127
+ mock_command(cmd, %w(--help)).output.should =~ /Total Perspective Vortex/
128
+ end
129
+
130
+ it 'should use the banner provided' do
131
+ cmd = @set.create_command 'deep-thought', "The second-best computer ever" do
132
+ banner <<-BANNER
133
+ Who's merest operational parameters, I am not worthy to compute.
134
+ BANNER
135
+ end
136
+
137
+ mock_command(cmd, %w(--help)).output.should =~ /Who\'s merest/
138
+ end
139
+ end
140
+
141
+ describe 'context' do
142
+ context = {
143
+ :target => binding,
144
+ :output => StringIO.new,
145
+ :eval_string => "eval-string",
146
+ :command_set => @set,
147
+ :pry_instance => Object.new
148
+ }
149
+
150
+ it 'should capture lots of stuff from the hash passed to new before setup' do
151
+ cmd = @set.create_command 'fenchurch', "Floats slightly off the ground" do
152
+ define_method(:setup) do
153
+ self.context.should == context
154
+ target.should == context[:target]
155
+ target_self.should == context[:target].eval('self')
156
+ output.should == context[:output]
157
+ end
158
+
159
+ define_method(:process) do
160
+ eval_string.should == "eval-string"
161
+ command_set.should == @set
162
+ _pry_.should == context[:pry_instance]
163
+ end
164
+ end
165
+
166
+ cmd.new(context).call
167
+ end
168
+ end
169
+
170
+ describe 'classy api' do
171
+
172
+ it 'should call setup, then subcommands, then options, then process' do
173
+ cmd = @set.create_command 'rooster', "Has a tasty towel" do
174
+ def setup
175
+ output.puts "setup"
176
+ end
177
+
178
+ def subcommands(cmd)
179
+ output.puts "subcommands"
180
+ end
181
+
182
+ def options(opt)
183
+ output.puts "options"
184
+ end
185
+
186
+ def process
187
+ output.puts "process"
188
+ end
189
+ end
190
+
191
+ mock_command(cmd).output.should == "setup\nsubcommands\noptions\nprocess\n"
192
+ end
193
+
194
+ it 'should raise a command error if process is not overridden' do
195
+ cmd = @set.create_command 'jeltz', "Commander of a Vogon constructor fleet" do
196
+ def proccces
197
+ #
198
+ end
199
+ end
200
+
201
+ lambda {
202
+ mock_command(cmd)
203
+ }.should.raise(Pry::CommandError)
204
+ end
205
+
206
+ it 'should work if neither options, nor setup is overridden' do
207
+ cmd = @set.create_command 'wowbagger', "Immortal, insulting.", :keep_retval => true do
208
+ def process
209
+ 5
210
+ end
211
+ end
212
+
213
+ mock_command(cmd).return.should == 5
214
+ end
215
+
216
+ it 'should provide opts and args as provided by slop' do
217
+ cmd = @set.create_command 'lintilla', "One of 800,000,000 clones" do
218
+ def options(opt)
219
+ opt.on :f, :four, "A numeric four", :as => Integer, :optional_argument => true
220
+ end
221
+
222
+ def process
223
+ args.should == ['four']
224
+ opts[:f].should == 4
225
+ end
226
+ end
227
+
228
+ mock_command(cmd, %w(--four 4 four))
229
+ end
230
+
231
+ it 'should allow overriding options after definition' do
232
+ cmd = @set.create_command /number-(one|two)/, "Lieutenants of the Golgafrinchan Captain", :shellwords => false do
233
+
234
+ command_options :listing => 'number-one'
235
+ end
236
+
237
+ cmd.command_options[:shellwords].should == false
238
+ cmd.command_options[:listing].should == 'number-one'
239
+ end
240
+
241
+ it "should create subcommands" do
242
+ cmd = @set.create_command 'mum', 'Your mum' do
243
+ def subcommands(cmd)
244
+ cmd.command :yell
245
+ end
246
+
247
+ def process
248
+ opts.fetch_command(:blahblah).should == nil
249
+ opts.fetch_command(:yell).present?.should == true
250
+ end
251
+ end
252
+
253
+ mock_command(cmd, ['yell'])
254
+ end
255
+
256
+ it "should create subcommand options" do
257
+ cmd = @set.create_command 'mum', 'Your mum' do
258
+ def subcommands(cmd)
259
+ cmd.command :yell do
260
+ on :p, :person
261
+ end
262
+ end
263
+
264
+ def process
265
+ args.should == ['papa']
266
+ opts.fetch_command(:yell).present?.should == true
267
+ opts.fetch_command(:yell).person?.should == true
268
+ end
269
+ end
270
+
271
+ mock_command(cmd, %w|yell --person papa|)
272
+ end
273
+
274
+ it "should accept top-level arguments" do
275
+ cmd = @set.create_command 'mum', 'Your mum' do
276
+ def subcommands(cmd)
277
+ cmd.on :yell
278
+ end
279
+
280
+ def process
281
+ args.should == ['yell', 'papa', 'sonny', 'daughter']
282
+ end
283
+ end
284
+
285
+ mock_command(cmd, %w|yell papa sonny daughter|)
286
+ end
287
+
288
+ describe "explicit classes" do
289
+ before do
290
+ @x = Class.new(Pry::ClassCommand) do
291
+ options :baby => :pig
292
+ match /goat/
293
+ description "waaaninngggiiigygygygygy"
294
+ end
295
+ end
296
+
297
+ it 'subclasses should inherit options, match and description from superclass' do
298
+ k = Class.new(@x)
299
+ k.options.should == @x.options
300
+ k.match.should == @x.match
301
+ k.description.should == @x.description
302
+ end
303
+ end
304
+ end
305
+
306
+ describe 'tokenize' do
307
+ it 'should interpolate string with #{} in them' do
308
+ cmd = @set.command 'random-dent' do |*args|
309
+ args.should == ["3", "8"]
310
+ end
311
+
312
+ foo = 5
313
+
314
+ cmd.new(:target => binding).process_line 'random-dent #{1 + 2} #{3 + foo}'
315
+ end
316
+
317
+ it 'should not fail if interpolation is not needed and target is not set' do
318
+ cmd = @set.command 'the-book' do |*args|
319
+ args.should == ['--help']
320
+ end
321
+
322
+ cmd.new.process_line 'the-book --help'
323
+ end
324
+
325
+ it 'should not interpolate commands with :interpolate => false' do
326
+ cmd = @set.command 'thor', 'norse god', :interpolate => false do |*args|
327
+ args.should == ['%(#{foo})']
328
+ end
329
+
330
+ cmd.new.process_line 'thor %(#{foo})'
331
+ end
332
+
333
+ it 'should use shell-words to split strings' do
334
+ cmd = @set.command 'eccentrica' do |*args|
335
+ args.should == ['gallumbits', 'eroticon', '6']
336
+ end
337
+
338
+ cmd.new.process_line %(eccentrica "gallumbits" 'erot''icon' 6)
339
+ end
340
+
341
+ it 'should split on spaces if shellwords is not used' do
342
+ cmd = @set.command 'bugblatter-beast', 'would eat its grandmother', :shellwords => false do |*args|
343
+ args.should == ['"of', 'traal"']
344
+ end
345
+
346
+ cmd.new.process_line %(bugblatter-beast "of traal")
347
+ end
348
+
349
+ it 'should add captures to arguments for regex commands' do
350
+ cmd = @set.command /perfectly (normal)( beast)?/i do |*args|
351
+ args.should == ['Normal', ' Beast', '(honest!)']
352
+ end
353
+
354
+ cmd.new.process_line %(Perfectly Normal Beast (honest!))
355
+ end
356
+ end
357
+
358
+ describe 'process_line' do
359
+ it 'should check for command name collisions if configured' do
360
+ old = Pry.config.collision_warning
361
+ Pry.config.collision_warning = true
362
+
363
+ cmd = @set.command 'frankie' do
364
+
365
+ end
366
+
367
+ frankie = 'boyle'
368
+ output = StringIO.new
369
+ cmd.new(:target => binding, :output => output).process_line %(frankie mouse)
370
+
371
+ output.string.should =~ /command .* conflicts/
372
+
373
+ Pry.config.collision_warning = old
374
+ end
375
+
376
+ it 'should spot collision warnings on assignment if configured' do
377
+ old = Pry.config.collision_warning
378
+ Pry.config.collision_warning = true
379
+
380
+ cmd = @set.command 'frankie' do
381
+
382
+ end
383
+
384
+ output = StringIO.new
385
+ cmd.new(:target => binding, :output => output).process_line %(frankie = mouse)
386
+
387
+ output.string.should =~ /command .* conflicts/
388
+
389
+ Pry.config.collision_warning = old
390
+ end
391
+
392
+ it "should set the commands' arg_string and captures" do
393
+
394
+ cmd = @set.command /benj(ie|ei)/ do |*args|
395
+ self.arg_string.should == "mouse"
396
+ self.captures.should == ['ie']
397
+ args.should == ['ie', 'mouse']
398
+ end
399
+
400
+ cmd.new.process_line %(benjie mouse)
401
+ end
402
+
403
+ it "should raise an error if the line doesn't match the command" do
404
+ cmd = @set.command 'grunthos', 'the flatulent'
405
+
406
+ lambda {
407
+ cmd.new.process_line %(grumpos)
408
+ }.should.raise(Pry::CommandError)
409
+ end
410
+ end
411
+
412
+ describe "block parameters" do
413
+ before do
414
+ @context = Object.new
415
+ @set.command "walking-spanish", "down the hall", :takes_block => true do
416
+ PryTestHelpers.inject_var(:@x, command_block.call, target)
417
+ end
418
+ @set.import Pry::Commands
419
+
420
+ @t = pry_tester(@context, :commands => @set)
421
+ end
422
+
423
+ it 'should accept multiline blocks' do
424
+ @t.eval <<-EOS
425
+ walking-spanish | do
426
+ :jesus
427
+ end
428
+ EOS
429
+
430
+ @context.instance_variable_get(:@x).should == :jesus
431
+ end
432
+
433
+ it 'should accept normal parameters along with block' do
434
+ @set.block_command "walking-spanish",
435
+ "litella's been screeching for a blind pig.",
436
+ :takes_block => true do |x, y|
437
+ PryTestHelpers.inject_var(:@x, x, target)
438
+ PryTestHelpers.inject_var(:@y, y, target)
439
+ PryTestHelpers.inject_var(:@block_var, command_block.call, target)
440
+ end
441
+
442
+ @t.eval 'walking-spanish john carl| { :jesus }'
443
+
444
+ @context.instance_variable_get(:@x).should == "john"
445
+ @context.instance_variable_get(:@y).should == "carl"
446
+ @context.instance_variable_get(:@block_var).should == :jesus
447
+ end
448
+
449
+ describe "single line blocks" do
450
+ it 'should accept blocks with do ; end' do
451
+ @t.eval 'walking-spanish | do ; :jesus; end'
452
+ @context.instance_variable_get(:@x).should == :jesus
453
+ end
454
+
455
+ it 'should accept blocks with do; end' do
456
+ @t.eval 'walking-spanish | do; :jesus; end'
457
+ @context.instance_variable_get(:@x).should == :jesus
458
+ end
459
+
460
+ it 'should accept blocks with { }' do
461
+ @t.eval 'walking-spanish | { :jesus }'
462
+ @context.instance_variable_get(:@x).should == :jesus
463
+ end
464
+ end
465
+
466
+ describe "block-related content removed from arguments" do
467
+
468
+ describe "arg_string" do
469
+ it 'should remove block-related content from arg_string (with one normal arg)' do
470
+ @set.block_command "walking-spanish", "down the hall", :takes_block => true do |x, y|
471
+ PryTestHelpers.inject_var(:@arg_string, arg_string, target)
472
+ PryTestHelpers.inject_var(:@x, x, target)
473
+ end
474
+
475
+ @t.eval 'walking-spanish john| { :jesus }'
476
+
477
+ @context.instance_variable_get(:@arg_string).should == @context.instance_variable_get(:@x)
478
+ end
479
+
480
+ it 'should remove block-related content from arg_string (with no normal args)' do
481
+ @set.block_command "walking-spanish", "down the hall", :takes_block => true do
482
+ PryTestHelpers.inject_var(:@arg_string, arg_string, target)
483
+ end
484
+
485
+ @t.eval 'walking-spanish | { :jesus }'
486
+
487
+ @context.instance_variable_get(:@arg_string).should == ""
488
+ end
489
+
490
+ it 'should NOT remove block-related content from arg_string when :takes_block => false' do
491
+ block_string = "| { :jesus }"
492
+ @set.block_command "walking-spanish", "homemade special", :takes_block => false do
493
+ PryTestHelpers.inject_var(:@arg_string, arg_string, target)
494
+ end
495
+
496
+ @t.eval "walking-spanish #{block_string}"
497
+
498
+ @context.instance_variable_get(:@arg_string).should == block_string
499
+ end
500
+ end
501
+
502
+ describe "args" do
503
+ describe "block_command" do
504
+ it "should remove block-related content from arguments" do
505
+ @set.block_command "walking-spanish", "glass is full of sand", :takes_block => true do |x, y|
506
+ PryTestHelpers.inject_var(:@x, x, target)
507
+ PryTestHelpers.inject_var(:@y, y, target)
508
+ end
509
+
510
+ @t.eval 'walking-spanish | { :jesus }'
511
+
512
+ @context.instance_variable_get(:@x).should == nil
513
+ @context.instance_variable_get(:@y).should == nil
514
+ end
515
+
516
+ it "should NOT remove block-related content from arguments if :takes_block => false" do
517
+ @set.block_command "walking-spanish", "litella screeching for a blind pig", :takes_block => false do |x, y|
518
+ PryTestHelpers.inject_var(:@x, x, target)
519
+ PryTestHelpers.inject_var(:@y, y, target)
520
+ end
521
+
522
+ @t.eval 'walking-spanish | { :jesus }'
523
+
524
+ @context.instance_variable_get(:@x).should == "|"
525
+ @context.instance_variable_get(:@y).should == "{"
526
+ end
527
+ end
528
+
529
+ describe "create_command" do
530
+ it "should remove block-related content from arguments" do
531
+ @set.create_command "walking-spanish", "punk sanders carved one out of wood", :takes_block => true do
532
+ def process(x, y)
533
+ PryTestHelpers.inject_var(:@x, x, target)
534
+ PryTestHelpers.inject_var(:@y, y, target)
535
+ end
536
+ end
537
+
538
+ @t.eval 'walking-spanish | { :jesus }'
539
+
540
+ @context.instance_variable_get(:@x).should == nil
541
+ @context.instance_variable_get(:@y).should == nil
542
+ end
543
+
544
+ it "should NOT remove block-related content from arguments if :takes_block => false" do
545
+ @set.create_command "walking-spanish", "down the hall", :takes_block => false do
546
+ def process(x, y)
547
+ PryTestHelpers.inject_var(:@x, x, target)
548
+ PryTestHelpers.inject_var(:@y, y, target)
549
+ end
550
+ end
551
+
552
+ @t.eval 'walking-spanish | { :jesus }'
553
+
554
+ @context.instance_variable_get(:@x).should == "|"
555
+ @context.instance_variable_get(:@y).should == "{"
556
+ end
557
+ end
558
+ end
559
+ end
560
+
561
+ describe "blocks can take parameters" do
562
+ describe "{} style blocks" do
563
+ it 'should accept multiple parameters' do
564
+ @set.block_command "walking-spanish", "down the hall", :takes_block => true do
565
+ PryTestHelpers.inject_var(:@x, command_block.call(1, 2), target)
566
+ end
567
+
568
+ @t.eval 'walking-spanish | { |x, y| [x, y] }'
569
+
570
+ @context.instance_variable_get(:@x).should == [1, 2]
571
+ end
572
+ end
573
+
574
+ describe "do/end style blocks" do
575
+ it 'should accept multiple parameters' do
576
+ @set.create_command "walking-spanish", "litella", :takes_block => true do
577
+ def process
578
+ PryTestHelpers.inject_var(:@x, command_block.call(1, 2), target)
579
+ end
580
+ end
581
+
582
+ @t.eval <<-EOS
583
+ walking-spanish | do |x, y|
584
+ [x, y]
585
+ end
586
+ EOS
587
+
588
+ @context.instance_variable_get(:@x).should == [1, 2]
589
+ end
590
+ end
591
+ end
592
+
593
+ describe "closure behaviour" do
594
+ it 'should close over locals in the definition context' do
595
+ @t.eval 'var = :hello', 'walking-spanish | { var }'
596
+ @context.instance_variable_get(:@x).should == :hello
597
+ end
598
+ end
599
+
600
+ describe "exposing block parameter" do
601
+ describe "block_command" do
602
+ it "should expose block in command_block method" do
603
+ @set.block_command "walking-spanish", "glass full of sand", :takes_block => true do
604
+ PryTestHelpers.inject_var(:@x, command_block.call, target)
605
+ end
606
+
607
+ @t.eval 'walking-spanish | { :jesus }'
608
+
609
+ @context.instance_variable_get(:@x).should == :jesus
610
+ end
611
+ end
612
+
613
+ describe "create_command" do
614
+ it "should NOT expose &block in create_command's process method" do
615
+ @set.create_command "walking-spanish", "down the hall", :takes_block => true do
616
+ def process(&block)
617
+ block.call
618
+ end
619
+ end
620
+ @out = StringIO.new
621
+
622
+ proc {
623
+ @t.eval 'walking-spanish | { :jesus }'
624
+ }.should.raise(NoMethodError)
625
+ end
626
+
627
+ it "should expose block in command_block method" do
628
+ @set.create_command "walking-spanish", "homemade special", :takes_block => true do
629
+ def process
630
+ PryTestHelpers.inject_var(:@x, command_block.call, target)
631
+ end
632
+ end
633
+
634
+ @t.eval 'walking-spanish | { :jesus }'
635
+
636
+ @context.instance_variable_get(:@x).should == :jesus
637
+ end
638
+ end
639
+ end
640
+ end
641
+
642
+ describe "a command made with a custom sub-class" do
643
+
644
+ before do
645
+ class MyTestCommand < Pry::ClassCommand
646
+ match /my-*test/
647
+ description 'So just how many sound technicians does it take to' \
648
+ 'change a lightbulb? 1? 2? 3? 1-2-3? Testing?'
649
+ options :shellwords => false, :listing => 'my-test'
650
+
651
+ def process
652
+ output.puts command_name * 2
653
+ end
654
+ end
655
+
656
+ Pry.commands.add_command MyTestCommand
657
+ end
658
+
659
+ after do
660
+ Pry.commands.delete 'my-test'
661
+ end
662
+
663
+ it "allows creation of custom subclasses of Pry::Command" do
664
+ pry_eval('my---test').should =~ /my-testmy-test/
665
+ end
666
+
667
+ if !mri18_and_no_real_source_location?
668
+ it "shows the source of the process method" do
669
+ pry_eval('show-source my-test').should =~ /output.puts command_name/
670
+ end
671
+ end
672
+
673
+ describe "command options hash" do
674
+ it "is always present" do
675
+ options_hash = {
676
+ :requires_gem => [],
677
+ :keep_retval => false,
678
+ :argument_required => false,
679
+ :interpolate => true,
680
+ :shellwords => false,
681
+ :listing => 'my-test',
682
+ :use_prefix => true,
683
+ :takes_block => false
684
+ }
685
+ MyTestCommand.options.should == options_hash
686
+ end
687
+
688
+ describe ":listing option" do
689
+ it "defaults to :match if not set explicitly" do
690
+ class HappyNewYear < Pry::ClassCommand
691
+ match 'happy-new-year'
692
+ description 'Happy New Year 2013'
693
+ end
694
+ Pry.commands.add_command HappyNewYear
695
+
696
+ HappyNewYear.options[:listing].should == 'happy-new-year'
697
+
698
+ Pry.commands.delete 'happy-new-year'
699
+ end
700
+
701
+ it "can be set explicitly" do
702
+ class MerryChristmas < Pry::ClassCommand
703
+ match 'merry-christmas'
704
+ description 'Merry Christmas!'
705
+ command_options :listing => 'happy-holidays'
706
+ end
707
+ Pry.commands.add_command MerryChristmas
708
+
709
+ MerryChristmas.options[:listing].should == 'happy-holidays'
710
+
711
+ Pry.commands.delete 'merry-christmas'
712
+ end
713
+
714
+ it "equals to :match option's inspect, if :match is Regexp" do
715
+ class CoolWinter < Pry::ClassCommand
716
+ match /.*winter/
717
+ description 'Is winter cool or cool?'
718
+ end
719
+ Pry.commands.add_command CoolWinter
720
+
721
+ CoolWinter.options[:listing].should == '/.*winter/'
722
+
723
+ Pry.commands.delete /.*winter/
724
+ end
725
+ end
726
+ end
727
+
728
+ end
729
+
730
+ describe "commands can save state" do
731
+ before do
732
+ @set = Pry::CommandSet.new do
733
+ create_command "litella", "desc" do
734
+ def process
735
+ state.my_state ||= 0
736
+ state.my_state += 1
737
+ end
738
+ end
739
+
740
+ create_command "sanders", "desc" do
741
+ def process
742
+ state.my_state = "wood"
743
+ end
744
+ end
745
+
746
+ create_command /[Hh]ello-world/, "desc" do
747
+ def process
748
+ state.my_state ||= 0
749
+ state.my_state += 2
750
+ end
751
+ end
752
+
753
+ end.import Pry::Commands
754
+
755
+ @t = pry_tester(:commands => @set)
756
+ end
757
+
758
+ it 'should save state for the command on the Pry#command_state hash' do
759
+ @t.eval 'litella'
760
+ @t.pry.command_state["litella"].my_state.should == 1
761
+ end
762
+
763
+ it 'should ensure state is maintained between multiple invocations of command' do
764
+ @t.eval 'litella'
765
+ @t.eval 'litella'
766
+ @t.pry.command_state["litella"].my_state.should == 2
767
+ end
768
+
769
+ it 'should ensure state with same name stored seperately for each command' do
770
+ @t.eval 'litella', 'sanders'
771
+
772
+ @t.pry.command_state["litella"].my_state.should == 1
773
+ @t.pry.command_state["sanders"].my_state.should =="wood"
774
+ end
775
+
776
+ it 'should ensure state is properly saved for regex commands' do
777
+ @t.eval 'hello-world', 'Hello-world'
778
+ @t.pry.command_state[/[Hh]ello-world/].my_state.should == 4
779
+ end
780
+ end
781
+
782
+ if defined?(Bond)
783
+ describe 'complete' do
784
+ it 'should return the arguments that are defined' do
785
+ @set.create_command "torrid" do
786
+ def options(opt)
787
+ opt.on :test
788
+ opt.on :lest
789
+ opt.on :pests
790
+ end
791
+ end
792
+
793
+ @set.complete('torrid ').should.include('--test ')
794
+ end
795
+ end
796
+ end
797
+
798
+ describe 'group' do
799
+ before do
800
+ @set.import(
801
+ Pry::CommandSet.new do
802
+ create_command("magic") { group("Not for a public use") }
803
+ end
804
+ )
805
+ end
806
+
807
+ it 'should be correct for default commands' do
808
+ @set.commands["help"].group.should == "Help"
809
+ end
810
+
811
+ it 'should not change once it is initialized' do
812
+ @set.commands["magic"].group("-==CD COMMAND==-")
813
+ @set.commands["magic"].group.should == "Not for a public use"
814
+ end
815
+
816
+ it 'should not disappear after the call without parameters' do
817
+ @set.commands["magic"].group
818
+ @set.commands["magic"].group.should == "Not for a public use"
819
+ end
820
+ end
821
+ end