Module: PryTestHelpers

Defined in:
lib/pry/test/helper.rb

Overview

A global space for storing temporary state during tests.

Class Method Summary collapse

Class Method Details

.constant_scope(*names) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

inject a variable into a binding


31
32
33
34
35
36
# 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 = {}) ⇒ Object


64
65
66
67
68
69
# File 'lib/pry/test/helper.rb', line 64

def mock_command(cmd, args=[], opts={})
  output = StringIO.new
  pry = Pry.new(output: output)
  ret = cmd.new(opts.merge(pry_instance: pry, :output => output)).call_safely(*args)
  Struct.new(:output, :return).new(output.string, ret)
end

.mock_exception(*mock_backtrace) ⇒ Object


71
72
73
74
75
# File 'lib/pry/test/helper.rb', line 71

def mock_exception(*mock_backtrace)
  StandardError.new.tap do |e|
    e.define_singleton_method(:backtrace) { mock_backtrace }
  end
end

.temp_file(ext = '.rb') ⇒ String

Open a temp file and yield it to the block, closing it after

Returns:

  • (String)

    The path of the temp file


52
53
54
55
56
57
58
# File 'lib/pry/test/helper.rb', line 52

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) ⇒ Object


60
61
62
# File 'lib/pry/test/helper.rb', line 60

def unindent(*args)
  Pry::Helpers::CommandHelpers.unindent(*args)
end