Sha256: 781a5446e132bb503617cdb9a706b140c4f53c9d9c1373ba02ba9e1b76f704b0

Contents?: true

Size: 789 Bytes

Versions: 2

Compression:

Stored size: 789 Bytes

Contents

def mock_pry(*args)
  args.flatten!
  binding = args.first.is_a?(Binding) ? args.shift : binding()
  options = args.last.is_a?(Hash) ? args.pop : {}

  input = InputTester.new(*args)
  output = StringIO.new

  redirect_pry_io(input, output) do
    binding.pry(options)
  end

  output.string
end

# Set I/O streams. Out defaults to an anonymous StringIO.
def redirect_pry_io(new_in, new_out = StringIO.new)
  old_in = Pry.input
  old_out = Pry.output

  Pry.input = new_in
  Pry.output = new_out
  begin
    yield
  ensure
    Pry.input = old_in
    Pry.output = old_out
  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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pry-1.0.0.pre1-i386-mswin32 spec/helpers/mock_pry.rb
pry-1.0.0.pre1-i386-mingw32 spec/helpers/mock_pry.rb