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

Update specs using inner_scope helper

This commit is contained in:
Christian Haase 2014-09-12 18:40:37 +02:00 committed by Ryan Fitzgerald
parent 72b486f43b
commit aec888e7bc
3 changed files with 67 additions and 41 deletions

View file

@ -73,6 +73,12 @@ module PryTestHelpers
e.define_singleton_method(:backtrace) { mock_backtrace } e.define_singleton_method(:backtrace) { mock_backtrace }
end end
end end
def inner_scope
catch(:inner_scope) do
yield ->{ throw(:inner_scope, self) }
end
end
end end
def pry_tester(*args, &block) def pry_tester(*args, &block)

View file

@ -55,15 +55,15 @@ describe Pry::CommandSet do
end end
it 'should use the first argument as context' do it 'should use the first argument as context' do
ctx = @ctx inside = inner_scope do |probe|
@set.command('foo', &probe)
@set.command 'foo' do
self.context.should == ctx
end
@set.run_command @ctx, 'foo' @set.run_command @ctx, 'foo'
end end
expect(inside.context).to eq(@ctx)
end
it 'should raise an error when calling an undefined command' do it 'should raise an error when calling an undefined command' do
@set.command('foo') {} @set.command('foo') {}
expect { @set.run_command @ctx, 'bar' }.to raise_error Pry::NoCommandError expect { @set.run_command @ctx, 'bar' }.to raise_error Pry::NoCommandError
@ -620,28 +620,32 @@ describe Pry::CommandSet do
:output => StringIO.new, :output => StringIO.new,
:target => binding :target => binding
} }
inside = inner_scope do |probe|
@set.create_command('agnes') do @set.create_command('agnes') do
define_method(:process) do define_method(:process, &probe)
eval_string.should == ctx[:eval_string]
output.should == ctx[:output]
target.should == ctx[:target]
_pry_.should == ctx[:pry_instance]
end
end end
@set.process_line('agnes', ctx) @set.process_line('agnes', ctx)
end end
it 'should add command_set to context' do expect(inside.eval_string).to eq(ctx[:eval_string])
set = @set expect(inside.output).to eq(ctx[:output])
@set.create_command(/nann+y ogg+/) do expect(inside.target).to eq(ctx[:target])
define_method(:process) do expect(inside._pry_).to eq(ctx[:pry_instance])
command_set.should == set
end end
it 'should add command_set to context' do
inside = inner_scope do |probe|
@set.create_command(/nann+y ogg+/) do
define_method(:process, &probe)
end end
@set.process_line('nannnnnny oggggg') @set.process_line('nannnnnny oggggg')
end end
expect(inside.command_set).to eq(@set)
end
end end
if defined?(Bond) if defined?(Bond)

View file

@ -137,32 +137,48 @@ describe "Pry::Command" do
end end
describe 'context' do describe 'context' do
context = { let(:context) {
{
:target => binding, :target => binding,
:output => StringIO.new, :output => StringIO.new,
:eval_string => "eval-string", :eval_string => "eval-string",
:command_set => @set, :command_set => @set,
:pry_instance => Pry.new :pry_instance => Pry.new
} }
}
describe '#setup' do
it 'should capture lots of stuff from the hash passed to new before setup' do it 'should capture lots of stuff from the hash passed to new before setup' do
cmd = @set.create_command 'fenchurch', "Floats slightly off the ground" do inside = inner_scope do |probe|
define_method(:setup) do cmd = @set.create_command('fenchurch', "Floats slightly off the ground") do
self.context.should == context define_method(:setup, &probe)
target.should == context[:target]
target_self.should == context[:target].eval('self')
output.should == context[:output]
end
define_method(:process) do
eval_string.should == "eval-string"
command_set.should == @set
_pry_.should == context[:pry_instance]
end
end end
cmd.new(context).call cmd.new(context).call
end end
expect(inside.context).to eq(context)
expect(inside.target).to eq(context[:target])
expect(inside.target_self).to eq(context[:target].eval('self'))
expect(inside.output).to eq(context[:output])
end
end
describe '#process' do
it 'should capture lots of stuff from the hash passed to new before setup' do
inside = inner_scope do |probe|
cmd = @set.create_command('fenchurch', "Floats slightly off the ground") do
define_method(:process, &probe)
end
cmd.new(context).call
end
expect(inside.eval_string).to eq("eval-string")
expect(inside.__send__(:command_set)).to eq(@set)
expect(inside._pry_).to eq(context[:pry_instance])
end
end
end end
describe 'classy api' do describe 'classy api' do
@ -385,14 +401,14 @@ describe "Pry::Command" do
end end
it "should set the commands' arg_string and captures" do it "should set the commands' arg_string and captures" do
inside = inner_scope { |probe|
cmd = @set.command /benj(ie|ei)/ do |*args| cmd = @set.command(/benj(ie|ei)/, &probe)
self.arg_string.should == "mouse"
self.captures.should == ['ie']
args.should == ['ie', 'mouse']
end
cmd.new.process_line %(benjie mouse) cmd.new.process_line %(benjie mouse)
}
expect(inside.arg_string).to eq("mouse")
expect(inside.captures).to eq(['ie'])
end end
it "should raise an error if the line doesn't match the command" do it "should raise an error if the line doesn't match the command" do