diff --git a/lib/pry/commands/show_info.rb b/lib/pry/commands/show_info.rb index 3e3da608..5d2a0cf2 100644 --- a/lib/pry/commands/show_info.rb +++ b/lib/pry/commands/show_info.rb @@ -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? diff --git a/spec/commands/show_doc_spec.rb b/spec/commands/show_doc_spec.rb index 66ac440a..83890edb 100644 --- a/spec/commands/show_doc_spec.rb +++ b/spec/commands/show_doc_spec.rb @@ -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{ - # daddy initialize! - def initialize(*args) ;end - } + describe "finding find super method docs with help of `--super` switch" do + before do + class Daddy + # daddy initialize! + def initialize(*args); end + end - c = Class.new(b){ - # classy initialize! - def initialize(*args); end - } + class Classy < Daddy + # classy initialize! + def initialize(*args); end + end - d = Class.new(c){ - # grungy initialize?? - def initialize(*args, &block); end - } + class Grungy < Classy + # grungy initialize?? + def initialize(*args); end + end - o = d.new + @o = Grungy.new - # instancey initialize! - def o.initialize; end + # instancey initialize! + 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 diff --git a/spec/commands/show_source_spec.rb b/spec/commands/show_source_spec.rb index a5b9a96d..dcc6aa27 100644 --- a/spec/commands/show_source_spec.rb +++ b/spec/commands/show_source_spec.rb @@ -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