From 4fb8230acab8fe4134c1bd9ff03bd8482e60a9f4 Mon Sep 17 00:00:00 2001 From: John Mair Date: Sun, 17 Feb 2013 02:38:16 +0100 Subject: [PATCH] allow `whereami -c` to work outside of a method context i.e: class Hello def hi; end binding.pry end --- lib/pry/commands/whereami.rb | 17 +++- spec/commands/whereami_spec.rb | 168 ++++++++++++++++++--------------- 2 files changed, 103 insertions(+), 82 deletions(-) diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index e9c143e7..61cda7fd 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -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? diff --git a/spec/commands/whereami_spec.rb b/spec/commands/whereami_spec.rb index e13221cf..664211ab 100644 --- a/spec/commands/whereami_spec.rb +++ b/spec/commands/whereami_spec.rb @@ -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