1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

updated rename_command() to accept either actual command name or listing name, tests also updated

This commit is contained in:
John Mair 2011-11-24 22:33:37 +13:00
parent 54df4bf7bd
commit 830e6c8f12
2 changed files with 20 additions and 11 deletions

View file

@ -242,8 +242,9 @@ class Pry
commands[new_name].description = desc
end
# Rename a command. You must use the actual name of the command,
# not the listing name.
# Rename a command. Accepts either actual name or listing name for
# the `old_name`.
# `new_name` must be the actual name of the new command.
# @param [String, Regexp] new_name The new name for the command.
# @param [String, Regexp] old_name The command's current name.
# @param [Hash] options The optional configuration parameters,
@ -252,18 +253,18 @@ class Pry
# @example Renaming the `ls` command and changing its description.
# Pry.config.commands.rename "dir", "ls", :description => "DOS friendly ls"
def rename_command(new_name, old_name, options={})
raise ArgumentError, "A command called '#{old_name}' was not found!" if !commands[old_name]
cmd = find_command_by_name_or_listing(old_name)
options = {
:listing => new_name,
:description => commands[old_name].description
:description => cmd.description
}.merge!(options)
commands[new_name] = commands[old_name]
commands[new_name] = cmd.dup
commands[new_name].name = new_name
commands[new_name].description = options.delete(:description)
commands[new_name].options.merge!(options)
commands.delete(old_name)
commands.delete(cmd.name)
end
# Runs a command.

View file

@ -280,9 +280,16 @@ describe Pry::CommandSet do
run.should == true
end
it 'should not accept listing name when renaming a command' do
@set.command('foo', "", :listing => 'love') { }
lambda { @set.rename_command('bar', 'love') }.should.raise ArgumentError
it 'should accept listing name when renaming a command' do
run = false
@set.command('foo', "", :listing => 'love') { run = true }
@set.rename_command('bar', 'love')
@set.run_command(@ctx, 'bar')
run.should == true
end
it 'should raise exception trying to rename non-existent command' do
lambda { @set.rename_command('bar', 'foo') }.should.raise ArgumentError
end
it 'should make old command name inaccessible' do
@ -291,6 +298,7 @@ describe Pry::CommandSet do
lambda { @set.run_command(@ctx, 'foo') }.should.raise Pry::NoCommandError
end
it 'should be able to pass in options when renaming command' do
desc = "hello"
listing = "bing"