pry 0.9.7.4-i386-mswin32 → 0.9.8-i386-mswin32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. data/.gitignore +2 -3
  2. data/CHANGELOG +43 -0
  3. data/README.markdown +3 -1
  4. data/Rakefile +51 -32
  5. data/bin/pry +2 -80
  6. data/lib/pry.rb +33 -26
  7. data/lib/pry/cli.rb +152 -0
  8. data/lib/pry/code.rb +351 -0
  9. data/lib/pry/command.rb +422 -0
  10. data/lib/pry/command_set.rb +259 -129
  11. data/lib/pry/commands.rb +0 -1
  12. data/lib/pry/config.rb +43 -9
  13. data/lib/pry/default_commands/context.rb +109 -92
  14. data/lib/pry/default_commands/documentation.rb +174 -63
  15. data/lib/pry/default_commands/easter_eggs.rb +26 -2
  16. data/lib/pry/default_commands/gems.rb +65 -37
  17. data/lib/pry/default_commands/input.rb +175 -243
  18. data/lib/pry/default_commands/introspection.rb +173 -112
  19. data/lib/pry/default_commands/ls.rb +96 -114
  20. data/lib/pry/default_commands/shell.rb +175 -70
  21. data/lib/pry/helpers/base_helpers.rb +7 -2
  22. data/lib/pry/helpers/command_helpers.rb +71 -77
  23. data/lib/pry/helpers/options_helpers.rb +10 -41
  24. data/lib/pry/helpers/text.rb +24 -4
  25. data/lib/pry/history.rb +55 -17
  26. data/lib/pry/history_array.rb +2 -0
  27. data/lib/pry/hooks.rb +252 -0
  28. data/lib/pry/indent.rb +9 -5
  29. data/lib/pry/method.rb +149 -50
  30. data/lib/pry/plugins.rb +12 -4
  31. data/lib/pry/pry_class.rb +69 -26
  32. data/lib/pry/pry_instance.rb +187 -115
  33. data/lib/pry/version.rb +1 -1
  34. data/lib/pry/wrapped_module.rb +73 -0
  35. data/man/pry.1 +195 -0
  36. data/man/pry.1.html +204 -0
  37. data/man/pry.1.ronn +141 -0
  38. data/pry.gemspec +29 -32
  39. data/test/helper.rb +32 -36
  40. data/test/test_cli.rb +78 -0
  41. data/test/test_code.rb +201 -0
  42. data/test/test_command.rb +327 -0
  43. data/test/test_command_integration.rb +512 -0
  44. data/test/test_command_set.rb +338 -12
  45. data/test/test_completion.rb +1 -1
  46. data/test/test_default_commands.rb +1 -2
  47. data/test/test_default_commands/test_context.rb +27 -5
  48. data/test/test_default_commands/test_documentation.rb +20 -8
  49. data/test/test_default_commands/test_input.rb +84 -45
  50. data/test/test_default_commands/test_introspection.rb +74 -17
  51. data/test/test_default_commands/test_ls.rb +9 -36
  52. data/test/test_default_commands/test_shell.rb +240 -13
  53. data/test/test_hooks.rb +490 -0
  54. data/test/test_indent.rb +2 -0
  55. data/test/test_method.rb +60 -0
  56. data/test/test_pry.rb +29 -904
  57. data/test/test_pry_defaults.rb +380 -0
  58. data/test/test_pry_history.rb +24 -24
  59. data/test/test_syntax_checking.rb +63 -0
  60. data/test/test_wrapped_module.rb +71 -0
  61. metadata +50 -39
  62. data/lib/pry/command_context.rb +0 -53
  63. data/lib/pry/command_processor.rb +0 -181
  64. data/lib/pry/extended_commands/user_command_api.rb +0 -65
  65. data/test/test_command_processor.rb +0 -176
data/test/test_indent.rb CHANGED
@@ -196,6 +196,8 @@ TXT
196
196
  @indent.reset.indent("foo 'hi' if bar\n#").should == "foo 'hi' if bar\n#"
197
197
  @indent.reset.indent("foo 1 while bar\n#").should == "foo 1 while bar\n#"
198
198
  @indent.reset.indent("super if true\n#").should == "super if true\n#"
199
+ @indent.reset.indent("true if false\n#").should == "true if false\n#"
200
+ @indent.reset.indent("String if false\n#").should == "String if false\n#"
199
201
  end
200
202
 
201
203
  it "should indent cunningly disguised ifs" do
data/test/test_method.rb CHANGED
@@ -74,6 +74,56 @@ describe Pry::Method do
74
74
  end
75
75
  end
76
76
 
77
+ describe '.from_binding' do
78
+ it 'should be able to pick a method out of a binding' do
79
+ Pry::Method.from_binding(Class.new{ def self.foo; binding; end }.foo).name.should == "foo"
80
+ end
81
+
82
+ it 'should NOT find a method from the special pry bindings' do
83
+ Pry::Method.from_binding(5.__binding__).should == nil
84
+ end
85
+
86
+ it 'should NOT find a method from the toplevel binding' do
87
+ Pry::Method.from_binding(TOPLEVEL_BINDING).should == nil
88
+ end
89
+
90
+ it "should find methods that have been undef'd" do
91
+ m = Pry::Method.from_binding(Class.new do
92
+ def self.bar
93
+ class << self; undef bar; end
94
+ binding
95
+ end
96
+ end.bar)
97
+ m.name.should == "bar"
98
+ end
99
+
100
+ # Our source_location trick doesn't work, due to https://github.com/rubinius/rubinius/issues/953
101
+ unless Pry::Helpers::BaseHelpers.rbx?
102
+ it 'should find the super method correctly' do
103
+ a = Class.new{ def gag; binding; end; def self.line; __LINE__; end }
104
+ b = Class.new(a){ def gag; super; end }
105
+
106
+ g = b.new.gag
107
+ m = Pry::Method.from_binding(g)
108
+
109
+ m.owner.should == a
110
+ m.source_line.should == a.line
111
+ m.name.should == "gag"
112
+ end
113
+ end
114
+
115
+ it 'should find the right method if a super method exists' do
116
+ a = Class.new{ def gag; binding; end; }
117
+ b = Class.new(a){ def gag; super; binding; end; def self.line; __LINE__; end }
118
+
119
+ m = Pry::Method.from_binding(b.new.gag)
120
+
121
+ m.owner.should == b
122
+ m.source_line.should == b.line
123
+ m.name.should == "gag"
124
+ end
125
+ end
126
+
77
127
  describe 'all_from_class' do
78
128
  def should_find_method(name)
79
129
  Pry::Method.all_from_class(@class).map(&:name).should.include(name)
@@ -287,5 +337,15 @@ describe Pry::Method do
287
337
  end
288
338
  end
289
339
  end
340
+
341
+ describe 'method_name_from_first_line' do
342
+ it 'should work in all simple cases' do
343
+ meth = Pry::Method.new(nil)
344
+ meth.send(:method_name_from_first_line, "def x").should == "x"
345
+ meth.send(:method_name_from_first_line, "def self.x").should == "x"
346
+ meth.send(:method_name_from_first_line, "def ClassName.x").should == "x"
347
+ meth.send(:method_name_from_first_line, "def obj_name.x").should == "x"
348
+ end
349
+ end
290
350
  end
291
351
 
data/test/test_pry.rb CHANGED
@@ -2,16 +2,6 @@ require 'helper'
2
2
 
3
3
  describe Pry do
4
4
 
5
- # if RUBY_PLATFORM !~ /mingw/ && RUBY_PLATFORM !~ /mswin/ && RUBY_PLATFORM != 'java'
6
- # describe 'warning emissions' do
7
- # it 'should emit no warnings' do
8
- # Open4.popen4 'ruby -I lib -rubygems -r"pry" -W -e "exit"' do |pid, stdin, stdout, stderr|
9
- # stderr.read.empty?.should == true
10
- # end
11
- # end
12
- # end
13
- # end
14
-
15
5
  if RUBY_VERSION =~ /1.9/
16
6
  describe "Exotic object support" do
17
7
  # regression test for exotic object support
@@ -53,7 +43,7 @@ describe Pry do
53
43
 
54
44
  # bug fix for https://github.com/banister/pry/issues/93
55
45
  it 'should not leak pry constants into Object namespace' do
56
- input_string = "CommandContext"
46
+ input_string = "Command"
57
47
  str_output = StringIO.new
58
48
  o = Object.new
59
49
  pry_tester = Pry.new(:input => StringIO.new(input_string),
@@ -180,28 +170,6 @@ describe Pry do
180
170
  end
181
171
  end
182
172
 
183
- describe "Pry#run_command" do
184
- it 'should run a command in a specified context' do
185
- b = Pry.binding_for(7)
186
- p = Pry.new(:output => StringIO.new)
187
- p.run_command("ls -m", "", b)
188
- p.output.string.should =~ /divmod/
189
- end
190
-
191
- it 'should run a command that modifies the passed in eval_string' do
192
- b = Pry.binding_for(7)
193
- p = Pry.new(:output => StringIO.new)
194
- eval_string = "def hello\npeter pan\n"
195
- p.run_command("amend-line !", eval_string, b)
196
- eval_string.should =~ /def hello/
197
- eval_string.should.not =~ /peter pan/
198
- end
199
-
200
- it 'should run a command in the context of a session' do
201
- mock_pry("@session_ivar = 10", "_pry_.run_command('ls')").should =~ /@session_ivar/
202
- end
203
- end
204
-
205
173
  describe "repl" do
206
174
  describe "basic functionality" do
207
175
  it 'should set an ivar on an object and exit the repl' do
@@ -216,11 +184,11 @@ describe Pry do
216
184
  end
217
185
  end
218
186
 
219
- describe "valid_expression?" do
187
+ describe "complete_expression?" do
220
188
  it "should not mutate the input!" do
221
189
  clean = "puts <<-FOO\nhi\nFOO\n"
222
190
  a = clean.dup
223
- Pry.new.valid_expression?(a)
191
+ Pry.new.complete_expression?(a)
224
192
  a.should == clean
225
193
  end
226
194
  end
@@ -286,6 +254,24 @@ describe Pry do
286
254
  end
287
255
  end
288
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
+
289
275
  describe "test loading rc files" do
290
276
 
291
277
  before do
@@ -418,36 +404,6 @@ describe Pry do
418
404
  end
419
405
  end
420
406
 
421
- describe "commands" do
422
- it 'should run a command with no parameter' do
423
- pry_tester = Pry.new
424
- pry_tester.commands = CommandTester
425
- pry_tester.input = InputTester.new("command1", "exit-all")
426
- pry_tester.commands = CommandTester
427
-
428
- str_output = StringIO.new
429
- pry_tester.output = str_output
430
-
431
- pry_tester.rep
432
-
433
- str_output.string.should =~ /command1/
434
- end
435
-
436
- it 'should run a command with one parameter' do
437
- pry_tester = Pry.new
438
- pry_tester.commands = CommandTester
439
- pry_tester.input = InputTester.new("command2 horsey", "exit-all")
440
- pry_tester.commands = CommandTester
441
-
442
- str_output = StringIO.new
443
- pry_tester.output = str_output
444
-
445
- pry_tester.rep
446
-
447
- str_output.string.should =~ /horsey/
448
- end
449
- end
450
-
451
407
  describe "Object#pry" do
452
408
 
453
409
  after do
@@ -490,845 +446,14 @@ describe Pry do
490
446
  Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
491
447
  end
492
448
  end
449
+ end
450
+ end
493
451
 
494
-
495
- describe "test Pry defaults" do
496
-
497
- after do
498
- Pry.reset_defaults
499
- Pry.color = false
500
- end
501
-
502
- describe "input" do
503
-
504
- after do
505
- Pry.reset_defaults
506
- Pry.color = false
507
- end
508
-
509
- it 'should set the input default, and the default should be overridable' do
510
- Pry.input = InputTester.new("5")
511
-
512
- str_output = StringIO.new
513
- Pry.output = str_output
514
- Pry.new.rep
515
- str_output.string.should =~ /5/
516
-
517
- Pry.new(:input => InputTester.new("6")).rep
518
- str_output.string.should =~ /6/
519
- end
520
-
521
- it 'should pass in the prompt if readline arity is 1' do
522
- Pry.prompt = proc { "A" }
523
-
524
- arity_one_input = Class.new do
525
- attr_accessor :prompt
526
- def readline(prompt)
527
- @prompt = prompt
528
- "exit-all"
529
- end
530
- end.new
531
-
532
- Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
533
- arity_one_input.prompt.should == Pry.prompt.call
534
- end
535
-
536
- it 'should not pass in the prompt if the arity is 0' do
537
- Pry.prompt = proc { "A" }
538
-
539
- arity_zero_input = Class.new do
540
- def readline
541
- "exit-all"
542
- end
543
- end.new
544
-
545
- lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
546
- end
547
-
548
- it 'should not pass in the prompt if the arity is -1' do
549
- Pry.prompt = proc { "A" }
550
-
551
- arity_multi_input = Class.new do
552
- attr_accessor :prompt
553
-
554
- def readline(*args)
555
- @prompt = args.first
556
- "exit-all"
557
- end
558
- end.new
559
-
560
- Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
561
- arity_multi_input.prompt.should == nil
562
- end
563
-
564
- end
565
-
566
- it 'should set the output default, and the default should be overridable' do
567
- Pry.input = InputTester.new("5", "6", "7")
568
-
569
- str_output = StringIO.new
570
- Pry.output = str_output
571
-
572
- Pry.new.rep
573
- str_output.string.should =~ /5/
574
-
575
- Pry.new.rep
576
- str_output.string.should =~ /5\n.*6/
577
-
578
- str_output2 = StringIO.new
579
- Pry.new(:output => str_output2).rep
580
- str_output2.string.should.not =~ /5\n.*6/
581
- str_output2.string.should =~ /7/
582
- end
583
-
584
- describe "commands" do
585
- it 'should interpolate ruby code into commands' do
586
- klass = Pry::CommandSet.new do
587
- command "hello", "", :keep_retval => true do |arg|
588
- arg
589
- end
590
- end
591
-
592
- $test_interpolation = "bing"
593
- str_output = StringIO.new
594
- Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
595
- str_output.string.should =~ /bing/
596
- $test_interpolation = nil
597
- end
598
-
599
- # bug fix for https://github.com/pry/pry/issues/170
600
- it 'should not choke on complex string interpolation when checking if ruby code is a command' do
601
- redirect_pry_io(InputTester.new('/#{Regexp.escape(File.expand_path("."))}/'), str_output = StringIO.new) do
602
- pry
603
- end
604
-
605
- str_output.string.should.not =~ /SyntaxError/
606
- end
607
-
608
- it 'should NOT interpolate ruby code into commands if :interpolate => false' do
609
- klass = Pry::CommandSet.new do
610
- command "hello", "", :keep_retval => true, :interpolate => false do |arg|
611
- arg
612
- end
613
- end
614
-
615
- $test_interpolation = "bing"
616
- str_output = StringIO.new
617
- Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
618
- str_output.string.should =~ /test_interpolation/
619
- $test_interpolation = nil
620
- end
621
-
622
- it 'should NOT try to interpolate pure ruby code (no commands) ' do
623
- str_output = StringIO.new
624
- Pry.new(:input => StringIO.new('format \'#{aggy}\''), :output => str_output).rep
625
- str_output.string.should.not =~ /NameError/
626
-
627
- Pry.new(:input => StringIO.new('format #{aggy}'), :output => str_output).rep
628
- str_output.string.should.not =~ /NameError/
629
-
630
- $test_interpolation = "blah"
631
- Pry.new(:input => StringIO.new('format \'#{$test_interpolation}\''), :output => str_output).rep
632
-
633
- str_output.string.should.not =~ /blah/
634
- $test_interpolation = nil
635
- end
636
-
637
- it 'should create a command with a space in its name' do
638
- set = Pry::CommandSet.new do
639
- command "hello baby", "" do
640
- output.puts "hello baby command"
641
- end
642
- end
643
-
644
- str_output = StringIO.new
645
- redirect_pry_io(InputTester.new("hello baby", "exit-all"), str_output) do
646
- Pry.new(:commands => set).rep
647
- end
648
-
649
- str_output.string.should =~ /hello baby command/
650
- end
651
-
652
- it 'should create a command with a space in its name and pass an argument' do
653
- set = Pry::CommandSet.new do
654
- command "hello baby", "" do |arg|
655
- output.puts "hello baby command #{arg}"
656
- end
657
- end
658
-
659
- str_output = StringIO.new
660
- redirect_pry_io(InputTester.new("hello baby john"), str_output) do
661
- Pry.new(:commands => set).rep
662
- end
663
-
664
- str_output.string.should =~ /hello baby command john/
665
- end
666
-
667
- it 'should create a regex command and be able to invoke it' do
668
- set = Pry::CommandSet.new do
669
- command /hello(.)/, "" do
670
- c = captures.first
671
- output.puts "hello#{c}"
672
- end
673
- end
674
-
675
- str_output = StringIO.new
676
- redirect_pry_io(InputTester.new("hello1"), str_output) do
677
- Pry.new(:commands => set).rep
678
- end
679
-
680
- str_output.string.should =~ /hello1/
681
- end
682
-
683
- it 'should create a regex command and pass captures into the args list before regular arguments' do
684
- set = Pry::CommandSet.new do
685
- command /hello(.)/, "" do |c1, a1|
686
- output.puts "hello #{c1} #{a1}"
687
- end
688
- end
689
-
690
- str_output = StringIO.new
691
- redirect_pry_io(InputTester.new("hello1 baby"), str_output) do
692
- Pry.new(:commands => set).rep
693
- end
694
-
695
- str_output.string.should =~ /hello 1 baby/
696
- end
697
-
698
- it 'should create a regex command and interpolate the captures' do
699
- set = Pry::CommandSet.new do
700
- command /hello (.*)/, "" do |c1|
701
- output.puts "hello #{c1}"
702
- end
703
- end
704
-
705
- str_output = StringIO.new
706
- $obj = "bing"
707
- redirect_pry_io(InputTester.new('hello #{$obj}'), str_output) do
708
- Pry.new(:commands => set).rep
709
- end
710
-
711
- str_output.string.should =~ /hello bing/
712
- $obj = nil
713
- end
714
-
715
- it 'should create a regex command and arg_string should be interpolated' do
716
- set = Pry::CommandSet.new do
717
- command /hello(\w+)/, "" do |c1, a1, a2, a3|
718
- output.puts "hello #{c1} #{a1} #{a2} #{a3}"
719
- end
720
- end
721
-
722
- str_output = StringIO.new
723
- $a1 = "bing"
724
- $a2 = "bong"
725
- $a3 = "bang"
726
- redirect_pry_io(InputTester.new('hellojohn #{$a1} #{$a2} #{$a3}'), str_output) do
727
- Pry.new(:commands => set).rep
728
- end
729
-
730
- str_output.string.should =~ /hello john bing bong bang/
731
-
732
- $a1 = nil
733
- $a2 = nil
734
- $a3 = nil
735
- end
736
-
737
-
738
- it 'if a regex capture is missing it should be nil' do
739
- set = Pry::CommandSet.new do
740
- command /hello(.)?/, "" do |c1, a1|
741
- output.puts "hello #{c1.inspect} #{a1}"
742
- end
743
- end
744
-
745
- str_output = StringIO.new
746
- redirect_pry_io(InputTester.new("hello baby"), str_output) do
747
- Pry.new(:commands => set).rep
748
- end
749
-
750
- str_output.string.should =~ /hello nil baby/
751
- end
752
-
753
- it 'should create a command in a nested context and that command should be accessible from the parent' do
754
- str_output = StringIO.new
755
- x = "@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all"
756
- 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
757
- Pry.new.repl(0)
758
- end
759
-
760
- str_output.string.should =~ /@x/
761
- end
762
-
763
- it 'should define a command that keeps its return value' do
764
- klass = Pry::CommandSet.new do
765
- command "hello", "", :keep_retval => true do
766
- :kept_hello
767
- end
768
- end
769
- str_output = StringIO.new
770
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
771
- str_output.string.should =~ /:kept_hello/
772
- str_output.string.should =~ /=>/
773
- end
774
-
775
- it 'should define a command that does NOT keep its return value' do
776
- klass = Pry::CommandSet.new do
777
- command "hello", "", :keep_retval => false do
778
- :kept_hello
779
- end
780
- end
781
- str_output = StringIO.new
782
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
783
- (str_output.string =~ /:kept_hello/).should == nil
784
- str_output.string !~ /=>/
785
- end
786
-
787
- it 'should define a command that keeps its return value even when nil' do
788
- klass = Pry::CommandSet.new do
789
- command "hello", "", :keep_retval => true do
790
- nil
791
- end
792
- end
793
- str_output = StringIO.new
794
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
795
- str_output.string.should =~ /nil/
796
- str_output.string.should =~ /=>/
797
- end
798
-
799
- it 'should define a command that keeps its return value but does not return when value is void' do
800
- klass = Pry::CommandSet.new do
801
- command "hello", "", :keep_retval => true do
802
- void
803
- end
804
- end
805
- str_output = StringIO.new
806
- Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
807
- str_output.string.empty?.should == true
808
- end
809
-
810
- it 'a command (with :keep_retval => false) that replaces eval_string with a valid expression should not have the expression value suppressed' do
811
- klass = Pry::CommandSet.new do
812
- command "hello", "" do
813
- eval_string.replace("6")
814
- end
815
- end
816
- str_output = StringIO.new
817
- Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep
818
- str_output.string.should =~ /6/
819
- end
820
-
821
-
822
- 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
823
- klass = Pry::CommandSet.new do
824
- command "hello", "", :keep_retval => true do
825
- eval_string.replace("6")
826
- 7
827
- end
828
- end
829
- str_output = StringIO.new
830
- Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep
831
- str_output.string.should =~ /7/
832
- str_output.string.should.not =~ /6/
833
- end
834
-
835
- it 'a command that return a value in a multi-line expression should clear the expression and return the value' do
836
- klass = Pry::CommandSet.new do
837
- command "hello", "", :keep_retval => true do
838
- 5
839
- end
840
- end
841
- str_output = StringIO.new
842
- Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep
843
- str_output.string.should =~ /5/
844
- end
845
-
846
-
847
- it 'should set the commands default, and the default should be overridable' do
848
- klass = Pry::CommandSet.new do
849
- command "hello" do
850
- output.puts "hello world"
851
- end
852
- end
853
-
854
- Pry.commands = klass
855
-
856
- str_output = StringIO.new
857
- Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
858
- str_output.string.should =~ /hello world/
859
-
860
- other_klass = Pry::CommandSet.new do
861
- command "goodbye", "" do
862
- output.puts "goodbye world"
863
- end
864
- end
865
-
866
- str_output = StringIO.new
867
-
868
- Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => other_klass).rep
869
- str_output.string.should =~ /goodbye world/
870
- end
871
-
872
- it 'should inherit "help" command from Pry::CommandBase' do
873
- klass = Pry::CommandSet.new do
874
- command "h", "h command" do
875
- end
876
- end
877
-
878
- klass.commands.keys.size.should == 3
879
- klass.commands.keys.include?("help").should == true
880
- klass.commands.keys.include?("install-command").should == true
881
- klass.commands.keys.include?("h").should == true
882
- end
883
-
884
- it 'should inherit commands from Pry::Commands' do
885
- klass = Pry::CommandSet.new Pry::Commands do
886
- command "v" do
887
- end
888
- end
889
-
890
- klass.commands.include?("nesting").should == true
891
- klass.commands.include?("jump-to").should == true
892
- klass.commands.include?("cd").should == true
893
- klass.commands.include?("v").should == true
894
- end
895
-
896
- it 'should alias a command with another command' do
897
- klass = Pry::CommandSet.new do
898
- alias_command "help2", "help"
899
- end
900
- klass.commands["help2"].block.should == klass.commands["help"].block
901
- end
902
-
903
- it 'should change description of a command using desc' do
904
- klass = Pry::CommandSet.new do; end
905
- orig = klass.commands["help"].description
906
- klass.instance_eval do
907
- desc "help", "blah"
908
- end
909
- klass.commands["help"].description.should.not == orig
910
- klass.commands["help"].description.should == "blah"
911
- end
912
-
913
- it 'should run a command from within a command' do
914
- klass = Pry::CommandSet.new do
915
- command "v" do
916
- output.puts "v command"
917
- end
918
-
919
- command "run_v" do
920
- run "v"
921
- end
922
- end
923
-
924
- str_output = StringIO.new
925
- Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => klass).rep
926
- str_output.string.should =~ /v command/
927
- end
928
-
929
- it 'should run a regex command from within a command' do
930
- klass = Pry::CommandSet.new do
931
- command /v(.*)?/ do |arg|
932
- output.puts "v #{arg}"
933
- end
934
-
935
- command "run_v" do
936
- run "vbaby"
937
- end
938
- end
939
-
940
- str_output = StringIO.new
941
- redirect_pry_io(InputTester.new("run_v"), str_output) do
942
- Pry.new(:commands => klass).rep
943
- end
944
-
945
- str_output.string.should =~ /v baby/
946
- end
947
-
948
- it 'should run a command from within a command with arguments' do
949
- klass = Pry::CommandSet.new do
950
- command /v(\w+)/ do |arg1, arg2|
951
- output.puts "v #{arg1} #{arg2}"
952
- end
953
-
954
- command "run_v_explicit_parameter" do
955
- run "vbaby", "param"
956
- end
957
-
958
- command "run_v_embedded_parameter" do
959
- run "vbaby param"
960
- end
961
- end
962
-
963
- ["run_v_explicit_parameter", "run_v_embedded_parameter"].each do |cmd|
964
- str_output = StringIO.new
965
- redirect_pry_io(InputTester.new(cmd), str_output) do
966
- Pry.new(:commands => klass).rep
967
- end
968
- str_output.string.should =~ /v baby param/
969
- end
970
- end
971
-
972
- it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
973
- klass = Pry::CommandSet.new do
974
- command "v" do
975
- output.puts "#{target.eval('self')}"
976
- end
977
- end
978
-
979
- child_klass = Pry::CommandSet.new klass do
980
- end
981
-
982
- str_output = StringIO.new
983
- Pry.new(:print => proc {}, :input => InputTester.new("v"),
984
- :output => str_output, :commands => child_klass).rep("john")
985
-
986
- str_output.string.rstrip.should == "john"
987
- end
988
-
989
- it 'should import commands from another command object' do
990
- klass = Pry::CommandSet.new do
991
- import_from Pry::Commands, "ls", "jump-to"
992
- end
993
-
994
- klass.commands.include?("ls").should == true
995
- klass.commands.include?("jump-to").should == true
996
- end
997
-
998
- it 'should delete some inherited commands when using delete method' do
999
- klass = Pry::CommandSet.new Pry::Commands do
1000
- command "v" do
1001
- end
1002
-
1003
- delete "show-doc", "show-method"
1004
- delete "ls"
1005
- end
1006
-
1007
- klass.commands.include?("nesting").should == true
1008
- klass.commands.include?("jump-to").should == true
1009
- klass.commands.include?("cd").should == true
1010
- klass.commands.include?("v").should == true
1011
- klass.commands.include?("show-doc").should == false
1012
- klass.commands.include?("show-method").should == false
1013
- klass.commands.include?("ls").should == false
1014
- end
1015
-
1016
- it 'should override some inherited commands' do
1017
- klass = Pry::CommandSet.new Pry::Commands do
1018
- command "jump-to" do
1019
- output.puts "jump-to the music"
1020
- end
1021
-
1022
- command "help" do
1023
- output.puts "help to the music"
1024
- end
1025
- end
1026
-
1027
- # suppress evaluation output
1028
- Pry.print = proc {}
1029
-
1030
- str_output = StringIO.new
1031
- Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => klass).rep
1032
- str_output.string.rstrip.should == "jump-to the music"
1033
-
1034
- str_output = StringIO.new
1035
- Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => klass).rep
1036
- str_output.string.rstrip.should == "help to the music"
1037
-
1038
-
1039
- Pry.reset_defaults
1040
- Pry.color = false
1041
- end
1042
- end
1043
-
1044
- it "should set the print default, and the default should be overridable" do
1045
- new_print = proc { |out, value| out.puts value }
1046
- Pry.print = new_print
1047
-
1048
- Pry.new.print.should == Pry.print
1049
- str_output = StringIO.new
1050
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
1051
- str_output.string.should == "test\n"
1052
-
1053
- str_output = StringIO.new
1054
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
1055
- :print => proc { |out, value| out.puts value.reverse }).rep
1056
- str_output.string.should == "tset\n"
1057
-
1058
- Pry.new.print.should == Pry.print
1059
- str_output = StringIO.new
1060
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
1061
- str_output.string.should == "test\n"
1062
- end
1063
-
1064
- describe "pry return values" do
1065
- it 'should return the target object' do
1066
- Pry.start(self, :input => StringIO.new("exit-all"), :output => Pry::NullOutput).should == self
1067
- end
1068
-
1069
- it 'should return the parameter given to exit-all' do
1070
- Pry.start(self, :input => StringIO.new("exit-all 10"), :output => Pry::NullOutput).should == 10
1071
- end
1072
-
1073
- it 'should return the parameter (multi word string) given to exit-all' do
1074
- Pry.start(self, :input => StringIO.new("exit-all \"john mair\""), :output => Pry::NullOutput).should == "john mair"
1075
- end
1076
-
1077
- it 'should return the parameter (function call) given to exit-all' do
1078
- Pry.start(self, :input => StringIO.new("exit-all 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
1079
- end
1080
-
1081
- it 'should return the parameter (self) given to exit-all' do
1082
- Pry.start("carl", :input => StringIO.new("exit-all self"), :output => Pry::NullOutput).should == "carl"
1083
- end
1084
- end
1085
-
1086
- describe "prompts" do
1087
- it 'should set the prompt default, and the default should be overridable (single prompt)' do
1088
- new_prompt = proc { "test prompt> " }
1089
- Pry.prompt = new_prompt
1090
-
1091
- Pry.new.prompt.should == Pry.prompt
1092
- Pry.new.select_prompt(true, 0).should == "test prompt> "
1093
- Pry.new.select_prompt(false, 0).should == "test prompt> "
1094
-
1095
- new_prompt = proc { "A" }
1096
- pry_tester = Pry.new(:prompt => new_prompt)
1097
- pry_tester.prompt.should == new_prompt
1098
- pry_tester.select_prompt(true, 0).should == "A"
1099
- pry_tester.select_prompt(false, 0).should == "A"
1100
-
1101
- Pry.new.prompt.should == Pry.prompt
1102
- Pry.new.select_prompt(true, 0).should == "test prompt> "
1103
- Pry.new.select_prompt(false, 0).should == "test prompt> "
1104
- end
1105
-
1106
- it 'should set the prompt default, and the default should be overridable (multi prompt)' do
1107
- new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
1108
- Pry.prompt = new_prompt
1109
-
1110
- Pry.new.prompt.should == Pry.prompt
1111
- Pry.new.select_prompt(true, 0).should == "test prompt> "
1112
- Pry.new.select_prompt(false, 0).should == "test prompt* "
1113
-
1114
- new_prompt = [proc { "A" }, proc { "B" }]
1115
- pry_tester = Pry.new(:prompt => new_prompt)
1116
- pry_tester.prompt.should == new_prompt
1117
- pry_tester.select_prompt(true, 0).should == "A"
1118
- pry_tester.select_prompt(false, 0).should == "B"
1119
-
1120
- Pry.new.prompt.should == Pry.prompt
1121
- Pry.new.select_prompt(true, 0).should == "test prompt> "
1122
- Pry.new.select_prompt(false, 0).should == "test prompt* "
1123
- end
1124
-
1125
- describe 'storing and restoring the prompt' do
1126
- before do
1127
- make = lambda do |name,i|
1128
- prompt = [ proc { "#{i}>" } , proc { "#{i+1}>" } ]
1129
- (class << prompt; self; end).send(:define_method, :inspect) { "<Prompt-#{name}>" }
1130
- prompt
1131
- end
1132
- @a , @b , @c = make[:a,0] , make[:b,1] , make[:c,2]
1133
- @pry = Pry.new :prompt => @a
1134
- end
1135
- it 'should have a prompt stack' do
1136
- @pry.push_prompt @b
1137
- @pry.push_prompt @c
1138
- @pry.prompt.should == @c
1139
- @pry.pop_prompt
1140
- @pry.prompt.should == @b
1141
- @pry.pop_prompt
1142
- @pry.prompt.should == @a
1143
- end
1144
-
1145
- it 'should restore overridden prompts when returning from file-mode' do
1146
- pry = Pry.new :input => InputTester.new('shell-mode', 'shell-mode'),
1147
- :prompt => [ proc { 'P>' } ] * 2
1148
- pry.select_prompt(true, 0).should == "P>"
1149
- pry.re
1150
- pry.select_prompt(true, 0).should =~ /\Apry .* \$ \z/
1151
- pry.re
1152
- pry.select_prompt(true, 0).should == "P>"
1153
- end
1154
-
1155
- it '#pop_prompt should return the popped prompt' do
1156
- @pry.push_prompt @b
1157
- @pry.push_prompt @c
1158
- @pry.pop_prompt.should == @c
1159
- @pry.pop_prompt.should == @b
1160
- end
1161
-
1162
- it 'should not pop the last prompt' do
1163
- @pry.push_prompt @b
1164
- @pry.pop_prompt.should == @b
1165
- @pry.pop_prompt.should == @a
1166
- @pry.pop_prompt.should == @a
1167
- @pry.prompt.should == @a
1168
- end
1169
-
1170
- describe '#prompt= should replace the current prompt with the new prompt' do
1171
- it 'when only one prompt on the stack' do
1172
- @pry.prompt = @b
1173
- @pry.prompt.should == @b
1174
- @pry.pop_prompt.should == @b
1175
- @pry.pop_prompt.should == @b
1176
- end
1177
- it 'when several prompts on the stack' do
1178
- @pry.push_prompt @b
1179
- @pry.prompt = @c
1180
- @pry.pop_prompt.should == @c
1181
- @pry.pop_prompt.should == @a
1182
- end
1183
- end
1184
- end
1185
- end
1186
-
1187
- describe "view_clip used for displaying an object in a truncated format" do
1188
-
1189
- VC_MAX_LENGTH = 60
1190
-
1191
- describe "given an object with an #inspect string" do
1192
- it "returns the #<> format of the object (never use inspect)" do
1193
- o = Object.new
1194
- def o.inspect; "a" * VC_MAX_LENGTH; end
1195
-
1196
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
1197
- end
1198
- end
1199
-
1200
- describe "given the 'main' object" do
1201
- it "returns the #to_s of main (special case)" do
1202
- o = TOPLEVEL_BINDING.eval('self')
1203
- Pry.view_clip(o, VC_MAX_LENGTH).should == o.to_s
1204
- end
1205
- end
1206
-
1207
- describe "given the a Numeric, String or Symbol object" do
1208
- [1, 2.0, -5, "hello", :test].each do |o|
1209
- it "returns the #inspect of the special-cased immediate object: #{o}" do
1210
- Pry.view_clip(o, VC_MAX_LENGTH).should == o.inspect
1211
- end
1212
- end
1213
-
1214
- # only testing with String here :)
1215
- it "returns #<> format of the special-cased immediate object if #inspect is longer than maximum" do
1216
- o = "o" * (VC_MAX_LENGTH + 1)
1217
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<String/
1218
- end
1219
- end
1220
-
1221
- describe "given an object with an #inspect string as long as the maximum specified" do
1222
- it "returns the #<> format of the object (never use inspect)" do
1223
- o = Object.new
1224
- def o.inspect; "a" * VC_MAX_LENGTH; end
1225
-
1226
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
1227
- end
1228
- end
1229
-
1230
- describe "given a regular object with an #inspect string longer than the maximum specified" do
1231
-
1232
- describe "when the object is a regular one" do
1233
- it "returns a string of the #<class name:object idish> format" do
1234
- o = Object.new
1235
- def o.inspect; "a" * (VC_MAX_LENGTH + 1); end
1236
-
1237
- Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
1238
- end
1239
- end
1240
-
1241
- describe "when the object is a Class or a Module" do
1242
- describe "without a name (usually a c = Class.new)" do
1243
- it "returns a string of the #<class name:object idish> format" do
1244
- c, m = Class.new, Module.new
1245
-
1246
- Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
1247
- Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
1248
- end
1249
- end
1250
-
1251
- describe "with a #name longer than the maximum specified" do
1252
- it "returns a string of the #<class name:object idish> format" do
1253
- c, m = Class.new, Module.new
1254
-
1255
-
1256
- def c.name; "a" * (VC_MAX_LENGTH + 1); end
1257
- def m.name; "a" * (VC_MAX_LENGTH + 1); end
1258
-
1259
- Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
1260
- Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
1261
- end
1262
- end
1263
-
1264
- describe "with a #name shorter than or equal to the maximum specified" do
1265
- it "returns a string of the #<class name:object idish> format" do
1266
- c, m = Class.new, Module.new
1267
-
1268
- def c.name; "a" * VC_MAX_LENGTH; end
1269
- def m.name; "a" * VC_MAX_LENGTH; end
1270
-
1271
- Pry.view_clip(c, VC_MAX_LENGTH).should == c.name
1272
- Pry.view_clip(m, VC_MAX_LENGTH).should == m.name
1273
- end
1274
- end
1275
-
1276
- end
1277
-
1278
- end
1279
-
1280
- end
1281
-
1282
- it 'should set the hooks default, and the default should be overridable' do
1283
- Pry.input = InputTester.new("exit-all")
1284
- Pry.hooks = {
1285
- :before_session => proc { |out,_,_| out.puts "HELLO" },
1286
- :after_session => proc { |out,_,_| out.puts "BYE" }
1287
- }
1288
-
1289
- str_output = StringIO.new
1290
- Pry.new(:output => str_output).repl
1291
- str_output.string.should =~ /HELLO/
1292
- str_output.string.should =~ /BYE/
1293
-
1294
- Pry.input.rewind
1295
-
1296
- str_output = StringIO.new
1297
- Pry.new(:output => str_output,
1298
- :hooks => {
1299
- :before_session => proc { |out,_,_| out.puts "MORNING" },
1300
- :after_session => proc { |out,_,_| out.puts "EVENING" }
1301
- }
1302
- ).repl
1303
-
1304
- str_output.string.should =~ /MORNING/
1305
- str_output.string.should =~ /EVENING/
1306
-
1307
- # try below with just defining one hook
1308
- Pry.input.rewind
1309
- str_output = StringIO.new
1310
- Pry.new(:output => str_output,
1311
- :hooks => {
1312
- :before_session => proc { |out,_,_| out.puts "OPEN" }
1313
- }
1314
- ).repl
1315
-
1316
- str_output.string.should =~ /OPEN/
1317
-
1318
- Pry.input.rewind
1319
- str_output = StringIO.new
1320
- Pry.new(:output => str_output,
1321
- :hooks => {
1322
- :after_session => proc { |out,_,_| out.puts "CLOSE" }
1323
- }
1324
- ).repl
1325
-
1326
- str_output.string.should =~ /CLOSE/
1327
-
1328
- Pry.reset_defaults
1329
- Pry.color = false
1330
- end
1331
- end
1332
- 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
+ }
1333
457
  end
1334
458
  end
459
+ end