2011-04-29 10:55:35 -04:00
|
|
|
unless Object.const_defined? 'Pry'
|
|
|
|
$:.unshift File.expand_path '../../lib', __FILE__
|
|
|
|
require 'pry'
|
|
|
|
end
|
|
|
|
|
2012-05-30 16:33:13 -04:00
|
|
|
puts "Ruby v#{RUBY_VERSION} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"}), Pry v#{Pry::VERSION}, method_source v#{MethodSource::VERSION}, CodeRay v#{CodeRay::VERSION}, Slop v#{Slop::VERSION}"
|
2011-09-12 04:06:47 -04:00
|
|
|
|
2011-04-29 10:55:35 -04:00
|
|
|
require 'bacon'
|
2011-06-05 18:41:59 -04:00
|
|
|
require 'open4'
|
2011-04-29 10:55:35 -04:00
|
|
|
|
2012-09-09 00:42:02 -04:00
|
|
|
# Colorize output (based on greeneggs (c) 2009 Michael Fleet)
|
|
|
|
# TODO: Make own gem (assigned to rking)
|
2012-09-09 00:19:15 -04:00
|
|
|
module Bacon
|
2012-09-09 00:46:37 -04:00
|
|
|
COLORS = {'F' => 31, 'E' => 35, 'M' => 33, '.' => 32}
|
|
|
|
USE_COLOR = Pry::Helpers::BaseHelpers.use_ansi_codes?
|
2012-09-09 00:19:15 -04:00
|
|
|
|
2012-09-09 00:42:02 -04:00
|
|
|
module TestUnitOutput
|
|
|
|
def handle_requirement(description)
|
|
|
|
error = yield
|
2012-09-09 00:19:15 -04:00
|
|
|
|
2012-09-09 00:42:02 -04:00
|
|
|
if error.empty?
|
|
|
|
print colorize_string('.')
|
|
|
|
else
|
|
|
|
print colorize_string(error[0..0])
|
|
|
|
end
|
2012-09-09 00:19:15 -04:00
|
|
|
end
|
|
|
|
|
2012-09-09 00:42:02 -04:00
|
|
|
def handle_summary
|
|
|
|
puts
|
|
|
|
puts ErrorLog if Backtraces
|
|
|
|
|
|
|
|
out = "%d tests, %d assertions, %d failures, %d errors" %
|
|
|
|
Counter.values_at(:specifications, :requirements, :failed, :errors)
|
|
|
|
|
|
|
|
if Counter.values_at(:failed, :errors).inject(:+) > 0
|
|
|
|
puts colorize_string(out, 'F')
|
|
|
|
else
|
|
|
|
puts colorize_string(out, '.')
|
|
|
|
end
|
2012-09-09 00:19:15 -04:00
|
|
|
end
|
|
|
|
|
2012-09-09 00:42:02 -04:00
|
|
|
def colorize_string(text, color = nil)
|
2012-09-09 00:46:37 -04:00
|
|
|
if USE_COLOR
|
2012-09-09 00:42:02 -04:00
|
|
|
"\e[#{ COLORS[color || text] }m#{ text }\e[0m"
|
|
|
|
else
|
|
|
|
text
|
|
|
|
end
|
2012-09-09 00:19:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
# A global space for storing temporary state during tests.
|
2012-06-27 11:53:38 -04:00
|
|
|
Pad = OpenStruct.new
|
|
|
|
def Pad.clear
|
2012-06-27 07:54:07 -04:00
|
|
|
@table = {}
|
|
|
|
end
|
2012-01-24 03:43:20 -05:00
|
|
|
|
|
|
|
# turn warnings off (esp for Pry::Hooks which will generate warnings
|
|
|
|
# in tests)
|
|
|
|
$VERBOSE = nil
|
|
|
|
|
2012-02-23 09:30:48 -05:00
|
|
|
# inject a variable into a binding
|
|
|
|
def inject_var(name, value, b)
|
|
|
|
Thread.current[:__pry_local__] = value
|
|
|
|
b.eval("#{name} = Thread.current[:__pry_local__]")
|
|
|
|
ensure
|
|
|
|
Thread.current[:__pry_local__] = nil
|
|
|
|
end
|
|
|
|
|
2012-04-18 01:19:25 -04:00
|
|
|
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
|
|
|
|
|
2012-04-18 02:18:33 -04:00
|
|
|
def mri18_and_no_real_source_location?
|
|
|
|
Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method)
|
|
|
|
end
|
|
|
|
|
2012-04-18 01:19:25 -04:00
|
|
|
# used by test_show_source.rb and test_documentation.rb
|
|
|
|
class TestClassForShowSource
|
|
|
|
def alpha
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-03 13:00:11 -04:00
|
|
|
class TestClassForShowSourceClassEval
|
|
|
|
def alpha
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestClassForShowSourceInstanceEval
|
|
|
|
def alpha
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2011-10-08 07:40:55 -04:00
|
|
|
Pry.config.auto_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
|
|
|
|
|
2012-01-08 01:01:15 -05:00
|
|
|
def mock_exception(*mock_backtrace)
|
|
|
|
e = StandardError.new("mock exception")
|
|
|
|
(class << e; self; end).class_eval do
|
|
|
|
define_method(:backtrace) { mock_backtrace }
|
2011-09-13 13:40:30 -04:00
|
|
|
end
|
2012-01-08 01:01:15 -05:00
|
|
|
e
|
2011-09-13 13:40:30 -04:00
|
|
|
end
|
|
|
|
|
2011-05-28 09:38:00 -04:00
|
|
|
Pry.reset_defaults
|
2011-04-04 08:18:07 -04:00
|
|
|
|
2011-08-31 13:05:21 -04:00
|
|
|
# this is to test exception code (cat --ex)
|
|
|
|
def broken_method
|
|
|
|
this method is broken
|
|
|
|
end
|
|
|
|
|
2011-05-18 23:28:38 -04:00
|
|
|
# sample doc
|
|
|
|
def sample_method
|
|
|
|
:sample
|
|
|
|
end
|
|
|
|
|
2011-08-27 10:13:15 -04:00
|
|
|
# another sample doc
|
|
|
|
def another_sample_method
|
|
|
|
:another_sample
|
|
|
|
end
|
|
|
|
|
2011-09-08 19:49:11 -04:00
|
|
|
# Set I/O streams.
|
|
|
|
#
|
|
|
|
# Out defaults to an anonymous StringIO.
|
|
|
|
#
|
|
|
|
def redirect_pry_io(new_in, new_out = StringIO.new)
|
2011-05-18 23:28:38 -04:00
|
|
|
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
|
|
|
|
|
2011-08-24 03:59:48 -04:00
|
|
|
def mock_pry(*args)
|
2012-07-17 21:55:18 -04:00
|
|
|
args.flatten!
|
2011-10-15 04:30:20 -04:00
|
|
|
binding = args.first.is_a?(Binding) ? args.shift : binding()
|
|
|
|
|
2011-08-24 03:59:48 -04:00
|
|
|
input = InputTester.new(*args)
|
|
|
|
output = StringIO.new
|
|
|
|
|
|
|
|
redirect_pry_io(input, output) do
|
2011-10-15 04:30:20 -04:00
|
|
|
binding.pry
|
2011-08-24 03:59:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
output.string
|
|
|
|
end
|
|
|
|
|
2011-12-31 10:21:58 -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-05-06 06:44:59 -04:00
|
|
|
def redirect_global_pry_input(new_io)
|
|
|
|
old_io = Pry.input
|
|
|
|
Pry.input = new_io
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Pry.input = old_io
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_global_pry_output(new_io)
|
|
|
|
old_io = Pry.output
|
|
|
|
Pry.output = new_io
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Pry.output = old_io
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
class Module
|
|
|
|
public :remove_const
|
2011-05-24 10:57:18 -04:00
|
|
|
public :remove_method
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
|
2011-03-05 09:17:54 -05:00
|
|
|
|
2010-12-21 09:03:52 -05:00
|
|
|
class InputTester
|
2010-12-24 03:30:51 -05:00
|
|
|
def initialize(*actions)
|
|
|
|
@orig_actions = actions.dup
|
|
|
|
@actions = actions
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
|
2011-01-09 06:51:45 -05:00
|
|
|
def readline(*)
|
2012-01-18 02:05:07 -05:00
|
|
|
@actions.shift
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rewind
|
2011-01-07 07:18:09 -05:00
|
|
|
@actions = @orig_actions.dup
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-19 03:50:45 -05:00
|
|
|
class Pry
|
|
|
|
|
|
|
|
# null output class - doesn't write anywwhere.
|
|
|
|
class NullOutput
|
|
|
|
def self.puts(*) end
|
2011-04-07 21:06:39 -04:00
|
|
|
def self.string(*) end
|
2011-01-19 03:50:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-13 13:40:30 -04:00
|
|
|
# Open a temp file and yield it to the block, closing it after
|
|
|
|
# @return [String] The path of the temp file
|
2012-01-15 01:06:24 -05:00
|
|
|
def temp_file(ext='.rb')
|
|
|
|
file = Tempfile.new(['pry', ext])
|
2011-09-13 13:40:30 -04:00
|
|
|
yield file
|
|
|
|
ensure
|
2012-01-15 01:06:24 -05:00
|
|
|
file.close(true) if file
|
2012-08-18 23:58:48 -04:00
|
|
|
File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx
|
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
|
|
|
|
|
|
|
|
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-07-24 08:49:30 -04:00
|
|
|
attr_reader :pry, :out
|
2012-07-24 04:36:53 -04:00
|
|
|
|
2012-09-09 01:46:45 -04:00
|
|
|
def initialize(context = TOPLEVEL_BINDING, options = {})
|
|
|
|
@pry = Pry.new(options)
|
2012-07-24 04:36:53 -04:00
|
|
|
|
|
|
|
if context
|
|
|
|
@pry.binding_stack << Pry.binding_for(context)
|
|
|
|
end
|
2012-08-18 18:17:26 -04:00
|
|
|
|
2012-08-18 23:58:48 -04:00
|
|
|
@pry.input_array << nil # TODO: shouldn't need this
|
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-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-08-19 16:04:05 -04:00
|
|
|
def context=(context)
|
|
|
|
@pry.binding_stack << Pry.binding_for(context)
|
|
|
|
end
|
|
|
|
|
2012-08-18 20:53:05 -04:00
|
|
|
# TODO: eliminate duplication with Pry#repl
|
|
|
|
def simulate_repl
|
|
|
|
didnt_exit = nil
|
|
|
|
break_data = nil
|
|
|
|
|
|
|
|
didnt_exit = catch(:didnt_exit) do
|
|
|
|
break_data = catch(:breakout) do
|
|
|
|
yield self
|
|
|
|
throw(:didnt_exit, true)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
raise "Failed to exit REPL" if didnt_exit
|
|
|
|
break_data
|
|
|
|
end
|
|
|
|
|
2012-07-24 08:49:30 -04:00
|
|
|
def last_output
|
|
|
|
@out.string if @out
|
|
|
|
end
|
|
|
|
|
2012-08-18 18:17:26 -04:00
|
|
|
def process_command(command_str, eval_str = '')
|
|
|
|
@pry.process_command(command_str, eval_str) or raise "Not a valid command"
|
|
|
|
last_command_result_or_output
|
|
|
|
end
|
|
|
|
|
2012-07-24 08:49:30 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2011-01-19 03:50:45 -05:00
|
|
|
|
2011-05-07 01:32:05 -04:00
|
|
|
CommandTester = Pry::CommandSet.new do
|
2011-01-17 09:38:09 -05:00
|
|
|
command "command1", "command 1 test" do
|
2011-01-21 04:17:12 -05:00
|
|
|
output.puts "command1"
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
|
2011-01-17 09:38:09 -05:00
|
|
|
command "command2", "command 2 test" do |arg|
|
2011-01-21 04:17:12 -05:00
|
|
|
output.puts arg
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
end
|
2011-11-26 02:24:50 -05:00
|
|
|
|
2012-08-18 18:17:26 -04:00
|
|
|
def unindent(*args)
|
|
|
|
Pry::Helpers::CommandHelpers.unindent(*args)
|
|
|
|
end
|
|
|
|
|
2011-11-26 02:24:50 -05:00
|
|
|
# to help with tracking down bugs that cause an infinite loop in the test suite
|
|
|
|
if ENV["SET_TRACE_FUNC"]
|
2011-12-27 17:38:25 -05:00
|
|
|
require 'set_trace' if Pry::Helpers::BaseHelpers.rbx?
|
2011-11-26 02:24:50 -05:00
|
|
|
set_trace_func proc { |event, file, line, id, binding, classname|
|
|
|
|
STDERR.printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
|
|
|
|
}
|
|
|
|
end
|