pry 0.8.3-java → 0.8.4pre1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,184 @@
1
+ require 'helper'
2
+
3
+ describe Pry::CommandSet do
4
+ before do
5
+ @set = Pry::CommandSet.new(:some_name)
6
+ end
7
+
8
+ it 'should use the name specified at creation' do
9
+ @set.name.should == :some_name
10
+ end
11
+
12
+ it 'should call the block used for the command when it is called' do
13
+ run = false
14
+ @set.command 'foo' do
15
+ run = true
16
+ end
17
+
18
+ @set.run_command nil, 'foo'
19
+ run.should == true
20
+ end
21
+
22
+ it 'should pass arguments of the command to the block' do
23
+ @set.command 'foo' do |*args|
24
+ args.should == [1, 2, 3]
25
+ end
26
+
27
+ @set.run_command nil, 'foo', 1, 2, 3
28
+ end
29
+
30
+ it 'should use the first argument as self' do
31
+ @set.command 'foo' do
32
+ self.should == true
33
+ end
34
+
35
+ @set.run_command true, 'foo'
36
+ end
37
+
38
+ it 'should raise an error when calling an undefined comand' do
39
+ @set.command('foo') {}
40
+ lambda {
41
+ @set.run_command nil, 'bar'
42
+ }.should.raise(Pry::NoCommandError)
43
+ end
44
+
45
+ it 'should be able to remove its own commands' do
46
+ @set.command('foo') {}
47
+ @set.delete 'foo'
48
+
49
+ lambda {
50
+ @set.run_command nil, 'foo'
51
+ }.should.raise(Pry::NoCommandError)
52
+ end
53
+
54
+ it 'should be able to import some commands from other sets' do
55
+ run = false
56
+
57
+ other_set = Pry::CommandSet.new :foo do
58
+ command('foo') { run = true }
59
+ command('bar') {}
60
+ end
61
+
62
+ @set.import_from(other_set, 'foo')
63
+
64
+ @set.run_command nil, 'foo'
65
+ run.should == true
66
+
67
+ lambda {
68
+ @set.run_command nil, 'bar'
69
+ }.should.raise(Pry::NoCommandError)
70
+ end
71
+
72
+ it 'should be able to import a whole set' do
73
+ run = []
74
+
75
+ other_set = Pry::CommandSet.new :foo do
76
+ command('foo') { run << true }
77
+ command('bar') { run << true }
78
+ end
79
+
80
+ @set.import other_set
81
+
82
+ @set.run_command nil, 'foo'
83
+ @set.run_command nil, 'bar'
84
+ run.should == [true, true]
85
+ end
86
+
87
+ it 'should be able to import sets at creation' do
88
+ run = false
89
+ @set.command('foo') { run = true }
90
+
91
+ Pry::CommandSet.new(:other, @set).run_command nil, 'foo'
92
+ run.should == true
93
+ end
94
+
95
+ it 'should set the descriptions of commands' do
96
+ @set.command('foo', 'some stuff') {}
97
+ @set.commands['foo'].description.should == 'some stuff'
98
+ end
99
+
100
+ it 'should be able to alias method' do
101
+ run = false
102
+ @set.command('foo', 'stuff') { run = true }
103
+
104
+ @set.alias_command 'bar', 'foo'
105
+ @set.commands['bar'].name.should == 'bar'
106
+ @set.commands['bar'].description.should == 'stuff'
107
+
108
+ @set.run_command nil, 'bar'
109
+ run.should == true
110
+ end
111
+
112
+ it 'should be able to change the descritpions of methods' do
113
+ @set.command('foo', 'bar') {}
114
+ @set.desc 'foo', 'baz'
115
+
116
+ @set.commands['foo'].description.should == 'baz'
117
+ end
118
+
119
+ it 'should return nil for commands by default' do
120
+ @set.command('foo') { 3 }
121
+ @set.run_command(nil, 'foo').should == nil
122
+ end
123
+
124
+ it 'should be able to keep return values' do
125
+ @set.command('foo', '', :keep_retval => true) { 3 }
126
+ @set.run_command(nil, 'foo').should == 3
127
+ end
128
+
129
+ it 'should be able to have its own helpers' do
130
+ @set.command('foo') do
131
+ should.respond_to :my_helper
132
+ end
133
+
134
+ @set.helpers do
135
+ def my_helper; end
136
+ end
137
+
138
+ @set.run_command(Pry::CommandContext.new, 'foo')
139
+ Pry::CommandContext.new.should.not.respond_to :my_helper
140
+ end
141
+
142
+ it 'should not recreate a new heler module when helpers is called' do
143
+ @set.command('foo') do
144
+ should.respond_to :my_helper
145
+ should.respond_to :my_other_helper
146
+ end
147
+
148
+ @set.helpers do
149
+ def my_helper; end
150
+ end
151
+
152
+ @set.helpers do
153
+ def my_other_helper; end
154
+ end
155
+
156
+ @set.run_command(Pry::CommandContext.new, 'foo')
157
+ end
158
+
159
+ it 'should import helpers from imported sets' do
160
+ imported_set = Pry::CommandSet.new :test do
161
+ helpers do
162
+ def imported_helper_method; end
163
+ end
164
+ end
165
+
166
+ @set.import imported_set
167
+ @set.command('foo') { should.respond_to :imported_helper_method }
168
+ @set.run_command(Pry::CommandContext.new, 'foo')
169
+ end
170
+
171
+ it 'should import helpers even if only some commands are imported' do
172
+ imported_set = Pry::CommandSet.new :test do
173
+ helpers do
174
+ def imported_helper_method; end
175
+ end
176
+
177
+ command('bar') {}
178
+ end
179
+
180
+ @set.import_from imported_set, 'bar'
181
+ @set.command('foo') { should.respond_to :imported_helper_method }
182
+ @set.run_command(Pry::CommandContext.new, 'foo')
183
+ end
184
+ end
@@ -1,11 +1,4 @@
1
- direc = File.dirname(__FILE__)
2
-
3
- $LOAD_PATH.unshift "#{direc}/../lib"
4
-
5
- require 'rubygems'
6
- require 'bacon'
7
- require "pry"
8
- require "#{direc}/test_helper"
1
+ require 'helper'
9
2
 
10
3
  puts "Ruby Version #{RUBY_VERSION}"
11
4
  puts "Testing Pry #{Pry::VERSION}"
@@ -24,6 +17,20 @@ describe Pry do
24
17
  Object.send(:remove_const, :Hello)
25
18
  end
26
19
 
20
+ # bug fix for https://github.com/banister/pry/issues/93
21
+ it 'should not leak pry constants into Object namespace' do
22
+ input_string = "CommandContext"
23
+ str_output = StringIO.new
24
+ o = Object.new
25
+ pry_tester = Pry.new(:input => StringIO.new(input_string),
26
+ :output => str_output,
27
+ :exception_handler => proc { |_, exception| @excep = exception },
28
+ :print => proc {}
29
+ ).rep(o)
30
+
31
+ @excep.is_a?(NameError).should == true
32
+ end
33
+
27
34
  it 'should set an ivar on an object' do
28
35
  input_string = "@x = 10"
29
36
  input = InputTester.new(input_string)
@@ -135,7 +142,7 @@ describe Pry do
135
142
 
136
143
  it "should run the rc file only once" do
137
144
  Pry.should_load_rc = true
138
- Pry::RC_FILES << "#{direc}/testrc"
145
+ Pry::RC_FILES << File.expand_path("../testrc", __FILE__)
139
146
 
140
147
  Pry.start(self, :input => StringIO.new("exit\n"), :output => Pry::NullOutput)
141
148
  TEST_RC.should == [0]
@@ -379,56 +386,56 @@ describe Pry do
379
386
  str_output2.string.should =~ /7/
380
387
  end
381
388
 
382
- describe "Pry.run_command" do
383
- before do
384
- class RCTest
385
- def a() end
386
- B = 20
387
- @x = 10
388
- end
389
- end
390
-
391
- after do
392
- Object.remove_const(:RCTest)
393
- end
394
-
395
- it "should execute command in the appropriate object context" do
396
- result = Pry.run_command "ls", :context => RCTest
397
- result.map(&:to_sym).should == [:@x]
398
- end
399
-
400
- it "should execute command with parameters in the appropriate object context" do
401
- result = Pry.run_command "ls -M", :context => RCTest
402
- result.map(&:to_sym).should == [:a]
403
- end
404
-
405
- it "should execute command and show output with :show_output => true flag" do
406
- str = StringIO.new
407
- Pry.output = str
408
- result = Pry.run_command "ls -afv", :context => RCTest, :show_output => true
409
- str.string.should =~ /global variables/
410
- Pry.output = $stdout
411
- end
412
-
413
- it "should execute command with multiple parameters" do
414
- result = Pry.run_command "ls -c -M RCTest"
415
- result.map(&:to_sym).should == [:a, :B]
416
- end
417
- end
389
+ # describe "Pry.run_command" do
390
+ # before do
391
+ # class RCTest
392
+ # def a() end
393
+ # B = 20
394
+ # @x = 10
395
+ # end
396
+ # end
397
+
398
+ # after do
399
+ # Object.remove_const(:RCTest)
400
+ # end
401
+
402
+ # it "should execute command in the appropriate object context" do
403
+ # result = Pry.run_command "ls", :context => RCTest
404
+ # result.map(&:to_sym).should == [:@x]
405
+ # end
406
+
407
+ # it "should execute command with parameters in the appropriate object context" do
408
+ # result = Pry.run_command "ls -M", :context => RCTest
409
+ # result.map(&:to_sym).should == [:a]
410
+ # end
411
+
412
+ # it "should execute command and show output with :show_output => true flag" do
413
+ # str = StringIO.new
414
+ # Pry.output = str
415
+ # result = Pry.run_command "ls -afv", :context => RCTest, :show_output => true
416
+ # str.string.should =~ /global variables/
417
+ # Pry.output = $stdout
418
+ # end
419
+
420
+ # it "should execute command with multiple parameters" do
421
+ # result = Pry.run_command "ls -c -M RCTest"
422
+ # result.map(&:to_sym).should == [:a, :B]
423
+ # end
424
+ # end
418
425
 
419
426
  describe "commands" do
420
427
  it 'should interpolate ruby code into commands' do
421
- klass = Class.new(Pry::CommandBase) do
428
+ klass = Pry::CommandSet.new :test do
422
429
  command "hello", "", :keep_retval => true do |arg|
423
430
  arg
424
431
  end
425
432
  end
426
433
 
427
- $test_interpolation = "bing"
434
+ @test_interpolation = "bing"
428
435
  str_output = StringIO.new
429
- Pry.new(:input => StringIO.new("hello #{$test_interpolation}"), :output => str_output, :commands => klass).rep
436
+ Pry.new(:input => StringIO.new("hello #{@test_interpolation}"), :output => str_output, :commands => klass).rep
430
437
  str_output.string.should =~ /bing/
431
- $test_interpolation = nil
438
+ @test_interpolation = nil
432
439
  end
433
440
 
434
441
  it 'should create a comand in a nested context and that command should be accessible from the parent' do
@@ -442,47 +449,43 @@ describe Pry do
442
449
  end
443
450
 
444
451
  it 'should define a command that keeps its return value' do
445
- class Command68 < Pry::CommandBase
452
+ klass = Pry::CommandSet.new :test do
446
453
  command "hello", "", :keep_retval => true do
447
454
  :kept_hello
448
455
  end
449
456
  end
450
457
  str_output = StringIO.new
451
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => Command68).rep
458
+ Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
452
459
  str_output.string.should =~ /:kept_hello/
453
460
  str_output.string.should =~ /=>/
454
-
455
- Object.remove_const(:Command68)
456
461
  end
457
462
 
458
463
  it 'should define a command that does NOT keep its return value' do
459
- class Command68 < Pry::CommandBase
464
+ klass = Pry::CommandSet.new :test do
460
465
  command "hello", "", :keep_retval => false do
461
466
  :kept_hello
462
467
  end
463
468
  end
464
469
  str_output = StringIO.new
465
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => Command68).rep
470
+ Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
466
471
  (str_output.string =~ /:kept_hello/).should == nil
467
472
  str_output.string !~ /=>/
468
-
469
- Object.remove_const(:Command68)
470
473
  end
471
474
 
472
475
  it 'should set the commands default, and the default should be overridable' do
473
- class Command0 < Pry::CommandBase
476
+ klass = Pry::CommandSet.new :test do
474
477
  command "hello" do
475
478
  output.puts "hello world"
476
479
  end
477
480
  end
478
481
 
479
- Pry.commands = Command0
482
+ Pry.commands = klass
480
483
 
481
484
  str_output = StringIO.new
482
485
  Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
483
486
  str_output.string.should =~ /hello world/
484
487
 
485
- class Command1 < Pry::CommandBase
488
+ other_klass = Pry::CommandSet.new :test2 do
486
489
  command "goodbye", "" do
487
490
  output.puts "goodbye world"
488
491
  end
@@ -490,73 +493,55 @@ describe Pry do
490
493
 
491
494
  str_output = StringIO.new
492
495
 
493
- Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => Command1).rep
496
+ Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => other_klass).rep
494
497
  str_output.string.should =~ /goodbye world/
495
-
496
- Object.remove_const(:Command0)
497
- Object.remove_const(:Command1)
498
498
  end
499
499
 
500
500
  it 'should inherit "help" command from Pry::CommandBase' do
501
- class Command2 < Pry::CommandBase
501
+ klass = Pry::CommandSet.new :test do
502
502
  command "h", "h command" do
503
503
  end
504
504
  end
505
505
 
506
- Command2.commands.keys.size.should == 3
507
- Command2.commands.keys.include?("help").should == true
508
- Command2.commands.keys.include?("install").should == true
509
- Command2.commands.keys.include?("h").should == true
510
-
511
- Object.remove_const(:Command2)
506
+ klass.commands.keys.size.should == 3
507
+ klass.commands.keys.include?("help").should == true
508
+ klass.commands.keys.include?("install").should == true
509
+ klass.commands.keys.include?("h").should == true
512
510
  end
513
511
 
514
512
  it 'should inherit commands from Pry::Commands' do
515
- class Command3 < Pry::Commands
513
+ klass = Pry::CommandSet.new :test, Pry::Commands do
516
514
  command "v" do
517
515
  end
518
516
  end
519
517
 
520
- Command3.commands.include?("nesting").should == true
521
- Command3.commands.include?("jump-to").should == true
522
- Command3.commands.include?("cd").should == true
523
- Command3.commands.include?("v").should == true
524
-
525
- Object.remove_const(:Command3)
518
+ klass.commands.include?("nesting").should == true
519
+ klass.commands.include?("jump-to").should == true
520
+ klass.commands.include?("cd").should == true
521
+ klass.commands.include?("v").should == true
526
522
  end
527
523
 
528
524
  it 'should alias a command with another command' do
529
- class Command6 < Pry::CommandBase
525
+ klass = Pry::CommandSet.new :test do
530
526
  alias_command "help2", "help"
531
527
  end
532
528
 
533
- Command6.commands["help2"].should == Command6.commands["help"]
534
- # str_output = StringIO.new
535
- # Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command3).rep
536
- # str_output.string.should =~ /v command/
537
529
 
538
- Object.remove_const(:Command6)
530
+ klass.commands["help2"].block.should == klass.commands["help"].block
539
531
  end
540
532
 
541
533
  it 'should change description of a command using desc' do
542
-
543
- class Command7 < Pry::Commands
544
- end
545
-
546
- orig = Command7.commands["help"][:description]
547
-
548
- class Command7
534
+ klass = Pry::CommandSet.new :test do; end
535
+ orig = klass.commands["help"].description
536
+ klass.instance_eval do
549
537
  desc "help", "blah"
550
538
  end
551
-
552
- Command7.commands["help"][:description].should.not == orig
553
- Command7.commands["help"][:description].should == "blah"
554
-
555
- Object.remove_const(:Command7)
539
+ klass.commands["help"].description.should.not == orig
540
+ klass.commands["help"].description.should == "blah"
556
541
  end
557
542
 
558
543
  it 'should run a command from within a command' do
559
- class Command01 < Pry::Commands
544
+ klass = Pry::CommandSet.new :test do
560
545
  command "v" do
561
546
  output.puts "v command"
562
547
  end
@@ -567,69 +552,56 @@ describe Pry do
567
552
  end
568
553
 
569
554
  str_output = StringIO.new
570
- Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command01).rep
555
+ Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => klass).rep
571
556
  str_output.string.should =~ /v command/
572
-
573
- Object.remove_const(:Command01)
574
557
  end
575
558
 
576
559
  it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
577
- class Command3 < Pry::Commands
560
+ klass = Pry::CommandSet.new :test do
578
561
  command "v" do
579
562
  output.puts "#{target.eval('self')}"
580
563
  end
581
564
  end
582
565
 
583
- class Command4 < Command3
566
+ child_klass = Pry::CommandSet.new :test2, klass do
584
567
  end
585
568
 
586
569
  str_output = StringIO.new
587
570
  Pry.new(:print => proc {}, :input => InputTester.new("v"),
588
- :output => str_output, :commands => Command4).rep("john")
571
+ :output => str_output, :commands => child_klass).rep("john")
589
572
 
590
573
  str_output.string.rstrip.should == "john"
591
-
592
- Object.remove_const(:Command3)
593
- Object.remove_const(:Command4)
594
574
  end
595
575
 
596
576
  it 'should import commands from another command object' do
597
- Object.remove_const(:Command77) if Object.const_defined?(:Command77)
598
-
599
- class Command77 < Pry::CommandBase
600
- import_from Pry::Commands, "status", "jump-to"
577
+ klass = Pry::CommandSet.new :test do
578
+ import_from Pry::Commands, "ls", "jump-to"
601
579
  end
602
580
 
603
- str_output = StringIO.new
604
- Pry.new(:print => proc {}, :input => InputTester.new("status"),
605
- :output => str_output, :commands => Command77).rep("john")
606
- str_output.string.should =~ /Status:/
607
-
608
- Object.remove_const(:Command77)
609
- end
581
+ klass.commands.include?("ls").should == true
582
+ klass.commands.include?("jump-to").should == true
583
+ end
610
584
 
611
585
  it 'should delete some inherited commands when using delete method' do
612
- class Command3 < Pry::Commands
586
+ klass = Pry::CommandSet.new :test, Pry::Commands do
613
587
  command "v" do
614
588
  end
615
589
 
616
- delete "show_doc", "show_method"
590
+ delete "show-doc", "show-method"
617
591
  delete "ls"
618
592
  end
619
593
 
620
- Command3.commands.include?("nesting").should == true
621
- Command3.commands.include?("jump-to").should == true
622
- Command3.commands.include?("cd").should == true
623
- Command3.commands.include?("v").should == true
624
- Command3.commands.include?("show_doc").should == false
625
- Command3.commands.include?("show_method").should == false
626
- Command3.commands.include?("ls").should == false
627
-
628
- Object.remove_const(:Command3)
594
+ klass.commands.include?("nesting").should == true
595
+ klass.commands.include?("jump-to").should == true
596
+ klass.commands.include?("cd").should == true
597
+ klass.commands.include?("v").should == true
598
+ klass.commands.include?("show-doc").should == false
599
+ klass.commands.include?("show-method").should == false
600
+ klass.commands.include?("ls").should == false
629
601
  end
630
602
 
631
603
  it 'should override some inherited commands' do
632
- class Command3 < Pry::Commands
604
+ klass = Pry::CommandSet.new :test, Pry::Commands do
633
605
  command "jump-to" do
634
606
  output.puts "jump-to the music"
635
607
  end
@@ -643,14 +615,13 @@ describe Pry do
643
615
  Pry.print = proc {}
644
616
 
645
617
  str_output = StringIO.new
646
- Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => Command3).rep
618
+ Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => klass).rep
647
619
  str_output.string.rstrip.should == "jump-to the music"
648
620
 
649
621
  str_output = StringIO.new
650
- Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => Command3).rep
622
+ Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => klass).rep
651
623
  str_output.string.rstrip.should == "help to the music"
652
624
 
653
- Object.remove_const(:Command3)
654
625
 
655
626
  Pry.reset_defaults
656
627
  Pry.color = false
@@ -737,6 +708,67 @@ describe Pry do
737
708
  Pry.new.select_prompt(true, 0).should == "test prompt> "
738
709
  Pry.new.select_prompt(false, 0).should == "test prompt* "
739
710
  end
711
+
712
+ describe 'storing and restoring the prompt' do
713
+ before do
714
+ make = lambda do |name,i|
715
+ prompt = [ proc { "#{i}>" } , proc { "#{i+1}>" } ]
716
+ (class << prompt; self; end).send(:define_method, :inspect) { "<Prompt-#{name}>" }
717
+ prompt
718
+ end
719
+ @a , @b , @c = make[:a,0] , make[:b,1] , make[:c,2]
720
+ @pry = Pry.new :prompt => @a
721
+ end
722
+ it 'should have a prompt stack' do
723
+ @pry.push_prompt @b
724
+ @pry.push_prompt @c
725
+ @pry.prompt.should == @c
726
+ @pry.pop_prompt
727
+ @pry.prompt.should == @b
728
+ @pry.pop_prompt
729
+ @pry.prompt.should == @a
730
+ end
731
+
732
+ it 'should restore overridden prompts when returning from file-mode' do
733
+ pry = Pry.new :input => InputTester.new('shell-mode', 'shell-mode'),
734
+ :prompt => [ proc { 'P>' } ] * 2
735
+ pry.select_prompt(true, 0).should == "P>"
736
+ pry.re
737
+ pry.select_prompt(true, 0).should =~ /\Apry .* \$ \z/
738
+ pry.re
739
+ pry.select_prompt(true, 0).should == "P>"
740
+ end
741
+
742
+ it '#pop_prompt should return the popped prompt' do
743
+ @pry.push_prompt @b
744
+ @pry.push_prompt @c
745
+ @pry.pop_prompt.should == @c
746
+ @pry.pop_prompt.should == @b
747
+ end
748
+
749
+ it 'should not pop the last prompt' do
750
+ @pry.push_prompt @b
751
+ @pry.pop_prompt.should == @b
752
+ @pry.pop_prompt.should == @a
753
+ @pry.pop_prompt.should == @a
754
+ @pry.prompt.should == @a
755
+ end
756
+
757
+ describe '#prompt= should replace the current prompt with the new prompt' do
758
+ it 'when only one prompt on the stack' do
759
+ @pry.prompt = @b
760
+ @pry.prompt.should == @b
761
+ @pry.pop_prompt.should == @b
762
+ @pry.pop_prompt.should == @b
763
+ end
764
+ it 'when several prompts on the stack' do
765
+ @pry.push_prompt @b
766
+ @pry.prompt = @c
767
+ @pry.pop_prompt.should == @c
768
+ @pry.pop_prompt.should == @a
769
+ end
770
+ end
771
+ end
740
772
  end
741
773
 
742
774
  it 'should set the hooks default, and the default should be overridable' do