pry 0.3.0 → 0.4.0pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  class Pry
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0pre1"
3
3
  end
@@ -0,0 +1,528 @@
1
+ direc = File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'bacon'
5
+ require "#{direc}/../lib/pry"
6
+ require "#{direc}/test_helper"
7
+
8
+ puts "Ruby Version #{RUBY_VERSION}"
9
+ puts "Testing Pry #{Pry::VERSION}"
10
+ puts "With method_source version #{MethodSource::VERSION}"
11
+ puts "--"
12
+
13
+ describe Pry do
14
+ describe "open a Pry session on an object" do
15
+ describe "rep" do
16
+
17
+ before do
18
+ class Hello
19
+ end
20
+ end
21
+
22
+ after do
23
+ Object.send(:remove_const, :Hello)
24
+ end
25
+
26
+ it 'should set an ivar on an object' do
27
+ input_string = "@x = 10"
28
+ input = InputTester.new(input_string)
29
+ o = Object.new
30
+
31
+ pry_tester = Pry.new(:input => input, :output => Pry::NullOutput)
32
+ pry_tester.rep(o)
33
+ o.instance_variable_get(:@x).should == 10
34
+ end
35
+
36
+ it 'should make self evaluate to the receiver of the rep session' do
37
+ o = Object.new
38
+ str_output = StringIO.new
39
+
40
+ pry_tester = Pry.new(:input => InputTester.new("self"), :output => str_output)
41
+ pry_tester.rep(o)
42
+ str_output.string.should =~ /#{o.to_s}/
43
+ end
44
+
45
+ it 'should work with multi-line input' do
46
+ o = Object.new
47
+ str_output = StringIO.new
48
+
49
+ pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => str_output)
50
+ pry_tester.rep(o)
51
+ str_output.string.should =~ /5/
52
+ end
53
+
54
+ it 'should define a nested class under Hello and not on top-level or Pry' do
55
+ pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput)
56
+ pry_tester.rep(Hello)
57
+ Hello.const_defined?(:Nested).should == true
58
+ end
59
+ end
60
+
61
+ describe "repl" do
62
+ describe "basic functionality" do
63
+ it 'should set an ivar on an object and exit the repl' do
64
+ input_strings = ["@x = 10", "exit"]
65
+ input = InputTester.new(*input_strings)
66
+
67
+ o = Object.new
68
+
69
+ pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput)
70
+
71
+ o.instance_variable_get(:@x).should == 10
72
+ end
73
+
74
+ it 'should execute start session and end session hooks' do
75
+ input = InputTester.new("exit")
76
+ str_output = StringIO.new
77
+ o = Object.new
78
+
79
+ pry_tester = Pry.start(o, :input => input, :output => str_output)
80
+ str_output.string.should =~ /Beginning.*#{o}/
81
+ str_output.string.should =~ /Ending.*#{o}/
82
+ end
83
+ end
84
+
85
+ describe "nesting" do
86
+ after do
87
+ Pry.reset_defaults
88
+ end
89
+
90
+ it 'should nest properly' do
91
+ Pry.input = InputTester.new("pry", "pry", "pry", "\"nest:\#\{Pry.nesting.level\}\"", "exit_all")
92
+
93
+ str_output = StringIO.new
94
+ Pry.output = str_output
95
+
96
+ o = Object.new
97
+
98
+ pry_tester = o.pry
99
+ str_output.string.should =~ /nest:3/
100
+ end
101
+ end
102
+
103
+ describe "commands" do
104
+ it 'should run command1' do
105
+ pry_tester = Pry.new
106
+ pry_tester.commands = CommandTester
107
+ pry_tester.input = InputTester.new("command1", "exit_all")
108
+ pry_tester.commands = CommandTester
109
+
110
+ str_output = StringIO.new
111
+ pry_tester.output = str_output
112
+
113
+ pry_tester.rep
114
+
115
+ str_output.string.should =~ /command1/
116
+ end
117
+
118
+ it 'should run command2' do
119
+ pry_tester = Pry.new
120
+ pry_tester.commands = CommandTester
121
+ pry_tester.input = InputTester.new("command2 horsey", "exit_all")
122
+ pry_tester.commands = CommandTester
123
+
124
+ str_output = StringIO.new
125
+ pry_tester.output = str_output
126
+
127
+ pry_tester.rep
128
+
129
+ str_output.string.should =~ /horsey/
130
+ end
131
+ end
132
+
133
+ describe "Object#pry" do
134
+
135
+ after do
136
+ Pry.reset_defaults
137
+ end
138
+
139
+ it "should start a pry session on the receiver (first form)" do
140
+ Pry.input = InputTester.new("self", "exit")
141
+
142
+ str_output = StringIO.new
143
+ Pry.output = str_output
144
+
145
+ 20.pry
146
+
147
+ str_output.string.should =~ /20/
148
+ end
149
+
150
+ it "should start a pry session on the receiver (second form)" do
151
+ Pry.input = InputTester.new("self", "exit")
152
+
153
+ str_output = StringIO.new
154
+ Pry.output = str_output
155
+
156
+ pry 20
157
+
158
+ str_output.string.should =~ /20/
159
+ end
160
+
161
+ it "should error if more than one argument is passed to Object#pry" do
162
+ lambda { pry(20, :input => Readline) }.should.raise ArgumentError
163
+ end
164
+ end
165
+
166
+ describe "Pry#binding_for" do
167
+ it 'should return TOPLEVEL_BINDING if parameter self is main' do
168
+ _main_ = lambda { TOPLEVEL_BINDING.eval('self') }
169
+ Pry.new.binding_for(_main_.call).is_a?(Binding).should == true
170
+ Pry.new.binding_for(_main_.call).should == TOPLEVEL_BINDING
171
+ Pry.new.binding_for(_main_.call).should == Pry.new.binding_for(_main_.call)
172
+ end
173
+ end
174
+
175
+
176
+ describe "test Pry defaults" do
177
+
178
+ after do
179
+ Pry.reset_defaults
180
+ end
181
+
182
+ describe "input" do
183
+
184
+ after do
185
+ Pry.reset_defaults
186
+ end
187
+
188
+ it 'should set the input default, and the default should be overridable' do
189
+ Pry.input = InputTester.new("5")
190
+
191
+ str_output = StringIO.new
192
+ Pry.output = str_output
193
+ Pry.new.rep
194
+ str_output.string.should =~ /5/
195
+
196
+ Pry.new(:input => InputTester.new("6")).rep
197
+ str_output.string.should =~ /6/
198
+ end
199
+
200
+ it 'should pass in the prompt if readline arity is 1' do
201
+ Pry.prompt = proc { "A" }
202
+
203
+ arity_one_input = Class.new do
204
+ attr_accessor :prompt
205
+ def readline(prompt)
206
+ @prompt = prompt
207
+ "exit"
208
+ end
209
+ end.new
210
+
211
+ Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
212
+ arity_one_input.prompt.should == Pry.prompt.call
213
+ end
214
+
215
+ it 'should not pass in the prompt if the arity is 0' do
216
+ Pry.prompt = proc { "A" }
217
+
218
+ arity_zero_input = Class.new do
219
+ def readline
220
+ "exit"
221
+ end
222
+ end.new
223
+
224
+ lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
225
+ end
226
+
227
+ it 'should not pass in the prompt if the arity is -1' do
228
+ Pry.prompt = proc { "A" }
229
+
230
+ arity_multi_input = Class.new do
231
+ attr_accessor :prompt
232
+
233
+ def readline(*args)
234
+ @prompt = args.first
235
+ "exit"
236
+ end
237
+ end.new
238
+
239
+ Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
240
+ arity_multi_input.prompt.should == nil
241
+ end
242
+
243
+ end
244
+
245
+ it 'should set the output default, and the default should be overridable' do
246
+ Pry.input = InputTester.new("5", "6", "7")
247
+
248
+ str_output = StringIO.new
249
+ Pry.output = str_output
250
+
251
+ Pry.new.rep
252
+ str_output.string.should =~ /5/
253
+
254
+ Pry.new.rep
255
+ str_output.string.should =~ /5\n.*6/
256
+
257
+ str_output2 = StringIO.new
258
+ Pry.new(:output => str_output2).rep
259
+ str_output2.string.should.not =~ /5\n.*6/
260
+ str_output2.string.should =~ /7/
261
+ end
262
+
263
+ describe "commands" do
264
+ it 'should set the commands default, and the default should be overridable' do
265
+ class Command0 < Pry::CommandBase
266
+ command "hello" do
267
+ output.puts "hello world"
268
+ end
269
+ end
270
+
271
+ Pry.commands = Command0
272
+
273
+ str_output = StringIO.new
274
+ Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
275
+ str_output.string.should =~ /hello world/
276
+
277
+ class Command1 < Pry::CommandBase
278
+ command "goodbye", "" do
279
+ output.puts "goodbye world"
280
+ end
281
+ end
282
+
283
+ str_output = StringIO.new
284
+
285
+ Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => Command1).rep
286
+ str_output.string.should =~ /goodbye world/
287
+
288
+ Object.remove_const(:Command0)
289
+ Object.remove_const(:Command1)
290
+ end
291
+
292
+ it 'should inherit "help" command from Pry::CommandBase' do
293
+ class Command2 < Pry::CommandBase
294
+ command "h", "h command" do
295
+ end
296
+ end
297
+
298
+ Command2.commands.keys.size.should == 2
299
+ Command2.commands.keys.include?("help").should == true
300
+ Command2.commands.keys.include?("h").should == true
301
+
302
+ Object.remove_const(:Command2)
303
+ end
304
+
305
+ it 'should inherit commands from Pry::Commands' do
306
+ class Command3 < Pry::Commands
307
+ command "v" do
308
+ end
309
+ end
310
+
311
+ Command3.commands.include?("nesting").should == true
312
+ Command3.commands.include?("jump_to").should == true
313
+ Command3.commands.include?("cd").should == true
314
+ Command3.commands.include?("v").should == true
315
+
316
+ Object.remove_const(:Command3)
317
+ end
318
+
319
+ it 'should run a command from within a command' do
320
+ class Command3 < Pry::Commands
321
+ command "v" do
322
+ output.puts "v command"
323
+ end
324
+
325
+ command "run_v" do
326
+ run "v"
327
+ end
328
+ end
329
+
330
+ str_output = StringIO.new
331
+ Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command3).rep
332
+ str_output.string.should =~ /v command/
333
+
334
+ Object.remove_const(:Command3)
335
+ end
336
+
337
+ it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
338
+ class Command3 < Pry::Commands
339
+ command "v" do
340
+ output.puts "#{target.eval('self')}"
341
+ end
342
+ end
343
+
344
+ class Command4 < Command3
345
+ end
346
+
347
+ str_output = StringIO.new
348
+ Pry.new(:print => proc {}, :input => InputTester.new("v"),
349
+ :output => str_output, :commands => Command4).rep("john")
350
+ str_output.string.chomp.should == "john"
351
+
352
+ Object.remove_const(:Command3)
353
+ Object.remove_const(:Command4)
354
+ end
355
+
356
+ it 'should import commands from another command object' do
357
+ class Command3 < Pry::CommandBase
358
+ import_from Pry::Commands, "status", "jump_to"
359
+ end
360
+
361
+ str_output = StringIO.new
362
+ Pry.new(:print => proc {}, :input => InputTester.new("status"),
363
+ :output => str_output, :commands => Command3).rep("john")
364
+ str_output.string.should =~ /Status:/
365
+
366
+ Object.remove_const(:Command3)
367
+ end
368
+
369
+ it 'should delete some inherited commands when using delete method' do
370
+ class Command3 < Pry::Commands
371
+ command "v" do
372
+ end
373
+
374
+ delete "show_doc", "show_method"
375
+ delete "ls"
376
+ end
377
+
378
+ Command3.commands.include?("nesting").should == true
379
+ Command3.commands.include?("jump_to").should == true
380
+ Command3.commands.include?("cd").should == true
381
+ Command3.commands.include?("v").should == true
382
+ Command3.commands.include?("show_doc").should == false
383
+ Command3.commands.include?("show_method").should == false
384
+ Command3.commands.include?("ls").should == false
385
+
386
+ Object.remove_const(:Command3)
387
+ end
388
+
389
+ it 'should override some inherited commands' do
390
+ class Command3 < Pry::Commands
391
+ command "jump_to" do
392
+ output.puts "jump_to the music"
393
+ end
394
+
395
+ command "help" do
396
+ output.puts "help to the music"
397
+ end
398
+ end
399
+
400
+ # suppress evaluation output
401
+ Pry.print = proc {}
402
+
403
+ str_output = StringIO.new
404
+ Pry.new(:input => InputTester.new("jump_to"), :output => str_output, :commands => Command3).rep
405
+ str_output.string.chomp.should == "jump_to the music"
406
+
407
+ str_output = StringIO.new
408
+ Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => Command3).rep
409
+ str_output.string.chomp.should == "help to the music"
410
+
411
+ Object.remove_const(:Command3)
412
+
413
+ Pry.reset_defaults
414
+ end
415
+ end
416
+
417
+ it "should set the print default, and the default should be overridable" do
418
+ new_print = proc { |out, value| out.puts value }
419
+ Pry.print = new_print
420
+
421
+ Pry.new.print.should == Pry.print
422
+ str_output = StringIO.new
423
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
424
+ str_output.string.should == "test\n"
425
+
426
+ str_output = StringIO.new
427
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
428
+ :print => proc { |out, value| out.puts value.reverse }).rep
429
+ str_output.string.should == "tset\n"
430
+
431
+ Pry.new.print.should == Pry.print
432
+ str_output = StringIO.new
433
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
434
+ str_output.string.should == "test\n"
435
+ end
436
+
437
+ describe "prompts" do
438
+ it 'should set the prompt default, and the default should be overridable (single prompt)' do
439
+ new_prompt = proc { "test prompt> " }
440
+ Pry.prompt = new_prompt
441
+
442
+ Pry.new.prompt.should == Pry.prompt
443
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
444
+ Pry.new.select_prompt(false, 0).should == "test prompt> "
445
+
446
+ new_prompt = proc { "A" }
447
+ pry_tester = Pry.new(:prompt => new_prompt)
448
+ pry_tester.prompt.should == new_prompt
449
+ pry_tester.select_prompt(true, 0).should == "A"
450
+ pry_tester.select_prompt(false, 0).should == "A"
451
+
452
+ Pry.new.prompt.should == Pry.prompt
453
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
454
+ Pry.new.select_prompt(false, 0).should == "test prompt> "
455
+ end
456
+
457
+ it 'should set the prompt default, and the default should be overridable (multi prompt)' do
458
+ new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
459
+ Pry.prompt = new_prompt
460
+
461
+ Pry.new.prompt.should == Pry.prompt
462
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
463
+ Pry.new.select_prompt(false, 0).should == "test prompt* "
464
+
465
+ new_prompt = [proc { "A" }, proc { "B" }]
466
+ pry_tester = Pry.new(:prompt => new_prompt)
467
+ pry_tester.prompt.should == new_prompt
468
+ pry_tester.select_prompt(true, 0).should == "A"
469
+ pry_tester.select_prompt(false, 0).should == "B"
470
+
471
+ Pry.new.prompt.should == Pry.prompt
472
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
473
+ Pry.new.select_prompt(false, 0).should == "test prompt* "
474
+ end
475
+ end
476
+
477
+ it 'should set the hooks default, and the default should be overridable' do
478
+ Pry.input = InputTester.new("exit")
479
+ Pry.hooks = {
480
+ :before_session => proc { |out,_| out.puts "HELLO" },
481
+ :after_session => proc { |out,_| out.puts "BYE" }
482
+ }
483
+
484
+ str_output = StringIO.new
485
+ Pry.new(:output => str_output).repl
486
+ str_output.string.should =~ /HELLO/
487
+ str_output.string.should =~ /BYE/
488
+
489
+ Pry.input.rewind
490
+
491
+ str_output = StringIO.new
492
+ Pry.new(:output => str_output,
493
+ :hooks => {
494
+ :before_session => proc { |out,_| out.puts "MORNING" },
495
+ :after_session => proc { |out,_| out.puts "EVENING" }
496
+ }
497
+ ).repl
498
+
499
+ str_output.string.should =~ /MORNING/
500
+ str_output.string.should =~ /EVENING/
501
+
502
+ # try below with just defining one hook
503
+ Pry.input.rewind
504
+ str_output = StringIO.new
505
+ Pry.new(:output => str_output,
506
+ :hooks => {
507
+ :before_session => proc { |out,_| out.puts "OPEN" }
508
+ }
509
+ ).repl
510
+
511
+ str_output.string.should =~ /OPEN/
512
+
513
+ Pry.input.rewind
514
+ str_output = StringIO.new
515
+ Pry.new(:output => str_output,
516
+ :hooks => {
517
+ :after_session => proc { |out,_| out.puts "CLOSE" }
518
+ }
519
+ ).repl
520
+
521
+ str_output.string.should =~ /CLOSE/
522
+
523
+ Pry.reset_defaults
524
+ end
525
+ end
526
+ end
527
+ end
528
+ end