pry 0.9.8pre3-i386-mswin32 → 0.9.8pre5-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -141,8 +141,15 @@ describe "Pry::DefaultCommands::Context" do
141
141
  $outer.should == :outer
142
142
  end
143
143
 
144
- it 'should break out of the repl loop of Pry instance when binding_stack has only one binding with cd ..' do
145
- Pry.start(0, :input => StringIO.new("cd ..")).should == nil
144
+ it "should not leave the REPL session when given 'cd ..'" do
145
+ b = Pry.binding_for(Object.new)
146
+ input = InputTester.new "cd ..", "$obj = self", "exit-all"
147
+
148
+ redirect_pry_io(input, StringIO.new) do
149
+ b.pry
150
+ end
151
+
152
+ $obj.should == b.eval("self")
146
153
  end
147
154
 
148
155
  it 'should break out to outer-most session with cd /' do
data/test/test_hooks.rb CHANGED
@@ -53,6 +53,125 @@ describe Pry::Hooks do
53
53
  end
54
54
  end
55
55
 
56
+ describe "Pry::Hooks#merge" do
57
+ describe "merge!" do
58
+ it 'should merge in the Pry::Hooks' do
59
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
60
+ h2 = Pry::Hooks.new
61
+
62
+ h2.merge!(h1)
63
+ h2.get_hook(:test_hook, :testing).should == h1.get_hook(:test_hook, :testing)
64
+ end
65
+
66
+ it 'should not share merged elements with original' do
67
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
68
+ h2 = Pry::Hooks.new
69
+
70
+ h2.merge!(h1)
71
+ h2.add_hook(:test_hook, :testing2) {}
72
+ h2.get_hook(:test_hook, :testing2).should.not == h1.get_hook(:test_hook, :testing2)
73
+ end
74
+
75
+ it 'should NOT overwrite hooks belonging to shared event in receiver' do
76
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
77
+ callable = proc {}
78
+ h2 = Pry::Hooks.new.add_hook(:test_hook, :testing2, callable)
79
+
80
+ h2.merge!(h1)
81
+ h2.get_hook(:test_hook, :testing2).should == callable
82
+ end
83
+
84
+ it 'should overwrite identical hook in receiver' do
85
+ callable1 = proc { :one }
86
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing, callable1)
87
+ callable2 = proc { :two }
88
+ h2 = Pry::Hooks.new.add_hook(:test_hook, :testing, callable2)
89
+
90
+ h2.merge!(h1)
91
+ h2.get_hook(:test_hook, :testing).should == callable1
92
+ h2.hook_count(:test_hook).should == 1
93
+ end
94
+
95
+ it 'should preserve hook order' do
96
+ name = ""
97
+ h1 = Pry::Hooks.new
98
+ h1.add_hook(:test_hook, :testing3) { name << "h" }
99
+ h1.add_hook(:test_hook, :testing4) { name << "n" }
100
+
101
+ h2 = Pry::Hooks.new
102
+ h2.add_hook(:test_hook, :testing1) { name << "j" }
103
+ h2.add_hook(:test_hook, :testing2) { name << "o" }
104
+
105
+ h2.merge!(h1)
106
+ h2.exec_hook(:test_hook)
107
+
108
+ name.should == "john"
109
+ end
110
+
111
+ describe "merge" do
112
+ it 'should return a fresh, independent instance' do
113
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
114
+ h2 = Pry::Hooks.new
115
+
116
+ h3 = h2.merge(h1)
117
+ h3.should.not == h1
118
+ h3.should.not == h2
119
+ end
120
+
121
+ it 'should contain hooks from original instance' do
122
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
123
+ h2 = Pry::Hooks.new.add_hook(:test_hook2, :testing) {}
124
+
125
+ h3 = h2.merge(h1)
126
+ h3.get_hook(:test_hook, :testing).should == h1.get_hook(:test_hook, :testing)
127
+ h3.get_hook(:test_hook2, :testing).should == h2.get_hook(:test_hook2, :testing)
128
+ end
129
+
130
+ it 'should not affect original instances when new hooks are added' do
131
+ h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
132
+ h2 = Pry::Hooks.new.add_hook(:test_hook2, :testing) {}
133
+
134
+ h3 = h2.merge(h1)
135
+ h3.add_hook(:test_hook3, :testing) {}
136
+
137
+ h1.get_hook(:test_hook3, :testing).should == nil
138
+ h2.get_hook(:test_hook3, :testing).should == nil
139
+ end
140
+ end
141
+
142
+ end
143
+ end
144
+
145
+ describe "dupping a Pry::Hooks instance" do
146
+ it 'should share hooks with original' do
147
+ @hooks.add_hook(:test_hook, :testing) do
148
+ :none_such
149
+ end
150
+
151
+ hooks_dup = @hooks.dup
152
+ hooks_dup.get_hook(:test_hook, :testing).should == @hooks.get_hook(:test_hook, :testing)
153
+ end
154
+
155
+ it 'adding a new event to dupped instance should not affect original' do
156
+ @hooks.add_hook(:test_hook, :testing) { :none_such }
157
+ hooks_dup = @hooks.dup
158
+
159
+ hooks_dup.add_hook(:other_test_hook, :testing) { :okay_man }
160
+
161
+ hooks_dup.get_hook(:other_test_hook, :testing).should.not == @hooks.get_hook(:other_test_hook, :testing)
162
+ end
163
+
164
+ it 'adding a new hook to dupped instance should not affect original' do
165
+ @hooks.add_hook(:test_hook, :testing) { :none_such }
166
+ hooks_dup = @hooks.dup
167
+
168
+ hooks_dup.add_hook(:test_hook, :testing2) { :okay_man }
169
+
170
+ hooks_dup.get_hook(:test_hook, :testing2).should.not == @hooks.get_hook(:test_hook, :testing2)
171
+ end
172
+
173
+ end
174
+
56
175
  describe "getting hooks" do
57
176
  describe "get_hook" do
58
177
  it 'should return the correct requested hook' do
@@ -186,6 +305,87 @@ describe Pry::Hooks do
186
305
 
187
306
  Pry.config.hooks.delete_hook(:when_started, :test_hook)
188
307
  end
308
+
309
+ it 'should allow overriding of target (and binding_stack)' do
310
+ options = nil
311
+ o = Object.new
312
+ class << o; attr_accessor :value; end
313
+
314
+ Pry.config.hooks.add_hook(:when_started, :test_hook) { |binding_stack, opt, _pry_| binding_stack.replace [Pry.binding_for(o)] }
315
+
316
+ redirect_pry_io(InputTester.new("@value = true","exit-all")) do
317
+ Pry.start binding, :hello => :baby
318
+ end
319
+
320
+ o.value.should == true
321
+ Pry.config.hooks.delete_hook(:when_started, :test_hook)
322
+ end
323
+
324
+ it 'should allow overriding of target (and binding_stack) via _pry_.binding_stack' do
325
+ options = nil
326
+ o = Object.new
327
+ class << o; attr_accessor :value; end
328
+
329
+ Pry.config.hooks.add_hook(:when_started, :test_hook) { |binding_stack, opt, _pry_| _pry_.binding_stack = [Pry.binding_for(o)] }
330
+
331
+ redirect_pry_io(InputTester.new("@value = true","exit-all")) do
332
+ Pry.start binding, :hello => :baby
333
+ end
334
+
335
+ o.value.should == true
336
+ Pry.config.hooks.delete_hook(:when_started, :test_hook)
337
+ end
338
+
339
+ it 'should give precedence to _pry_.binding_stack over binding_stack' do
340
+ options = nil
341
+ o = Object.new
342
+ class << o; attr_accessor :value; end
343
+
344
+ Pry.config.hooks.add_hook(:when_started, :test_hook) do |binding_stack, opt, _pry_|
345
+ _pry_.binding_stack = [Pry.binding_for(o)]
346
+ binding_stack.replace [Pry.binding_for(5)]
347
+ end
348
+
349
+ redirect_pry_io(InputTester.new("@value = true","exit-all")) do
350
+ Pry.start binding, :hello => :baby
351
+ end
352
+
353
+ o.value.should == true
354
+ Pry.config.hooks.delete_hook(:when_started, :test_hook)
355
+ end
356
+
357
+
358
+ end
359
+
360
+ describe "after_session hook" do
361
+ it 'should always run, even if uncaught exception bubbles out of repl' do
362
+ o = OpenStruct.new
363
+ o.great_escape = Class.new(StandardError)
364
+
365
+ old_ew = Pry.config.exception_whitelist
366
+ Pry.config.exception_whitelist << o.great_escape
367
+
368
+ array = [1, 2, 3, 4, 5]
369
+
370
+ begin
371
+ redirect_pry_io(StringIO.new("raise great_escape"), out=StringIO.new) do
372
+ Pry.start o, :hooks => Pry::Hooks.new.add_hook(:after_session, :cleanup) { array = nil }
373
+ end
374
+ rescue => ex
375
+ exception = ex
376
+ end
377
+
378
+ # ensure that an exception really was raised and it broke out
379
+ # of the repl
380
+ exception.is_a?(o.great_escape).should == true
381
+
382
+ # check that after_session hook ran
383
+ array.should == nil
384
+
385
+ # cleanup after test
386
+ Pry.config.exception_whitelist = old_ew
387
+ end
189
388
  end
389
+
190
390
  end
191
391
  end
data/test/test_pry.rb CHANGED
@@ -170,28 +170,6 @@ describe Pry do
170
170
  end
171
171
  end
172
172
 
173
- describe "Pry#run_command" do
174
- it 'should run a command in a specified context' do
175
- b = Pry.binding_for(7)
176
- p = Pry.new(:output => StringIO.new)
177
- p.run_command("ls -m", "", b)
178
- p.output.string.should =~ /divmod/
179
- end
180
-
181
- it 'should run a command that modifies the passed in eval_string' do
182
- b = Pry.binding_for(7)
183
- p = Pry.new(:output => StringIO.new)
184
- eval_string = "def hello\npeter pan\n"
185
- p.run_command("amend-line !", eval_string, b)
186
- eval_string.should =~ /def hello/
187
- eval_string.should.not =~ /peter pan/
188
- end
189
-
190
- it 'should run a command in the context of a session' do
191
- mock_pry("@session_ivar = 10", "_pry_.run_command('ls')").should =~ /@session_ivar/
192
- end
193
- end
194
-
195
173
  describe "repl" do
196
174
  describe "basic functionality" do
197
175
  it 'should set an ivar on an object and exit the repl' do
@@ -276,6 +254,24 @@ describe Pry do
276
254
  end
277
255
  end
278
256
 
257
+ describe "last_result" do
258
+ it "should be set to the most recent value" do
259
+ mock_pry("2", "_ + 82").should =~ /84/
260
+ end
261
+
262
+ it "should be set to the result of a command with :keep_retval" do
263
+ mock_pry("Pry::Commands.block_command '++', '', {:keep_retval => true} do |a| a.to_i + 1; end", '++ 86', '++ #{_}').should =~ /88/
264
+ end
265
+
266
+ it "should be preserved over an empty line" do
267
+ mock_pry("2 + 2", " ", "\t", " ", "_ + 92").should =~ /96/
268
+ end
269
+
270
+ it "should be preserved when evalling a command without :keep_retval" do
271
+ mock_pry("2 + 2", "ls -l", "_ + 96").should =~ /100/
272
+ end
273
+ end
274
+
279
275
  describe "test loading rc files" do
280
276
 
281
277
  before do
@@ -408,36 +404,6 @@ describe Pry do
408
404
  end
409
405
  end
410
406
 
411
- describe "commands" do
412
- it 'should run a command with no parameter' do
413
- pry_tester = Pry.new
414
- pry_tester.commands = CommandTester
415
- pry_tester.input = InputTester.new("command1", "exit-all")
416
- pry_tester.commands = CommandTester
417
-
418
- str_output = StringIO.new
419
- pry_tester.output = str_output
420
-
421
- pry_tester.rep
422
-
423
- str_output.string.should =~ /command1/
424
- end
425
-
426
- it 'should run a command with one parameter' do
427
- pry_tester = Pry.new
428
- pry_tester.commands = CommandTester
429
- pry_tester.input = InputTester.new("command2 horsey", "exit-all")
430
- pry_tester.commands = CommandTester
431
-
432
- str_output = StringIO.new
433
- pry_tester.output = str_output
434
-
435
- pry_tester.rep
436
-
437
- str_output.string.should =~ /horsey/
438
- end
439
- end
440
-
441
407
  describe "Object#pry" do
442
408
 
443
409
  after do
@@ -480,854 +446,14 @@ describe Pry do
480
446
  Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
481
447
  end
482
448
  end
483
-
484
- describe "test Pry defaults" do
485
-
486
- after do
487
- Pry.reset_defaults
488
- Pry.color = false
489
- end
490
-
491
- describe "input" do
492
-
493
- after do
494
- Pry.reset_defaults
495
- Pry.color = false
496
- end
497
-
498
- it 'should set the input default, and the default should be overridable' do
499
- Pry.input = InputTester.new("5")
500
-
501
- str_output = StringIO.new
502
- Pry.output = str_output
503
- Pry.new.rep
504
- str_output.string.should =~ /5/
505
-
506
- Pry.new(:input => InputTester.new("6")).rep
507
- str_output.string.should =~ /6/
508
- end
509
-
510
- it 'should pass in the prompt if readline arity is 1' do
511
- Pry.prompt = proc { "A" }
512
-
513
- arity_one_input = Class.new do
514
- attr_accessor :prompt
515
- def readline(prompt)
516
- @prompt = prompt
517
- "exit-all"
518
- end
519
- end.new
520
-
521
- Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
522
- arity_one_input.prompt.should == Pry.prompt.call
523
- end
524
-
525
- it 'should not pass in the prompt if the arity is 0' do
526
- Pry.prompt = proc { "A" }
527
-
528
- arity_zero_input = Class.new do
529
- def readline
530
- "exit-all"
531
- end
532
- end.new
533
-
534
- lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
535
- end
536
-
537
- it 'should not pass in the prompt if the arity is -1' do
538
- Pry.prompt = proc { "A" }
539
-
540
- arity_multi_input = Class.new do
541
- attr_accessor :prompt
542
-
543
- def readline(*args)
544
- @prompt = args.first
545
- "exit-all"
546
- end
547
- end.new
548
-
549
- Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
550
- arity_multi_input.prompt.should == nil
551
- end
552
-
553
- end
554
-
555
- it 'should set the output default, and the default should be overridable' do
556
- Pry.input = InputTester.new("5", "6", "7")
557
-
558
- str_output = StringIO.new
559
- Pry.output = str_output
560
-
561
- Pry.new.rep
562
- str_output.string.should =~ /5/
563
-
564
- Pry.new.rep
565
- str_output.string.should =~ /5\n.*6/
566
-
567
- str_output2 = StringIO.new
568
- Pry.new(:output => str_output2).rep
569
- str_output2.string.should.not =~ /5\n.*6/
570
- str_output2.string.should =~ /7/
571
- end
572
-
573
- describe "commands" do
574
- it 'should interpolate ruby code into commands' do
575
- klass = Pry::CommandSet.new do
576
- command "hello", "", :keep_retval => true do |arg|
577
- arg
578
- end
579
- end
580
-
581
- $test_interpolation = "bing"
582
- str_output = StringIO.new
583
- Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
584
- str_output.string.should =~ /bing/
585
- $test_interpolation = nil
586
- end
587
-
588
- # bug fix for https://github.com/pry/pry/issues/170
589
- it 'should not choke on complex string interpolation when checking if ruby code is a command' do
590
- redirect_pry_io(InputTester.new('/#{Regexp.escape(File.expand_path("."))}/'), str_output = StringIO.new) do
591
- pry
592
- end
593
-
594
- str_output.string.should.not =~ /SyntaxError/
595
- end
596
-
597
- it 'should NOT interpolate ruby code into commands if :interpolate => false' do
598
- klass = Pry::CommandSet.new do
599
- command "hello", "", :keep_retval => true, :interpolate => false do |arg|
600
- arg
601
- end
602
- end
603
-
604
- $test_interpolation = "bing"
605
- str_output = StringIO.new
606
- Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
607
- str_output.string.should =~ /test_interpolation/
608
- $test_interpolation = nil
609
- end
610
-
611
- it 'should NOT try to interpolate pure ruby code (no commands) ' do
612
- str_output = StringIO.new
613
- Pry.new(:input => StringIO.new('format \'#{aggy}\''), :output => str_output).rep
614
- str_output.string.should.not =~ /NameError/
615
-
616
- Pry.new(:input => StringIO.new('format #{aggy}'), :output => str_output).rep
617
- str_output.string.should.not =~ /NameError/
618
-
619
- $test_interpolation = "blah"
620
- Pry.new(:input => StringIO.new('format \'#{$test_interpolation}\''), :output => str_output).rep
621
-
622
- str_output.string.should.not =~ /blah/
623
- $test_interpolation = nil
624
- end
625
-
626
- it 'should create a command with a space in its name' do
627
- set = Pry::CommandSet.new do
628
- command "hello baby", "" do
629
- output.puts "hello baby command"
630
- end
631
- end
632
-
633
- str_output = StringIO.new
634
- redirect_pry_io(InputTester.new("hello baby", "exit-all"), str_output) do
635
- Pry.new(:commands => set).rep
636
- end
637
-
638
- str_output.string.should =~ /hello baby command/
639
- end
640
-
641
- it 'should create a command with a space in its name and pass an argument' do
642
- set = Pry::CommandSet.new do
643
- command "hello baby", "" do |arg|
644
- output.puts "hello baby command #{arg}"
645
- end
646
- end
647
-
648
- str_output = StringIO.new
649
- redirect_pry_io(InputTester.new("hello baby john"), str_output) do
650
- Pry.new(:commands => set).rep
651
- end
652
-
653
- str_output.string.should =~ /hello baby command john/
654
- end
655
-
656
- it 'should create a regex command and be able to invoke it' do
657
- set = Pry::CommandSet.new do
658
- command /hello(.)/, "" do
659
- c = captures.first
660
- output.puts "hello#{c}"
661
- end
662
- end
663
-
664
- str_output = StringIO.new
665
- redirect_pry_io(InputTester.new("hello1"), str_output) do
666
- Pry.new(:commands => set).rep
667
- end
668
-
669
- str_output.string.should =~ /hello1/
670
- end
671
-
672
- it 'should create a regex command and pass captures into the args list before regular arguments' do
673
- set = Pry::CommandSet.new do
674
- command /hello(.)/, "" do |c1, a1|
675
- output.puts "hello #{c1} #{a1}"
676
- end
677
- end
678
-
679
- str_output = StringIO.new
680
- redirect_pry_io(InputTester.new("hello1 baby"), str_output) do
681
- Pry.new(:commands => set).rep
682
- end
683
-
684
- str_output.string.should =~ /hello 1 baby/
685
- end
686
-
687
- it 'should create a regex command and interpolate the captures' do
688
- set = Pry::CommandSet.new do
689
- command /hello (.*)/, "" do |c1|
690
- output.puts "hello #{c1}"
691
- end
692
- end
693
-
694
- str_output = StringIO.new
695
- $obj = "bing"
696
- redirect_pry_io(InputTester.new('hello #{$obj}'), str_output) do
697
- Pry.new(:commands => set).rep
698
- end
699
-
700
- str_output.string.should =~ /hello bing/
701
- $obj = nil
702
- end
703
-
704
- it 'should create a regex command and arg_string should be interpolated' do
705
- set = Pry::CommandSet.new do
706
- command /hello(\w+)/, "" do |c1, a1, a2, a3|
707
- output.puts "hello #{c1} #{a1} #{a2} #{a3}"
708
- end
709
- end
710
-
711
- str_output = StringIO.new
712
- $a1 = "bing"
713
- $a2 = "bong"
714
- $a3 = "bang"
715
- redirect_pry_io(InputTester.new('hellojohn #{$a1} #{$a2} #{$a3}'), str_output) do
716
- Pry.new(:commands => set).rep
717
- end
718
-
719
- str_output.string.should =~ /hello john bing bong bang/
720
-
721
- $a1 = nil
722
- $a2 = nil
723
- $a3 = nil
724
- end
725
-
726
-
727
- it 'if a regex capture is missing it should be nil' do
728
- set = Pry::CommandSet.new do
729
- command /hello(.)?/, "" do |c1, a1|
730
- output.puts "hello #{c1.inspect} #{a1}"
731
- end
732
- end
733
-
734
- str_output = StringIO.new
735
- redirect_pry_io(InputTester.new("hello baby"), str_output) do
736
- Pry.new(:commands => set).rep
737
- end
738
-
739
- str_output.string.should =~ /hello nil baby/
740
- end
741
-
742
- it 'should create a command in a nested context and that command should be accessible from the parent' do
743
- str_output = StringIO.new
744
- x = "@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all"
745
- redirect_pry_io(StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all"), str_output) do
746
- Pry.new.repl(0)
747
- end
748
-
749
- str_output.string.should =~ /@x/
750
- end
751
-
752
- it 'should define a command that keeps its return value' do
753
- klass = Pry::CommandSet.new do
754
- command "hello", "", :keep_retval => true do
755
- :kept_hello
756
- end
757
- end
758
- str_output = StringIO.new
759
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
760
- str_output.string.should =~ /:kept_hello/
761
- str_output.string.should =~ /=>/
762
- end
763
-
764
- it 'should define a command that does NOT keep its return value' do
765
- klass = Pry::CommandSet.new do
766
- command "hello", "", :keep_retval => false do
767
- :kept_hello
768
- end
769
- end
770
- str_output = StringIO.new
771
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
772
- (str_output.string =~ /:kept_hello/).should == nil
773
- str_output.string !~ /=>/
774
- end
775
-
776
- it 'should define a command that keeps its return value even when nil' do
777
- klass = Pry::CommandSet.new do
778
- command "hello", "", :keep_retval => true do
779
- nil
780
- end
781
- end
782
- str_output = StringIO.new
783
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
784
- str_output.string.should =~ /nil/
785
- str_output.string.should =~ /=>/
786
- end
787
-
788
- it 'should define a command that keeps its return value but does not return when value is void' do
789
- klass = Pry::CommandSet.new do
790
- command "hello", "", :keep_retval => true do
791
- void
792
- end
793
- end
794
- str_output = StringIO.new
795
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
796
- str_output.string.empty?.should == true
797
- end
798
-
799
- it 'a command (with :keep_retval => false) that replaces eval_string with a valid expression should not have the expression value suppressed' do
800
- klass = Pry::CommandSet.new do
801
- command "hello", "" do
802
- eval_string.replace("6")
803
- end
804
- end
805
- str_output = StringIO.new
806
- Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep
807
- str_output.string.should =~ /6/
808
- end
809
-
810
-
811
- it 'a command (with :keep_retval => true) that replaces eval_string with a valid expression should overwrite the eval_string with the return value' do
812
- klass = Pry::CommandSet.new do
813
- command "hello", "", :keep_retval => true do
814
- eval_string.replace("6")
815
- 7
816
- end
817
- end
818
- str_output = StringIO.new
819
- Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep
820
- str_output.string.should =~ /7/
821
- str_output.string.should.not =~ /6/
822
- end
823
-
824
- it 'a command that return a value in a multi-line expression should clear the expression and return the value' do
825
- klass = Pry::CommandSet.new do
826
- command "hello", "", :keep_retval => true do
827
- 5
828
- end
829
- end
830
- str_output = StringIO.new
831
- Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep
832
- str_output.string.should =~ /5/
833
- end
834
-
835
-
836
- it 'should set the commands default, and the default should be overridable' do
837
- klass = Pry::CommandSet.new do
838
- command "hello" do
839
- output.puts "hello world"
840
- end
841
- end
842
-
843
- Pry.commands = klass
844
-
845
- str_output = StringIO.new
846
- Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
847
- str_output.string.should =~ /hello world/
848
-
849
- other_klass = Pry::CommandSet.new do
850
- command "goodbye", "" do
851
- output.puts "goodbye world"
852
- end
853
- end
854
-
855
- str_output = StringIO.new
856
-
857
- Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => other_klass).rep
858
- str_output.string.should =~ /goodbye world/
859
- end
860
-
861
- it 'should inherit "help" command from Pry::CommandBase' do
862
- klass = Pry::CommandSet.new do
863
- command "h", "h command" do
864
- end
865
- end
866
-
867
- klass.commands.keys.size.should == 3
868
- klass.commands.keys.include?("help").should == true
869
- klass.commands.keys.include?("install-command").should == true
870
- klass.commands.keys.include?("h").should == true
871
- end
872
-
873
- it 'should inherit commands from Pry::Commands' do
874
- klass = Pry::CommandSet.new Pry::Commands do
875
- command "v" do
876
- end
877
- end
878
-
879
- klass.commands.include?("nesting").should == true
880
- klass.commands.include?("jump-to").should == true
881
- klass.commands.include?("cd").should == true
882
- klass.commands.include?("v").should == true
883
- end
884
-
885
- it 'should alias a command with another command' do
886
- klass = Pry::CommandSet.new do
887
- alias_command "help2", "help"
888
- end
889
- klass.commands["help2"].block.should == klass.commands["help"].block
890
- end
891
-
892
- it 'should change description of a command using desc' do
893
- klass = Pry::CommandSet.new do; end
894
- orig = klass.commands["help"].description
895
- klass.instance_eval do
896
- desc "help", "blah"
897
- end
898
- klass.commands["help"].description.should.not == orig
899
- klass.commands["help"].description.should == "blah"
900
- end
901
-
902
- it 'should run a command from within a command' do
903
- klass = Pry::CommandSet.new do
904
- command "v" do
905
- output.puts "v command"
906
- end
907
-
908
- command "run_v" do
909
- run "v"
910
- end
911
- end
912
-
913
- str_output = StringIO.new
914
- Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => klass).rep
915
- str_output.string.should =~ /v command/
916
- end
917
-
918
- it 'should run a regex command from within a command' do
919
- klass = Pry::CommandSet.new do
920
- command /v(.*)?/ do |arg|
921
- output.puts "v #{arg}"
922
- end
923
-
924
- command "run_v" do
925
- run "vbaby"
926
- end
927
- end
928
-
929
- str_output = StringIO.new
930
- redirect_pry_io(InputTester.new("run_v"), str_output) do
931
- Pry.new(:commands => klass).rep
932
- end
933
-
934
- str_output.string.should =~ /v baby/
935
- end
936
-
937
- it 'should run a command from within a command with arguments' do
938
- klass = Pry::CommandSet.new do
939
- command /v(\w+)/ do |arg1, arg2|
940
- output.puts "v #{arg1} #{arg2}"
941
- end
942
-
943
- command "run_v_explicit_parameter" do
944
- run "vbaby", "param"
945
- end
946
-
947
- command "run_v_embedded_parameter" do
948
- run "vbaby param"
949
- end
950
- end
951
-
952
- ["run_v_explicit_parameter", "run_v_embedded_parameter"].each do |cmd|
953
- str_output = StringIO.new
954
- redirect_pry_io(InputTester.new(cmd), str_output) do
955
- Pry.new(:commands => klass).rep
956
- end
957
- str_output.string.should =~ /v baby param/
958
- end
959
- end
960
-
961
- it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
962
- klass = Pry::CommandSet.new do
963
- command "v" do
964
- output.puts "#{target.eval('self')}"
965
- end
966
- end
967
-
968
- child_klass = Pry::CommandSet.new klass do
969
- end
970
-
971
- str_output = StringIO.new
972
- Pry.new(:print => proc {}, :input => InputTester.new("v"),
973
- :output => str_output, :commands => child_klass).rep("john")
974
-
975
- str_output.string.rstrip.should == "john"
976
- end
977
-
978
- it 'should import commands from another command object' do
979
- klass = Pry::CommandSet.new do
980
- import_from Pry::Commands, "ls", "jump-to"
981
- end
982
-
983
- klass.commands.include?("ls").should == true
984
- klass.commands.include?("jump-to").should == true
985
- end
986
-
987
- it 'should delete some inherited commands when using delete method' do
988
- klass = Pry::CommandSet.new Pry::Commands do
989
- command "v" do
990
- end
991
-
992
- delete "show-doc", "show-method"
993
- delete "ls"
994
- end
995
-
996
- klass.commands.include?("nesting").should == true
997
- klass.commands.include?("jump-to").should == true
998
- klass.commands.include?("cd").should == true
999
- klass.commands.include?("v").should == true
1000
- klass.commands.include?("show-doc").should == false
1001
- klass.commands.include?("show-method").should == false
1002
- klass.commands.include?("ls").should == false
1003
- end
1004
-
1005
- it 'should override some inherited commands' do
1006
- klass = Pry::CommandSet.new Pry::Commands do
1007
- command "jump-to" do
1008
- output.puts "jump-to the music"
1009
- end
1010
-
1011
- command "help" do
1012
- output.puts "help to the music"
1013
- end
1014
- end
1015
-
1016
- # suppress evaluation output
1017
- Pry.print = proc {}
1018
-
1019
- str_output = StringIO.new
1020
- Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => klass).rep
1021
- str_output.string.rstrip.should == "jump-to the music"
1022
-
1023
- str_output = StringIO.new
1024
- Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => klass).rep
1025
- str_output.string.rstrip.should == "help to the music"
1026
-
1027
-
1028
- Pry.reset_defaults
1029
- Pry.color = false
1030
- end
1031
- end
1032
-
1033
- it "should set the print default, and the default should be overridable" do
1034
- new_print = proc { |out, value| out.puts value }
1035
- Pry.print = new_print
1036
-
1037
- Pry.new.print.should == Pry.print
1038
- str_output = StringIO.new
1039
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
1040
- str_output.string.should == "test\n"
1041
-
1042
- str_output = StringIO.new
1043
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
1044
- :print => proc { |out, value| out.puts value.reverse }).rep
1045
- str_output.string.should == "tset\n"
1046
-
1047
- Pry.new.print.should == Pry.print
1048
- str_output = StringIO.new
1049
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
1050
- str_output.string.should == "test\n"
1051
- end
1052
-
1053
- describe "pry return values" do
1054
- it 'should return nil' do
1055
- Pry.start(self, :input => StringIO.new("exit-all"), :output => Pry::NullOutput).should == nil
1056
- end
1057
-
1058
- it 'should return the parameter given to exit-all' do
1059
- Pry.start(self, :input => StringIO.new("exit-all 10"), :output => Pry::NullOutput).should == 10
1060
- end
1061
-
1062
- it 'should return the parameter (multi word string) given to exit-all' do
1063
- Pry.start(self, :input => StringIO.new("exit-all \"john mair\""), :output => Pry::NullOutput).should == "john mair"
1064
- end
1065
-
1066
- it 'should return the parameter (function call) given to exit-all' do
1067
- Pry.start(self, :input => StringIO.new("exit-all 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
1068
- end
1069
-
1070
- it 'should return the parameter (self) given to exit-all' do
1071
- Pry.start("carl", :input => StringIO.new("exit-all self"), :output => Pry::NullOutput).should == "carl"
1072
- end
1073
- end
1074
-
1075
- describe "prompts" do
1076
- before do
1077
- @empty_input_buffer = ""
1078
- @non_empty_input_buffer = "def hello"
1079
- @context = Pry.binding_for(0)
1080
- end
1081
-
1082
- it 'should set the prompt default, and the default should be overridable (single prompt)' do
1083
- new_prompt = proc { "test prompt> " }
1084
- Pry.prompt = new_prompt
1085
-
1086
- Pry.new.prompt.should == Pry.prompt
1087
- Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
1088
- Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt> "
1089
-
1090
- new_prompt = proc { "A" }
1091
- pry_tester = Pry.new(:prompt => new_prompt)
1092
- pry_tester.prompt.should == new_prompt
1093
- pry_tester.select_prompt(@empty_input_buffer, @context).should == "A"
1094
- pry_tester.select_prompt(@non_empty_input_buffer, @context).should == "A"
1095
-
1096
- Pry.new.prompt.should == Pry.prompt
1097
- Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
1098
- Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt> "
1099
- end
1100
-
1101
- it 'should set the prompt default, and the default should be overridable (multi prompt)' do
1102
- new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
1103
- Pry.prompt = new_prompt
1104
-
1105
- Pry.new.prompt.should == Pry.prompt
1106
- Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
1107
- Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt* "
1108
-
1109
- new_prompt = [proc { "A" }, proc { "B" }]
1110
- pry_tester = Pry.new(:prompt => new_prompt)
1111
- pry_tester.prompt.should == new_prompt
1112
- pry_tester.select_prompt(@empty_input_buffer, @context).should == "A"
1113
- pry_tester.select_prompt(@non_empty_input_buffer, @context).should == "B"
1114
-
1115
- Pry.new.prompt.should == Pry.prompt
1116
- Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
1117
- Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt* "
1118
- end
1119
-
1120
- describe 'storing and restoring the prompt' do
1121
- before do
1122
- make = lambda do |name,i|
1123
- prompt = [ proc { "#{i}>" } , proc { "#{i+1}>" } ]
1124
- (class << prompt; self; end).send(:define_method, :inspect) { "<Prompt-#{name}>" }
1125
- prompt
1126
- end
1127
- @a , @b , @c = make[:a,0] , make[:b,1] , make[:c,2]
1128
- @pry = Pry.new :prompt => @a
1129
- end
1130
- it 'should have a prompt stack' do
1131
- @pry.push_prompt @b
1132
- @pry.push_prompt @c
1133
- @pry.prompt.should == @c
1134
- @pry.pop_prompt
1135
- @pry.prompt.should == @b
1136
- @pry.pop_prompt
1137
- @pry.prompt.should == @a
1138
- end
1139
-
1140
- it 'should restore overridden prompts when returning from file-mode' do
1141
- pry = Pry.new :input => InputTester.new('shell-mode', 'shell-mode'),
1142
- :prompt => [ proc { 'P>' } ] * 2
1143
- pry.select_prompt(@empty_input_buffer, @context).should == "P>"
1144
- pry.re
1145
- pry.select_prompt(@empty_input_buffer, @context).should =~ /\Apry .* \$ \z/
1146
- pry.re
1147
- pry.select_prompt(@empty_input_buffer, @context).should == "P>"
1148
- end
1149
-
1150
- it '#pop_prompt should return the popped prompt' do
1151
- @pry.push_prompt @b
1152
- @pry.push_prompt @c
1153
- @pry.pop_prompt.should == @c
1154
- @pry.pop_prompt.should == @b
1155
- end
1156
-
1157
- it 'should not pop the last prompt' do
1158
- @pry.push_prompt @b
1159
- @pry.pop_prompt.should == @b
1160
- @pry.pop_prompt.should == @a
1161
- @pry.pop_prompt.should == @a
1162
- @pry.prompt.should == @a
1163
- end
1164
-
1165
- describe '#prompt= should replace the current prompt with the new prompt' do
1166
- it 'when only one prompt on the stack' do
1167
- @pry.prompt = @b
1168
- @pry.prompt.should == @b
1169
- @pry.pop_prompt.should == @b
1170
- @pry.pop_prompt.should == @b
1171
- end
1172
- it 'when several prompts on the stack' do
1173
- @pry.push_prompt @b
1174
- @pry.prompt = @c
1175
- @pry.pop_prompt.should == @c
1176
- @pry.pop_prompt.should == @a
1177
- end
1178
- end
1179
- end
1180
- end
1181
-
1182
- describe "view_clip used for displaying an object in a truncated format" do
1183
-
1184
- VC_MAX_LENGTH = 60
1185
-
1186
- describe "given an object with an #inspect string" do
1187
- it "returns the #<> format of the object (never use inspect)" do
1188
- o = Object.new
1189
- def o.inspect; "a" * VC_MAX_LENGTH; end
1190
-
1191
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
1192
- end
1193
- end
1194
-
1195
- describe "given the 'main' object" do
1196
- it "returns the #to_s of main (special case)" do
1197
- o = TOPLEVEL_BINDING.eval('self')
1198
- Pry.view_clip(o, VC_MAX_LENGTH).should == o.to_s
1199
- end
1200
- end
1201
-
1202
- describe "given the a Numeric, String or Symbol object" do
1203
- [1, 2.0, -5, "hello", :test].each do |o|
1204
- it "returns the #inspect of the special-cased immediate object: #{o}" do
1205
- Pry.view_clip(o, VC_MAX_LENGTH).should == o.inspect
1206
- end
1207
- end
1208
-
1209
- # only testing with String here :)
1210
- it "returns #<> format of the special-cased immediate object if #inspect is longer than maximum" do
1211
- o = "o" * (VC_MAX_LENGTH + 1)
1212
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<String/
1213
- end
1214
- end
1215
-
1216
- describe "given an object with an #inspect string as long as the maximum specified" do
1217
- it "returns the #<> format of the object (never use inspect)" do
1218
- o = Object.new
1219
- def o.inspect; "a" * VC_MAX_LENGTH; end
1220
-
1221
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
1222
- end
1223
- end
1224
-
1225
- describe "given a regular object with an #inspect string longer than the maximum specified" do
1226
-
1227
- describe "when the object is a regular one" do
1228
- it "returns a string of the #<class name:object idish> format" do
1229
- o = Object.new
1230
- def o.inspect; "a" * (VC_MAX_LENGTH + 1); end
1231
-
1232
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
1233
- end
1234
- end
1235
-
1236
- describe "when the object is a Class or a Module" do
1237
- describe "without a name (usually a c = Class.new)" do
1238
- it "returns a string of the #<class name:object idish> format" do
1239
- c, m = Class.new, Module.new
1240
-
1241
- Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
1242
- Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
1243
- end
1244
- end
1245
-
1246
- describe "with a #name longer than the maximum specified" do
1247
- it "returns a string of the #<class name:object idish> format" do
1248
- c, m = Class.new, Module.new
1249
-
1250
-
1251
- def c.name; "a" * (VC_MAX_LENGTH + 1); end
1252
- def m.name; "a" * (VC_MAX_LENGTH + 1); end
1253
-
1254
- Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
1255
- Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
1256
- end
1257
- end
1258
-
1259
- describe "with a #name shorter than or equal to the maximum specified" do
1260
- it "returns a string of the #<class name:object idish> format" do
1261
- c, m = Class.new, Module.new
1262
-
1263
- def c.name; "a" * VC_MAX_LENGTH; end
1264
- def m.name; "a" * VC_MAX_LENGTH; end
1265
-
1266
- Pry.view_clip(c, VC_MAX_LENGTH).should == c.name
1267
- Pry.view_clip(m, VC_MAX_LENGTH).should == m.name
1268
- end
1269
- end
1270
-
1271
- end
1272
-
1273
- end
1274
-
1275
- end
1276
-
1277
- it 'should set the hooks default, and the default should be overridable' do
1278
- Pry.input = InputTester.new("exit-all")
1279
- Pry.hooks = Pry::Hooks.new.
1280
- add_hook(:before_session, :my_name) { |out,_,_| out.puts "HELLO" }.
1281
- add_hook(:after_session, :my_name) { |out,_,_| out.puts "BYE" }
1282
-
1283
- str_output = StringIO.new
1284
- Pry.new(:output => str_output).repl
1285
- str_output.string.should =~ /HELLO/
1286
- str_output.string.should =~ /BYE/
1287
-
1288
- Pry.input.rewind
1289
-
1290
- str_output = StringIO.new
1291
- Pry.new(:output => str_output,
1292
- :hooks => Pry::Hooks.new.
1293
- add_hook( :before_session, :my_name) { |out,_,_| out.puts "MORNING" }.
1294
- add_hook(:after_session, :my_name) { |out,_,_| out.puts "EVENING" }
1295
- ).repl
1296
-
1297
- str_output.string.should =~ /MORNING/
1298
- str_output.string.should =~ /EVENING/
1299
-
1300
- # try below with just defining one hook
1301
- Pry.input.rewind
1302
- str_output = StringIO.new
1303
- Pry.new(:output => str_output,
1304
- :hooks => Pry::Hooks.new.
1305
- add_hook(:before_session, :my_name) { |out,_,_| out.puts "OPEN" }
1306
- ).repl
1307
-
1308
- str_output.string.should =~ /OPEN/
1309
-
1310
- Pry.input.rewind
1311
- str_output = StringIO.new
1312
- Pry.new(:output => str_output,
1313
- :hooks => Pry::Hooks.new.
1314
- add_hook(:after_session, :my_name) { |out,_,_| out.puts "CLOSE" }
1315
- ).repl
1316
-
1317
- str_output.string.should =~ /CLOSE/
1318
-
1319
- Pry.reset_defaults
1320
- Pry.color = false
1321
- end
1322
- end
1323
- end
1324
449
  end
450
+ end
1325
451
 
1326
- describe 'setting custom options' do
1327
- it 'should not raise for unrecognized options' do
1328
- should.not.raise?(NoMethodError) {
1329
- instance = Pry.new(:custom_option => 'custom value')
1330
- }
1331
- end
452
+ describe 'setting custom options' do
453
+ it 'should not raise for unrecognized options' do
454
+ should.not.raise?(NoMethodError) {
455
+ instance = Pry.new(:custom_option => 'custom value')
456
+ }
1332
457
  end
1333
458
  end
459
+ end