This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Pry
|
||||
module Testable
|
||||
module Evalable
|
||||
def pry_tester(*args, &block)
|
||||
args.unshift(Pry.toplevel_binding) if args.empty? || args[0].is_a?(Hash)
|
||||
Pry::Testable::PryTester.new(*args).tap do |t|
|
||||
t.singleton_class.class_eval(&block) if block
|
||||
end
|
||||
end
|
||||
|
||||
def pry_eval(*eval_strs)
|
||||
b =
|
||||
if eval_strs.first.is_a?(String)
|
||||
Pry.toplevel_binding
|
||||
else
|
||||
Pry.binding_for(eval_strs.shift)
|
||||
end
|
||||
pry_tester(b).eval(*eval_strs)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'stringio'
|
||||
|
||||
class Pry
|
||||
module Testable
|
||||
module Mockable
|
||||
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
|
||||
|
||||
def mock_exception(*mock_backtrace)
|
||||
StandardError.new.tap do |e|
|
||||
e.define_singleton_method(:backtrace) { mock_backtrace }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,88 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'stringio'
|
||||
|
||||
class Pry
|
||||
module Testable
|
||||
class PryTester
|
||||
extend Pry::Forwardable
|
||||
attr_reader :pry, :out
|
||||
def_delegators :@pry, :eval_string, :eval_string=
|
||||
|
||||
def initialize(target = TOPLEVEL_BINDING, options = {})
|
||||
@pry = Pry.new(options.merge(target: target))
|
||||
@history = options[:history]
|
||||
@pry.inject_sticky_locals!
|
||||
reset_output
|
||||
end
|
||||
|
||||
def eval(*strs)
|
||||
reset_output
|
||||
result = nil
|
||||
|
||||
strs.flatten.each do |str|
|
||||
# Check for space prefix. See #1369.
|
||||
str = "#{str.strip}\n" if str !~ /^\s\S/
|
||||
@history.push str if @history
|
||||
|
||||
result =
|
||||
if @pry.process_command(str)
|
||||
last_command_result_or_output
|
||||
else
|
||||
# Check if this is a multiline paste.
|
||||
begin
|
||||
complete_expr = Pry::Code.complete_expression?(str)
|
||||
rescue SyntaxError => exception
|
||||
@pry.output.puts(
|
||||
"SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
|
||||
)
|
||||
end
|
||||
@pry.evaluate_ruby(str) if complete_expr
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def push(*lines)
|
||||
Array(lines).flatten.each do |line|
|
||||
@pry.eval(line)
|
||||
end
|
||||
end
|
||||
|
||||
def push_binding(context)
|
||||
@pry.push_binding context
|
||||
end
|
||||
|
||||
def last_output
|
||||
@out.string if @out
|
||||
end
|
||||
|
||||
def process_command(command_str)
|
||||
@pry.process_command(command_str) || raise("Not a valid command")
|
||||
last_command_result_or_output
|
||||
end
|
||||
|
||||
def last_command_result
|
||||
result = Pry.current[:pry_cmd_result]
|
||||
result.retval if result
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'tempfile'
|
||||
|
||||
class Pry
|
||||
module Testable
|
||||
module Utility
|
||||
#
|
||||
# Creates a Tempfile then unlinks it after the block has yielded.
|
||||
#
|
||||
# @yieldparam [String] file
|
||||
# The path of the temp file
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def temp_file(ext = '.rb')
|
||||
file = Tempfile.open(['pry', ext])
|
||||
yield file
|
||||
ensure
|
||||
file.close(true) if file
|
||||
end
|
||||
|
||||
def unindent(*args)
|
||||
Pry::Helpers::CommandHelpers.unindent(*args)
|
||||
end
|
||||
|
||||
def inner_scope
|
||||
catch(:inner_scope) do
|
||||
yield -> { throw(:inner_scope, self) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Pry
|
||||
module Testable
|
||||
module Variables
|
||||
#
|
||||
# @example
|
||||
# temporary_constants(:Foo, :Bar) do
|
||||
# Foo = Class.new(RuntimeError)
|
||||
# Bar = Class.new(RuntimeError)
|
||||
# end
|
||||
# Foo # => NameError
|
||||
# Bar # => NameError
|
||||
#
|
||||
# @param [Array<Symbol>] names
|
||||
# An array of constant names that be defined by a block,
|
||||
# and removed by this method afterwards.
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def temporary_constants(*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
|
||||
|
||||
#
|
||||
# @param [String] name
|
||||
# The name of a variable.
|
||||
#
|
||||
# @param [String] value
|
||||
# Its value.
|
||||
#
|
||||
# @param [Binding] binding
|
||||
# The binding object to insert a variable into.
|
||||
#
|
||||
# @return [void]
|
||||
#
|
||||
def insert_variable(name, value, binding)
|
||||
Pry.current[:pry_local] = value
|
||||
binding.eval("#{name} = ::Pry.current[:pry_local]")
|
||||
ensure
|
||||
Pry.current[:pry_local] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user