1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Show{Source,Doc}: assume super definitions from the context

Fix issue #877 (show-source -s doesn't work (when no method arg is
given))

This behaviour is shared between `show-source` and `show-doc`. So with
one fix I kill two bugs.

Add tests and rewrite some of the existing ones (related to the
`--super` switch).
This commit is contained in:
Kyrylo Silin 2013-03-27 23:45:05 +02:00
parent 71fdd1fd13
commit 3546a3acd5
3 changed files with 94 additions and 38 deletions

View file

@ -155,7 +155,16 @@ class Pry
end
def obj_name
@obj_name ||= args.empty? ? nil : args.join(" ")
@obj_name ||=
if args.empty? && (opts[:super] && opts[:super] > 0)
current_method_name
else
args.join(' ')
end
end
def current_method_name
_pry_.current_context.eval('::Kernel.__method__').to_s
end
def use_line_numbers?

View file

@ -39,33 +39,61 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
@o.sample
end
it "should be able to find super methods" do
b = Class.new{
describe "finding find super method docs with help of `--super` switch" do
before do
class Daddy
# daddy initialize!
def initialize(*args) ;end
}
def initialize(*args); end
end
c = Class.new(b){
class Classy < Daddy
# classy initialize!
def initialize(*args); end
}
end
d = Class.new(c){
class Grungy < Classy
# grungy initialize??
def initialize(*args, &block); end
}
def initialize(*args); end
end
o = d.new
@o = Grungy.new
# instancey initialize!
def o.initialize; end
def @o.initialize; end
end
t = pry_tester(binding)
after do
Object.remove_const(:Grungy)
Object.remove_const(:Classy)
Object.remove_const(:Daddy)
end
t.eval("show-doc o.initialize").should =~ /instancey initialize/
t.eval("show-doc --super o.initialize").should =~ /grungy initialize/
t.eval("show-doc o.initialize -ss").should =~ /classy initialize/
t.eval("show-doc o.initialize -sss").should =~ /daddy initialize/
it "finds super method docs" do
output = pry_eval(binding, 'show-doc --super @o.initialize')
output.should =~ /grungy initialize/
end
it "traverses ancestor chain and finds super method docs" do
output = pry_eval(binding, 'show-doc -ss @o.initialize')
output.should =~ /classy initialize/
end
it "traverses ancestor chain even higher and finds super method doc" do
output = pry_eval(binding, 'show-doc @o.initialize -sss')
output.should =~ /daddy initialize/
end
it "finds super method docs without explicit method argument" do
fatty = Grungy.new
# fatty initialize!
def fatty.initialize
pry_eval(binding, 'show-doc --super')
end
output = fatty.initialize
output.should =~ /grungy initialize/
end
end
describe "rdoc highlighting" do

View file

@ -123,22 +123,6 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/
end
it "should find super methods" do
class Foo
def foo(*bars)
:super_wibble
end
end
o = Foo.new
Object.remove_const(:Foo)
def o.foo(*bars)
:wibble
end
pry_eval(binding, "show-source --super o.foo").
should =~ /:super_wibble/
end
it "should raise a CommandError when super method doesn't exist" do
def @o.foo(*bars); end
@ -182,6 +166,40 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
end
end
describe "finding super methods with help of `--super` switch" do
before do
class Foo
def foo(*bars)
:super_wibble
end
end
end
after do
Object.remove_const(:Foo)
end
it "finds super methods with explicit method argument" do
o = Foo.new
def o.foo(*bars)
:wibble
end
pry_eval(binding, "show-source --super o.foo").should =~ /:super_wibble/
end
it "finds super methods without explicit method argument" do
o = Foo.new
def o.foo(*bars)
:wibble
pry_eval(binding, 'show-source --super')
end
o.foo.should =~ /:super_wibble/
end
end
describe "on sourcable objects" do
if RUBY_VERSION =~ /1.9/
it "should output source defined inside pry" do
@ -370,6 +388,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
it 'should find class defined in repl' do
pry_eval('show-source TobinaMyDog').should =~ /class TobinaMyDog/
end
it 'should find superclass defined in repl' do
pry_eval('show-source -s TobinaMyDog').should =~ /class Dog/
end