Project

General

Profile

Bug #8778 ยป 0001-Bring-Test-Unit-up-to-100-documentation.patch

steveklabnik (Steve Klabnik), 08/18/2013 03:09 AM

View differences:

lib/test/unit.rb
# test/unit compatibility layer using minitest.
require 'minitest/unit'
require 'test/unit/assertions'
require 'test/unit/testcase'
require 'optparse'
module Test
##
# = Test/Unit
#
# Test/Unit is an implementation of the xUnit testing framework for Ruby.
#
# If you are writing new test code, please use MiniTest instead of Test/Unit.
# Test/Unit has been left in the standard library to support legacy test
# suites.
module Unit
TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest' # :nodoc:
module RunCount
@@run_count = 0
def self.have_run?
def self.have_run? # :nodoc:
@@run_count.nonzero?
end
def run(*)
def run(*) # :nodoc:
@@run_count += 1
super
end
def run_once
def run_once # :nodoc:
return if have_run?
return if $! # don't run if there was an exception
yield
......
end
module Options
def initialize(*, &block)
def initialize(*, &block) # :nodoc:
@init_hook = block
@options = nil
super(&nil)
end
def option_parser
def option_parser # :nodoc:
@option_parser ||= OptionParser.new
end
def process_args(args = [])
def process_args(args = []) # :nodoc:
return @options if @options
orig_args = args.dup
options = {}
......
module GlobOption
@@testfile_prefix = "test"
def setup_options(parser, options)
def setup_options(parser, options) # :nodoc:
super
parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir|
options[:base_directory] = dir
......
end
end
def non_options(files, options)
def non_options(files, options) # :nodoc:
paths = [options.delete(:base_directory), nil].uniq
if reject = options.delete(:reject)
reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
......
end
module LoadPathOption
def setup_options(parser, options)
def setup_options(parser, options) # :nodoc:
super
parser.on '-Idirectory', 'Add library load path' do |dirs|
dirs.split(':').each { |d| $LOAD_PATH.unshift d }
......
end
module GCStressOption
def setup_options(parser, options)
def setup_options(parser, options) # :nodoc:
super
parser.on '--[no-]gc-stress', 'Set GC.stress as true' do |flag|
options[:gc_stress] = flag
end
end
def non_options(files, options)
def non_options(files, options) # :nodoc:
if options.delete(:gc_stress)
MiniTest::Unit::TestCase.class_eval do
oldrun = instance_method(:run)
......
end
module RequireFiles
def non_options(files, options)
def non_options(files, options) # :nodoc:
return false if !super
result = false
files.each {|f|
......
include Test::Unit::RunCount
class Worker
def self.launch(ruby,args=[])
def self.launch(ruby,args=[]) # :nodoc:
io = IO.popen([*ruby,
"#{File.dirname(__FILE__)}/unit/parallel.rb",
*args], "rb+")
new(io, io.pid, :waiting)
end
attr_reader :quit_called
attr_reader :quit_called # :nodoc:
def initialize(io, pid, status)
def initialize(io, pid, status) # :nodoc:
@io = io
@pid = pid
@status = status
......
@quit_called = false
end
def puts(*args)
def puts(*args) # :nodoc:
@io.puts(*args)
end
def run(task,type)
def run(task,type) # :nodoc:
@file = File.basename(task, ".rb")
@real_file = task
begin
......
end
end
def hook(id,&block)
def hook(id,&block) # :nodoc:
@hooks[id] ||= []
@hooks[id] << block
self
end
def read
def read # :nodoc:
res = (@status == :quit) ? @io.read : @io.gets
res && res.chomp
end
def close
def close # :nodoc:
@io.close unless @io.closed?
self
rescue IOError
end
def quit
def quit # :nodoc:
return if @io.closed?
@quit_called = true
@io.puts "quit"
@io.close
end
def kill
def kill # :nodoc:
Process.kill(:KILL, @pid)
rescue Errno::ESRCH
end
def died(*additional)
def died(*additional) # :nodoc:
@status = :quit
@io.close
call_hook(:dead,*additional)
end
def to_s
def to_s # :nodoc:
if @file
"#{@pid}=#{@file}"
else
......
end
end
attr_reader :io, :pid
attr_accessor :status, :file, :real_file, :loadpath
attr_reader :io, :pid # :nodoc:
attr_accessor :status, :file, :real_file, :loadpath # :nodoc:
private
......
class << self; undef autorun; end
@@stop_auto_run = false
def self.autorun
def self.autorun # :nodoc:
at_exit {
Test::Unit::RunCount.run_once {
exit(Test::Unit::Runner.new.run(ARGV) || true)
......
@@installed_at_exit = true
end
def after_worker_down(worker, e=nil, c=false)
def after_worker_down(worker, e=nil, c=false) # :nodoc:
return unless @options[:parallel]
return if @interrupt
warn e if e
......
exit c
end
def terminal_width
def terminal_width # :nodoc:
unless @terminal_width ||= nil
begin
require 'io/console'
......
@terminal_width
end
def del_status_line
def del_status_line # :nodoc:
@status_line_size ||= 0
unless @options[:job_status] == :replace
$stdout.puts
......
@status_line_size = 0
end
def put_status(line)
def put_status(line) # :nodoc:
unless @options[:job_status] == :replace
print(line)
return
......
@status_line_size = line.size
end
def add_status(line)
def add_status(line) # :nodoc:
unless @options[:job_status] == :replace
print(line)
return
......
@status_line_size += line.size
end
def jobs_status
def jobs_status # :nodoc:
return unless @options[:job_status]
puts "" unless @options[:verbose] or @options[:job_status] == :replace
status_line = @workers.map(&:to_s).join(" ")
update_status(status_line) or (puts; nil)
end
def del_jobs_status
def del_jobs_status # :nodoc:
return unless @options[:job_status] == :replace && @status_line_size.nonzero?
del_status_line
end
def after_worker_quit(worker)
def after_worker_quit(worker) # :nodoc:
return unless @options[:parallel]
return if @interrupt
@workers.delete(worker)
......
@ios = @workers.map(&:io)
end
def launch_worker
def launch_worker # :nodoc:
begin
worker = Worker.launch(@options[:ruby],@args)
rescue => e
......
worker
end
def delete_worker(worker)
def delete_worker(worker) # :nodoc:
@workers_hash.delete worker.io
@workers.delete worker
@ios.delete worker.io
end
def quit_workers
def quit_workers # :nodoc:
return if @workers.empty?
@workers.reject! do |worker|
begin
......
end
end
def start_watchdog
def start_watchdog # :nodoc:
Thread.new do
while stat = Process.wait2
break if @interrupt # Break when interrupt
......
end
end
def deal(io, type, result, rep, shutting_down = false)
def deal(io, type, result, rep, shutting_down = false) # :nodoc:
worker = @workers_hash[io]
case worker.read
when /^okay$/
......
return false
end
def _run_parallel suites, type, result
def _run_parallel suites, type, result # :nodoc:
if @options[:parallel] < 1
warn "Error: parameter of -j option should be greater than 0."
return
......
end
end
def _run_suites suites, type
def _run_suites suites, type # :nodoc:
_prepare_run(suites, type)
@interrupt = nil
result = []
......
alias mini_run_suite _run_suite
def output
def output # :nodoc:
(@output ||= nil) || super
end
def _prepare_run(suites, type)
def _prepare_run(suites, type) # :nodoc:
options[:job_status] ||= :replace if @tty && !@verbose
case options[:color]
when :always
......
@total_tests = total.to_s(10)
end
def new_test(s)
def new_test(s) # :nodoc:
@test_count += 1
update_status(s)
end
def update_status(s)
def update_status(s) # :nodoc:
count = @test_count.to_s(10).rjust(@total_tests.size)
put_status("#{@passed_color}[#{count}/#{@total_tests}]#{@reset_color} #{s}")
end
def _print(s); $stdout.print(s); end
def succeed; del_status_line; end
def _print(s); $stdout.print(s); end # :nodoc:
def succeed; del_status_line; end # :nodoc:
def failed(s)
sep = "\n"
......
end
class StatusLineOutput < Struct.new(:runner)
def puts(*a) $stdout.puts(*a) unless a.empty? end
def respond_to_missing?(*a) $stdout.respond_to?(*a) end
def method_missing(*a, &b) $stdout.__send__(*a, &b) end
def puts(*a) $stdout.puts(*a) unless a.empty? end # :nodoc:
def respond_to_missing?(*a) $stdout.respond_to?(*a) end # :nodoc:
def method_missing(*a, &b) $stdout.__send__(*a, &b) end # :nodoc:
def print(s)
def print(s) # :nodoc:
case s
when /\A(.*\#.*) = \z/
runner.new_test($1)
......
include Test::Unit::RequireFiles
end
attr_accessor :to_run, :options
attr_accessor :to_run, :options # :nodoc:
def initialize(force_standalone = false, default_dir = nil, argv = ARGV)
def initialize(force_standalone = false, default_dir = nil, argv = ARGV) # :nodoc:
@force_standalone = force_standalone
@runner = Runner.new do |files, options|
options[:base_directory] ||= default_dir
......
@argv = argv
end
def process_args(*args)
def process_args(*args) # :nodoc:
@runner.process_args(*args)
!@to_run.empty?
end
def run
def run # :nodoc:
if @force_standalone and not process_args(@argv)
abort @options.banner
end
@runner.run(@argv) || true
end
def self.run(*args)
def self.run(*args) # :nodoc:
new(*args).run
end
end
class ProxyError < StandardError
def initialize(ex)
def initialize(ex) # :nodoc:
@message = ex.message
@backtrace = ex.backtrace
end
attr_accessor :message, :backtrace
attr_accessor :message, :backtrace # :nodoc:
end
end
end
module MiniTest # :nodoc:
module Unit # :nodoc:
end
end
class MiniTest::Unit::TestCase
undef run_test
RUN_TEST_TRACE = "#{__FILE__}:#{__LINE__+3}:in `run_test'".freeze
def run_test(name)
RUN_TEST_TRACE = "#{__FILE__}:#{__LINE__+3}:in `run_test'".freeze # :nodoc:
def run_test(name) # :nodoc:
progname, $0 = $0, "#{$0}: #{self.class}##{name}"
self.__send__(name)
ensure
lib/test/unit/assertions.rb
template.gsub(/\G((?:[^\\]|\\.)*?)(\\)?\?/) { $1 + ($2 ? "?" : mu_pp(arguments.shift)) }
end
def message(msg = nil, *args, &default)
def message(msg = nil, *args, &default) # :nodoc:
if Proc === msg
super(nil, *args) do
[msg.call, (default.call if default)].compact.reject(&:empty?).join(".\n")
lib/test/unit/parallel.rb
module Test
module Unit
class Worker < Runner
class Worker < Runner # :nodoc:
class << self
undef autorun
end
......
undef _run_suites
undef run
def increment_io(orig)
def increment_io(orig) # :nodoc:
*rest, io = 32.times.inject([orig.dup]){|ios, | ios << ios.last.dup }
rest.each(&:close)
io
end
def _run_suites(suites, type)
def _run_suites(suites, type) # :nodoc:
suites.map do |suite|
_run_suite(suite, type)
end
end
def _run_suite(suite, type)
def _run_suite(suite, type) # :nodoc:
@partial_report = []
orig_testout = MiniTest::Unit.output
i,o = IO.pipe
......
i.close if i && !i.closed?
end
def run(args = [])
def run(args = []) # :nodoc:
process_args args
@@stop_auto_run = true
@opts = @options.dup
......
end
end
def _report(res, *args)
def _report(res, *args) # :nodoc:
res = "#{res} #{args.pack("m0")}" unless args.empty?
@stdout.puts(res)
end
def puke(klass, meth, e)
def puke(klass, meth, e) # :nodoc:
@partial_report << [klass.name, meth, e.is_a?(MiniTest::Assertion) ? e : ProxyError.new(e)]
super
end
......
module Unit
class TestCase < MiniTest::Unit::TestCase
undef on_parallel_worker?
def on_parallel_worker?
def on_parallel_worker? # :nodoc:
true
end
end
end
end
require 'rubygems'
module Gem # :nodoc
end
class Gem::TestCase < MiniTest::Unit::TestCase
@@project_dir = File.expand_path('../../../..', __FILE__)
end
lib/test/unit/testcase.rb
class TestCase < MiniTest::Unit::TestCase
include Assertions
def on_parallel_worker?
def on_parallel_worker? # :nodoc:
false
end
def run runner
def run runner # :nodoc:
@options = runner.options
super runner
end
def self.test_order
def self.test_order # :nodoc:
:sorted
end
def self.method_added(name)
def self.method_added(name) # :nodoc:
return unless name.to_s.start_with?("test_")
@test_methods ||= {}
if @test_methods[name]
    (1-1/1)