aboutsummaryrefslogtreecommitdiffstats
path: root/lib/minitest/unit.rb
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-02 04:48:43 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-02 04:48:43 +0000
commitf8e5c7c79e720d3b0af3cb96f27d421f08eb7744 (patch)
tree0d950c668cf9141d516152aa7ca117d62afa3676 /lib/minitest/unit.rb
parentce3029cd19dc40f4ffe09b8c6633500bd182b324 (diff)
downloadruby-f8e5c7c79e720d3b0af3cb96f27d421f08eb7744.tar.gz
Imported minitest 4.7.4 (r8483)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest/unit.rb')
-rw-r--r--lib/minitest/unit.rb249
1 files changed, 97 insertions, 152 deletions
diff --git a/lib/minitest/unit.rb b/lib/minitest/unit.rb
index a221dda4b4..a29c8ec93b 100644
--- a/lib/minitest/unit.rb
+++ b/lib/minitest/unit.rb
@@ -5,10 +5,8 @@
# File a patch instead and assign it to Ryan Davis.
######################################################################
-require 'optparse'
-require 'rbconfig'
-require 'thread' # required for 1.8
-require 'minitest/parallel_each'
+require "optparse"
+require "rbconfig"
##
# Minimal (mostly drop-in) replacement for test-unit.
@@ -40,6 +38,9 @@ module MiniTest
class Skip < Assertion; end
class << self
+ ##
+ # Filter object for backtraces.
+
attr_accessor :backtrace_filter
end
@@ -87,16 +88,17 @@ module MiniTest
# figure out what diff to use.
def self.diff
- @diff = if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ then
+ @diff = if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ &&
+ system("diff.exe", __FILE__, __FILE__)) then
"diff.exe -u"
+ elsif Minitest::Unit::Guard.maglev? then # HACK
+ "diff -u"
+ elsif system("gdiff", __FILE__, __FILE__)
+ "gdiff -u" # solaris and kin suck
+ elsif system("diff", __FILE__, __FILE__)
+ "diff -u"
else
- if system("gdiff", __FILE__, __FILE__)
- "gdiff -u" # solaris and kin suck
- elsif system("diff", __FILE__, __FILE__)
- "diff -u"
- else
- nil
- end
+ nil
end unless defined? @diff
@diff
@@ -177,8 +179,8 @@ module MiniTest
# newlines and makes hex-values generic (like object_ids). This
# uses mu_pp to do the first pass and then cleans it up.
- def mu_pp_for_diff obj # TODO: possibly rename
- mu_pp(obj).gsub(/\\n/, "\n").gsub(/0x[a-f0-9]+/m, '0xXXXXXX')
+ def mu_pp_for_diff obj
+ mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ':0xXXXXXX')
end
def _assertions= n # :nodoc:
@@ -203,18 +205,6 @@ module MiniTest
end
##
- # Fails unless the block returns a true value.
- #
- # NOTE: This method is deprecated, use assert. It will be removed
- # on 2013-01-01."
-
- def assert_block msg = nil
- warn "NOTE: MiniTest::Unit::TestCase#assert_block is deprecated, use assert. It will be removed on 2013-01-01. Called from #{caller.first}"
- msg = message(msg) { "Expected block to return true value" }
- assert yield, msg
- end
-
- ##
# Fails unless +obj+ is empty.
def assert_empty obj, msg = nil
@@ -237,7 +227,7 @@ module MiniTest
def assert_equal exp, act, msg = nil
msg = message(msg, "") { diff exp, act }
- assert(exp == act, msg)
+ assert exp == act, msg
end
##
@@ -248,7 +238,9 @@ module MiniTest
def assert_in_delta exp, act, delta = 0.001, msg = nil
n = (exp - act).abs
- msg = message(msg) { "Expected |#{exp} - #{act}| (#{n}) to be < #{delta}"}
+ msg = message(msg) {
+ "Expected |#{exp} - #{act}| (#{n}) to be <= #{delta}"
+ }
assert delta >= n, msg
end
@@ -562,6 +554,7 @@ module MiniTest
def message msg = nil, ending = ".", &default
proc {
+ msg = msg.call.chomp(".") if Proc === msg
custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
"#{custom_message}#{default.call}#{ending}"
}
@@ -611,9 +604,9 @@ module MiniTest
def refute_in_delta exp, act, delta = 0.001, msg = nil
n = (exp - act).abs
msg = message(msg) {
- "Expected |#{exp} - #{act}| (#{n}) to not be < #{delta}"
+ "Expected |#{exp} - #{act}| (#{n}) to not be <= #{delta}"
}
- refute delta > n, msg
+ refute delta >= n, msg
end
##
@@ -723,10 +716,18 @@ module MiniTest
def skip msg = nil, bt = caller
msg ||= "Skipped, no message given"
+ @skip = true
raise MiniTest::Skip, msg, bt
end
##
+ # Was this testcase skipped? Meant for #teardown.
+
+ def skipped?
+ defined?(@skip) and @skip
+ end
+
+ ##
# Takes a block and wraps it with the runner's shared mutex.
def synchronize
@@ -737,16 +738,28 @@ module MiniTest
end
class Unit # :nodoc:
- VERSION = "4.3.2" # :nodoc:
+ VERSION = "4.7.4" # :nodoc:
attr_accessor :report, :failures, :errors, :skips # :nodoc:
- attr_accessor :test_count, :assertion_count # :nodoc:
+ attr_accessor :assertion_count # :nodoc:
+ attr_writer :test_count # :nodoc:
attr_accessor :start_time # :nodoc:
attr_accessor :help # :nodoc:
attr_accessor :verbose # :nodoc:
attr_writer :options # :nodoc:
##
+ # :attr:
+ #
+ # if true, installs an "INFO" signal handler (only available to BSD and
+ # OS X users) which prints diagnostic information about the test run.
+ #
+ # This is auto-detected by default but may be overridden by custom
+ # runners.
+
+ attr_accessor :info_signal
+
+ ##
# Lazy accessor for options.
def options
@@ -847,6 +860,10 @@ module MiniTest
output.print(*a)
end
+ def test_count # :nodoc:
+ @test_count ||= 0
+ end
+
##
# Runner for a given +type+ (eg, test vs bench).
@@ -888,15 +905,13 @@ module MiniTest
end
##
- # Runs all the +suites+ for a given +type+. Runs suites declaring
- # a test_order of +:parallel+ in parallel, and everything else
- # serial.
+ # Runs all the +suites+ for a given +type+.
+ #
+ # NOTE: this method is redefined in parallel_each.rb, which is
+ # loaded if a test-suite calls parallelize_me!.
def _run_suites suites, type
- parallel, serial = suites.partition { |s| s.test_order == :parallel }
-
- ParallelEach.new(parallel).map { |suite| _run_suite suite, type } +
- serial.map { |suite| _run_suite suite, type }
+ suites.map { |suite| _run_suite suite, type }
end
##
@@ -909,7 +924,13 @@ module MiniTest
filter = options[:filter] || '/./'
filter = Regexp.new $1 if filter =~ /\/(.*)\//
- assertions = suite.send("#{type}_methods").grep(filter).map { |method|
+ all_test_methods = suite.send "#{type}_methods"
+
+ filtered_test_methods = all_test_methods.find_all { |m|
+ filter === m || filter === "#{suite}##{m}"
+ }
+
+ assertions = filtered_test_methods.map { |method|
inst = suite.new method
inst._assertions = 0
@@ -929,7 +950,7 @@ module MiniTest
end
##
- # Record the result of a single run. Makes it very easy to gather
+ # Record the result of a single test. Makes it very easy to gather
# information. Eg:
#
# class StatisticsRecorder < MiniTest::Unit
@@ -939,6 +960,11 @@ module MiniTest
# end
#
# MiniTest::Unit.runner = StatisticsRecorder.new
+ #
+ # NOTE: record might be sent more than once per test. It will be
+ # sent once with the results from the test itself. If there is a
+ # failure or error in teardown, it will be sent again with the
+ # error or failure.
def record suite, method, assertions, time, error
end
@@ -961,14 +987,14 @@ module MiniTest
when MiniTest::Skip then
@skips += 1
return "S" unless @verbose
- "Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
+ "Skipped:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
when MiniTest::Assertion then
@failures += 1
- "Failure:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
+ "Failure:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
else
@errors += 1
bt = MiniTest::filter_backtrace(e.backtrace).join "\n "
- "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n #{bt}\n"
+ "Error:\n#{klass}##{meth}:\n#{e.class}: #{e.message}\n #{bt}\n"
end
@report << e
e[0, 1]
@@ -978,11 +1004,16 @@ module MiniTest
@report = []
@errors = @failures = @skips = 0
@verbose = false
- @mutex = Mutex.new
+ @mutex = defined?(Mutex) ? Mutex.new : nil
+ @info_signal = Signal.list['INFO']
end
def synchronize # :nodoc:
- @mutex.synchronize { yield }
+ if @mutex then
+ @mutex.synchronize { yield }
+ else
+ yield
+ end
end
def process_args args = [] # :nodoc:
@@ -1039,7 +1070,8 @@ module MiniTest
# Top level driver, controls all output and filtering.
def _run args = []
- self.options = process_args args
+ args = process_args args # ARGH!! blame test/unit process_args
+ self.options.merge! args
puts "Run options: #{help}"
@@ -1048,7 +1080,7 @@ module MiniTest
break unless report.empty?
end
- return failures + errors if @test_count > 0 # or return nil...
+ return failures + errors if self.test_count > 0 # or return nil...
rescue Interrupt
abort 'Interrupted'
end
@@ -1095,6 +1127,15 @@ module MiniTest
##
# Is this running on mri?
+ def maglev? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE
+ "maglev" == platform
+ end
+
+ module_function :maglev?
+
+ ##
+ # Is this running on mri?
+
def mri? platform = RUBY_DESCRIPTION
/^ruby/ =~ platform
end
@@ -1183,79 +1224,6 @@ module MiniTest
def after_teardown; end
end
- module Deprecated # :nodoc:
-
- ##
- # This entire module is deprecated and slated for removal on 2013-01-01.
-
- module Hooks
- def run_setup_hooks # :nodoc:
- _run_hooks self.class.setup_hooks
- end
-
- def _run_hooks hooks # :nodoc:
- hooks.each do |hook|
- if hook.respond_to?(:arity) && hook.arity == 1
- hook.call(self)
- else
- hook.call
- end
- end
- end
-
- def run_teardown_hooks # :nodoc:
- _run_hooks self.class.teardown_hooks.reverse
- end
- end
-
- ##
- # This entire module is deprecated and slated for removal on 2013-01-01.
-
- module HooksCM
- ##
- # Adds a block of code that will be executed before every
- # TestCase is run.
- #
- # NOTE: This method is deprecated, use before/after_setup. It
- # will be removed on 2013-01-01.
-
- def add_setup_hook arg=nil, &block
- warn "NOTE: MiniTest::Unit::TestCase.add_setup_hook is deprecated, use before/after_setup via a module (and call super!). It will be removed on 2013-01-01. Called from #{caller.first}"
- hook = arg || block
- @setup_hooks << hook
- end
-
- def setup_hooks # :nodoc:
- if superclass.respond_to? :setup_hooks then
- superclass.setup_hooks
- else
- []
- end + @setup_hooks
- end
-
- ##
- # Adds a block of code that will be executed after every
- # TestCase is run.
- #
- # NOTE: This method is deprecated, use before/after_teardown. It
- # will be removed on 2013-01-01.
-
- def add_teardown_hook arg=nil, &block
- warn "NOTE: MiniTest::Unit::TestCase#add_teardown_hook is deprecated, use before/after_teardown. It will be removed on 2013-01-01. Called from #{caller.first}"
- hook = arg || block
- @teardown_hooks << hook
- end
-
- def teardown_hooks # :nodoc:
- if superclass.respond_to? :teardown_hooks then
- superclass.teardown_hooks
- else
- []
- end + @teardown_hooks
- end
- end
- end
-
##
# Subclass TestCase to create your own tests. Typically you'll want a
# TestCase subclass per implementation class.
@@ -1264,8 +1232,6 @@ module MiniTest
class TestCase
include LifecycleHooks
- include Deprecated::Hooks
- extend Deprecated::HooksCM # UGH... I can't wait 'til 2013!
include Guard
extend Guard
@@ -1274,8 +1240,6 @@ module MiniTest
PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException,
Interrupt, SystemExit] # :nodoc:
- SUPPORTS_INFO_SIGNAL = Signal.list['INFO'] # :nodoc:
-
##
# Runs the tests reporting the status to +runner+
@@ -1288,7 +1252,7 @@ module MiniTest
time = runner.start_time ? Time.now - runner.start_time : 0
warn "Current Test: %s#%s %.2fs" % [self.class, self.__name__, time]
runner.status $stderr
- end if SUPPORTS_INFO_SIGNAL
+ end if runner.info_signal
start_time = Time.now
@@ -1306,7 +1270,7 @@ module MiniTest
rescue *PASSTHROUGH_EXCEPTIONS
raise
rescue Exception => e
- @passed = false
+ @passed = Skip === e
time = Time.now - start_time
runner.record self.class, self.__name__, self._assertions, time, e
result = runner.puke self.class, self.__name__, e
@@ -1318,10 +1282,11 @@ module MiniTest
raise
rescue Exception => e
@passed = false
+ runner.record self.class, self.__name__, self._assertions, time, e
result = runner.puke self.class, self.__name__, e
end
end
- trap 'INFO', 'DEFAULT' if SUPPORTS_INFO_SIGNAL
+ trap 'INFO', 'DEFAULT' if runner.info_signal
end
result
end
@@ -1332,11 +1297,11 @@ module MiniTest
@__name__ = name
@__io__ = nil
@passed = nil
- @@current = self
+ @@current = self # FIX: make thread local
end
def self.current # :nodoc:
- @@current
+ @@current # FIX: make thread local
end
##
@@ -1392,6 +1357,8 @@ module MiniTest
# and your tests are awesome.
def self.parallelize_me!
+ require "minitest/parallel_each"
+
class << self
undef_method :test_order if method_defined? :test_order
define_method :test_order do :parallel end
@@ -1400,7 +1367,6 @@ module MiniTest
def self.inherited klass # :nodoc:
@@test_suites[klass] = true
- klass.reset_setup_teardown_hooks
super
end
@@ -1448,30 +1414,9 @@ module MiniTest
def teardown; end
- def self.reset_setup_teardown_hooks # :nodoc:
- # also deprecated... believe it.
- @setup_hooks = []
- @teardown_hooks = []
- end
-
- reset_setup_teardown_hooks
-
include MiniTest::Assertions
end # class TestCase
end # class Unit
end # module MiniTest
Minitest = MiniTest # :nodoc: because ugh... I typo this all the time
-
-if $DEBUG then
- module Test # :nodoc:
- module Unit # :nodoc:
- class TestCase # :nodoc:
- def self.inherited x # :nodoc:
- # this helps me ferret out porting issues
- raise "Using minitest and test/unit in the same process: #{x}"
- end
- end
- end
- end
-end