2011-05-23 02:34:11 -04:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
describe "Pry::CommandProcessor" do
|
|
|
|
before do
|
|
|
|
@pry = Pry.new
|
|
|
|
@pry.commands = Pry::CommandSet.new
|
|
|
|
@command_processor = Pry::CommandProcessor.new(@pry)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@pry.commands = Pry::CommandSet.new
|
|
|
|
end
|
|
|
|
|
2011-06-08 10:44:51 -04:00
|
|
|
it 'should accurately determine if a command is valid' do
|
|
|
|
@pry.commands.command("test-command") {}
|
|
|
|
valid = @command_processor.valid_command? "test-command"
|
|
|
|
valid.should == true
|
|
|
|
|
|
|
|
valid = @command_processor.valid_command? "blah"
|
|
|
|
valid.should == false
|
|
|
|
end
|
|
|
|
|
2011-05-23 02:34:11 -04:00
|
|
|
it 'should correctly match a simple string command' do
|
|
|
|
@pry.commands.command("test-command") {}
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "test-command", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == "test-command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == "test-command".length
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly match a simple string command with parameters' do
|
|
|
|
@pry.commands.command("test-command") { |arg|}
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "test-command hello", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == "test-command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == "test-command".length
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not match when the relevant command does not exist' do
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "test-command", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.should == nil
|
|
|
|
captures.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly match a regex command' do
|
|
|
|
@pry.commands.command(/rue(.?)/) { }
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "rue hello", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == /rue(.?)/
|
|
|
|
captures.should == [""]
|
|
|
|
pos.should == 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly match a regex command and extract the capture groups' do
|
|
|
|
@pry.commands.command(/rue(.?)/) { }
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "rue5 hello", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == /rue(.?)/
|
|
|
|
captures.should == ["5"]
|
|
|
|
pos.should == 4
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly match a string command with spaces in its name' do
|
|
|
|
@pry.commands.command("test command") {}
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "test command", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == "test command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == command.name.length
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly match a string command with spaces in its name with parameters' do
|
|
|
|
@pry.commands.command("test command") {}
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "test command param1 param2", binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == "test command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == command.name.length
|
|
|
|
end
|
|
|
|
|
2011-07-26 03:34:54 -04:00
|
|
|
it 'should correctly match a command preceded by the command_prefix if one is defined' do
|
|
|
|
Pry.config.command_prefix = "%"
|
|
|
|
|
|
|
|
@pry.commands.command("test-command") {}
|
|
|
|
command, captures, pos = @command_processor.command_matched "%test-command hello", binding
|
|
|
|
|
|
|
|
command.name.should == "test-command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == "test-command".length + "%".length
|
|
|
|
|
|
|
|
Pry.config.command_prefix = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not match a command not preceded by the command_prefix if one is defined' do
|
|
|
|
Pry.config.command_prefix = "%"
|
|
|
|
|
|
|
|
@pry.commands.command("test-command") {}
|
|
|
|
command, captures, pos = @command_processor.command_matched "test-command hello", binding
|
|
|
|
|
|
|
|
command.should == nil
|
|
|
|
captures.should == nil
|
|
|
|
|
|
|
|
Pry.config.command_prefix = ''
|
|
|
|
end
|
|
|
|
|
2011-07-26 05:42:44 -04:00
|
|
|
it 'should match a command preceded by the command_prefix when :use_prefix => false' do
|
2011-07-26 03:34:54 -04:00
|
|
|
Pry.config.command_prefix = "%"
|
|
|
|
|
2011-07-26 05:42:44 -04:00
|
|
|
@pry.commands.command("test-command", "", :use_prefix => false) {}
|
2011-07-26 03:34:54 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "%test-command hello", binding
|
|
|
|
|
|
|
|
command.name.should == "test-command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == "test-command".length + "%".length
|
|
|
|
|
|
|
|
Pry.config.command_prefix = ''
|
|
|
|
end
|
|
|
|
|
2011-07-26 05:42:44 -04:00
|
|
|
it 'should match a command not preceded by the command_prefix when :use_prefix => false' do
|
2011-07-26 03:34:54 -04:00
|
|
|
Pry.config.command_prefix = "%"
|
|
|
|
|
2011-07-26 05:42:44 -04:00
|
|
|
@pry.commands.command("test-command", "", :use_prefix => false) {}
|
2011-07-26 03:34:54 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched "test-command hello", binding
|
|
|
|
|
|
|
|
command.name.should == "test-command"
|
|
|
|
captures.should == []
|
|
|
|
pos.should == "test-command".length
|
|
|
|
|
|
|
|
Pry.config.command_prefix = ''
|
|
|
|
end
|
|
|
|
|
2011-05-23 02:34:11 -04:00
|
|
|
it 'should correctly match a regex command with spaces in its name' do
|
|
|
|
regex_command_name = /test\s+(.+)\s+command/
|
|
|
|
@pry.commands.command(regex_command_name) {}
|
|
|
|
|
|
|
|
sample_text = "test friendship command"
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched sample_text, binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == regex_command_name
|
|
|
|
captures.should == ["friendship"]
|
|
|
|
pos.should == sample_text.size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly match a complex regex command' do
|
|
|
|
regex_command_name = /\.(.*)/
|
|
|
|
@pry.commands.command(regex_command_name) {}
|
|
|
|
|
|
|
|
sample_text = ".cd ~/pry"
|
2011-06-08 10:44:51 -04:00
|
|
|
command, captures, pos = @command_processor.command_matched sample_text, binding
|
2011-05-23 02:34:11 -04:00
|
|
|
|
|
|
|
command.name.should == regex_command_name
|
|
|
|
captures.should == ["cd ~/pry"]
|
|
|
|
pos.should == sample_text.size
|
|
|
|
end
|
2011-06-08 10:44:51 -04:00
|
|
|
|
2011-08-15 13:56:28 -04:00
|
|
|
it 'should not interpolate commands that have :interpolate => false (interpolate_string should *not* be called)' do
|
2011-06-10 07:19:13 -04:00
|
|
|
@pry.commands.command("boast", "", :interpolate => false) {}
|
|
|
|
|
|
|
|
# remember to use '' instead of "" when testing interpolation or
|
|
|
|
# you'll cause yourself incredible confusion
|
|
|
|
lambda { @command_processor.command_matched('boast #{c}', binding) }.should.not.raise NameError
|
|
|
|
end
|
2011-08-15 13:56:28 -04:00
|
|
|
|
|
|
|
it 'should only execute the contents of an interpolation once' do
|
|
|
|
$obj = 'a'
|
|
|
|
|
2011-08-17 00:53:58 -04:00
|
|
|
redirect_pry_io(InputTester.new('cat #{$obj.succ!}'), StringIO.new) do
|
2011-08-15 13:56:28 -04:00
|
|
|
Pry.new.rep
|
|
|
|
end
|
|
|
|
|
|
|
|
$obj.should == 'b'
|
|
|
|
end
|
2011-05-23 02:34:11 -04:00
|
|
|
end
|