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

Allow PryTester to test commands that mutate eval_string

This commit is contained in:
Ryan Fitzgerald 2012-08-18 15:17:26 -07:00
parent 90b3f807f0
commit 997fe6af07
2 changed files with 38 additions and 10 deletions

View file

@ -219,19 +219,17 @@ class PryTester
if context if context
@pry.binding_stack << Pry.binding_for(context) @pry.binding_stack << Pry.binding_for(context)
end end
reset_output
end end
def eval(*strs) def eval(*strs)
@out = StringIO.new reset_output
@pry.output = @out
result = nil result = nil
strs.flatten.each do |str| strs.flatten.each do |str|
if @pry.process_command(str) if @pry.process_command(str)
result = last_command_result result = last_command_result_or_output
if result == Pry::Command::VOID_VALUE
result = last_output
end
else else
result = @pry.evaluate_ruby(str) result = @pry.evaluate_ruby(str)
end end
@ -244,12 +242,31 @@ class PryTester
@out.string if @out @out.string if @out
end end
def process_command(command_str, eval_str = '')
@pry.process_command(command_str, eval_str) or raise "Not a valid command"
last_command_result_or_output
end
protected protected
def last_command_result def last_command_result
result = Thread.current[:__pry_cmd_result__] result = Thread.current[:__pry_cmd_result__]
result.retval if result result.retval if result
end end
def last_command_result_or_output
result = last_command_result
if result != Pry::Command::VOID_VALUE
result
else
last_output
end
end
def reset_output
@out = StringIO.new
@pry.output = @out
end
end end
CommandTester = Pry::CommandSet.new do CommandTester = Pry::CommandSet.new do
@ -262,6 +279,10 @@ CommandTester = Pry::CommandSet.new do
end end
end end
def unindent(*args)
Pry::Helpers::CommandHelpers.unindent(*args)
end
# to help with tracking down bugs that cause an infinite loop in the test suite # to help with tracking down bugs that cause an infinite loop in the test suite
if ENV["SET_TRACE_FUNC"] if ENV["SET_TRACE_FUNC"]
require 'set_trace' if Pry::Helpers::BaseHelpers.rbx? require 'set_trace' if Pry::Helpers::BaseHelpers.rbx?

View file

@ -3,15 +3,22 @@ require 'helper'
describe "Pry::DefaultCommands::Input" do describe "Pry::DefaultCommands::Input" do
before do before do
@str_output = StringIO.new @str_output = StringIO.new
@t = pry_tester
end end
describe "amend-line" do describe "amend-line" do
it 'should correctly amend the last line of input when no line number specified ' do it 'should correctly amend the last line of input when no line number specified ' do
redirect_pry_io(InputTester.new("def hello", "puts :bing", "amend-line puts :blah", "show-input", "exit-all"), @str_output) do eval_str = unindent(<<-STR)
pry def hello
end puts :bing
STR
@str_output.string.should =~ /\A\d+: def hello\n\d+: puts :blah/ @t.process_command 'amend-line puts :blah', eval_str
eval_str.should == unindent(<<-STR)
def hello
puts :blah
STR
end end
it 'should correctly amend the specified line of input when line number given ' do it 'should correctly amend the specified line of input when line number given ' do