pry 0.9.11.4 → 0.9.12pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.travis.yml +2 -0
  2. data/Rakefile +4 -0
  3. data/lib/pry.rb +1 -1
  4. data/lib/pry/cli.rb +14 -8
  5. data/lib/pry/code.rb +3 -3
  6. data/lib/pry/command.rb +20 -5
  7. data/lib/pry/command_set.rb +3 -3
  8. data/lib/pry/commands.rb +1 -1
  9. data/lib/pry/commands/disabled_commands.rb +2 -0
  10. data/lib/pry/commands/ls.rb +1 -2
  11. data/lib/pry/commands/reload_code.rb +8 -1
  12. data/lib/pry/commands/show_info.rb +27 -4
  13. data/lib/pry/commands/show_source.rb +2 -1
  14. data/lib/pry/commands/whereami.rb +87 -19
  15. data/lib/pry/completion.rb +7 -4
  16. data/lib/pry/helpers/base_helpers.rb +5 -2
  17. data/lib/pry/helpers/command_helpers.rb +3 -1
  18. data/lib/pry/helpers/documentation_helpers.rb +18 -7
  19. data/lib/pry/helpers/table.rb +4 -4
  20. data/lib/pry/indent.rb +2 -7
  21. data/lib/pry/method.rb +89 -129
  22. data/lib/pry/method/disowned.rb +53 -0
  23. data/lib/pry/method/weird_method_locator.rb +186 -0
  24. data/lib/pry/module_candidate.rb +3 -3
  25. data/lib/pry/pager.rb +13 -11
  26. data/lib/pry/pry_class.rb +10 -3
  27. data/lib/pry/terminal.rb +73 -0
  28. data/lib/pry/version.rb +1 -1
  29. data/lib/pry/wrapped_module.rb +51 -1
  30. data/spec/command_helpers_spec.rb +21 -1
  31. data/spec/commands/ls_spec.rb +4 -0
  32. data/spec/commands/show_doc_spec.rb +28 -28
  33. data/spec/commands/show_source_spec.rb +70 -22
  34. data/spec/commands/whereami_spec.rb +60 -11
  35. data/spec/documentation_helper_spec.rb +73 -0
  36. data/spec/fixtures/whereami_helper.rb +6 -0
  37. data/spec/helpers/table_spec.rb +19 -0
  38. data/spec/method_spec.rb +24 -7
  39. metadata +13 -7
  40. data/lib/pry/commands/deprecated_commands.rb +0 -2
  41. data/lib/pry/terminal_info.rb +0 -48
@@ -93,6 +93,10 @@ describe "ls" do
93
93
  "ls Module.new{ def shinanagarns; 4; end; public :shinanagarns }")
94
94
  output.should =~ /shinanagarns/
95
95
  end
96
+
97
+ it "should behave normally when invoked on Module itself" do
98
+ pry_eval("ls Module").should.not =~ /Pry/
99
+ end
96
100
  end
97
101
 
98
102
  describe "constants" do
@@ -279,34 +279,34 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
279
279
 
280
280
  # FIXME: THis is nto a good spec anyway, because i dont think it
281
281
  # SHOULD skip!
282
- # describe "should skip over broken modules" do
283
- # before do
284
- # module TestHost
285
- # # hello
286
- # module M
287
- # binding.eval("def a; end", "dummy.rb", 1)
288
- # binding.eval("def b; end", "dummy.rb", 2)
289
- # binding.eval("def c; end", "dummy.rb", 3)
290
- # end
291
-
292
- # # goodbye
293
- # module M
294
- # def d; end
295
- # def e; end
296
- # end
297
- # end
298
- # end
299
-
300
- # after do
301
- # Object.remove_const(:TestHost)
302
- # end
303
-
304
- # it 'should return doc for first valid module' do
305
- # result = pry_eval("show-doc TestHost::M")
306
- # result.should =~ /goodbye/
307
- # result.should.not =~ /hello/
308
- # end
309
- # end
282
+ describe "should skip over broken modules" do
283
+ before do
284
+ module TestHost
285
+ # hello
286
+ module M
287
+ binding.eval("def a; end", "dummy.rb", 1)
288
+ binding.eval("def b; end", "dummy.rb", 2)
289
+ binding.eval("def c; end", "dummy.rb", 3)
290
+ end
291
+
292
+ # goodbye
293
+ module M
294
+ def d; end
295
+ def e; end
296
+ end
297
+ end
298
+ end
299
+
300
+ after do
301
+ Object.remove_const(:TestHost)
302
+ end
303
+
304
+ it 'should return doc for first valid module' do
305
+ result = pry_eval("show-doc TestHost::M")
306
+ result.should =~ /goodbye/
307
+ result.should.not =~ /hello/
308
+ end
309
+ end
310
310
  end
311
311
 
312
312
  describe "on commands" do
@@ -491,7 +491,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
491
491
  proc {
492
492
  pry_eval(TestHost::C, 'show-source')
493
493
  }.should.raise(Pry::CommandError).
494
- message.should =~ /Cannot find a definition for/
494
+ message.should =~ /Couldn't locate/
495
495
  end
496
496
 
497
497
  it 'should display method code (rather than class) if Pry started inside method binding' do
@@ -535,14 +535,11 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
535
535
  Object.remove_const(:BabyDuck)
536
536
  end
537
537
 
538
- # TODO: !!! This is a bad spec, should not be expected behaviour?!?!
539
- #
540
- # it 'should return source for first valid module' do
541
- # out = pry_eval('show-source BabyDuck::Muesli')
542
- # out.should =~ /def d; end/
543
- # out.should.not =~ /def a; end/
544
- # end
545
-
538
+ it 'should return source for first valid module' do
539
+ out = pry_eval('show-source BabyDuck::Muesli')
540
+ out.should =~ /def d; end/
541
+ out.should.not =~ /def a; end/
542
+ end
546
543
  end
547
544
  end
548
545
  end
@@ -560,28 +557,79 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
560
557
  Pry.config.commands = @oldset
561
558
  end
562
559
 
563
- it 'should show source for an ordinary command' do
564
- @set.command "foo", :body_of_foo do; end
560
+ describe "block commands" do
561
+ it 'should show source for an ordinary command' do
562
+ @set.command "foo", :body_of_foo do; end
565
563
 
566
- pry_eval('show-source foo').should =~ /:body_of_foo/
567
- end
564
+ pry_eval('show-source foo').should =~ /:body_of_foo/
565
+ end
566
+
567
+ it "should output source of commands using special characters" do
568
+ @set.command "!%$", "I gots the yellow fever" do; end
569
+
570
+ pry_eval('show-source !%$').should =~ /yellow fever/
571
+ end
568
572
 
569
- it "should output source of commands using special characters" do
570
- @set.command "!%$", "I gots the yellow fever" do; end
573
+ it 'should show source for a command with spaces in its name' do
574
+ @set.command "foo bar", :body_of_foo_bar do; end
571
575
 
572
- pry_eval('show-source !%$').should =~ /yellow fever/
576
+ pry_eval('show-source foo bar').should =~ /:body_of_foo_bar/
577
+ end
578
+
579
+ it 'should show source for a command by listing name' do
580
+ @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end
581
+
582
+ pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/
583
+ end
573
584
  end
574
585
 
575
- it 'should show source for a command with spaces in its name' do
576
- @set.command "foo bar", :body_of_foo_bar do; end
586
+ describe "create_command commands" do
587
+ it 'should show source for a command' do
588
+ @set.create_command "foo", "babble" do
589
+ def process() :body_of_foo end
590
+ end
591
+ pry_eval('show-source foo').should =~ /:body_of_foo/
592
+ end
577
593
 
578
- pry_eval('show-source foo bar').should =~ /:body_of_foo_bar/
594
+ it 'should show source for a command defined inside pry' do
595
+ pry_eval %{
596
+ _pry_.commands.create_command "foo", "babble" do
597
+ def process() :body_of_foo end
598
+ end
599
+ }
600
+ pry_eval('show-source foo').should =~ /:body_of_foo/
601
+ end
579
602
  end
580
603
 
581
- it 'should show source for a command by listing name' do
582
- @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end
604
+ describe "real class-based commands" do
605
+ before do
606
+ class ::TemporaryCommand < Pry::ClassCommand
607
+ match 'temp-command'
608
+ def process() :body_of_temp end
609
+ end
583
610
 
584
- pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/
611
+ Pry.commands.add_command(::TemporaryCommand)
612
+ end
613
+
614
+ after do
615
+ Object.remove_const(:TemporaryCommand)
616
+ end
617
+
618
+ it 'should show source for a command' do
619
+ pry_eval('show-source temp-command').should =~ /:body_of_temp/
620
+ end
621
+
622
+ it 'should show source for a command defined inside pry' do
623
+ pry_eval %{
624
+ class ::TemporaryCommandInPry < Pry::ClassCommand
625
+ match 'temp-command-in-pry'
626
+ def process() :body_of_temp end
627
+ end
628
+ }
629
+ Pry.commands.add_command(::TemporaryCommandInPry)
630
+ pry_eval('show-source temp-command-in-pry').should =~ /:body_of_temp/
631
+ Object.remove_const(:TemporaryCommandInPry)
632
+ end
585
633
  end
586
634
  end
587
635
 
@@ -63,7 +63,6 @@ describe "whereami" do
63
63
  end
64
64
 
65
65
  Cor.instance_method(:blimey!).source.should =~ /pry_eval/
66
-
67
66
  Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
68
67
  Object.remove_const(:Cor)
69
68
  end
@@ -85,26 +84,76 @@ describe "whereami" do
85
84
  Object.remove_const(:Cor)
86
85
  end
87
86
 
88
- it 'should display a description and and error if reading the file goes wrong' do
87
+ # Now that we use stagger_output (paging output) we no longer get
88
+ # the "From: " line, as we output everything in one go (not separate output.puts)
89
+ # and so the user just gets a single `Error: Cannot open
90
+ # "not.found.file.erb" for reading.`
91
+ # which is good enough IMO. Unfortunately we can't test for it
92
+ # though, as we don't hook stdout.
93
+ #
94
+ # it 'should display a description and error if reading the file goes wrong' do
95
+ # class Cor
96
+ # def blimey!
97
+ # eval <<-END, binding, "not.found.file.erb", 7
98
+ # Pad.tester = pry_tester(binding)
99
+ # Pad.tester.eval('whereami')
100
+ # END
101
+ # end
102
+ # end
103
+
104
+ # proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
105
+
106
+ # Pad.tester.last_output.should =~
107
+ # /From: not.found.file.erb @ line 7 Cor#blimey!:/
108
+ # Object.remove_const(:Cor)
109
+ # end
110
+
111
+ it 'should show code window (not just method source) if parameter passed to whereami' do
89
112
  class Cor
90
113
  def blimey!
91
- eval <<-END, binding, "not.found.file.erb", 7
92
- Pad.tester = pry_tester(binding)
93
- Pad.tester.eval('whereami')
94
- END
114
+ pry_eval(binding, 'whereami 3').should =~ /class Cor/
115
+ end
116
+ end
117
+ Cor.new.blimey!
118
+ Object.remove_const(:Cor)
119
+ end
120
+
121
+ it 'should show entire method when -m option used' do
122
+ old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
123
+ old_cutoff, Pry::Command::Whereami.method_size_cutoff = Pry::Command::Whereami.method_size_cutoff, 1
124
+ class Cor
125
+ def blimey!
126
+ 1
127
+ 2
128
+ pry_eval(binding, 'whereami -m').should =~ /def blimey/
95
129
  end
96
130
  end
131
+ Pry::Command::Whereami.method_size_cutoff, Pry.config.default_window_size = old_cutoff, old_size
132
+ Cor.new.blimey!
133
+ Object.remove_const(:Cor)
134
+ end
97
135
 
98
- proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
99
- Pad.tester.last_output.should =~
100
- /From: not.found.file.erb @ line 7 Cor#blimey!:/
136
+ it 'should show entire file when -f option used' do
137
+ class Cor
138
+ def blimey!
139
+ 1
140
+ 2
141
+ pry_eval(binding, 'whereami -f').should =~ /show entire file when -f option used/
142
+ end
143
+ end
144
+ Cor.new.blimey!
101
145
  Object.remove_const(:Cor)
102
146
  end
103
147
 
104
- it 'should show code window (not just method source) if parameter passed to whereami' do
148
+ it 'should show class when -c option used, and locate correct candidate' do
149
+ require 'fixtures/whereami_helper'
105
150
  class Cor
106
151
  def blimey!
107
- pry_eval(binding, 'whereami 3').should =~ /class Cor/
152
+ 1
153
+ 2
154
+ out = pry_eval(binding, 'whereami -c')
155
+ out.should =~ /class Cor/
156
+ out.should =~ /blimey/
108
157
  end
109
158
  end
110
159
  Cor.new.blimey!
@@ -0,0 +1,73 @@
1
+ require 'helper'
2
+
3
+ describe Pry::Helpers::DocumentationHelpers do
4
+ before do
5
+ @helper = Pry::Helpers::DocumentationHelpers
6
+ end
7
+
8
+ describe "get_comment_content" do
9
+ it "should strip off the hash and unindent" do
10
+ @helper.get_comment_content(" # hello\n # world\n").should == "hello\nworld\n"
11
+ end
12
+
13
+ it "should strip out leading lines of hashes" do
14
+ @helper.get_comment_content("###############\n#hello\n#world\n").should == "hello\nworld\n"
15
+ end
16
+
17
+ it "should remove shebangs" do
18
+ @helper.get_comment_content("#!/usr/bin/env ruby\n# This is a program\n").should == "This is a program\n"
19
+ end
20
+
21
+ it "should unindent past separators" do
22
+ @helper.get_comment_content(" # Copyright Me <me@cirw.in>\n #--\n # So there.\n").should == "Copyright Me <me@cirw.in>\n--\nSo there.\n"
23
+ end
24
+ end
25
+
26
+ describe "process_rdoc" do
27
+ before do
28
+ Pry.color = true
29
+ end
30
+
31
+ after do
32
+ Pry.color = false
33
+ end
34
+
35
+ it "should syntax highlight indented code" do
36
+ @helper.process_rdoc(" 4 + 4\n").should.not == " 4 + 4\n"
37
+ end
38
+
39
+ it "should highlight words surrounded by +s" do
40
+ @helper.process_rdoc("the +parameter+").should =~ /the \e.*parameter\e.*/
41
+ end
42
+
43
+ it "should syntax highlight things in backticks" do
44
+ @helper.process_rdoc("for `Example`").should =~ /for `\e.*Example\e.*`/
45
+ end
46
+
47
+ it "should emphasise em tags" do
48
+ @helper.process_rdoc("for <em>science</em>").should == "for \e[1mscience\e[0m"
49
+ end
50
+
51
+ it "should emphasise italic tags" do
52
+ @helper.process_rdoc("for <i>science</i>").should == "for \e[1mscience\e[0m"
53
+ end
54
+
55
+ it "should syntax highlight code in <code>" do
56
+ @helper.process_rdoc("for <code>Example</code>").should =~ /for \e.*Example\e.*/
57
+ end
58
+
59
+ it "should not double-highlight backticks inside indented code" do
60
+ @helper.process_rdoc(" `echo 5`").should =~ /echo 5/
61
+ end
62
+
63
+ it "should not remove ++" do
64
+ @helper.process_rdoc("--\n comment in a bubble\n++").should =~ /\+\+/
65
+ end
66
+
67
+ it "should do nothing if Pry.color is false" do
68
+ Pry.color = false
69
+ @helper.process_rdoc(" 4 + 4\n").should == " 4 + 4\n"
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,6 @@
1
+ class Cor
2
+ def a; end
3
+ def b; end
4
+ def c; end
5
+ def d; end
6
+ end
@@ -80,6 +80,25 @@ asfadsssaaad fasfaafdssd s
80
80
  end
81
81
  end
82
82
 
83
+ describe 'line length is smaller than the length of the longest word' do
84
+ before do
85
+ element = 'swizzle'
86
+ @elem_len = element.length
87
+ @out = [element, 'crime', 'fun']
88
+ end
89
+
90
+ it 'should not raise error' do
91
+ should.not.raise(FloatDomainError) {
92
+ Pry::Helpers.tablify(@out, @elem_len - 1)
93
+ }
94
+ end
95
+
96
+ it 'should format output as one column' do
97
+ table = Pry::Helpers.tablify(@out, @elem_len - 1).to_s
98
+ table.should == "swizzle\ncrime \nfun "
99
+ end
100
+ end
101
+
83
102
  describe 'decide between one-line or indented output' do
84
103
  Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'
85
104
  end
@@ -95,27 +95,29 @@ describe Pry::Method do
95
95
  end
96
96
 
97
97
  it "should find methods that have been undef'd" do
98
- m = Pry::Method.from_binding(Class.new do
98
+ c = Class.new do
99
99
  def self.bar
100
100
  class << self; undef bar; end
101
101
  binding
102
102
  end
103
- end.bar)
103
+ end
104
+
105
+ m = Pry::Method.from_binding(c.bar)
104
106
  m.name.should == "bar"
105
107
  end
106
108
 
107
109
  # Our source_location trick doesn't work, due to https://github.com/rubinius/rubinius/issues/953
108
110
  unless Pry::Helpers::BaseHelpers.rbx?
109
- it 'should find the super method correctly' do
110
- a = Class.new{ def gag; binding; end; def self.line; __LINE__; end }
111
- b = Class.new(a){ def gag; super; end }
111
+ it 'should find the super method correctly' do
112
+ a = Class.new{ def gag33; binding; end; def self.line; __LINE__; end }
113
+ b = Class.new(a){ def gag33; super; end }
112
114
 
113
- g = b.new.gag
115
+ g = b.new.gag33
114
116
  m = Pry::Method.from_binding(g)
115
117
 
116
118
  m.owner.should == a
117
119
  m.source_line.should == a.line
118
- m.name.should == "gag"
120
+ m.name.should == "gag33"
119
121
  end
120
122
  end
121
123
 
@@ -140,6 +142,21 @@ describe Pry::Method do
140
142
  m.source_line.should == a.line
141
143
  end
142
144
  end
145
+
146
+ it 'should find the right method even if it was renamed and replaced' do
147
+ o = Object.new
148
+ class << o
149
+ def borscht
150
+ "nips"
151
+ binding
152
+ end
153
+ alias paella borscht
154
+ def borscht() paella end
155
+ end
156
+
157
+ m = Pry::Method.from_binding(o.borscht)
158
+ m.source.should == Pry::Method(o.method(:paella)).source
159
+ end
143
160
  end
144
161
 
145
162
  describe 'super' do