show-source dependency on ruby18_source_location

* necessary to bring new show-source/show-doc functionality to ruby 1.8
* pry now also auto-requires ruby18_source_location if it's available
This commit is contained in:
John Mair 2012-04-18 18:18:33 +12:00
parent 011365f9df
commit 14b7eb5c65
6 changed files with 434 additions and 600 deletions

View File

@ -159,6 +159,13 @@ class Pry
end
end
if Pry::Helpers::BaseHelpers.mri_18?
begin
require 'ruby18_source_location'
rescue LoadError
end
end
require "method_source"
require 'shellwords'
require "readline"

View File

@ -45,6 +45,7 @@ class Pry
create_command "show-doc", "Show the comments above METH. Aliases: \?", :shellwords => false do
include ModuleIntrospectionHelpers
include Helpers::DocumentationHelpers
extend Helpers::BaseHelpers
banner <<-BANNER
Usage: show-doc [OPTIONS] [METH]
@ -56,6 +57,12 @@ class Pry
e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
BANNER
options :requires_gem => "ruby18_source_location" if mri_18?
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opt)
method_options(opt)
opt.on :l, "line-numbers", "Show line numbers."
@ -129,7 +136,7 @@ class Pry
if use_line_numbers?
doc = Code.new(doc, start_line, :text).
with_line_numbers(true)
with_line_numbers(true).to_s
end
doc
@ -183,6 +190,7 @@ class Pry
create_command "show-source" do
include ModuleIntrospectionHelpers
extend Helpers::BaseHelpers
description "Show the source for METH or CLASS. Aliases: $, show-method"
@ -201,9 +209,12 @@ class Pry
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
BANNER
command_options(
:shellwords => false
)
options :shellwords => false
options :requires_gem => "ruby18_source_location" if mri_18?
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opt)
method_options(opt)
@ -241,7 +252,7 @@ class Pry
set_file_and_dir_locals(file_name)
code = ""
code << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n\n"
code << Code.from_module(mod, module_start_line(mod)).with_line_numbers(use_line_numbers?)
code << Code.from_module(mod, module_start_line(mod)).with_line_numbers(use_line_numbers?).to_s
end
def all_modules
@ -309,7 +320,7 @@ class Pry
output.puts make_header(block)
output.puts
code = Code.from_method(block).with_line_numbers(opts.present?(:'line-numbers'))
code = Code.from_method(block).with_line_numbers(opts.present?(:'line-numbers')).to_s
render_output(code, opts)
else

View File

@ -36,7 +36,10 @@ ensure
end
end
# **DO NOT CHANGE THIS COMMENT, IT IS USED IN TESTS**
def mri18_and_no_real_source_location?
Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method)
end
# used by test_show_source.rb and test_documentation.rb
class TestClassForShowSource
def alpha

View File

@ -1,181 +1,186 @@
require 'helper'
describe "Pry::DefaultCommands::Documentation" do
describe "show-doc" do
it 'should output a method\'s documentation' do
redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output=StringIO.new) do
pry
end
str_output.string.should =~ /sample doc/
end
it 'should output a method\'s documentation with line numbers' do
redirect_pry_io(InputTester.new("show-doc sample_method -l", "exit-all"), str_output=StringIO.new) do
pry
end
str_output.string.should =~ /\d: sample doc/
end
it 'should output a method\'s documentation with line numbers (base one)' do
redirect_pry_io(InputTester.new("show-doc sample_method -b", "exit-all"), str_output=StringIO.new) do
pry
end
str_output.string.should =~ /1: sample doc/
end
it 'should output a method\'s documentation if inside method without needing to use method name' do
o = Object.new
# sample comment
def o.sample
redirect_pry_io(InputTester.new("show-doc", "exit-all"), $out=StringIO.new) do
binding.pry
end
end
o.sample
$out.string.should =~ /sample comment/
$out = nil
end
it "should be able to find super methods" do
c = Class.new{
# classy initialize!
def initialize(*args); end
}
d = Class.new(c){
# grungy initialize??
def initialize(*args, &block); end
}
o = d.new
# instancey initialize!
def o.initialize; end
mock_pry(binding, "show-doc o.initialize").should =~ /instancey initialize/
mock_pry(binding, "show-doc --super o.initialize").should =~ /grungy initialize/
mock_pry(binding, "show-doc o.initialize -ss").should =~ /classy initialize/
mock_pry(binding, "show-doc --super o.initialize -ss").should == mock_pry("show-doc Object#initialize")
end
end
describe "on modules" do
before do
# god this is boring1
class ShowSourceTestClass
def alpha
if !mri18_and_no_real_source_location?
describe "Pry::DefaultCommands::Documentation" do
describe "show-doc" do
it 'should output a method\'s documentation' do
redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output=StringIO.new) do
pry
end
str_output.string.should =~ /sample doc/
end
# god this is boring2
module ShowSourceTestModule
def alpha
it 'should output a method\'s documentation with line numbers' do
redirect_pry_io(InputTester.new("show-doc sample_method -l", "exit-all"), str_output=StringIO.new) do
pry
end
str_output.string.should =~ /\d: sample doc/
end
# god this is boring3
ShowSourceTestClassWeirdSyntax = Class.new do
def beta
it 'should output a method\'s documentation with line numbers (base one)' do
redirect_pry_io(InputTester.new("show-doc sample_method -b", "exit-all"), str_output=StringIO.new) do
pry
end
str_output.string.should =~ /1: sample doc/
end
# god this is boring4
ShowSourceTestModuleWeirdSyntax = Module.new do
def beta
it 'should output a method\'s documentation if inside method without needing to use method name' do
o = Object.new
# sample comment
def o.sample
redirect_pry_io(InputTester.new("show-doc", "exit-all"), $out=StringIO.new) do
binding.pry
end
end
o.sample
$out.string.should =~ /sample comment/
$out = nil
end
it "should be able to find super methods" do
c = Class.new{
# classy initialize!
def initialize(*args); end
}
d = Class.new(c){
# grungy initialize??
def initialize(*args, &block); end
}
o = d.new
# instancey initialize!
def o.initialize; end
mock_pry(binding, "show-doc o.initialize").should =~ /instancey initialize/
mock_pry(binding, "show-doc --super o.initialize").should =~ /grungy initialize/
mock_pry(binding, "show-doc o.initialize -ss").should =~ /classy initialize/
mock_pry(binding, "show-doc --super o.initialize -ss").should == mock_pry("show-doc Object#initialize")
end
end
after do
Object.remove_const :ShowSourceTestClass
Object.remove_const :ShowSourceTestClassWeirdSyntax
Object.remove_const :ShowSourceTestModule
Object.remove_const :ShowSourceTestModuleWeirdSyntax
end
describe "basic functionality, should show docs for top-level module definitions" do
it 'should show docs for a class' do
mock_pry("show-doc ShowSourceTestClass").should =~ /god this is boring1/
end
it 'should show docs for a module' do
mock_pry("show-doc ShowSourceTestModule").should =~ /god this is boring2/
end
it 'should show docs for a class when Const = Class.new syntax is used' do
mock_pry("show-doc ShowSourceTestClassWeirdSyntax").should =~ /god this is boring3/
end
it 'should show docs for a module when Const = Module.new syntax is used' do
mock_pry("show-doc ShowSourceTestModuleWeirdSyntax").should =~ /god this is boring4/
end
end
describe "in REPL" do
it 'should find class defined in repl' do
mock_pry("# hello tobina", "class TobinaMyDog", "def woof", "end", "end", "show-doc TobinaMyDog").should =~ /hello tobina/
Object.remove_const :TobinaMyDog
end
end
it 'should lookup module name with respect to current context' do
constant_scope(:AlphaClass, :BetaClass) do
# top-level beta
class BetaClass
describe "on modules" do
before do
# god this is boring1
class ShowSourceTestClass
def alpha
end
end
class AlphaClass
# nested beta
class BetaClass
def beta
end
# god this is boring2
module ShowSourceTestModule
def alpha
end
end
redirect_pry_io(InputTester.new("show-doc BetaClass", "exit-all"), out=StringIO.new) do
AlphaClass.pry
end
out.string.should =~ /nested beta/
end
end
it 'should lookup nested modules' do
constant_scope(:AlphaClass) do
class AlphaClass
# nested beta
class BetaClass
def beta
end
end
end
mock_pry("show-doc AlphaClass::BetaClass").should =~ /nested beta/
end
end
describe "show-doc -a" do
it 'should show the docs for all monkeypatches defined in different files' do
# local monkeypatch
class TestClassForShowSource
# god this is boring3
ShowSourceTestClassWeirdSyntax = Class.new do
def beta
end
end
mock_pry("show-doc TestClassForShowSource -a").should =~ /used by.*?local monkeypatch/m
# god this is boring4
ShowSourceTestModuleWeirdSyntax = Module.new do
def beta
end
end
end
after do
Object.remove_const :ShowSourceTestClass
Object.remove_const :ShowSourceTestClassWeirdSyntax
Object.remove_const :ShowSourceTestModule
Object.remove_const :ShowSourceTestModuleWeirdSyntax
end
describe "basic functionality, should show docs for top-level module definitions" do
it 'should show docs for a class' do
mock_pry("show-doc ShowSourceTestClass").should =~ /god this is boring1/
end
it 'should show docs for a module' do
mock_pry("show-doc ShowSourceTestModule").should =~ /god this is boring2/
end
it 'should show docs for a class when Const = Class.new syntax is used' do
mock_pry("show-doc ShowSourceTestClassWeirdSyntax").should =~ /god this is boring3/
end
it 'should show docs for a module when Const = Module.new syntax is used' do
mock_pry("show-doc ShowSourceTestModuleWeirdSyntax").should =~ /god this is boring4/
end
end
if !Pry::Helpers::BaseHelpers.mri_18?
describe "in REPL" do
it 'should find class defined in repl' do
mock_pry("# hello tobina", "class TobinaMyDog", "def woof", "end", "end", "show-doc TobinaMyDog").should =~ /hello tobina/
Object.remove_const :TobinaMyDog
end
end
end
it 'should lookup module name with respect to current context' do
constant_scope(:AlphaClass, :BetaClass) do
# top-level beta
class BetaClass
def alpha
end
end
class AlphaClass
# nested beta
class BetaClass
def beta
end
end
end
redirect_pry_io(InputTester.new("show-doc BetaClass", "exit-all"), out=StringIO.new) do
AlphaClass.pry
end
out.string.should =~ /nested beta/
end
end
it 'should lookup nested modules' do
constant_scope(:AlphaClass) do
class AlphaClass
# nested beta
class BetaClass
def beta
end
end
end
mock_pry("show-doc AlphaClass::BetaClass").should =~ /nested beta/
end
end
describe "show-doc -a" do
it 'should show the docs for all monkeypatches defined in different files' do
# local monkeypatch
class TestClassForShowSource
def beta
end
end
result = mock_pry("show-doc TestClassForShowSource -a")
result.should =~ /used by/
result.should =~ /local monkeypatch/
end
end
end
end
end

View File

@ -261,203 +261,6 @@ describe "Pry::DefaultCommands::Introspection" do
end
end
describe "show-source" do
it 'should output a method\'s source' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), str_output) do
pry
end
str_output.string.should =~ /def sample/
end
it 'should output help' do
mock_pry('show-source -h').should =~ /Usage: show-source/
end
it 'should output a method\'s source with line numbers' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), str_output) do
pry
end
str_output.string.should =~ /\d+: def sample/
end
it 'should output a method\'s source with line numbers starting at 1' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), str_output) do
pry
end
str_output.string.should =~ /1: def sample/
end
it 'should output a method\'s source if inside method without needing to use method name' do
$str_output = StringIO.new
o = Object.new
def o.sample
redirect_pry_io(InputTester.new("show-source", "exit-all"), $str_output) do
binding.pry
end
end
o.sample
$str_output.string.should =~ /def o.sample/
$str_output = nil
end
it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do
$str_output = StringIO.new
o = Object.new
def o.sample
redirect_pry_io(InputTester.new("show-source -l", "exit-all"), $str_output) do
binding.pry
end
end
o.sample
$str_output.string.should =~ /\d+: def o.sample/
$str_output = nil
end
it "should find methods even if there are spaces in the arguments" do
o = Object.new
def o.foo(*bars);
"Mr flibble"
self;
end
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source o.foo('bar', 'baz bam').foo", "exit-all"), str_output) do
binding.pry
end
str_output.string.should =~ /Mr flibble/
end
it "should find methods even if the object has an overridden method method" do
c = Class.new{
def method;
98
end
}
mock_pry(binding, "show-source c.new.method").should =~ /98/
end
it "should find instance_methods even if the class has an override instance_method method" do
c = Class.new{
def method;
98
end
def self.instance_method; 789; end
}
mock_pry(binding, "show-source c#method").should =~ /98/
end
it "should find instance methods with -M" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(binding, "cd c","show-source -M moo").should =~ /ve over/
end
it "should not find instance methods with -m" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -m moo").should =~ /could not be found/
end
it "should find normal methods with -m" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -m moo").should =~ /ve over/
end
it "should not find normal methods with -M" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -M moo").should =~ /could not be found/
end
it "should find normal methods with no -M or -m" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source moo").should =~ /ve over/
end
it "should find instance methods with no -M or -m" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(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
mock_pry(binding, "show-source --super o.foo").should =~ /:super_wibble/
end
it "should not raise an exception when a non-extant super method is requested" do
o = Object.new
def o.foo(*bars); end
mock_pry(binding, "show-source --super o.foo").should =~ /'self.foo' has no super method/
end
# dynamically defined method source retrieval is only supported in
# 1.9 - where Method#source_location is native
if RUBY_VERSION =~ /1.9/
it 'should output a method\'s source for a method defined inside pry' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-source dyna_method"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def dyna_method/
Object.remove_method :dyna_method
end
it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-source dyna_method"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def dyna_method/
Object.remove_method :dyna_method
end
it 'should output an instance method\'s source for a method defined inside pry' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-source A#yo"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def yo/
Object.remove_const :A
end
it 'should output an instance method\'s source for a method defined inside pry using define_method' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "show-source A#yup"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /define_method\(:yup\)/
Object.remove_const :A
end
end
end
describe "edit-method" do
describe "on a method defined in a file" do
before do

View File

@ -1,301 +1,306 @@
require 'helper'
describe "show-source" do
it 'should output a method\'s source' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), str_output) do
pry
if !mri18_and_no_real_source_location?
describe "show-source" do
it 'should output a method\'s source' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), str_output) do
pry
end
str_output.string.should =~ /def sample/
end
str_output.string.should =~ /def sample/
end
it 'should output help' do
mock_pry('show-source -h').should =~ /Usage: show-source/
end
it 'should output a method\'s source with line numbers' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), str_output) do
pry
it 'should output help' do
mock_pry('show-source -h').should =~ /Usage: show-source/
end
str_output.string.should =~ /\d+: def sample/
end
it 'should output a method\'s source with line numbers' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), str_output) do
pry
end
it 'should output a method\'s source with line numbers starting at 1' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), str_output) do
pry
str_output.string.should =~ /\d+: def sample/
end
str_output.string.should =~ /1: def sample/
end
it 'should output a method\'s source with line numbers starting at 1' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), str_output) do
pry
end
it 'should output a method\'s source if inside method without needing to use method name' do
$str_output = StringIO.new
str_output.string.should =~ /1: def sample/
end
o = Object.new
def o.sample
redirect_pry_io(InputTester.new("show-source", "exit-all"), $str_output) do
it 'should output a method\'s source if inside method without needing to use method name' do
$str_output = StringIO.new
o = Object.new
def o.sample
redirect_pry_io(InputTester.new("show-source", "exit-all"), $str_output) do
binding.pry
end
end
o.sample
$str_output.string.should =~ /def o.sample/
$str_output = nil
end
it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do
$str_output = StringIO.new
o = Object.new
def o.sample
redirect_pry_io(InputTester.new("show-source -l", "exit-all"), $str_output) do
binding.pry
end
end
o.sample
$str_output.string.should =~ /\d+: def o.sample/
$str_output = nil
end
it "should find methods even if there are spaces in the arguments" do
o = Object.new
def o.foo(*bars);
"Mr flibble"
self;
end
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source o.foo('bar', 'baz bam').foo", "exit-all"), str_output) do
binding.pry
end
end
o.sample
$str_output.string.should =~ /def o.sample/
$str_output = nil
end
it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do
$str_output = StringIO.new
o = Object.new
def o.sample
redirect_pry_io(InputTester.new("show-source -l", "exit-all"), $str_output) do
binding.pry
end
end
o.sample
$str_output.string.should =~ /\d+: def o.sample/
$str_output = nil
end
it "should find methods even if there are spaces in the arguments" do
o = Object.new
def o.foo(*bars);
"Mr flibble"
self;
str_output.string.should =~ /Mr flibble/
end
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-source o.foo('bar', 'baz bam').foo", "exit-all"), str_output) do
binding.pry
end
str_output.string.should =~ /Mr flibble/
end
it "should find methods even if the object has an overridden method method" do
c = Class.new{
def method;
98
end
}
it "should find methods even if the object has an overridden method method" do
c = Class.new{
def method;
98
end
}
mock_pry(binding, "show-source c.new.method").should =~ /98/
end
it "should find instance_methods even if the class has an override instance_method method" do
c = Class.new{
def method;
98
end
def self.instance_method; 789; end
}
mock_pry(binding, "show-source c#method").should =~ /98/
end
it "should find instance methods with -M" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(binding, "cd c","show-source -M moo").should =~ /ve over/
end
it "should not find instance methods with -m" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -m moo").should =~ /could not be found/
end
it "should find normal methods with -m" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -m moo").should =~ /ve over/
end
it "should not find normal methods with -M" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -M moo").should =~ /could not be found/
end
it "should find normal methods with no -M or -m" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source moo").should =~ /ve over/
end
it "should find instance methods with no -M or -m" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(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
mock_pry(binding, "show-source c.new.method").should =~ /98/
end
mock_pry(binding, "show-source --super o.foo").should =~ /:super_wibble/
end
it "should find instance_methods even if the class has an override instance_method method" do
c = Class.new{
def method;
98
end
it "should not raise an exception when a non-extant super method is requested" do
o = Object.new
def o.foo(*bars); end
def self.instance_method; 789; end
}
mock_pry(binding, "show-source --super o.foo").should =~ /'self.foo' has no super method/
end
# dynamically defined method source retrieval is only supported in
# 1.9 - where Method#source_location is native
if RUBY_VERSION =~ /1.9/
it 'should output a method\'s source for a method defined inside pry' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-source dyna_method"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def dyna_method/
Object.remove_method :dyna_method
mock_pry(binding, "show-source c#method").should =~ /98/
end
it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-source dyna_method"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def dyna_method/
Object.remove_method :dyna_method
it "should find instance methods with -M" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(binding, "cd c","show-source -M moo").should =~ /ve over/
end
it 'should output an instance method\'s source for a method defined inside pry' do
str_output = StringIO.new
Object.remove_const :A if defined?(A)
redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-source A#yo"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def yo/
Object.remove_const :A
it "should not find instance methods with -m" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -m moo").should =~ /could not be found/
end
it 'should output an instance method\'s source for a method defined inside pry using define_method' do
str_output = StringIO.new
Object.remove_const :A if defined?(A)
redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "show-source A#yup"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /define_method\(:yup\)/
Object.remove_const :A
it "should find normal methods with -m" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -m moo").should =~ /ve over/
end
end
describe "on modules" do
before do
class ShowSourceTestClass
def alpha
it "should not find normal methods with -M" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source -M moo").should =~ /could not be found/
end
it "should find normal methods with no -M or -m" do
c = Class.new{ def self.moo; "ve over!"; end }
mock_pry(binding, "cd c", "show-source moo").should =~ /ve over/
end
it "should find instance methods with no -M or -m" do
c = Class.new{ def moo; "ve over!"; end }
mock_pry(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
module ShowSourceTestModule
def alpha
mock_pry(binding, "show-source --super o.foo").should =~ /:super_wibble/
end
it "should not raise an exception when a non-extant super method is requested" do
o = Object.new
def o.foo(*bars); end
mock_pry(binding, "show-source --super o.foo").should =~ /'self.foo' has no super method/
end
# dynamically defined method source retrieval is only supported in
# 1.9 - where Method#source_location is native
if RUBY_VERSION =~ /1.9/
it 'should output a method\'s source for a method defined inside pry' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-source dyna_method"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def dyna_method/
Object.remove_method :dyna_method
end
ShowSourceTestClassWeirdSyntax = Class.new do
def beta
it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-source dyna_method"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def dyna_method/
Object.remove_method :dyna_method
end
ShowSourceTestModuleWeirdSyntax = Module.new do
def beta
it 'should output an instance method\'s source for a method defined inside pry' do
str_output = StringIO.new
Object.remove_const :A if defined?(A)
redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-source A#yo"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /def yo/
Object.remove_const :A
end
it 'should output an instance method\'s source for a method defined inside pry using define_method' do
str_output = StringIO.new
Object.remove_const :A if defined?(A)
redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "show-source A#yup"), str_output) do
TOPLEVEL_BINDING.pry
end
str_output.string.should =~ /define_method\(:yup\)/
Object.remove_const :A
end
end
after do
Object.remove_const :ShowSourceTestClass
Object.remove_const :ShowSourceTestClassWeirdSyntax
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
end
it 'should show source for a module' do
mock_pry("show-source ShowSourceTestModule").should =~ /module ShowSourceTestModule/
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 module when Const = Module.new syntax is used' do
mock_pry("show-source ShowSourceTestModuleWeirdSyntax").should =~ /ShowSourceTestModuleWeirdSyntax = Module.new/
end
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
end
end
it 'should lookup module name with respect to current context' do
constant_scope(:AlphaClass, :BetaClass) do
class BetaClass
describe "on modules" do
before do
class ShowSourceTestClass
def alpha
end
end
class AlphaClass
class BetaClass
def beta
end
module ShowSourceTestModule
def alpha
end
end
redirect_pry_io(InputTester.new("show-source BetaClass", "exit-all"), out=StringIO.new) do
AlphaClass.pry
end
out.string.should =~ /def beta/
end
end
it 'should lookup nested modules' do
constant_scope(:AlphaClass) do
class AlphaClass
class BetaClass
def beta
end
end
end
mock_pry("show-source AlphaClass::BetaClass").should =~ /class BetaClass/
end
end
describe "show-source -a" do
it 'should show the source for all monkeypatches defined in different files' do
class TestClassForShowSource
ShowSourceTestClassWeirdSyntax = Class.new do
def beta
end
end
mock_pry("show-source TestClassForShowSource -a").should =~ /def alpha.*?def beta/m
ShowSourceTestModuleWeirdSyntax = Module.new do
def beta
end
end
end
after do
Object.remove_const :ShowSourceTestClass
Object.remove_const :ShowSourceTestClassWeirdSyntax
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
end
it 'should show source for a module' do
mock_pry("show-source ShowSourceTestModule").should =~ /module ShowSourceTestModule/
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 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?
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
end
end
end
it 'should lookup module name with respect to current context' do
constant_scope(:AlphaClass, :BetaClass) do
class BetaClass
def alpha
end
end
class AlphaClass
class BetaClass
def beta
end
end
end
redirect_pry_io(InputTester.new("show-source BetaClass", "exit-all"), out=StringIO.new) do
AlphaClass.pry
end
out.string.should =~ /def beta/
end
end
it 'should lookup nested modules' do
constant_scope(:AlphaClass) do
class AlphaClass
class BetaClass
def beta
end
end
end
mock_pry("show-source AlphaClass::BetaClass").should =~ /class BetaClass/
end
end
describe "show-source -a" do
it 'should show the source for all monkeypatches defined in different files' do
class TestClassForShowSource
def beta
end
end
result = mock_pry("show-source TestClassForShowSource -a")
result.should =~ /def alpha/
result.should =~ /def beta/
end
end
end
end
end