2012-12-08 17:08:16 -08:00
|
|
|
def mock_pry(*args)
|
|
|
|
args.flatten!
|
|
|
|
binding = args.first.is_a?(Binding) ? args.shift : binding()
|
2012-12-18 00:10:52 -08:00
|
|
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
2012-12-08 17:08:16 -08:00
|
|
|
|
|
|
|
input = InputTester.new(*args)
|
|
|
|
output = StringIO.new
|
|
|
|
|
|
|
|
redirect_pry_io(input, output) do
|
2012-12-18 00:10:52 -08:00
|
|
|
binding.pry(options)
|
2012-12-08 17:08:16 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
output.string
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set I/O streams. Out defaults to an anonymous StringIO.
|
|
|
|
def redirect_pry_io(new_in, new_out = StringIO.new)
|
2014-04-29 00:03:15 -07:00
|
|
|
old_in = Pry.config.input
|
|
|
|
old_out = Pry.config.output
|
2012-12-08 17:08:16 -08:00
|
|
|
|
2014-04-29 00:03:15 -07:00
|
|
|
Pry.config.input = new_in
|
|
|
|
Pry.config.output = new_out
|
2012-12-08 17:08:16 -08:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2014-04-29 00:03:15 -07:00
|
|
|
Pry.config.input = old_in
|
|
|
|
Pry.config.output = old_out
|
2012-12-08 17:08:16 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InputTester
|
|
|
|
def initialize(*actions)
|
|
|
|
@orig_actions = actions.dup
|
|
|
|
@actions = actions
|
|
|
|
end
|
|
|
|
|
|
|
|
def readline(*)
|
|
|
|
@actions.shift
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewind
|
|
|
|
@actions = @orig_actions.dup
|
|
|
|
end
|
|
|
|
end
|