2011-05-06 06:43:08 -04:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
describe Pry::CommandSet do
|
|
|
|
before do
|
2011-05-07 01:32:05 -04:00
|
|
|
@set = Pry::CommandSet.new
|
2011-09-25 02:52:07 -04:00
|
|
|
@ctx = Pry::CommandContext.new
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'should use the first argument as self' do
|
2011-09-25 02:52:07 -04:00
|
|
|
ctx = @ctx
|
|
|
|
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command 'foo' do
|
2011-09-25 02:52:07 -04:00
|
|
|
self.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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'should be able to alias method' do
|
|
|
|
run = false
|
|
|
|
@set.command('foo', 'stuff') { run = true }
|
|
|
|
|
|
|
|
@set.alias_command 'bar', 'foo'
|
|
|
|
@set.commands['bar'].name.should == 'bar'
|
2011-09-18 02:36:39 -04:00
|
|
|
@set.commands['bar'].description.should == ''
|
2011-05-06 06:43:08 -04:00
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command @ctx, 'bar'
|
2011-05-06 06:43:08 -04:00
|
|
|
run.should == true
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:51:17 -05:00
|
|
|
it "should be able to alias a method by the command's listing name" do
|
|
|
|
run = false
|
|
|
|
@set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
|
|
|
|
|
|
|
|
@set.alias_command 'bar', 'foo'
|
|
|
|
@set.commands['bar'].name.should == 'bar'
|
|
|
|
@set.commands['bar'].description.should == ''
|
|
|
|
|
|
|
|
@set.run_command @ctx, 'bar'
|
|
|
|
run.should == true
|
|
|
|
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-09-03 21:30:20 -04:00
|
|
|
it 'should return Pry::CommandContext::VOID_VALUE for commands by default' do
|
2011-05-06 06:43:08 -04:00
|
|
|
@set.command('foo') { 3 }
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'foo').should == Pry::CommandContext::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-05-06 06:43:08 -04:00
|
|
|
Pry::CommandContext.new.should.not.respond_to :my_helper
|
|
|
|
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-09-25 02:52:07 -04: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
|
|
|
|
|
|
|
|
it "should sort the output of the 'help' command" do
|
|
|
|
@set.command 'foo', "Fooerizes" do; end
|
|
|
|
@set.command 'goo', "Gooerizes" do; end
|
|
|
|
@set.command 'moo', "Mooerizes" do; end
|
|
|
|
@set.command 'boo', "Booerizes" do; end
|
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@ctx.command_set = @set
|
|
|
|
@ctx.output = StringIO.new
|
2011-07-27 14:42:36 -04:00
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
@set.run_command(@ctx, 'help')
|
2011-07-27 14:42:36 -04:00
|
|
|
|
2011-09-25 02:52:07 -04:00
|
|
|
doc = @ctx.output.string
|
2011-07-27 14:42:36 -04:00
|
|
|
|
|
|
|
order = [doc.index("boo"),
|
|
|
|
doc.index("foo"),
|
|
|
|
doc.index("goo"),
|
|
|
|
doc.index("help"),
|
|
|
|
doc.index("moo")]
|
|
|
|
|
|
|
|
order.should == order.sort
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
@ctx.target = "test target string"
|
|
|
|
before_val = nil
|
|
|
|
orig_val = nil
|
|
|
|
@set.command('foo') { orig_val = target }
|
|
|
|
@set.before_command('foo') { before_val = target }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
before_val.should == @ctx.target
|
|
|
|
orig_val.should == @ctx.target
|
|
|
|
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
|
|
|
|
@ctx.target = "test target string"
|
|
|
|
after_val = nil
|
|
|
|
orig_val = nil
|
|
|
|
@set.command('foo') { orig_val = target }
|
|
|
|
@set.after_command('foo') { after_val = target }
|
|
|
|
@set.run_command(@ctx, 'foo')
|
|
|
|
|
|
|
|
after_val.should == @ctx.target
|
|
|
|
orig_val.should == @ctx.target
|
|
|
|
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
|
2011-05-06 06:43:08 -04:00
|
|
|
end
|