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

Merge pull request #660 from TreyLawrence/show-source-super

Add super option for show-source command and add three tests
This commit is contained in:
John Mair 2012-07-25 04:22:52 -07:00
commit 49867f3c66
3 changed files with 57 additions and 8 deletions

View file

@ -8,8 +8,18 @@ class Pry
attr_accessor :module_object
def module_object
name = args.first
@module_object ||= WrappedModule.from_str(name, target)
if @module_object
@module_object
else
name = args.first
@module_object = WrappedModule.from_str(name, target)
if @module_object
sup = @module_object.ancestors.select do |anc|
anc.class == @module_object.wrapped.class
end[opts[:super]]
@module_object = sup ? Pry::WrappedModule(sup) : nil
end
end
end
# @param [String]
@ -127,7 +137,7 @@ class Pry
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
opt.on :a, :all, "Show docs for all definitions and monkeypatches of the module/class"
end
def process_sourcable_object
name = args.first
object = target.eval(name)
@ -145,6 +155,7 @@ class Pry
end
def process_module
raise Pry::CommandError, "No documentation found." if module_object.nil?
if opts.present?(:all)
all_modules
else
@ -274,6 +285,7 @@ class Pry
e.g: `show-source Pry#rep` # source for Pry#rep method
e.g: `show-source Pry` # source for Pry class
e.g: `show-source Pry -a` # source for all Pry class definitions (all monkey patches)
e.g: `show-source Pry --super # source for superclass of Pry (Object class)
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
BANNER
@ -323,6 +335,7 @@ class Pry
end
def process_module
raise Pry::CommandError, "No documentation found." if module_object.nil?
if opts.present?(:all)
all_modules
else

View file

@ -17,7 +17,6 @@ class Pry
include Pry::Helpers::DocumentationHelpers
attr_reader :wrapped
private :wrapped
# Convert a string to a module.
#

View file

@ -251,12 +251,23 @@ if !mri18_and_no_real_source_location?
describe "on modules" do
before do
class ShowSourceTestClass
class ShowSourceTestSuperClass
def alpha
end
end
class ShowSourceTestClass<ShowSourceTestSuperClass
def alpha
end
end
module ShowSourceTestSuperModule
def alpha
end
end
module ShowSourceTestModule
include ShowSourceTestSuperModule
def alpha
end
end
@ -273,35 +284,61 @@ if !mri18_and_no_real_source_location?
end
after do
Object.remove_const :ShowSourceTestSuperClass
Object.remove_const :ShowSourceTestClass
Object.remove_const :ShowSourceTestClassWeirdSyntax
Object.remove_const :ShowSourceTestSuperModule
Object.remove_const :ShowSourceTestModule
Object.remove_const :ShowSourceTestModuleWeirdSyntax
end
describe "basic functionality, should find top-level module definitions" do
it 'should show source for a class' do
mock_pry("show-source ShowSourceTestClass").should =~ /class ShowSourceTest.*?def alpha/m
mock_pry("show-source ShowSourceTestClass").should =~ /class ShowSourceTestClass.*?def alpha/m
end
it 'should show source for a super class' do
mock_pry("show-source -s ShowSourceTestClass").should =~ /class ShowSourceTestSuperClass.*?def alpha/m
end
it 'should show source for a module' do
mock_pry("show-source ShowSourceTestModule").should =~ /module ShowSourceTestModule/
end
it 'should show source for an ancestor module' do
mock_pry("show-source -s ShowSourceTestModule").should =~ /module ShowSourceTestSuperModule/
end
it 'should show source for a class when Const = Class.new syntax is used' do
mock_pry("show-source ShowSourceTestClassWeirdSyntax").should =~ /ShowSourceTestClassWeirdSyntax = Class.new/
end
it 'should show source for a super class when Const = Class.new syntax is used' do
mock_pry("show-source -s ShowSourceTestClassWeirdSyntax").should =~ /class Object/
end
it 'should show source for a module when Const = Module.new syntax is used' do
mock_pry("show-source ShowSourceTestModuleWeirdSyntax").should =~ /ShowSourceTestModuleWeirdSyntax = Module.new/
end
end
if !Pry::Helpers::BaseHelpers.mri_18?
before do
mock_pry("class Dog", "def woof", "end", "end")
mock_pry("class TobinaMyDog<Dog", "def woof", "end", "end")
end
after do
Object.remove_const :Dog
Object.remove_const :TobinaMyDog
end
describe "in REPL" do
it 'should find class defined in repl' do
mock_pry("class TobinaMyDog", "def woof", "end", "end", "show-source TobinaMyDog").should =~ /class TobinaMyDog/
Object.remove_const :TobinaMyDog
mock_pry("show-source TobinaMyDog").should =~ /class TobinaMyDog/
end
it 'should find superclass defined in repl' do
mock_pry("show-source -s TobinaMyDog").should =~ /class Dog/
end
end
end