diff --git a/lib/pry/module_candidate.rb b/lib/pry/module_candidate.rb index 1da0968e..2f41e7ce 100644 --- a/lib/pry/module_candidate.rb +++ b/lib/pry/module_candidate.rb @@ -78,7 +78,8 @@ class Pry return nil if !file.is_a?(String) - class_regexes = [/^\s*#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/, + + class_regexes = [/^\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped.name.split(/::/).last}/, /^\s*(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/, /^\s*(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/] diff --git a/spec/code_object_spec.rb b/spec/code_object_spec.rb index 62ed62e0..5af46c63 100644 --- a/spec/code_object_spec.rb +++ b/spec/code_object_spec.rb @@ -43,7 +43,7 @@ describe Pry::CodeObject do @p = Pry.new end - it 'works' do + it 'should return command class' do @p.commands.command "jeremy-jones" do "lobster" end @@ -52,6 +52,38 @@ describe Pry::CodeObject do m.source.should =~ /lobster/ end + describe "class commands" do + before do + class LobsterLady < Pry::ClassCommand + match "lobster-lady" + description "nada." + def process + "lobster" + end + end + end + + after do + Object.remove_const(:LobsterLady) + end + + it 'should return Pry::ClassCommand class when looking up class command' do + Pry.commands.add_command(LobsterLady) + m = Pry::CodeObject.lookup("lobster-lady", binding, @p) + (m <= Pry::ClassCommand).should == true + m.source.should =~ /class LobsterLady/ + Pry.commands.delete("lobster-lady") + end + + it 'should return Pry::WrappedModule when looking up command class directly (as a class, not as a command)' do + Pry.commands.add_command(LobsterLady) + m = Pry::CodeObject.lookup("LobsterLady", binding, @p) + m.is_a?(Pry::WrappedModule).should == true + m.source.should =~ /class LobsterLady/ + Pry.commands.delete("lobster-lady") + end + end + it 'looks up commands by :listing name as well' do @p.commands.command /jeremy-.*/, "", :listing => "jeremy-baby" do "lobster" @@ -215,7 +247,7 @@ describe Pry::CodeObject do o.is_a?(Pry::WrappedModule).should == true end - # actually locals are never looked up (via co.other_object) when they're classes, it + # actually locals are never looked up (via co.default_lookup) when they're classes, it # just falls through to co.method_or_class it 'should look up classes before locals' do c = ClassyWassy diff --git a/spec/commands/show_doc_spec.rb b/spec/commands/show_doc_spec.rb index 4abd0fcb..b47c5213 100644 --- a/spec/commands/show_doc_spec.rb +++ b/spec/commands/show_doc_spec.rb @@ -335,6 +335,35 @@ if !PryTestHelpers.mri18_and_no_real_source_location? @set.command "command with spaces", "description of a command with spaces" do; end pry_eval('show-doc command with spaces').should =~ /description of a command with spaces/ end + + describe "class commands" do + before do + # cute pink pincers + class LobsterLady < Pry::ClassCommand + match "lobster-lady" + description "nada." + def process + "lobster" + end + end + + Pry.commands.add_command(LobsterLady) + end + + after do + Object.remove_const(:LobsterLady) + end + + it 'should display "help" when looking up by command name' do + pry_eval('show-doc lobster-lady').should =~ /nada/ + Pry.commands.delete("lobster-lady") + end + + it 'should display actual preceding comment for a class command, when class is used (rather than command name) when looking up' do + pry_eval('show-doc LobsterLady').should =~ /cute pink pincers/ + Pry.commands.delete("lobster-lady") + end + end end describe "should set _file_ and _dir_" do diff --git a/spec/wrapped_module_spec.rb b/spec/wrapped_module_spec.rb index 3468cdbc..aae0e80b 100644 --- a/spec/wrapped_module_spec.rb +++ b/spec/wrapped_module_spec.rb @@ -23,6 +23,13 @@ describe Pry::WrappedModule do end class ForeverAlone + class DoublyNested + # nested docs + class TriplyNested + def nested_method + end + end + end end end end @@ -83,6 +90,11 @@ describe Pry::WrappedModule do it 'should return source for third ranked candidate' do Pry::WrappedModule(Host::CandidateTest).candidate(2).source.should =~ /test6/ end + + it 'should return source for deeply nested class' do + Pry::WrappedModule(Host::ForeverAlone::DoublyNested::TriplyNested).source.should =~ /nested_method/ + Pry::WrappedModule(Host::ForeverAlone::DoublyNested::TriplyNested).source.lines.count.should == 4 + end end describe "doc" do @@ -102,6 +114,10 @@ describe Pry::WrappedModule do it 'should return doc for third ranked candidate' do Pry::WrappedModule(Host::CandidateTest).candidate(2).doc.should =~ /rank 2/ end + + it 'should return source for deeply nested class' do + Pry::WrappedModule(Host::ForeverAlone::DoublyNested::TriplyNested).doc.should =~ /nested docs/ + end end after do