2011-05-06 06:43:08 -04:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
describe Pry::CommandSet do
|
|
|
|
before do
|
2012-08-11 20:22:29 -04:00
|
|
|
@set = Pry::CommandSet.new do
|
|
|
|
import Pry::Commands
|
|
|
|
end
|
|
|
|
|
2011-12-31 06:10:23 -05:00
|
|
|
@ctx = {
|
|
|
|
:target => binding,
|
|
|
|
:command_set => @set
|
|
|
|
}
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
2013-09-22 05:21:51 -04:00
|
|
|
describe "[]=" do
|
|
|
|
it "removes a command from the command set" do
|
|
|
|
@set["help"].should.not == nil
|
|
|
|
@set["help"] = nil
|
|
|
|
@set["help"].should == nil
|
|
|
|
lambda { @set.run_command(TOPLEVEL_BINDING, "help") }.should.raise Pry::NoCommandError
|
|
|
|
end
|
|
|
|
|
|
|
|
it "replaces a command" do
|
|
|
|
old_help = @set["help"]
|
|
|
|
@set["help"] = @set["pry-version"]
|
|
|
|
@set["help"].should.not == old_help
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rebinds the command with key" do
|
|
|
|
@set["help-1"] = @set["help"]
|
|
|
|
@set["help-1"].match.should == "help-1"
|
|
|
|
end
|
2013-09-22 05:39:10 -04:00
|
|
|
|
|
|
|
it "raises a TypeError when command is not a subclass of Pry::Command" do
|
|
|
|
lambda { @set["help"] = "hello" }.should.raise TypeError
|
|
|
|
end
|
2013-09-22 05:21:51 -04:00
|
|
|
end
|
|
|
|
|
2011-05-06 06:43:08 -04:00
|
|
|
it 'should call the block used for the command when it is called' do
|
|
|
|
run = false
|
|
|
|
@set.command 'foo' do
|
|
|
|
run = true
|
|
|
|
end
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'foo'
|
2011-05-06 06:43:08 -04:00
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should pass arguments of the command to the block' do
|
|
|
|
@set.command 'foo' do |*args|
|
|
|
|
args.should == [1, 2, 3]
|
|
|
|
end
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'foo', 1, 2, 3
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
2011-12-31 06:10:23 -05:00
|
|
|
it 'should use the first argument as context' do
|
2011-09-25 02:52:07 -04:00
|
|
|
ctx = @ctx
|
|
|
|
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command 'foo' do
|
2011-12-31 06:10:23 -05:00
|
|
|
self.context.should == ctx
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'foo'
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
2011-05-23 02:34:11 -04:00
|
|
|
it 'should raise an error when calling an undefined command' do
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command('foo') {}
|
|
|
|
lambda {
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'bar'
|
2011-05-06 06:43:08 -04:00
|
|
|
}.should.raise(Pry::NoCommandError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to remove its own commands' do
|
|
|
|
@set.command('foo') {}
|
|
|
|
@set.delete 'foo'
|
|
|
|
|
|
|
|
lambda {
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'foo'
|
2011-05-06 06:43:08 -04:00
|
|
|
}.should.raise(Pry::NoCommandError)
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:51:17 -05:00
|
|
|
it 'should be able to remove its own commands, by listing name' do
|
|
|
|
@set.command(/^foo1/, 'desc', :listing => 'foo') {}
|
|
|
|
@set.delete 'foo'
|
|
|
|
|
|
|
|
lambda {
|
|
|
|
@set.run_command @ctx, /^foo1/
|
|
|
|
}.should.raise(Pry::NoCommandError)
|
|
|
|
end
|
|
|
|
|
2011-05-06 06:43:08 -04:00
|
|
|
it 'should be able to import some commands from other sets' do
|
|
|
|
run = false
|
|
|
|
|
2011-05-07 01:32:05 -04:00
|
|
|
other_set = Pry::CommandSet.new do
|
2011-05-06 06:43:08 -04:00
|
|
|
command('foo') { run = true }
|
|
|
|
command('bar') {}
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import_from(other_set, 'foo')
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'foo'
|
2011-05-06 06:43:08 -04:00
|
|
|
run.should == true
|
|
|
|
|
|
|
|
lambda {
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'bar'
|
2011-05-06 06:43:08 -04:00
|
|
|
}.should.raise(Pry::NoCommandError)
|
|
|
|
end
|
|
|
|
|
2012-04-02 06:15:27 -04:00
|
|
|
it 'should return command set after import' do
|
|
|
|
run = false
|
|
|
|
|
|
|
|
other_set = Pry::CommandSet.new do
|
|
|
|
command('foo') { run = true }
|
|
|
|
command('bar') {}
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import(other_set).should == @set
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return command set after import_from' do
|
|
|
|
run = false
|
|
|
|
|
|
|
|
other_set = Pry::CommandSet.new do
|
|
|
|
command('foo') { run = true }
|
|
|
|
command('bar') {}
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import_from(other_set, 'foo').should == @set
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:51:17 -05:00
|
|
|
it 'should be able to import some commands from other sets using listing name' do
|
|
|
|
run = false
|
|
|
|
|
|
|
|
other_set = Pry::CommandSet.new do
|
|
|
|
command(/^foo1/, 'desc', :listing => 'foo') { run = true }
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import_from(other_set, 'foo')
|
|
|
|
|
|
|
|
@set.run_command @ctx, /^foo1/
|
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
2011-05-06 06:43:08 -04:00
|
|
|
it 'should be able to import a whole set' do
|
|
|
|
run = []
|
|
|
|
|
2011-05-07 01:32:05 -04:00
|
|
|
other_set = Pry::CommandSet.new do
|
2011-05-06 06:43:08 -04:00
|
|
|
command('foo') { run << true }
|
|
|
|
command('bar') { run << true }
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import other_set
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'foo'
|
|
|
|
@set.run_command @ctx, 'bar'
|
2011-05-06 06:43:08 -04:00
|
|
|
run.should == [true, true]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to import sets at creation' do
|
|
|
|
run = false
|
|
|
|
@set.command('foo') { run = true }
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
Pry::CommandSet.new(@set).run_command @ctx, 'foo'
|
2011-05-06 06:43:08 -04:00
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the descriptions of commands' do
|
|
|
|
@set.command('foo', 'some stuff') {}
|
|
|
|
@set.commands['foo'].description.should == 'some stuff'
|
|
|
|
end
|
|
|
|
|
2012-03-25 07:26:48 -04:00
|
|
|
describe "aliases" do
|
|
|
|
it 'should be able to alias command' do
|
|
|
|
run = false
|
|
|
|
@set.command('foo', 'stuff') { run = true }
|
2011-05-06 06:43:08 -04:00
|
|
|
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.alias_command 'bar', 'foo'
|
2012-04-01 19:00:59 -04:00
|
|
|
@set.commands['bar'].match.should == 'bar'
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.commands['bar'].description.should == 'Alias for `foo`'
|
2011-05-06 06:43:08 -04:00
|
|
|
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.run_command @ctx, 'bar'
|
|
|
|
run.should == true
|
|
|
|
end
|
2011-05-06 06:43:08 -04:00
|
|
|
|
2012-04-12 04:58:59 -04:00
|
|
|
it 'should inherit options from original command' do
|
|
|
|
run = false
|
|
|
|
@set.command('foo', 'stuff', :shellwords => true, :interpolate => false) { run = true }
|
|
|
|
|
|
|
|
@set.alias_command 'bar', 'foo'
|
|
|
|
@set.commands['bar'].options[:shellwords].should == @set.commands['foo'].options[:shellwords]
|
|
|
|
@set.commands['bar'].options[:interpolate].should == @set.commands['foo'].options[:interpolate]
|
|
|
|
|
|
|
|
# however some options should not be inherited
|
|
|
|
@set.commands['bar'].options[:listing].should.not == @set.commands['foo'].options[:listing]
|
|
|
|
@set.commands['bar'].options[:listing].should == "bar"
|
|
|
|
end
|
|
|
|
|
2012-03-25 07:26:48 -04:00
|
|
|
it 'should be able to specify alias\'s description when aliasing' do
|
|
|
|
run = false
|
|
|
|
@set.command('foo', 'stuff') { run = true }
|
2011-11-15 08:51:17 -05:00
|
|
|
|
2012-03-25 11:01:07 -04:00
|
|
|
@set.alias_command 'bar', 'foo', :desc => "tobina"
|
2012-04-01 19:00:59 -04:00
|
|
|
@set.commands['bar'].match.should == 'bar'
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.commands['bar'].description.should == "tobina"
|
2011-11-15 08:51:17 -05:00
|
|
|
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.run_command @ctx, 'bar'
|
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to alias a command by its invocation line" do
|
|
|
|
run = false
|
|
|
|
@set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
|
|
|
|
|
|
|
|
@set.alias_command 'bar', 'foo1'
|
2012-04-01 19:00:59 -04:00
|
|
|
@set.commands['bar'].match.should == 'bar'
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.commands['bar'].description.should == 'Alias for `foo1`'
|
|
|
|
|
|
|
|
@set.run_command @ctx, 'bar'
|
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to specify options when creating alias" do
|
|
|
|
run = false
|
|
|
|
@set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
|
|
|
|
|
2012-03-25 11:01:07 -04:00
|
|
|
@set.alias_command /^b.r/, 'foo1', :listing => "bar"
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.commands[/^b.r/].options[:listing].should == "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should set description to default if description parameter is nil" do
|
|
|
|
run = false
|
|
|
|
@set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
|
|
|
|
|
2012-03-25 11:01:07 -04:00
|
|
|
@set.alias_command "bar", 'foo1'
|
2012-03-25 07:26:48 -04:00
|
|
|
@set.commands["bar"].description.should == "Alias for `foo1`"
|
|
|
|
end
|
2011-11-15 08:51:17 -05:00
|
|
|
end
|
|
|
|
|
2011-11-15 09:09:16 -05:00
|
|
|
it 'should be able to change the descriptions of commands' do
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command('foo', 'bar') {}
|
|
|
|
@set.desc 'foo', 'baz'
|
|
|
|
|
|
|
|
@set.commands['foo'].description.should == 'baz'
|
|
|
|
end
|
|
|
|
|
2011-11-15 09:09:16 -05:00
|
|
|
it 'should get the descriptions of commands' do
|
|
|
|
@set.command('foo', 'bar') {}
|
|
|
|
@set.desc('foo').should == 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should get the descriptions of commands, by listing' do
|
|
|
|
@set.command(/^foo1/, 'bar', :listing => 'foo') {}
|
|
|
|
@set.desc('foo').should == 'bar'
|
|
|
|
end
|
|
|
|
|
2011-12-31 06:50:04 -05:00
|
|
|
it 'should return Pry::Command::VOID_VALUE for commands by default' do
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command('foo') { 3 }
|
2011-12-31 06:50:04 -05:00
|
|
|
@set.run_command(@ctx, 'foo').should == Pry::Command::VOID_VALUE
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to keep return values' do
|
|
|
|
@set.command('foo', '', :keep_retval => true) { 3 }
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo').should == 3
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
2011-09-03 21:30:20 -04:00
|
|
|
it 'should be able to keep return values, even if return value is nil' do
|
|
|
|
@set.command('foo', '', :keep_retval => true) { nil }
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo').should == nil
|
2011-09-03 21:30:20 -04:00
|
|
|
end
|
|
|
|
|
2011-05-06 06:43:08 -04:00
|
|
|
it 'should be able to have its own helpers' do
|
|
|
|
@set.command('foo') do
|
|
|
|
should.respond_to :my_helper
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.helpers do
|
|
|
|
def my_helper; end
|
|
|
|
end
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo')
|
2011-12-31 06:50:04 -05:00
|
|
|
Pry::Command.subclass('foo', '', {}, Module.new).new({:target => binding}).should.not.respond_to :my_helper
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
2011-05-23 02:34:11 -04:00
|
|
|
it 'should not recreate a new helper module when helpers is called' do
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command('foo') do
|
|
|
|
should.respond_to :my_helper
|
|
|
|
should.respond_to :my_other_helper
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.helpers do
|
|
|
|
def my_helper; end
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.helpers do
|
|
|
|
def my_other_helper; end
|
|
|
|
end
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo')
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should import helpers from imported sets' do
|
2011-05-07 01:32:05 -04:00
|
|
|
imported_set = Pry::CommandSet.new do
|
2011-05-06 06:43:08 -04:00
|
|
|
helpers do
|
|
|
|
def imported_helper_method; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import imported_set
|
|
|
|
@set.command('foo') { should.respond_to :imported_helper_method }
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo')
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should import helpers even if only some commands are imported' do
|
2011-05-07 01:32:05 -04:00
|
|
|
imported_set = Pry::CommandSet.new do
|
2011-05-06 06:43:08 -04:00
|
|
|
helpers do
|
|
|
|
def imported_helper_method; end
|
|
|
|
end
|
|
|
|
|
|
|
|
command('bar') {}
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.import_from imported_set, 'bar'
|
|
|
|
@set.command('foo') { should.respond_to :imported_helper_method }
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo')
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
2011-05-28 02:05:41 -04:00
|
|
|
|
|
|
|
it 'should provide a :listing for a command that defaults to its name' do
|
|
|
|
@set.command 'foo', "" do;end
|
|
|
|
@set.commands['foo'].options[:listing].should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should provide a :listing for a command that differs from its name' do
|
|
|
|
@set.command 'foo', "", :listing => 'bar' do;end
|
|
|
|
@set.commands['foo'].options[:listing].should == 'bar'
|
|
|
|
end
|
2011-07-27 14:42:36 -04:00
|
|
|
|
|
|
|
it "should provide a 'help' command" do
|
2011-12-31 06:10:23 -05:00
|
|
|
@ctx[:command_set] = @set
|
|
|
|
@ctx[:output] = StringIO.new
|
2011-07-27 14:42:36 -04:00
|
|
|
|
|
|
|
lambda {
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'help')
|
2011-07-27 14:42:36 -04:00
|
|
|
}.should.not.raise
|
|
|
|
end
|
|
|
|
|
2011-11-14 09:12:19 -05:00
|
|
|
|
2011-11-24 00:27:30 -05:00
|
|
|
describe "renaming a command" do
|
|
|
|
it 'should be able to rename and run a command' do
|
|
|
|
run = false
|
|
|
|
@set.command('foo') { run = true }
|
|
|
|
@set.rename_command('bar', 'foo')
|
|
|
|
@set.run_command(@ctx, 'bar')
|
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
2011-11-24 04:33:37 -05:00
|
|
|
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
|
2011-11-24 00:27:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should make old command name inaccessible' do
|
|
|
|
@set.command('foo') { }
|
|
|
|
@set.rename_command('bar', 'foo')
|
|
|
|
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"
|
|
|
|
@set.command('foo') { }
|
|
|
|
@set.rename_command('bar', 'foo', :description => desc, :listing => listing, :keep_retval => true)
|
|
|
|
@set.commands['bar'].description.should == desc
|
|
|
|
@set.commands['bar'].options[:listing].should == listing
|
|
|
|
@set.commands['bar'].options[:keep_retval].should == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-14 09:12:19 -05:00
|
|
|
describe "command decorators - before_command and after_command" do
|
|
|
|
describe "before_command" do
|
|
|
|
it 'should be called before the original command' do
|
|
|
|
foo = []
|
|
|
|
@set.command('foo') { foo << 1 }
|
|
|
|
@set.before_command('foo') { foo << 2 }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
foo.should == [2, 1]
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:51:17 -05:00
|
|
|
it 'should be called before the original command, using listing name' do
|
|
|
|
foo = []
|
|
|
|
@set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
|
|
|
|
@set.before_command('foo') { foo << 2 }
|
|
|
|
@set.run_command(@ctx, /^foo1/)
|
|
|
|
|
|
|
|
foo.should == [2, 1]
|
|
|
|
end
|
|
|
|
|
2011-11-14 09:12:19 -05:00
|
|
|
it 'should share the context with the original command' do
|
2011-12-31 06:10:23 -05:00
|
|
|
@ctx[:target] = "test target string".__binding__
|
2011-11-14 09:12:19 -05:00
|
|
|
before_val = nil
|
|
|
|
orig_val = nil
|
|
|
|
@set.command('foo') { orig_val = target }
|
|
|
|
@set.before_command('foo') { before_val = target }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
2011-12-31 06:10:23 -05:00
|
|
|
before_val.should == @ctx[:target]
|
|
|
|
orig_val.should == @ctx[:target]
|
2011-11-14 09:12:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should work when applied multiple times' do
|
|
|
|
foo = []
|
|
|
|
@set.command('foo') { foo << 1 }
|
|
|
|
@set.before_command('foo') { foo << 2 }
|
|
|
|
@set.before_command('foo') { foo << 3 }
|
|
|
|
@set.before_command('foo') { foo << 4 }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
foo.should == [4, 3, 2, 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "after_command" do
|
|
|
|
it 'should be called after the original command' do
|
|
|
|
foo = []
|
|
|
|
@set.command('foo') { foo << 1 }
|
|
|
|
@set.after_command('foo') { foo << 2 }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
foo.should == [1, 2]
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:51:17 -05:00
|
|
|
it 'should be called after the original command, using listing name' do
|
|
|
|
foo = []
|
|
|
|
@set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
|
|
|
|
@set.after_command('foo') { foo << 2 }
|
|
|
|
@set.run_command(@ctx, /^foo1/)
|
|
|
|
|
|
|
|
foo.should == [1, 2]
|
|
|
|
end
|
|
|
|
|
2011-11-14 09:12:19 -05:00
|
|
|
it 'should share the context with the original command' do
|
2011-12-31 06:10:23 -05:00
|
|
|
@ctx[:target] = "test target string".__binding__
|
2011-12-29 06:07:49 -05:00
|
|
|
after_val = nil
|
2011-11-14 09:12:19 -05:00
|
|
|
orig_val = nil
|
|
|
|
@set.command('foo') { orig_val = target }
|
|
|
|
@set.after_command('foo') { after_val = target }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
2011-12-31 06:10:23 -05:00
|
|
|
after_val.should == @ctx[:target]
|
|
|
|
orig_val.should == @ctx[:target]
|
2011-11-14 09:12:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should determine the return value for the command' do
|
|
|
|
@set.command('foo', 'bar', :keep_retval => true) { 1 }
|
|
|
|
@set.after_command('foo') { 2 }
|
|
|
|
@set.run_command(@ctx, 'foo').should == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should work when applied multiple times' do
|
|
|
|
foo = []
|
|
|
|
@set.command('foo') { foo << 1 }
|
|
|
|
@set.after_command('foo') { foo << 2 }
|
|
|
|
@set.after_command('foo') { foo << 3 }
|
|
|
|
@set.after_command('foo') { foo << 4 }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
foo.should == [1, 2, 3, 4]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "before_command and after_command" do
|
|
|
|
it 'should work when combining both before_command and after_command' do
|
|
|
|
foo = []
|
|
|
|
@set.command('foo') { foo << 1 }
|
|
|
|
@set.after_command('foo') { foo << 2 }
|
|
|
|
@set.before_command('foo') { foo << 3 }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
foo.should == [3, 1, 2]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
|
|
|
describe 'find_command' do
|
|
|
|
it 'should find commands with the right string' do
|
|
|
|
cmd = @set.command('rincewind'){ }
|
|
|
|
@set.find_command('rincewind').should == cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not find commands with spaces before' do
|
|
|
|
cmd = @set.command('luggage'){ }
|
|
|
|
@set.find_command(' luggage').should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find commands with arguments after' do
|
|
|
|
cmd = @set.command('vetinari'){ }
|
|
|
|
@set.find_command('vetinari --knock 3').should == cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find commands with names containing spaces' do
|
|
|
|
cmd = @set.command('nobby nobbs'){ }
|
|
|
|
@set.find_command('nobby nobbs --steal petty-cash').should == cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find command defined by regex' do
|
|
|
|
cmd = @set.command(/(capt|captain) vimes/i){ }
|
|
|
|
@set.find_command('Capt Vimes').should == cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find commands defined by regex with arguments' do
|
|
|
|
cmd = @set.command(/(cpl|corporal) Carrot/i){ }
|
|
|
|
@set.find_command('cpl carrot --write-home').should == cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not find commands by listing' do
|
|
|
|
cmd = @set.command(/werewol(f|ve)s?/, 'only once a month', :listing => "angua"){ }
|
|
|
|
@set.find_command('angua').should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not find commands without command_prefix' do
|
|
|
|
Pry.config.command_prefix = '%'
|
|
|
|
cmd = @set.command('detritus'){ }
|
|
|
|
@set.find_command('detritus').should == nil
|
|
|
|
Pry.config.command_prefix = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should find commands that don't use the prefix" do
|
|
|
|
Pry.config.command_prefix = '%'
|
|
|
|
cmd = @set.command('colon', 'Sergeant Fred', :use_prefix => false){ }
|
|
|
|
@set.find_command('colon').should == cmd
|
|
|
|
Pry.config.command_prefix = ''
|
|
|
|
end
|
2012-02-11 18:15:22 -05:00
|
|
|
|
|
|
|
it "should find the command that has the longest match" do
|
|
|
|
cmd = @set.command(/\.(.*)/){ }
|
|
|
|
cmd2 = @set.command(/\.\|\|(.*)/){ }
|
|
|
|
@set.find_command('.||').should == cmd2
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should find the command that has the longest name" do
|
|
|
|
cmd = @set.command(/\.(.*)/){ }
|
|
|
|
cmd2 = @set.command('.||'){ }
|
|
|
|
@set.find_command('.||').should == cmd2
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.valid_command?' do
|
|
|
|
it 'should be true for commands that can be found' do
|
|
|
|
cmd = @set.command('archchancellor')
|
|
|
|
@set.valid_command?('archchancellor of_the?(:University)').should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be false for commands that can\'' do
|
|
|
|
@set.valid_command?('def monkey(ape)').should == false
|
|
|
|
end
|
2012-01-02 20:47:15 -05:00
|
|
|
|
|
|
|
it 'should not cause argument interpolation' do
|
|
|
|
cmd = @set.command('hello')
|
|
|
|
lambda {
|
|
|
|
@set.valid_command?('hello #{raise "futz"}')
|
|
|
|
}.should.not.raise
|
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.process_line' do
|
|
|
|
|
|
|
|
it 'should return Result.new(false) if there is no matching command' do
|
|
|
|
result = @set.process_line('1 + 42')
|
|
|
|
result.command?.should == false
|
|
|
|
result.void_command?.should == false
|
|
|
|
result.retval.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return Result.new(true, VOID) if the command is not keep_retval' do
|
2012-01-21 18:17:43 -05:00
|
|
|
@set.create_command('mrs-cake') do
|
2012-01-02 19:04:47 -05:00
|
|
|
def process; 42; end
|
|
|
|
end
|
|
|
|
|
|
|
|
result = @set.process_line('mrs-cake')
|
|
|
|
result.command?.should == true
|
|
|
|
result.void_command?.should == true
|
|
|
|
result.retval.should == Pry::Command::VOID_VALUE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return Result.new(true, retval) if the command is keep_retval' do
|
2012-01-21 18:17:43 -05:00
|
|
|
@set.create_command('magrat', 'the maiden', :keep_retval => true) do
|
2012-01-02 19:04:47 -05:00
|
|
|
def process; 42; end
|
|
|
|
end
|
|
|
|
|
|
|
|
result = @set.process_line('magrat')
|
|
|
|
result.command?.should == true
|
|
|
|
result.void_command?.should == false
|
|
|
|
result.retval.should == 42
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should pass through context' do
|
|
|
|
ctx = {
|
|
|
|
:eval_string => "bloomers",
|
|
|
|
:pry_instance => Object.new,
|
|
|
|
:output => StringIO.new,
|
|
|
|
:target => binding
|
|
|
|
}
|
2012-01-21 18:17:43 -05:00
|
|
|
@set.create_command('agnes') do
|
2012-01-02 19:04:47 -05:00
|
|
|
define_method(:process) do
|
|
|
|
eval_string.should == ctx[:eval_string]
|
|
|
|
output.should == ctx[:output]
|
|
|
|
target.should == ctx[:target]
|
|
|
|
_pry_.should == ctx[:pry_instance]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.process_line('agnes', ctx)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should add command_set to context' do
|
|
|
|
set = @set
|
2012-01-21 18:17:43 -05:00
|
|
|
@set.create_command(/nann+y ogg+/) do
|
2012-01-02 19:04:47 -05:00
|
|
|
define_method(:process) do
|
|
|
|
command_set.should == set
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@set.process_line('nannnnnny oggggg')
|
|
|
|
end
|
|
|
|
end
|
2012-08-21 01:12:03 -04:00
|
|
|
|
2012-09-06 01:39:29 -04:00
|
|
|
if defined?(Bond)
|
|
|
|
describe '.complete' do
|
|
|
|
it "should list all command names" do
|
|
|
|
@set.create_command('susan'){ }
|
2012-11-14 04:52:21 -05:00
|
|
|
@set.complete('sus').should.include 'susan '
|
2012-09-06 01:39:29 -04:00
|
|
|
end
|
2012-08-21 01:12:03 -04:00
|
|
|
|
2012-09-06 01:39:29 -04:00
|
|
|
it "should delegate to commands" do
|
|
|
|
@set.create_command('susan'){ def complete(search); ['--foo']; end }
|
|
|
|
@set.complete('susan ').should == ['--foo']
|
|
|
|
end
|
2012-08-21 01:12:03 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|