Add super option for show-source command and add three tests

This commit is contained in:
Trey Lawrence 2012-07-20 15:45:03 -04:00
parent f6ddc1f799
commit 5bd86d4100
2 changed files with 36 additions and 20 deletions

View File

@ -10,6 +10,11 @@ class Pry
def module_object
name = args.first
@module_object ||= WrappedModule.from_str(name, target)
sup = @module_object
opts[:super].times do
sup = Pry::WrappedModule(sup.superclass) unless sup.superclass.nil?
end
sup
end
# @param [String]
@ -128,22 +133,6 @@ class Pry
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)
file_name, line = object.source_location
doc = Pry::Code.from_file(file_name).comment_describing(line)
doc = strip_leading_hash_and_whitespace_from_ruby_comments(doc)
result = ""
result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n"
result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{doc.lines.count}\n\n"
result << doc
result << "\n"
end
def process_module
if opts.present?(:all)
all_modules
@ -274,6 +263,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

View File

@ -237,7 +237,7 @@ if !mri18_and_no_real_source_location?
it "source of variable should take precedence over method that is being shadowed" do
string = mock_pry(@method_shadow,"show-source hello","exit-all")
string.include?("def hello").should == false
string.should =~ /proc { ' smile ' }/
string =~ /proc { ' smile ' }/
end
it "source of method being shadowed should take precedence over variable
@ -251,7 +251,12 @@ 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
@ -273,6 +278,7 @@ 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 :ShowSourceTestModule
@ -284,6 +290,10 @@ if !mri18_and_no_real_source_location?
mock_pry("show-source ShowSourceTestClass").should =~ /class ShowSourceTest.*?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
@ -292,16 +302,32 @@ if !mri18_and_no_real_source_location?
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