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

now using 'retry' (in read loop) to switch input objects in input_stack

This is so we can make the switch to the new input object seemless on encountering EOFError in previous input object.
We also detect whether we are going to go into an infinite EOFError state (i.e if Pry.config.input generates EOFError) and so we break out of the REPL with an error message
and a throw(:breakout)
This commit is contained in:
John Mair 2011-09-16 03:17:24 +12:00
parent 207f2c8bb1
commit 4e3853ea33
4 changed files with 65 additions and 12 deletions

View file

@ -13,6 +13,7 @@
* local ./.pryrc now loaded after ~/.pryrc if it exists
* cat --ex N and edit --ex N now can navigate through backtrace, where cat --ex (with no args) moves throuh successive levels of the backtrace automatically with state stored on the exceptino object itself
* new option Pry.config.exception_window_size determines window size for cat --ex
* input_stack now implemented - pushing objects onto a pry instance's input_stack causes the instance to read from those objects in turn as it encounters EOF on the previous object. On finishing the input_stack the input object for the pry instance is set back to Pry.config.input, if this fails, pry breaks out of the REPL (throw(:breakout)) with an error message
8/9/2011 version 0.9.5

View file

@ -395,6 +395,7 @@ class Pry
Pry.history << line.dup if line
line
else
should_retry = true
begin
if input.method(:readline).arity == 1
input.readline(current_prompt)
@ -403,8 +404,18 @@ class Pry
end
rescue EOFError
self.input = input_stack.empty? ? Pry.config.input : input_stack.pop
""
if input_stack.empty?
self.input = Pry.config.input
if !should_retry
output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
throw(:breakout)
end
should_retry = false
else
self.input = input_stack.pop
end
retry
end
end
end

View file

@ -12,7 +12,7 @@ describe "Pry#input_stack" do
Pry.config.input_stack = []
end
it 'should read from all input objects on stack and exit session' do
it 'should read from all input objects on stack and exit session (usingn repl)' do
stack = [b = StringIO.new(":cloister\nexit\n"), c = StringIO.new(":baron\n")]
instance = Pry.new(:input => StringIO.new(":alex\n"),
:output => str_output = StringIO.new,
@ -24,4 +24,47 @@ describe "Pry#input_stack" do
str_output.string.should =~ /:cloister/
end
it 'input objects should be popped off stack as they are used up' do
stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
instance = Pry.new(:input => StringIO.new(":alex\n"),
:output => str_output = StringIO.new,
:input_stack => stack)
stack.size.should == 2
instance.rep
str_output.string.should =~ /:alex/
instance.rep
str_output.string.should =~ /:baron/
stack.size.should == 1
instance.rep
str_output.string.should =~ /:cloister/
stack.size.should == 0
end
it 'should revert to Pry.config.input when it runs out of input objects in input_stack' do
redirect_pry_io(StringIO.new(":rimbaud\nexit\n"), StringIO.new) do
stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
instance = Pry.new(:input => StringIO.new(":alex\n"),
:output => str_output = StringIO.new,
:input_stack => stack)
instance.repl
str_output.string.should =~ /:alex/
str_output.string.should =~ /:baron/
str_output.string.should =~ /:cloister/
str_output.string.should =~ /:rimbaud/
end
end
it 'should display error and throw(:breakout) if at end of input after using up input_stack objects' do
str_output = StringIO.new
catch(:breakout) do
redirect_pry_io(StringIO.new(":rimbaud\n"), str_output) do
Pry.new(:input_stack => [StringIO.new(":a\n"), StringIO.new(":b\n")]).repl
end
end
str_output.string.should =~ /Error: Pry ran out of things to read/
end
end

View file

@ -86,16 +86,14 @@ describe Pry do
o.instance_variable_get(:@x).should == 10
end
it 'should not output anything for no input' do
outp = StringIO.new
# note i could not use mock_pry() for this test for some
# reason, as i'd always get "\n" as output instead of ""
redirect_pry_io(StringIO.new(""), outp) do
Pry.new.rep(self)
it 'should display error and throw(:breakout) if Pry instance runs out of input' do
str_output = StringIO.new
catch(:breakout) do
redirect_pry_io(StringIO.new(":nothing\n"), str_output) do
Pry.new.repl
end
end
outp.string.empty?.should == true
str_output.string.should =~ /Error: Pry ran out of things to read/
end
it 'should make self evaluate to the receiver of the rep session' do