Replace PryTester#simulate_repl with Pry#eval

This commit is contained in:
Ryan Fitzgerald 2012-12-28 18:35:08 -08:00
parent c1f8c3f954
commit 351d8b6d61
3 changed files with 34 additions and 63 deletions

View File

@ -122,6 +122,7 @@ class PryTester
strs.flatten.each do |str| strs.flatten.each do |str|
str = "#{str.strip}\n" str = "#{str.strip}\n"
@history.push str if @history @history.push str if @history
if @pry.process_command(str) if @pry.process_command(str)
result = last_command_result_or_output result = last_command_result_or_output
else else
@ -142,23 +143,6 @@ class PryTester
@pry.push_binding context @pry.push_binding context
end end
# TODO: eliminate duplication with Pry#repl
def simulate_repl
didnt_exit = nil
break_data = nil
didnt_exit = catch(:didnt_exit) do
break_data = catch(:breakout) do
yield self
throw(:didnt_exit, true)
end
nil
end
raise "Failed to exit REPL" if didnt_exit
break_data
end
def last_output def last_output
@out.string if @out @out.string if @out
end end

View File

@ -1,34 +1,27 @@
require 'helper' require 'helper'
describe "exit-all" do describe "exit-all" do
it 'should break out of the repl loop of Pry instance and return nil' do before { @pry = Pry.new }
pry_tester(0).simulate_repl do |t|
t.eval 'exit-all' it "should break out of the repl and return nil" do
end.should == nil @pry.eval("exit-all").should.be.false
@pry.exit_value.should.be.nil
end end
it 'should break out of the repl loop of Pry instance wth a user specified value' do it "should break out of the repl wth a user specified value" do
pry_tester(0).simulate_repl do |t| @pry.eval("exit-all 'message'").should.be.false
t.eval "exit-all 'message'" @pry.exit_value.should == "message"
end.should == 'message'
end end
it 'should break of the repl loop even if multiple bindings still on stack' do it "should break out of the repl even if multiple bindings still on stack" do
pry_tester(0).simulate_repl do |t| ["cd 1", "cd 2"].each { |line| @pry.eval(line).should.be.true }
t.eval 'cd 1', 'cd 2', "exit-all 'message'" @pry.eval("exit-all 'message'").should.be.false
end.should == 'message' @pry.exit_value.should == "message"
end end
it 'binding_stack should be empty after breaking out of the repl loop' do it "should have empty binding_stack after breaking out of the repl" do
t = pry_tester(0) do ["cd 1", "cd 2"].each { |line| @pry.eval(line).should.be.true }
def binding_stack @pry.eval("exit-all").should.be.false
@pry.binding_stack @pry.binding_stack.should.be.empty
end
end
t.simulate_repl do |t|
t.eval 'cd 1', 'cd 2', 'exit-all'
end
t.binding_stack.empty?.should == true
end end
end end

View File

@ -1,34 +1,28 @@
require 'helper' require 'helper'
describe "exit" do describe "exit" do
it 'should pop a binding with exit' do before { @pry = Pry.new(:target => :outer, :output => StringIO.new) }
pry_tester(:outer).simulate_repl do |t|
t.eval 'cd :inner' it "should pop a binding" do
t.eval('self').should == :inner @pry.eval "cd :inner"
t.eval 'exit' @pry.evaluate_ruby("self").should == :inner
t.eval('self').should == :outer @pry.eval "exit"
t.eval 'exit-all' @pry.evaluate_ruby("self").should == :outer
end
end end
it 'should break out of the repl loop of Pry instance when binding_stack has only one binding with exit' do it "should break out of the repl when binding_stack has only one binding" do
pry_tester(0).simulate_repl do |t| @pry.eval("exit").should.be.false
t.eval 'exit' @pry.exit_value.should.be.nil
end.should == nil
end end
it 'should break out of the repl loop of Pry instance when binding_stack has only one binding with exit, and return user-given value' do it "should break out of the repl and return user-given value" do
pry_tester(0).simulate_repl do |t| @pry.eval("exit :john").should.be.false
t.eval 'exit :john' @pry.exit_value.should == :john
end.should == :john
end end
it 'should break out the repl loop of Pry instance even after an exception in user-given value' do it "should break out of the repl even after an exception" do
pry_tester(0).simulate_repl do |t| @pry.eval "exit = 42"
proc { @pry.output.string.should =~ /^SyntaxError/
t.eval 'exit = 42' @pry.eval("exit").should.be.false
}.should.raise(SyntaxError)
t.eval 'exit'
end.should == nil
end end
end end