mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
allow whereami -c
to work outside of a method context
i.e: class Hello def hi; end binding.pry end
This commit is contained in:
parent
af8a21bb4f
commit
4fb8230aca
2 changed files with 103 additions and 82 deletions
|
@ -144,14 +144,21 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# This either returns the `target_self`
|
||||
# or it returns the class of `target_self` if `target_self` is not a class.
|
||||
# @return [Pry::WrappedModule]
|
||||
def target_class
|
||||
target_self.is_a?(Module) ? Pry::WrappedModule(target_self) :
|
||||
Pry::WrappedModule(target_self.class)
|
||||
end
|
||||
|
||||
def class_code
|
||||
return @class_code if @class_code
|
||||
|
||||
if valid_method?
|
||||
mod = Pry::WrappedModule(@method.owner)
|
||||
idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
|
||||
@class_code = idx && Pry::Code.from_module(mod, idx)
|
||||
end
|
||||
mod = @method ? Pry::WrappedModule(@method.owner) : target_class
|
||||
|
||||
idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
|
||||
@class_code = idx && Pry::Code.from_module(mod, idx)
|
||||
end
|
||||
|
||||
def valid_method?
|
||||
|
|
|
@ -73,92 +73,106 @@ describe "whereami" do
|
|||
def blimey!
|
||||
pry_eval(binding, 'whereami')
|
||||
end
|
||||
END
|
||||
END
|
||||
end
|
||||
|
||||
lambda{
|
||||
Cor.instance_method(:blimey!).source
|
||||
}.should.raise(MethodSource::SourceNotFoundError)
|
||||
|
||||
Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
lambda{
|
||||
Cor.instance_method(:blimey!).source
|
||||
}.should.raise(MethodSource::SourceNotFoundError)
|
||||
# Now that we use stagger_output (paging output) we no longer get
|
||||
# the "From: " line, as we output everything in one go (not separate output.puts)
|
||||
# and so the user just gets a single `Error: Cannot open
|
||||
# "not.found.file.erb" for reading.`
|
||||
# which is good enough IMO. Unfortunately we can't test for it
|
||||
# though, as we don't hook stdout.
|
||||
#
|
||||
# it 'should display a description and error if reading the file goes wrong' do
|
||||
# class Cor
|
||||
# def blimey!
|
||||
# eval <<-END, binding, "not.found.file.erb", 7
|
||||
# Pad.tester = pry_tester(binding)
|
||||
# Pad.tester.eval('whereami')
|
||||
# END
|
||||
# end
|
||||
# end
|
||||
|
||||
Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
# proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
|
||||
|
||||
# Now that we use stagger_output (paging output) we no longer get
|
||||
# the "From: " line, as we output everything in one go (not separate output.puts)
|
||||
# and so the user just gets a single `Error: Cannot open
|
||||
# "not.found.file.erb" for reading.`
|
||||
# which is good enough IMO. Unfortunately we can't test for it
|
||||
# though, as we don't hook stdout.
|
||||
#
|
||||
# it 'should display a description and error if reading the file goes wrong' do
|
||||
# class Cor
|
||||
# def blimey!
|
||||
# eval <<-END, binding, "not.found.file.erb", 7
|
||||
# Pad.tester = pry_tester(binding)
|
||||
# Pad.tester.eval('whereami')
|
||||
# END
|
||||
# end
|
||||
# end
|
||||
# Pad.tester.last_output.should =~
|
||||
# /From: not.found.file.erb @ line 7 Cor#blimey!:/
|
||||
# Object.remove_const(:Cor)
|
||||
# end
|
||||
|
||||
# proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
|
||||
it 'should show code window (not just method source) if parameter passed to whereami' do
|
||||
class Cor
|
||||
def blimey!
|
||||
pry_eval(binding, 'whereami 3').should =~ /class Cor/
|
||||
end
|
||||
end
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
# Pad.tester.last_output.should =~
|
||||
# /From: not.found.file.erb @ line 7 Cor#blimey!:/
|
||||
# Object.remove_const(:Cor)
|
||||
# end
|
||||
it 'should show entire method when -m option used' do
|
||||
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
|
||||
old_cutoff, Pry::Command::Whereami.method_size_cutoff = Pry::Command::Whereami.method_size_cutoff, 1
|
||||
class Cor
|
||||
def blimey!
|
||||
1
|
||||
2
|
||||
pry_eval(binding, 'whereami -m').should =~ /def blimey/
|
||||
end
|
||||
end
|
||||
Pry::Command::Whereami.method_size_cutoff, Pry.config.default_window_size = old_cutoff, old_size
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
it 'should show code window (not just method source) if parameter passed to whereami' do
|
||||
class Cor
|
||||
def blimey!
|
||||
pry_eval(binding, 'whereami 3').should =~ /class Cor/
|
||||
it 'should show entire file when -f option used' do
|
||||
class Cor
|
||||
def blimey!
|
||||
1
|
||||
2
|
||||
pry_eval(binding, 'whereami -f').should =~ /show entire file when -f option used/
|
||||
end
|
||||
end
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
describe "-c" do
|
||||
it 'should show class when -c option used, and locate correct candidate' do
|
||||
require 'fixtures/whereami_helper'
|
||||
class Cor
|
||||
def blimey!
|
||||
1
|
||||
2
|
||||
out = pry_eval(binding, 'whereami -c')
|
||||
out.should =~ /class Cor/
|
||||
out.should =~ /blimey/
|
||||
end
|
||||
end
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
it 'should show class when -c option used, and binding is outside a method' do
|
||||
require 'fixtures/whereami_helper'
|
||||
class Cor
|
||||
def blimey;end
|
||||
|
||||
out = pry_eval(binding, 'whereami -c')
|
||||
out.should =~ /class Cor/
|
||||
out.should =~ /blimey/
|
||||
end
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
end
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
it 'should show entire method when -m option used' do
|
||||
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
|
||||
old_cutoff, Pry::Command::Whereami.method_size_cutoff = Pry::Command::Whereami.method_size_cutoff, 1
|
||||
class Cor
|
||||
def blimey!
|
||||
1
|
||||
2
|
||||
pry_eval(binding, 'whereami -m').should =~ /def blimey/
|
||||
end
|
||||
end
|
||||
Pry::Command::Whereami.method_size_cutoff, Pry.config.default_window_size = old_cutoff, old_size
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
it 'should show entire file when -f option used' do
|
||||
class Cor
|
||||
def blimey!
|
||||
1
|
||||
2
|
||||
pry_eval(binding, 'whereami -f').should =~ /show entire file when -f option used/
|
||||
end
|
||||
end
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
it 'should show class when -c option used, and locate correct candidate' do
|
||||
require 'fixtures/whereami_helper'
|
||||
class Cor
|
||||
def blimey!
|
||||
1
|
||||
2
|
||||
out = pry_eval(binding, 'whereami -c')
|
||||
out.should =~ /class Cor/
|
||||
out.should =~ /blimey/
|
||||
end
|
||||
end
|
||||
Cor.new.blimey!
|
||||
Object.remove_const(:Cor)
|
||||
end
|
||||
|
||||
it 'should not show line numbers or marker when -n switch is used' do
|
||||
class Cor
|
||||
|
|
Loading…
Reference in a new issue