module PryTestHelpers

A global space for storing temporary state during tests.

Public Instance Methods

constant_scope(*names) { || ... } click to toggle source
# File lib/pry/test/helper.rb, line 38
def constant_scope(*names)
  names.each do |name|
    Object.remove_const name if Object.const_defined?(name)
  end

  yield
ensure
  names.each do |name|
    Object.remove_const name if Object.const_defined?(name)
  end
end
inject_var(name, value, b) click to toggle source

inject a variable into a binding

# File lib/pry/test/helper.rb, line 31
def inject_var(name, value, b)
  Pry.current[:pry_local] = value
  b.eval("#{name} = ::Pry.current[:pry_local]")
ensure
  Pry.current[:pry_local] = nil
end
mock_command(cmd, args=[], opts={}) click to toggle source
# File lib/pry/test/helper.rb, line 68
def mock_command(cmd, args=[], opts={})
  output = StringIO.new
  ret = cmd.new(opts.merge(:output => output)).call_safely(*args)
  Struct.new(:output, :return).new(output.string, ret)
end
mock_exception(*mock_backtrace) click to toggle source
# File lib/pry/test/helper.rb, line 74
def mock_exception(*mock_backtrace)
  e = StandardError.new("mock exception")
  (class << e; self; end).class_eval do
    define_method(:backtrace) { mock_backtrace }
  end
  e
end
mri18_and_no_real_source_location?() click to toggle source
# File lib/pry/test/helper.rb, line 50
def mri18_and_no_real_source_location?
  Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method)
end
temp_file(ext='.rb') { |file| ... } click to toggle source

Open a temp file and yield it to the block, closing it after @return [String] The path of the temp file

# File lib/pry/test/helper.rb, line 56
def temp_file(ext='.rb')
  file = Tempfile.new(['pry', ext])
  yield file
ensure
  file.close(true) if file
  File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx
end
unindent(*args) click to toggle source
# File lib/pry/test/helper.rb, line 64
def unindent(*args)
  Pry::Helpers::CommandHelpers.unindent(*args)
end