Skip to content
Browse files

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

Add super option for show-source command and add three tests
  • Loading branch information...
2 parents dd98daa + 9b5b26f commit 49867f3c6644ed294f7a6163a8b9cbec101b1a3f @banister banister committed Jul 25, 2012
View
19 lib/pry/default_commands/introspection.rb
@@ -8,8 +8,18 @@ module ModuleIntrospectionHelpers
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 @@ def options(opt)
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 @@ def process_sourcable_object
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 @@ def process
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 @@ def process_method
end
def process_module
+ raise Pry::CommandError, "No documentation found." if module_object.nil?
if opts.present?(:all)
all_modules
else
View
1 lib/pry/wrapped_module.rb
@@ -17,7 +17,6 @@ class WrappedModule
include Pry::Helpers::DocumentationHelpers
attr_reader :wrapped
- private :wrapped
# Convert a string to a module.
#
View
45 test/test_default_commands/test_show_source.rb
@@ -251,12 +251,23 @@ def @o.hi; puts 'hi world'; end
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 @@ def beta
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

0 comments on commit 49867f3

Please sign in to comment.
Something went wrong with that request. Please try again.