Permalink
Browse files

allow `whereami -c` to work outside of a method context

i.e:
class Hello
  def hi; end
  binding.pry
end
  • Loading branch information...
1 parent af8a21b commit 4fb8230acab8fe4134c1bd9ff03bd8482e60a9f4 @banister banister committed Feb 17, 2013
Showing with 98 additions and 77 deletions.
  1. +12 −5 lib/pry/commands/whereami.rb
  2. +86 −72 spec/commands/whereami_spec.rb
@@ -144,14 +144,21 @@ def method_code
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 @@ class Cor
def blimey!
pry_eval(binding, 'whereami')
end
- END
- end
+ END
+ end
- lambda{
- Cor.instance_method(:blimey!).source
- }.should.raise(MethodSource::SourceNotFoundError)
+ 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
+ Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
+ Object.remove_const(:Cor)
+ end
- # 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
-
- # proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
-
- # Pad.tester.last_output.should =~
- # /From: not.found.file.erb @ line 7 Cor#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/
+ # 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
+
+ # proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
+
+ # Pad.tester.last_output.should =~
+ # /From: not.found.file.erb @ line 7 Cor#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/
+ end
end
+ Cor.new.blimey!
+ Object.remove_const(:Cor)
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/
+ 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
- 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/
+ 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
- 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/
+ 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 not show line numbers or marker when -n switch is used' do
class Cor

0 comments on commit 4fb8230

Please sign in to comment.