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

new tests for Pry::CommandProcessor and regex command and spaced command functionality, also tests for :interpolate flag for commands

This commit is contained in:
John Mair 2011-05-23 18:34:11 +12:00
parent 54de8361e2
commit bf0081170e
3 changed files with 414 additions and 338 deletions

View file

@ -0,0 +1,99 @@
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
it 'should correctly match a simple string command' do
@pry.commands.command("test-command") {}
command, captures, pos = @command_processor.command_matched "test-command"
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|}
command, captures, pos = @command_processor.command_matched "test-command hello"
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
command, captures, pos = @command_processor.command_matched "test-command"
command.should == nil
captures.should == nil
end
it 'should correctly match a regex command' do
@pry.commands.command(/rue(.?)/) { }
command, captures, pos = @command_processor.command_matched "rue hello"
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(.?)/) { }
command, captures, pos = @command_processor.command_matched "rue5 hello"
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") {}
command, captures, pos = @command_processor.command_matched "test command"
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") {}
command, captures, pos = @command_processor.command_matched "test command param1 param2"
command.name.should == "test command"
captures.should == []
pos.should == command.name.length
end
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"
command, captures, pos = @command_processor.command_matched sample_text
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"
command, captures, pos = @command_processor.command_matched sample_text
command.name.should == regex_command_name
captures.should == ["cd ~/pry"]
pos.should == sample_text.size
end
end

View file

@ -31,7 +31,7 @@ describe Pry::CommandSet do
@set.run_command true, 'foo'
end
it 'should raise an error when calling an undefined comand' do
it 'should raise an error when calling an undefined command' do
@set.command('foo') {}
lambda {
@set.run_command nil, 'bar'
@ -135,7 +135,7 @@ describe Pry::CommandSet do
Pry::CommandContext.new.should.not.respond_to :my_helper
end
it 'should not recreate a new heler module when helpers is called' do
it 'should not recreate a new helper module when helpers is called' do
@set.command('foo') do
should.respond_to :my_helper
should.respond_to :my_other_helper

View file

@ -386,43 +386,6 @@ describe Pry do
str_output2.string.should =~ /7/
end
# describe "Pry.run_command" do
# before do
# class RCTest
# def a() end
# B = 20
# @x = 10
# end
# end
# after do
# Object.remove_const(:RCTest)
# end
# it "should execute command in the appropriate object context" do
# result = Pry.run_command "ls", :context => RCTest
# result.map(&:to_sym).should == [:@x]
# end
# it "should execute command with parameters in the appropriate object context" do
# result = Pry.run_command "ls -M", :context => RCTest
# result.map(&:to_sym).should == [:a]
# end
# it "should execute command and show output with :show_output => true flag" do
# str = StringIO.new
# Pry.output = str
# result = Pry.run_command "ls -afv", :context => RCTest, :show_output => true
# str.string.should =~ /global variables/
# Pry.output = $stdout
# end
# it "should execute command with multiple parameters" do
# result = Pry.run_command "ls -c -M RCTest"
# result.map(&:to_sym).should == [:a, :B]
# end
# end
describe "commands" do
it 'should interpolate ruby code into commands' do
klass = Pry::CommandSet.new do
@ -431,11 +394,25 @@ describe Pry do
end
end
@test_interpolation = "bing"
$test_interpolation = "bing"
str_output = StringIO.new
Pry.new(:input => StringIO.new("hello #{@test_interpolation}"), :output => str_output, :commands => klass).rep
Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
str_output.string.should =~ /bing/
@test_interpolation = nil
$test_interpolation = nil
end
it 'should NOT interpolate ruby code into commands if :interpolate => false' do
klass = Pry::CommandSet.new do
command "hello", "", :keep_retval => true, :interpolate => false do |arg|
arg
end
end
$test_interpolation = "bing"
str_output = StringIO.new
Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
str_output.string.should =~ /test_interpolation/
$test_interpolation = nil
end
it 'should create a comand in a nested context and that command should be accessible from the parent' do