2012-12-06 19:52:37 -05:00
|
|
|
require 'pry'
|
2011-04-29 10:55:35 -04:00
|
|
|
|
2011-05-28 09:38:00 -04:00
|
|
|
# in case the tests call reset_defaults, ensure we reset them to
|
|
|
|
# amended (test friendly) values
|
|
|
|
class << Pry
|
|
|
|
alias_method :orig_reset_defaults, :reset_defaults
|
|
|
|
def reset_defaults
|
|
|
|
orig_reset_defaults
|
|
|
|
|
|
|
|
Pry.color = false
|
|
|
|
Pry.pager = false
|
2011-10-05 13:04:44 -04:00
|
|
|
Pry.config.should_load_rc = false
|
2012-07-03 23:09:01 -04:00
|
|
|
Pry.config.should_load_local_rc= false
|
2012-01-24 07:07:50 -05:00
|
|
|
Pry.config.should_load_plugins = false
|
2011-06-09 06:26:57 -04:00
|
|
|
Pry.config.history.should_load = false
|
|
|
|
Pry.config.history.should_save = false
|
2012-12-28 18:59:41 -05:00
|
|
|
Pry.config.correct_indent = false
|
2011-11-12 08:03:02 -05:00
|
|
|
Pry.config.hooks = Pry::Hooks.new
|
2011-11-16 22:06:00 -05:00
|
|
|
Pry.config.collision_warning = false
|
2011-05-28 09:38:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
Pry.reset_defaults
|
2011-04-04 08:18:07 -04:00
|
|
|
|
2012-12-06 19:52:37 -05:00
|
|
|
# A global space for storing temporary state during tests.
|
2011-08-31 13:05:21 -04:00
|
|
|
|
2012-12-06 19:52:37 -05:00
|
|
|
module PryTestHelpers
|
2012-12-07 17:08:49 -05:00
|
|
|
|
|
|
|
module_function
|
|
|
|
|
2012-12-06 19:52:37 -05:00
|
|
|
# inject a variable into a binding
|
2012-12-07 17:08:49 -05:00
|
|
|
def inject_var(name, value, b)
|
2012-12-06 19:52:37 -05:00
|
|
|
Thread.current[:__pry_local__] = value
|
|
|
|
b.eval("#{name} = Thread.current[:__pry_local__]")
|
|
|
|
ensure
|
|
|
|
Thread.current[:__pry_local__] = nil
|
|
|
|
end
|
|
|
|
|
2012-12-07 17:08:49 -05:00
|
|
|
def constant_scope(*names)
|
2012-12-06 19:52:37 -05:00
|
|
|
names.each do |name|
|
|
|
|
Object.remove_const name if Object.const_defined?(name)
|
|
|
|
end
|
2011-05-18 23:28:38 -04:00
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
2012-12-06 19:52:37 -05:00
|
|
|
names.each do |name|
|
|
|
|
Object.remove_const name if Object.const_defined?(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 17:08:49 -05:00
|
|
|
def mri18_and_no_real_source_location?
|
2012-12-06 19:52:37 -05:00
|
|
|
Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Open a temp file and yield it to the block, closing it after
|
|
|
|
# @return [String] The path of the temp file
|
2012-12-07 17:08:49 -05:00
|
|
|
def temp_file(ext='.rb')
|
2012-12-06 19:52:37 -05:00
|
|
|
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
|
|
|
|
|
2012-12-07 17:08:49 -05:00
|
|
|
def unindent(*args)
|
2012-12-06 19:52:37 -05:00
|
|
|
Pry::Helpers::CommandHelpers.unindent(*args)
|
2011-05-18 23:28:38 -04:00
|
|
|
end
|
|
|
|
|
2012-12-08 17:56:57 -05:00
|
|
|
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
|
2011-12-31 10:21:58 -05:00
|
|
|
|
2012-12-08 17:56:57 -05:00
|
|
|
def mock_exception(*mock_backtrace)
|
|
|
|
e = StandardError.new("mock exception")
|
|
|
|
(class << e; self; end).class_eval do
|
|
|
|
define_method(:backtrace) { mock_backtrace }
|
|
|
|
end
|
|
|
|
e
|
2011-01-19 03:50:45 -05:00
|
|
|
end
|
2011-09-13 13:40:30 -04:00
|
|
|
end
|
|
|
|
|
2012-09-09 01:46:45 -04:00
|
|
|
def pry_tester(*args, &block)
|
2012-09-09 02:12:25 -04:00
|
|
|
if args.length == 0 || args[0].is_a?(Hash)
|
|
|
|
args.unshift(Pry.toplevel_binding)
|
|
|
|
end
|
2012-09-09 01:46:45 -04:00
|
|
|
|
|
|
|
PryTester.new(*args).tap do |t|
|
2012-07-24 08:49:30 -04:00
|
|
|
(class << t; self; end).class_eval(&block) if block
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-04 19:23:24 -04:00
|
|
|
def pry_eval(*eval_strs)
|
2012-08-19 15:39:54 -04:00
|
|
|
if eval_strs.first.is_a? String
|
2012-09-09 01:29:00 -04:00
|
|
|
binding = Pry.toplevel_binding
|
2012-08-19 15:39:54 -04:00
|
|
|
else
|
|
|
|
binding = Pry.binding_for(eval_strs.shift)
|
|
|
|
end
|
|
|
|
|
2012-11-18 19:41:56 -05:00
|
|
|
pry_tester(binding).eval(*eval_strs)
|
2012-08-04 19:23:24 -04:00
|
|
|
end
|
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
class PryTester
|
2012-12-18 03:45:10 -05:00
|
|
|
extend Forwardable
|
|
|
|
|
2012-07-24 08:49:30 -04:00
|
|
|
attr_reader :pry, :out
|
2012-07-24 04:36:53 -04:00
|
|
|
|
2012-12-28 01:06:50 -05:00
|
|
|
def_delegators :@pry, :eval_string, :eval_string=
|
2012-12-18 03:45:10 -05:00
|
|
|
|
2012-12-18 02:54:07 -05:00
|
|
|
def initialize(target = TOPLEVEL_BINDING, options = {})
|
|
|
|
@pry = Pry.new(options.merge(:target => target))
|
2012-12-09 16:37:36 -05:00
|
|
|
@history = options[:history]
|
2012-07-24 04:36:53 -04:00
|
|
|
|
2012-12-18 02:54:07 -05:00
|
|
|
@pry.inject_sticky_locals!
|
2012-08-18 18:17:26 -04:00
|
|
|
reset_output
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def eval(*strs)
|
2012-08-18 18:17:26 -04:00
|
|
|
reset_output
|
2012-07-24 08:49:30 -04:00
|
|
|
result = nil
|
|
|
|
|
|
|
|
strs.flatten.each do |str|
|
2012-09-09 01:46:45 -04:00
|
|
|
str = "#{str.strip}\n"
|
2012-12-09 16:37:36 -05:00
|
|
|
@history.push str if @history
|
2012-12-28 21:35:08 -05:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
if @pry.process_command(str)
|
2012-08-18 18:17:26 -04:00
|
|
|
result = last_command_result_or_output
|
2012-07-24 04:36:53 -04:00
|
|
|
else
|
2012-07-24 08:49:30 -04:00
|
|
|
result = @pry.evaluate_ruby(str)
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
2012-07-24 08:49:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2012-12-28 01:06:50 -05:00
|
|
|
def push(*lines)
|
|
|
|
Array(lines).flatten.each do |line|
|
|
|
|
@pry.eval(line)
|
|
|
|
end
|
2012-12-18 03:45:10 -05:00
|
|
|
end
|
|
|
|
|
2012-12-18 02:58:02 -05:00
|
|
|
def push_binding(context)
|
2012-12-16 20:12:03 -05:00
|
|
|
@pry.push_binding context
|
2012-08-19 16:04:05 -04:00
|
|
|
end
|
|
|
|
|
2012-07-24 08:49:30 -04:00
|
|
|
def last_output
|
|
|
|
@out.string if @out
|
|
|
|
end
|
|
|
|
|
2012-12-18 03:45:10 -05:00
|
|
|
def process_command(command_str)
|
|
|
|
@pry.process_command(command_str) or raise "Not a valid command"
|
2012-08-18 18:17:26 -04:00
|
|
|
last_command_result_or_output
|
|
|
|
end
|
|
|
|
|
2012-07-24 08:49:30 -04:00
|
|
|
def last_command_result
|
|
|
|
result = Thread.current[:__pry_cmd_result__]
|
|
|
|
result.retval if result
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
2012-08-18 18:17:26 -04:00
|
|
|
|
2012-12-15 19:34:56 -05:00
|
|
|
protected
|
|
|
|
|
2012-08-18 18:17:26 -04:00
|
|
|
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
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|