Permalink
Browse files

show-source: support create_command based commands

  • Loading branch information...
banister committed Jan 28, 2013
1 parent a99c974 commit 4afb8d34a1e2b20eb1022c822d85d9170d83c9e9
Showing with 83 additions and 17 deletions.
  1. +19 −4 lib/pry/command.rb
  2. +64 −13 spec/commands/show_source_spec.rb
View
@@ -520,26 +520,41 @@ def inherited(klass)
end
def source
- Pry::WrappedModule(self).source
+ source_object.source
end
def doc
new.help
end
def source_location
- Pry::WrappedModule(self).source_location
+ source_object.source_location
end
def source_file
- Pry::WrappedModule(self).source_file
+ source_object.source_file
end
alias_method :file, :source_file
def source_line
- Pry::WrappedModule(self).source_line
+ source_object.source_line
end
alias_method :line, :source_line
+
+ private
+
+ # The object used to extract the source for the command.
+ #
+ # This should be a `Pry::Method(block)` for a command made with `create_command`
+ # and a `Pry::WrappedModule(self)` for a command that's a standard class.
+ # @return [Pry::WrappedModule, Pry::Method]
+ def source_object
+ @source_object ||= if name =~ /^[A-Z]/
+ Pry::WrappedModule(self)
+ else
+ Pry::Method(block)
+ end
+ end
end
attr_accessor :opts
@@ -560,28 +560,79 @@ def e; end
Pry.config.commands = @oldset
end
- it 'should show source for an ordinary command' do
- @set.command "foo", :body_of_foo do; end
+ describe "block commands" do
+ it 'should show source for an ordinary command' do
+ @set.command "foo", :body_of_foo do; end
- pry_eval('show-source foo').should =~ /:body_of_foo/
- end
+ pry_eval('show-source foo').should =~ /:body_of_foo/
+ end
+
+ it "should output source of commands using special characters" do
+ @set.command "!%$", "I gots the yellow fever" do; end
+
+ pry_eval('show-source !%$').should =~ /yellow fever/
+ end
+
+ it 'should show source for a command with spaces in its name' do
+ @set.command "foo bar", :body_of_foo_bar do; end
- it "should output source of commands using special characters" do
- @set.command "!%$", "I gots the yellow fever" do; end
+ pry_eval('show-source foo bar').should =~ /:body_of_foo_bar/
+ end
+
+ it 'should show source for a command by listing name' do
+ @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end
- pry_eval('show-source !%$').should =~ /yellow fever/
+ pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/
+ end
end
- it 'should show source for a command with spaces in its name' do
- @set.command "foo bar", :body_of_foo_bar do; end
+ describe "create_command commands" do
+ it 'should show source for a command' do
+ @set.create_command "foo", "babble" do
+ def process() :body_of_foo end
+ end
+ pry_eval('show-source foo').should =~ /:body_of_foo/
+ end
- pry_eval('show-source foo bar').should =~ /:body_of_foo_bar/
+ it 'should show source for a command defined inside pry' do
+ pry_eval %{
+ _pry_.commands.create_command "foo", "babble" do
+ def process() :body_of_foo end
+ end
+ }
+ pry_eval('show-source foo').should =~ /:body_of_foo/
+ end
end
- it 'should show source for a command by listing name' do
- @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end
+ describe "real class-based commands" do
+ before do
+ class ::TemporaryCommand < Pry::ClassCommand
+ match 'temp-command'
+ def process() :body_of_temp end
+ end
+
+ Pry.commands.add_command(::TemporaryCommand)
+ end
+
+ after do
+ Object.remove_const(:TemporaryCommand)
+ end
+
+ it 'should show source for a command' do
+ pry_eval('show-source temp-command').should =~ /:body_of_temp/
+ end
- pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/
+ it 'should show source for a command defined inside pry' do
+ pry_eval %{
+ class ::TemporaryCommandInPry < Pry::ClassCommand
+ match 'temp-command-in-pry'
+ def process() :body_of_temp end
+ end
+ }
+ Pry.commands.add_command(::TemporaryCommandInPry)
+ pry_eval('show-source temp-command-in-pry').should =~ /:body_of_temp/
+ Object.remove_const(:TemporaryCommandInPry)
+ end
end
end

0 comments on commit 4afb8d3

Please sign in to comment.