pry 0.6.7pre4-i386-mswin32 → 0.6.8-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
- class Pry
2
- VERSION = "0.6.7pre4"
3
- end
1
+ class Pry
2
+ VERSION = "0.6.8"
3
+ end
@@ -1,681 +1,725 @@
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 "defining methods" do
104
- it 'should define a method on the singleton class of an object when performing "def meth;end" inside the object' do
105
- [Object.new, {}, []].each do |val|
106
- str_input = StringIO.new("def hello;end")
107
- Pry.new(:input => str_input, :output => StringIO.new).rep(val)
108
-
109
- val.methods(false).map(&:to_sym).include?(:hello).should == true
110
- end
111
- end
112
-
113
- it 'should define an instance method on the module when performing "def meth;end" inside the module' do
114
- str_input = StringIO.new("def hello;end")
115
- hello = Module.new
116
- Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
117
- hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
118
- end
119
-
120
- it 'should define an instance method on the class when performing "def meth;end" inside the class' do
121
- str_input = StringIO.new("def hello;end")
122
- hello = Class.new
123
- Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
124
- hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
125
- end
126
-
127
- it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
128
- # should include float in here, but test fails for some reason
129
- # on 1.8.7, no idea why!
130
- [:test, 0, true, false, nil].each do |val|
131
- str_input = StringIO.new("def hello;end")
132
- Pry.new(:input => str_input, :output => StringIO.new).rep(val)
133
- val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
134
- end
135
- end
136
-
137
- end
138
-
139
-
140
- describe "commands" do
141
- it 'should run a command with no parameter' do
142
- pry_tester = Pry.new
143
- pry_tester.commands = CommandTester
144
- pry_tester.input = InputTester.new("command1", "exit_all")
145
- pry_tester.commands = CommandTester
146
-
147
- str_output = StringIO.new
148
- pry_tester.output = str_output
149
-
150
- pry_tester.rep
151
-
152
- str_output.string.should =~ /command1/
153
- end
154
-
155
- it 'should run a command with one parameter' do
156
- pry_tester = Pry.new
157
- pry_tester.commands = CommandTester
158
- pry_tester.input = InputTester.new("command2 horsey", "exit_all")
159
- pry_tester.commands = CommandTester
160
-
161
- str_output = StringIO.new
162
- pry_tester.output = str_output
163
-
164
- pry_tester.rep
165
-
166
- str_output.string.should =~ /horsey/
167
- end
168
- end
169
-
170
- describe "Object#pry" do
171
-
172
- after do
173
- Pry.reset_defaults
174
- end
175
-
176
- it "should start a pry session on the receiver (first form)" do
177
- Pry.input = InputTester.new("self", "exit")
178
-
179
- str_output = StringIO.new
180
- Pry.output = str_output
181
-
182
- 20.pry
183
-
184
- str_output.string.should =~ /20/
185
- end
186
-
187
- it "should start a pry session on the receiver (second form)" do
188
- Pry.input = InputTester.new("self", "exit")
189
-
190
- str_output = StringIO.new
191
- Pry.output = str_output
192
-
193
- pry 20
194
-
195
- str_output.string.should =~ /20/
196
- end
197
-
198
- it "should error if more than one argument is passed to Object#pry" do
199
- lambda { pry(20, :input => Readline) }.should.raise ArgumentError
200
- end
201
- end
202
-
203
- describe "Pry.binding_for" do
204
- it 'should return TOPLEVEL_BINDING if parameter self is main' do
205
- _main_ = lambda { TOPLEVEL_BINDING.eval('self') }
206
- Pry.binding_for(_main_.call).is_a?(Binding).should == true
207
- Pry.binding_for(_main_.call).should == TOPLEVEL_BINDING
208
- Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
209
- end
210
- end
211
-
212
-
213
- describe "test Pry defaults" do
214
-
215
- after do
216
- Pry.reset_defaults
217
- end
218
-
219
- describe "input" do
220
-
221
- after do
222
- Pry.reset_defaults
223
- end
224
-
225
- it 'should set the input default, and the default should be overridable' do
226
- Pry.input = InputTester.new("5")
227
-
228
- str_output = StringIO.new
229
- Pry.output = str_output
230
- Pry.new.rep
231
- str_output.string.should =~ /5/
232
-
233
- Pry.new(:input => InputTester.new("6")).rep
234
- str_output.string.should =~ /6/
235
- end
236
-
237
- it 'should pass in the prompt if readline arity is 1' do
238
- Pry.prompt = proc { "A" }
239
-
240
- arity_one_input = Class.new do
241
- attr_accessor :prompt
242
- def readline(prompt)
243
- @prompt = prompt
244
- "exit"
245
- end
246
- end.new
247
-
248
- Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
249
- arity_one_input.prompt.should == Pry.prompt.call
250
- end
251
-
252
- it 'should not pass in the prompt if the arity is 0' do
253
- Pry.prompt = proc { "A" }
254
-
255
- arity_zero_input = Class.new do
256
- def readline
257
- "exit"
258
- end
259
- end.new
260
-
261
- lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
262
- end
263
-
264
- it 'should not pass in the prompt if the arity is -1' do
265
- Pry.prompt = proc { "A" }
266
-
267
- arity_multi_input = Class.new do
268
- attr_accessor :prompt
269
-
270
- def readline(*args)
271
- @prompt = args.first
272
- "exit"
273
- end
274
- end.new
275
-
276
- Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
277
- arity_multi_input.prompt.should == nil
278
- end
279
-
280
- end
281
-
282
- it 'should set the output default, and the default should be overridable' do
283
- Pry.input = InputTester.new("5", "6", "7")
284
-
285
- str_output = StringIO.new
286
- Pry.output = str_output
287
-
288
- Pry.new.rep
289
- str_output.string.should =~ /5/
290
-
291
- Pry.new.rep
292
- str_output.string.should =~ /5\n.*6/
293
-
294
- str_output2 = StringIO.new
295
- Pry.new(:output => str_output2).rep
296
- str_output2.string.should.not =~ /5\n.*6/
297
- str_output2.string.should =~ /7/
298
- end
299
-
300
- describe "Pry.run_command" do
301
- before do
302
- class RCTest
303
- def a() end
304
- B = 20
305
- @x = 10
306
- end
307
- end
308
-
309
- after do
310
- Object.remove_const(:RCTest)
311
- end
312
-
313
- it "should execute command in the appropriate object context" do
314
- result = Pry.run_command "ls", :context => RCTest
315
- result.map(&:to_sym).should == [:@x]
316
- end
317
-
318
- it "should execute command with parameters in the appropriate object context" do
319
- result = Pry.run_command "ls -M", :context => RCTest
320
- result.map(&:to_sym).should == [:a]
321
- end
322
-
323
- it "should execute command and show output with :show_output => true flag" do
324
- str = StringIO.new
325
- Pry.output = str
326
- result = Pry.run_command "ls -av", :context => RCTest, :show_output => true
327
- str.string.should =~ /global variables/
328
- Pry.output = $stdout
329
- end
330
-
331
- it "should execute command with multiple parameters" do
332
- result = Pry.run_command "ls -c -M RCTest"
333
- result.map(&:to_sym).should == [:a, :B]
334
- end
335
- end
336
-
337
- describe "commands" do
338
- it 'should define a command that keeps its return value' do
339
- class Command68 < Pry::CommandBase
340
- command "hello", "", :keep_retval => true do
341
- :kept_hello
342
- end
343
- end
344
- str_output = StringIO.new
345
- Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
346
- str_output.string.should =~ /:kept_hello/
347
-
348
- Object.remove_const(:Command68)
349
- end
350
-
351
- it 'should define a command that does NOT keep its return value' do
352
- class Command68 < Pry::CommandBase
353
- command "hello", "", :keep_retval => false do
354
- :kept_hello
355
- end
356
- end
357
- str_output = StringIO.new
358
- Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
359
- (str_output.string =~ /:kept_hello/).should == nil
360
-
361
- Object.remove_const(:Command68)
362
- end
363
-
364
-
365
- it 'should set the commands default, and the default should be overridable' do
366
- class Command0 < Pry::CommandBase
367
- command "hello" do
368
- output.puts "hello world"
369
- end
370
- end
371
-
372
- Pry.commands = Command0
373
-
374
- str_output = StringIO.new
375
- Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
376
- str_output.string.should =~ /hello world/
377
-
378
- class Command1 < Pry::CommandBase
379
- command "goodbye", "" do
380
- output.puts "goodbye world"
381
- end
382
- end
383
-
384
- str_output = StringIO.new
385
-
386
- Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => Command1).rep
387
- str_output.string.should =~ /goodbye world/
388
-
389
- Object.remove_const(:Command0)
390
- Object.remove_const(:Command1)
391
- end
392
-
393
- it 'should inherit "help" command from Pry::CommandBase' do
394
- class Command2 < Pry::CommandBase
395
- command "h", "h command" do
396
- end
397
- end
398
-
399
- Command2.commands.keys.size.should == 2
400
- Command2.commands.keys.include?("help").should == true
401
- Command2.commands.keys.include?("h").should == true
402
-
403
- Object.remove_const(:Command2)
404
- end
405
-
406
- it 'should inherit commands from Pry::Commands' do
407
- class Command3 < Pry::Commands
408
- command "v" do
409
- end
410
- end
411
-
412
- Command3.commands.include?("nesting").should == true
413
- Command3.commands.include?("jump-to").should == true
414
- Command3.commands.include?("cd").should == true
415
- Command3.commands.include?("v").should == true
416
-
417
- Object.remove_const(:Command3)
418
- end
419
-
420
- it 'should alias a command with another command' do
421
- class Command6 < Pry::CommandBase
422
- alias_command "help2", "help"
423
- end
424
-
425
- Command6.commands["help2"].should == Command6.commands["help"]
426
- # str_output = StringIO.new
427
- # Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command3).rep
428
- # str_output.string.should =~ /v command/
429
-
430
- Object.remove_const(:Command6)
431
- end
432
-
433
- it 'should change description of a command using desc' do
434
-
435
- class Command7 < Pry::Commands
436
- end
437
-
438
- orig = Command7.commands["help"][:description]
439
-
440
- class Command7
441
- desc "help", "blah"
442
- end
443
-
444
- Command7.commands["help"][:description].should.not == orig
445
- Command7.commands["help"][:description].should == "blah"
446
-
447
- Object.remove_const(:Command7)
448
- end
449
-
450
- it 'should run a command from within a command' do
451
- class Command01 < Pry::Commands
452
- command "v" do
453
- output.puts "v command"
454
- end
455
-
456
- command "run_v" do
457
- run "v"
458
- end
459
- end
460
-
461
- str_output = StringIO.new
462
- Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command01).rep
463
- str_output.string.should =~ /v command/
464
-
465
- Object.remove_const(:Command01)
466
- end
467
-
468
- it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
469
- class Command3 < Pry::Commands
470
- command "v" do
471
- output.puts "#{target.eval('self')}"
472
- end
473
- end
474
-
475
- class Command4 < Command3
476
- end
477
-
478
- str_output = StringIO.new
479
- Pry.new(:print => proc {}, :input => InputTester.new("v"),
480
- :output => str_output, :commands => Command4).rep("john")
481
- str_output.string.chomp.should == "john"
482
-
483
- Object.remove_const(:Command3)
484
- Object.remove_const(:Command4)
485
- end
486
-
487
- it 'should import commands from another command object' do
488
- class Command3 < Pry::CommandBase
489
- import_from Pry::Commands, "status", "jump-to"
490
- end
491
-
492
- str_output = StringIO.new
493
- Pry.new(:print => proc {}, :input => InputTester.new("status"),
494
- :output => str_output, :commands => Command3).rep("john")
495
- str_output.string.should =~ /Status:/
496
-
497
- Object.remove_const(:Command3)
498
- end
499
-
500
- it 'should delete some inherited commands when using delete method' do
501
- class Command3 < Pry::Commands
502
- command "v" do
503
- end
504
-
505
- delete "show_doc", "show_method"
506
- delete "ls"
507
- end
508
-
509
- Command3.commands.include?("nesting").should == true
510
- Command3.commands.include?("jump-to").should == true
511
- Command3.commands.include?("cd").should == true
512
- Command3.commands.include?("v").should == true
513
- Command3.commands.include?("show_doc").should == false
514
- Command3.commands.include?("show_method").should == false
515
- Command3.commands.include?("ls").should == false
516
-
517
- Object.remove_const(:Command3)
518
- end
519
-
520
- it 'should override some inherited commands' do
521
- class Command3 < Pry::Commands
522
- command "jump-to" do
523
- output.puts "jump-to the music"
524
- end
525
-
526
- command "help" do
527
- output.puts "help to the music"
528
- end
529
- end
530
-
531
- # suppress evaluation output
532
- Pry.print = proc {}
533
-
534
- str_output = StringIO.new
535
- Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => Command3).rep
536
- str_output.string.chomp.should == "jump-to the music"
537
-
538
- str_output = StringIO.new
539
- Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => Command3).rep
540
- str_output.string.chomp.should == "help to the music"
541
-
542
- Object.remove_const(:Command3)
543
-
544
- Pry.reset_defaults
545
- end
546
- end
547
-
548
- it "should set the print default, and the default should be overridable" do
549
- new_print = proc { |out, value| out.puts value }
550
- Pry.print = new_print
551
-
552
- Pry.new.print.should == Pry.print
553
- str_output = StringIO.new
554
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
555
- str_output.string.should == "test\n"
556
-
557
- str_output = StringIO.new
558
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
559
- :print => proc { |out, value| out.puts value.reverse }).rep
560
- str_output.string.should == "tset\n"
561
-
562
- Pry.new.print.should == Pry.print
563
- str_output = StringIO.new
564
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
565
- str_output.string.should == "test\n"
566
- end
567
-
568
- describe "pry return values" do
569
- it 'should return the target object' do
570
- Pry.start(self, :input => StringIO.new("exit"), :output => Pry::NullOutput).should == self
571
- end
572
-
573
- it 'should return the parameter given to exit' do
574
- Pry.start(self, :input => StringIO.new("exit 10"), :output => Pry::NullOutput).should == 10
575
- end
576
-
577
- it 'should return the parameter (multi word string) given to exit' do
578
- Pry.start(self, :input => StringIO.new("exit \"john mair\""), :output => Pry::NullOutput).should == "john mair"
579
- end
580
-
581
- it 'should return the parameter (function call) given to exit' do
582
- Pry.start(self, :input => StringIO.new("exit 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
583
- end
584
-
585
- it 'should return the parameter (self) given to exit' do
586
- Pry.start("carl", :input => StringIO.new("exit self"), :output => Pry::NullOutput).should == "carl"
587
- end
588
- end
589
-
590
- describe "prompts" do
591
- it 'should set the prompt default, and the default should be overridable (single prompt)' do
592
- new_prompt = proc { "test prompt> " }
593
- Pry.prompt = new_prompt
594
-
595
- Pry.new.prompt.should == Pry.prompt
596
- Pry.new.select_prompt(true, 0).should == "test prompt> "
597
- Pry.new.select_prompt(false, 0).should == "test prompt> "
598
-
599
- new_prompt = proc { "A" }
600
- pry_tester = Pry.new(:prompt => new_prompt)
601
- pry_tester.prompt.should == new_prompt
602
- pry_tester.select_prompt(true, 0).should == "A"
603
- pry_tester.select_prompt(false, 0).should == "A"
604
-
605
- Pry.new.prompt.should == Pry.prompt
606
- Pry.new.select_prompt(true, 0).should == "test prompt> "
607
- Pry.new.select_prompt(false, 0).should == "test prompt> "
608
- end
609
-
610
- it 'should set the prompt default, and the default should be overridable (multi prompt)' do
611
- new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
612
- Pry.prompt = new_prompt
613
-
614
- Pry.new.prompt.should == Pry.prompt
615
- Pry.new.select_prompt(true, 0).should == "test prompt> "
616
- Pry.new.select_prompt(false, 0).should == "test prompt* "
617
-
618
- new_prompt = [proc { "A" }, proc { "B" }]
619
- pry_tester = Pry.new(:prompt => new_prompt)
620
- pry_tester.prompt.should == new_prompt
621
- pry_tester.select_prompt(true, 0).should == "A"
622
- pry_tester.select_prompt(false, 0).should == "B"
623
-
624
- Pry.new.prompt.should == Pry.prompt
625
- Pry.new.select_prompt(true, 0).should == "test prompt> "
626
- Pry.new.select_prompt(false, 0).should == "test prompt* "
627
- end
628
- end
629
-
630
- it 'should set the hooks default, and the default should be overridable' do
631
- Pry.input = InputTester.new("exit")
632
- Pry.hooks = {
633
- :before_session => proc { |out,_| out.puts "HELLO" },
634
- :after_session => proc { |out,_| out.puts "BYE" }
635
- }
636
-
637
- str_output = StringIO.new
638
- Pry.new(:output => str_output).repl
639
- str_output.string.should =~ /HELLO/
640
- str_output.string.should =~ /BYE/
641
-
642
- Pry.input.rewind
643
-
644
- str_output = StringIO.new
645
- Pry.new(:output => str_output,
646
- :hooks => {
647
- :before_session => proc { |out,_| out.puts "MORNING" },
648
- :after_session => proc { |out,_| out.puts "EVENING" }
649
- }
650
- ).repl
651
-
652
- str_output.string.should =~ /MORNING/
653
- str_output.string.should =~ /EVENING/
654
-
655
- # try below with just defining one hook
656
- Pry.input.rewind
657
- str_output = StringIO.new
658
- Pry.new(:output => str_output,
659
- :hooks => {
660
- :before_session => proc { |out,_| out.puts "OPEN" }
661
- }
662
- ).repl
663
-
664
- str_output.string.should =~ /OPEN/
665
-
666
- Pry.input.rewind
667
- str_output = StringIO.new
668
- Pry.new(:output => str_output,
669
- :hooks => {
670
- :after_session => proc { |out,_| out.puts "CLOSE" }
671
- }
672
- ).repl
673
-
674
- str_output.string.should =~ /CLOSE/
675
-
676
- Pry.reset_defaults
677
- end
678
- end
679
- end
680
- end
681
- end
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
+ # Ensure we do not execute any rc files
14
+ Pry::RC_FILES.clear
15
+ Pry.color = false
16
+ Pry.should_load_rc = false
17
+
18
+ describe Pry do
19
+ describe "open a Pry session on an object" do
20
+ describe "rep" do
21
+
22
+ before do
23
+ class Hello
24
+ end
25
+ end
26
+
27
+ after do
28
+ Object.send(:remove_const, :Hello)
29
+ end
30
+
31
+ it 'should set an ivar on an object' do
32
+ input_string = "@x = 10"
33
+ input = InputTester.new(input_string)
34
+ o = Object.new
35
+
36
+ pry_tester = Pry.new(:input => input, :output => Pry::NullOutput)
37
+ pry_tester.rep(o)
38
+ o.instance_variable_get(:@x).should == 10
39
+ end
40
+
41
+ it 'should make self evaluate to the receiver of the rep session' do
42
+ o = Object.new
43
+ str_output = StringIO.new
44
+
45
+ pry_tester = Pry.new(:input => InputTester.new("self"), :output => str_output)
46
+ pry_tester.rep(o)
47
+ str_output.string.should =~ /#{o.to_s}/
48
+ end
49
+
50
+ it 'should work with multi-line input' do
51
+ o = Object.new
52
+ str_output = StringIO.new
53
+
54
+ pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => str_output)
55
+ pry_tester.rep(o)
56
+ str_output.string.should =~ /5/
57
+ end
58
+
59
+ it 'should define a nested class under Hello and not on top-level or Pry' do
60
+ pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput)
61
+ pry_tester.rep(Hello)
62
+ Hello.const_defined?(:Nested).should == true
63
+ end
64
+ end
65
+
66
+ describe "repl" do
67
+ describe "basic functionality" do
68
+ it 'should set an ivar on an object and exit the repl' do
69
+ input_strings = ["@x = 10", "exit"]
70
+ input = InputTester.new(*input_strings)
71
+
72
+ o = Object.new
73
+
74
+ pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput)
75
+
76
+ o.instance_variable_get(:@x).should == 10
77
+ end
78
+
79
+ it 'should execute start session and end session hooks' do
80
+ input = InputTester.new("exit")
81
+ str_output = StringIO.new
82
+ o = Object.new
83
+
84
+ pry_tester = Pry.start(o, :input => input, :output => str_output)
85
+ str_output.string.should =~ /Beginning.*#{o}/
86
+ str_output.string.should =~ /Ending.*#{o}/
87
+ end
88
+ end
89
+
90
+ describe "test loading rc files" do
91
+ after do
92
+ Pry::RC_FILES.clear
93
+ Pry.should_load_rc = false
94
+ end
95
+
96
+ it "should run the rc file only once" do
97
+ Pry.should_load_rc = true
98
+ Pry::RC_FILES << "#{direc}/testrc"
99
+
100
+ Pry.start(self, :input => StringIO.new("exit\n"), :output => Pry::NullOutput)
101
+ TEST_RC.should == [0]
102
+
103
+ Pry.start(self, :input => StringIO.new("exit\n"), :output => Pry::NullOutput)
104
+ TEST_RC.should == [0]
105
+
106
+ Object.remove_const(:TEST_RC)
107
+ end
108
+
109
+ it "should not run the rc file at all if Pry.should_load_rc is false" do
110
+ Pry.should_load_rc = false
111
+ Pry.start(self, :input => StringIO.new("exit\n"), :output => Pry::NullOutput)
112
+ Object.const_defined?(:TEST_RC).should == false
113
+ end
114
+
115
+ it "should not load the rc file if #repl method invoked" do
116
+ Pry.should_load_rc = true
117
+ Pry.new(:input => StringIO.new("exit\n"), :output => Pry::NullOutput).repl(self)
118
+ Object.const_defined?(:TEST_RC).should == false
119
+ Pry.should_load_rc = false
120
+ end
121
+ end
122
+
123
+ describe "nesting" do
124
+ after do
125
+ Pry.reset_defaults
126
+ Pry.color = false
127
+ end
128
+
129
+ it 'should nest properly' do
130
+ Pry.input = InputTester.new("pry", "pry", "pry", "\"nest:\#\{Pry.nesting.level\}\"", "exit_all")
131
+
132
+ str_output = StringIO.new
133
+ Pry.output = str_output
134
+
135
+ o = Object.new
136
+
137
+ pry_tester = o.pry
138
+ str_output.string.should =~ /nest:3/
139
+ end
140
+ end
141
+
142
+ describe "defining methods" do
143
+ it 'should define a method on the singleton class of an object when performing "def meth;end" inside the object' do
144
+ [Object.new, {}, []].each do |val|
145
+ str_input = StringIO.new("def hello;end")
146
+ Pry.new(:input => str_input, :output => StringIO.new).rep(val)
147
+
148
+ val.methods(false).map(&:to_sym).include?(:hello).should == true
149
+ end
150
+ end
151
+
152
+ it 'should define an instance method on the module when performing "def meth;end" inside the module' do
153
+ str_input = StringIO.new("def hello;end")
154
+ hello = Module.new
155
+ Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
156
+ hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
157
+ end
158
+
159
+ it 'should define an instance method on the class when performing "def meth;end" inside the class' do
160
+ str_input = StringIO.new("def hello;end")
161
+ hello = Class.new
162
+ Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
163
+ hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
164
+ end
165
+
166
+ it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
167
+ # should include float in here, but test fails for some reason
168
+ # on 1.8.7, no idea why!
169
+ [:test, 0, true, false, nil].each do |val|
170
+ str_input = StringIO.new("def hello;end")
171
+ Pry.new(:input => str_input, :output => StringIO.new).rep(val)
172
+ val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
173
+ end
174
+ end
175
+
176
+ end
177
+
178
+
179
+ describe "commands" do
180
+ it 'should run a command with no parameter' do
181
+ pry_tester = Pry.new
182
+ pry_tester.commands = CommandTester
183
+ pry_tester.input = InputTester.new("command1", "exit_all")
184
+ pry_tester.commands = CommandTester
185
+
186
+ str_output = StringIO.new
187
+ pry_tester.output = str_output
188
+
189
+ pry_tester.rep
190
+
191
+ str_output.string.should =~ /command1/
192
+ end
193
+
194
+ it 'should run a command with one parameter' do
195
+ pry_tester = Pry.new
196
+ pry_tester.commands = CommandTester
197
+ pry_tester.input = InputTester.new("command2 horsey", "exit_all")
198
+ pry_tester.commands = CommandTester
199
+
200
+ str_output = StringIO.new
201
+ pry_tester.output = str_output
202
+
203
+ pry_tester.rep
204
+
205
+ str_output.string.should =~ /horsey/
206
+ end
207
+ end
208
+
209
+ describe "Object#pry" do
210
+
211
+ after do
212
+ Pry.reset_defaults
213
+ Pry.color = false
214
+ end
215
+
216
+ it "should start a pry session on the receiver (first form)" do
217
+ Pry.input = InputTester.new("self", "exit")
218
+
219
+ str_output = StringIO.new
220
+ Pry.output = str_output
221
+
222
+ 20.pry
223
+
224
+ str_output.string.should =~ /20/
225
+ end
226
+
227
+ it "should start a pry session on the receiver (second form)" do
228
+ Pry.input = InputTester.new("self", "exit")
229
+
230
+ str_output = StringIO.new
231
+ Pry.output = str_output
232
+
233
+ pry 20
234
+
235
+ str_output.string.should =~ /20/
236
+ end
237
+
238
+ it "should error if more than one argument is passed to Object#pry" do
239
+ lambda { pry(20, :input => Readline) }.should.raise ArgumentError
240
+ end
241
+ end
242
+
243
+ describe "Pry.binding_for" do
244
+ it 'should return TOPLEVEL_BINDING if parameter self is main' do
245
+ _main_ = lambda { TOPLEVEL_BINDING.eval('self') }
246
+ Pry.binding_for(_main_.call).is_a?(Binding).should == true
247
+ Pry.binding_for(_main_.call).should == TOPLEVEL_BINDING
248
+ Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
249
+ end
250
+ end
251
+
252
+
253
+ describe "test Pry defaults" do
254
+
255
+ after do
256
+ Pry.reset_defaults
257
+ Pry.color = false
258
+ end
259
+
260
+ describe "input" do
261
+
262
+ after do
263
+ Pry.reset_defaults
264
+ Pry.color = false
265
+ end
266
+
267
+ it 'should set the input default, and the default should be overridable' do
268
+ Pry.input = InputTester.new("5")
269
+
270
+ str_output = StringIO.new
271
+ Pry.output = str_output
272
+ Pry.new.rep
273
+ str_output.string.should =~ /5/
274
+
275
+ Pry.new(:input => InputTester.new("6")).rep
276
+ str_output.string.should =~ /6/
277
+ end
278
+
279
+ it 'should pass in the prompt if readline arity is 1' do
280
+ Pry.prompt = proc { "A" }
281
+
282
+ arity_one_input = Class.new do
283
+ attr_accessor :prompt
284
+ def readline(prompt)
285
+ @prompt = prompt
286
+ "exit"
287
+ end
288
+ end.new
289
+
290
+ Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
291
+ arity_one_input.prompt.should == Pry.prompt.call
292
+ end
293
+
294
+ it 'should not pass in the prompt if the arity is 0' do
295
+ Pry.prompt = proc { "A" }
296
+
297
+ arity_zero_input = Class.new do
298
+ def readline
299
+ "exit"
300
+ end
301
+ end.new
302
+
303
+ lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
304
+ end
305
+
306
+ it 'should not pass in the prompt if the arity is -1' do
307
+ Pry.prompt = proc { "A" }
308
+
309
+ arity_multi_input = Class.new do
310
+ attr_accessor :prompt
311
+
312
+ def readline(*args)
313
+ @prompt = args.first
314
+ "exit"
315
+ end
316
+ end.new
317
+
318
+ Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
319
+ arity_multi_input.prompt.should == nil
320
+ end
321
+
322
+ end
323
+
324
+ it 'should set the output default, and the default should be overridable' do
325
+ Pry.input = InputTester.new("5", "6", "7")
326
+
327
+ str_output = StringIO.new
328
+ Pry.output = str_output
329
+
330
+ Pry.new.rep
331
+ str_output.string.should =~ /5/
332
+
333
+ Pry.new.rep
334
+ str_output.string.should =~ /5\n.*6/
335
+
336
+ str_output2 = StringIO.new
337
+ Pry.new(:output => str_output2).rep
338
+ str_output2.string.should.not =~ /5\n.*6/
339
+ str_output2.string.should =~ /7/
340
+ end
341
+
342
+ describe "Pry.run_command" do
343
+ before do
344
+ class RCTest
345
+ def a() end
346
+ B = 20
347
+ @x = 10
348
+ end
349
+ end
350
+
351
+ after do
352
+ Object.remove_const(:RCTest)
353
+ end
354
+
355
+ it "should execute command in the appropriate object context" do
356
+ result = Pry.run_command "ls", :context => RCTest
357
+ result.map(&:to_sym).should == [:@x]
358
+ end
359
+
360
+ it "should execute command with parameters in the appropriate object context" do
361
+ result = Pry.run_command "ls -M", :context => RCTest
362
+ result.map(&:to_sym).should == [:a]
363
+ end
364
+
365
+ it "should execute command and show output with :show_output => true flag" do
366
+ str = StringIO.new
367
+ Pry.output = str
368
+ result = Pry.run_command "ls -av", :context => RCTest, :show_output => true
369
+ str.string.should =~ /global variables/
370
+ Pry.output = $stdout
371
+ end
372
+
373
+ it "should execute command with multiple parameters" do
374
+ result = Pry.run_command "ls -c -M RCTest"
375
+ result.map(&:to_sym).should == [:a, :B]
376
+ end
377
+ end
378
+
379
+ describe "commands" do
380
+ it 'should define a command that keeps its return value' do
381
+ class Command68 < Pry::CommandBase
382
+ command "hello", "", :keep_retval => true do
383
+ :kept_hello
384
+ end
385
+ end
386
+ str_output = StringIO.new
387
+ Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
388
+ str_output.string.should =~ /:kept_hello/
389
+
390
+ Object.remove_const(:Command68)
391
+ end
392
+
393
+ it 'should define a command that does NOT keep its return value' do
394
+ class Command68 < Pry::CommandBase
395
+ command "hello", "", :keep_retval => false do
396
+ :kept_hello
397
+ end
398
+ end
399
+ str_output = StringIO.new
400
+ Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
401
+ (str_output.string =~ /:kept_hello/).should == nil
402
+
403
+ Object.remove_const(:Command68)
404
+ end
405
+
406
+
407
+ it 'should set the commands default, and the default should be overridable' do
408
+ class Command0 < Pry::CommandBase
409
+ command "hello" do
410
+ output.puts "hello world"
411
+ end
412
+ end
413
+
414
+ Pry.commands = Command0
415
+
416
+ str_output = StringIO.new
417
+ Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
418
+ str_output.string.should =~ /hello world/
419
+
420
+ class Command1 < Pry::CommandBase
421
+ command "goodbye", "" do
422
+ output.puts "goodbye world"
423
+ end
424
+ end
425
+
426
+ str_output = StringIO.new
427
+
428
+ Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => Command1).rep
429
+ str_output.string.should =~ /goodbye world/
430
+
431
+ Object.remove_const(:Command0)
432
+ Object.remove_const(:Command1)
433
+ end
434
+
435
+ it 'should inherit "help" command from Pry::CommandBase' do
436
+ class Command2 < Pry::CommandBase
437
+ command "h", "h command" do
438
+ end
439
+ end
440
+
441
+ Command2.commands.keys.size.should == 2
442
+ Command2.commands.keys.include?("help").should == true
443
+ Command2.commands.keys.include?("h").should == true
444
+
445
+ Object.remove_const(:Command2)
446
+ end
447
+
448
+ it 'should inherit commands from Pry::Commands' do
449
+ class Command3 < Pry::Commands
450
+ command "v" do
451
+ end
452
+ end
453
+
454
+ Command3.commands.include?("nesting").should == true
455
+ Command3.commands.include?("jump-to").should == true
456
+ Command3.commands.include?("cd").should == true
457
+ Command3.commands.include?("v").should == true
458
+
459
+ Object.remove_const(:Command3)
460
+ end
461
+
462
+ it 'should alias a command with another command' do
463
+ class Command6 < Pry::CommandBase
464
+ alias_command "help2", "help"
465
+ end
466
+
467
+ Command6.commands["help2"].should == Command6.commands["help"]
468
+ # str_output = StringIO.new
469
+ # Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command3).rep
470
+ # str_output.string.should =~ /v command/
471
+
472
+ Object.remove_const(:Command6)
473
+ end
474
+
475
+ it 'should change description of a command using desc' do
476
+
477
+ class Command7 < Pry::Commands
478
+ end
479
+
480
+ orig = Command7.commands["help"][:description]
481
+
482
+ class Command7
483
+ desc "help", "blah"
484
+ end
485
+
486
+ Command7.commands["help"][:description].should.not == orig
487
+ Command7.commands["help"][:description].should == "blah"
488
+
489
+ Object.remove_const(:Command7)
490
+ end
491
+
492
+ it 'should run a command from within a command' do
493
+ class Command01 < Pry::Commands
494
+ command "v" do
495
+ output.puts "v command"
496
+ end
497
+
498
+ command "run_v" do
499
+ run "v"
500
+ end
501
+ end
502
+
503
+ str_output = StringIO.new
504
+ Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command01).rep
505
+ str_output.string.should =~ /v command/
506
+
507
+ Object.remove_const(:Command01)
508
+ end
509
+
510
+ it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
511
+ class Command3 < Pry::Commands
512
+ command "v" do
513
+ output.puts "#{target.eval('self')}"
514
+ end
515
+ end
516
+
517
+ class Command4 < Command3
518
+ end
519
+
520
+ str_output = StringIO.new
521
+ Pry.new(:print => proc {}, :input => InputTester.new("v"),
522
+ :output => str_output, :commands => Command4).rep("john")
523
+ str_output.string.chomp.should == "john"
524
+
525
+ Object.remove_const(:Command3)
526
+ Object.remove_const(:Command4)
527
+ end
528
+
529
+ it 'should import commands from another command object' do
530
+ class Command3 < Pry::CommandBase
531
+ import_from Pry::Commands, "status", "jump-to"
532
+ end
533
+
534
+ str_output = StringIO.new
535
+ Pry.new(:print => proc {}, :input => InputTester.new("status"),
536
+ :output => str_output, :commands => Command3).rep("john")
537
+ str_output.string.should =~ /Status:/
538
+
539
+ Object.remove_const(:Command3)
540
+ end
541
+
542
+ it 'should delete some inherited commands when using delete method' do
543
+ class Command3 < Pry::Commands
544
+ command "v" do
545
+ end
546
+
547
+ delete "show_doc", "show_method"
548
+ delete "ls"
549
+ end
550
+
551
+ Command3.commands.include?("nesting").should == true
552
+ Command3.commands.include?("jump-to").should == true
553
+ Command3.commands.include?("cd").should == true
554
+ Command3.commands.include?("v").should == true
555
+ Command3.commands.include?("show_doc").should == false
556
+ Command3.commands.include?("show_method").should == false
557
+ Command3.commands.include?("ls").should == false
558
+
559
+ Object.remove_const(:Command3)
560
+ end
561
+
562
+ it 'should override some inherited commands' do
563
+ class Command3 < Pry::Commands
564
+ command "jump-to" do
565
+ output.puts "jump-to the music"
566
+ end
567
+
568
+ command "help" do
569
+ output.puts "help to the music"
570
+ end
571
+ end
572
+
573
+ # suppress evaluation output
574
+ Pry.print = proc {}
575
+
576
+ str_output = StringIO.new
577
+ Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => Command3).rep
578
+ str_output.string.chomp.should == "jump-to the music"
579
+
580
+ str_output = StringIO.new
581
+ Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => Command3).rep
582
+ str_output.string.chomp.should == "help to the music"
583
+
584
+ Object.remove_const(:Command3)
585
+
586
+ Pry.reset_defaults
587
+ Pry.color = false
588
+ end
589
+ end
590
+
591
+ it "should set the print default, and the default should be overridable" do
592
+ new_print = proc { |out, value| out.puts value }
593
+ Pry.print = new_print
594
+
595
+ Pry.new.print.should == Pry.print
596
+ str_output = StringIO.new
597
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
598
+ str_output.string.should == "test\n"
599
+
600
+ str_output = StringIO.new
601
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
602
+ :print => proc { |out, value| out.puts value.reverse }).rep
603
+ str_output.string.should == "tset\n"
604
+
605
+ Pry.new.print.should == Pry.print
606
+ str_output = StringIO.new
607
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
608
+ str_output.string.should == "test\n"
609
+ end
610
+
611
+ describe "pry return values" do
612
+ it 'should return the target object' do
613
+ Pry.start(self, :input => StringIO.new("exit"), :output => Pry::NullOutput).should == self
614
+ end
615
+
616
+ it 'should return the parameter given to exit' do
617
+ Pry.start(self, :input => StringIO.new("exit 10"), :output => Pry::NullOutput).should == 10
618
+ end
619
+
620
+ it 'should return the parameter (multi word string) given to exit' do
621
+ Pry.start(self, :input => StringIO.new("exit \"john mair\""), :output => Pry::NullOutput).should == "john mair"
622
+ end
623
+
624
+ it 'should return the parameter (function call) given to exit' do
625
+ Pry.start(self, :input => StringIO.new("exit 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
626
+ end
627
+
628
+ it 'should return the parameter (self) given to exit' do
629
+ Pry.start("carl", :input => StringIO.new("exit self"), :output => Pry::NullOutput).should == "carl"
630
+ end
631
+ end
632
+
633
+ describe "prompts" do
634
+ it 'should set the prompt default, and the default should be overridable (single prompt)' do
635
+ new_prompt = proc { "test prompt> " }
636
+ Pry.prompt = new_prompt
637
+
638
+ Pry.new.prompt.should == Pry.prompt
639
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
640
+ Pry.new.select_prompt(false, 0).should == "test prompt> "
641
+
642
+ new_prompt = proc { "A" }
643
+ pry_tester = Pry.new(:prompt => new_prompt)
644
+ pry_tester.prompt.should == new_prompt
645
+ pry_tester.select_prompt(true, 0).should == "A"
646
+ pry_tester.select_prompt(false, 0).should == "A"
647
+
648
+ Pry.new.prompt.should == Pry.prompt
649
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
650
+ Pry.new.select_prompt(false, 0).should == "test prompt> "
651
+ end
652
+
653
+ it 'should set the prompt default, and the default should be overridable (multi prompt)' do
654
+ new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
655
+ Pry.prompt = new_prompt
656
+
657
+ Pry.new.prompt.should == Pry.prompt
658
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
659
+ Pry.new.select_prompt(false, 0).should == "test prompt* "
660
+
661
+ new_prompt = [proc { "A" }, proc { "B" }]
662
+ pry_tester = Pry.new(:prompt => new_prompt)
663
+ pry_tester.prompt.should == new_prompt
664
+ pry_tester.select_prompt(true, 0).should == "A"
665
+ pry_tester.select_prompt(false, 0).should == "B"
666
+
667
+ Pry.new.prompt.should == Pry.prompt
668
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
669
+ Pry.new.select_prompt(false, 0).should == "test prompt* "
670
+ end
671
+ end
672
+
673
+ it 'should set the hooks default, and the default should be overridable' do
674
+ Pry.input = InputTester.new("exit")
675
+ Pry.hooks = {
676
+ :before_session => proc { |out,_| out.puts "HELLO" },
677
+ :after_session => proc { |out,_| out.puts "BYE" }
678
+ }
679
+
680
+ str_output = StringIO.new
681
+ Pry.new(:output => str_output).repl
682
+ str_output.string.should =~ /HELLO/
683
+ str_output.string.should =~ /BYE/
684
+
685
+ Pry.input.rewind
686
+
687
+ str_output = StringIO.new
688
+ Pry.new(:output => str_output,
689
+ :hooks => {
690
+ :before_session => proc { |out,_| out.puts "MORNING" },
691
+ :after_session => proc { |out,_| out.puts "EVENING" }
692
+ }
693
+ ).repl
694
+
695
+ str_output.string.should =~ /MORNING/
696
+ str_output.string.should =~ /EVENING/
697
+
698
+ # try below with just defining one hook
699
+ Pry.input.rewind
700
+ str_output = StringIO.new
701
+ Pry.new(:output => str_output,
702
+ :hooks => {
703
+ :before_session => proc { |out,_| out.puts "OPEN" }
704
+ }
705
+ ).repl
706
+
707
+ str_output.string.should =~ /OPEN/
708
+
709
+ Pry.input.rewind
710
+ str_output = StringIO.new
711
+ Pry.new(:output => str_output,
712
+ :hooks => {
713
+ :after_session => proc { |out,_| out.puts "CLOSE" }
714
+ }
715
+ ).repl
716
+
717
+ str_output.string.should =~ /CLOSE/
718
+
719
+ Pry.reset_defaults
720
+ Pry.color = false
721
+ end
722
+ end
723
+ end
724
+ end
725
+ end