2012-05-04 17:46:01 -04:00
# encoding: utf-8
2011-02-19 15:44:08 -05:00
######################################################################
# This file is imported from the minitest project.
# DO NOT make modifications in this repo. They _will_ be reverted!
# File a patch instead and assign it to Ryan Davis.
######################################################################
2008-10-09 21:18:03 -04:00
2010-03-27 21:37:07 -04:00
require 'optparse'
2011-06-01 01:12:55 -04:00
require 'rbconfig'
2012-11-28 22:23:11 -05:00
require 'thread' # required for 1.8
require 'minitest/parallel_each'
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
##
2010-03-27 21:37:07 -04:00
# Minimal (mostly drop-in) replacement for test-unit.
2010-12-01 00:33:32 -05:00
#
# :include: README.txt
2008-10-09 21:18:03 -04:00
module MiniTest
2010-03-27 21:37:07 -04:00
2011-11-17 18:02:16 -05:00
def self . const_missing name # :nodoc:
case name
when :MINI_DIR then
msg = " MiniTest::MINI_DIR was removed. Don't violate other's internals. "
warn " WAR \ NING: #{ msg } "
warn " WAR \ NING: Used by #{ caller . first } . "
const_set :MINI_DIR , " bad value "
else
super
end
end
2010-03-27 21:37:07 -04:00
##
# Assertion base class
2008-10-09 21:18:03 -04:00
class Assertion < Exception ; end
2010-03-27 21:37:07 -04:00
##
# Assertion raised when skipping a test
2008-10-09 21:18:03 -04:00
class Skip < Assertion ; end
2012-11-28 22:23:11 -05:00
class << self
attr_accessor :backtrace_filter
end
2008-10-09 21:18:03 -04:00
2012-11-28 22:23:11 -05:00
class BacktraceFilter # :nodoc:
def filter bt
return [ " No backtrace " ] unless bt
2010-12-01 00:33:32 -05:00
2012-11-28 22:23:11 -05:00
new_bt = [ ]
unless $DEBUG then
bt . each do | line |
break if line =~ / lib \/ minitest /
new_bt << line
end
new_bt = bt . reject { | line | line =~ / lib \/ minitest / } if new_bt . empty?
new_bt = bt . dup if new_bt . empty?
else
new_bt = bt . dup
2010-12-01 00:33:32 -05:00
end
2012-11-28 22:23:11 -05:00
new_bt
2008-10-09 21:18:03 -04:00
end
2012-11-28 22:23:11 -05:00
end
2008-10-09 21:18:03 -04:00
2012-11-28 22:23:11 -05:00
self . backtrace_filter = BacktraceFilter . new
def self . filter_backtrace bt # :nodoc:
backtrace_filter . filter bt
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# MiniTest Assertions. All assertion methods accept a +msg+ which is
# printed if the assertion fails.
2008-10-09 21:18:03 -04:00
module Assertions
2012-05-04 17:46:01 -04:00
UNDEFINED = Object . new # :nodoc:
2010-03-27 21:37:07 -04:00
2012-05-04 17:46:01 -04:00
def UNDEFINED . inspect # :nodoc:
" UNDEFINED " # again with the rdoc bugs... :(
end
2010-03-27 21:37:07 -04:00
##
2011-06-01 01:12:55 -04:00
# Returns the diff command to use in #diff. Tries to intelligently
# figure out what diff to use.
def self . diff
2012-11-28 22:23:11 -05:00
@diff = if RbConfig :: CONFIG [ 'host_os' ] =~ / mswin|mingw / then
2011-06-01 01:12:55 -04:00
" diff.exe -u "
else
if system ( " gdiff " , __FILE__ , __FILE__ )
" gdiff -u " # solaris and kin suck
elsif system ( " diff " , __FILE__ , __FILE__ )
" diff -u "
else
nil
end
end unless defined? @diff
@diff
end
##
# Set the diff command to use in #diff.
def self . diff = o
@diff = o
end
##
# Returns a diff between +exp+ and +act+. If there is no known
# diff command or if it doesn't make sense to diff the output
# (single line, short output), then it simply returns a basic
# comparison between the two.
def diff exp , act
require " tempfile "
expect = mu_pp_for_diff exp
butwas = mu_pp_for_diff act
result = nil
need_to_diff =
MiniTest :: Assertions . diff &&
( expect . include? ( " \n " ) ||
butwas . include? ( " \n " ) ||
expect . size > 30 ||
butwas . size > 30 ||
expect == butwas )
return " Expected: #{ mu_pp exp } \n Actual: #{ mu_pp act } " unless
need_to_diff
Tempfile . open ( " expect " ) do | a |
a . puts expect
2011-06-01 18:34:09 -04:00
a . flush
2011-06-01 01:12:55 -04:00
Tempfile . open ( " butwas " ) do | b |
b . puts butwas
2011-06-01 18:34:09 -04:00
b . flush
2011-06-01 01:12:55 -04:00
result = ` #{ MiniTest :: Assertions . diff } #{ a . path } #{ b . path } `
result . sub! ( / ^ \ - \ - \ - .+ / , " --- expected " )
result . sub! ( / ^ \ + \ + \ + .+ / , " +++ actual " )
if result . empty? then
klass = exp . class
result = [
2012-11-28 22:23:11 -05:00
" No visible difference in the #{ klass } # inspect output. \n " ,
" You should look at the implementation of # == on " ,
" #{ klass } or its members. \n " ,
expect ,
] . join
2011-06-01 01:12:55 -04:00
end
end
end
result
end
##
# This returns a human-readable version of +obj+. By default
# #inspect is called. You can override this to use #pretty_print
# if you want.
2010-03-27 21:37:07 -04:00
def mu_pp obj
2008-10-09 21:18:03 -04:00
s = obj . inspect
2011-08-23 17:47:25 -04:00
s = s . encode Encoding . default_external if defined? Encoding
2008-10-09 21:18:03 -04:00
s
end
2011-06-01 01:12:55 -04:00
##
# This returns a diff-able human-readable version of +obj+. This
# differs from the regular mu_pp because it expands escaped
# 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' )
end
2010-03-27 21:37:07 -04:00
def _assertions = n # :nodoc:
2008-10-09 21:18:03 -04:00
@_assertions = n
end
2010-03-27 21:37:07 -04:00
def _assertions # :nodoc:
2008-10-09 21:18:03 -04:00
@_assertions || = 0
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +test+ is a true value.
2008-10-09 21:18:03 -04:00
def assert test , msg = nil
msg || = " Failed assertion, no message given. "
self . _assertions += 1
unless test then
msg = msg . call if Proc === msg
raise MiniTest :: Assertion , msg
end
true
end
2010-03-27 21:37:07 -04:00
##
# Fails unless the block returns a true value.
2012-08-20 20:39:57 -04:00
#
# NOTE: This method is deprecated, use assert. It will be removed
# on 2013-01-01."
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def assert_block msg = nil
2012-08-20 20:39:57 -04:00
warn " NOTE: MiniTest::Unit::TestCase # assert_block is deprecated, use assert. It will be removed on 2013-01-01. Called from #{ caller . first } "
2011-02-19 15:44:08 -05:00
msg = message ( msg ) { " Expected block to return true value " }
assert yield , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +obj+ is empty.
2008-10-09 21:18:03 -04:00
def assert_empty obj , msg = nil
2010-12-01 00:33:32 -05:00
msg = message ( msg ) { " Expected #{ mu_pp ( obj ) } to be empty " }
2008-10-09 21:18:03 -04:00
assert_respond_to obj , :empty?
assert obj . empty? , msg
end
2010-03-27 21:37:07 -04:00
##
2011-06-01 01:12:55 -04:00
# Fails unless <tt>exp == act</tt> printing the difference between
# the two, if possible.
#
# If there is no visible difference but the assertion fails, you
# should suspect that your #== is buggy, or your inspect output is
# missing crucial details.
2010-03-27 21:37:07 -04:00
#
2011-06-01 01:12:55 -04:00
# For floats use assert_in_delta.
#
# See also: MiniTest::Assertions.diff
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def assert_equal exp , act , msg = nil
2011-06-01 18:34:09 -04:00
msg = message ( msg , " " ) { diff exp , act }
2008-10-09 21:18:03 -04:00
assert ( exp == act , msg )
end
2010-03-27 21:37:07 -04:00
##
# For comparing Floats. Fails unless +exp+ and +act+ are within +delta+
# of each other.
#
# assert_in_delta Math::PI, (22.0 / 7.0), 0.01
2008-10-09 21:18:03 -04:00
def assert_in_delta exp , act , delta = 0 . 001 , msg = nil
n = ( exp - act ) . abs
2012-05-04 17:46:01 -04:00
msg = message ( msg ) { " Expected | #{ exp } - #{ act } | ( #{ n } ) to be < #{ delta } " }
2008-10-25 18:38:09 -04:00
assert delta > = n , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# For comparing Floats. Fails unless +exp+ and +act+ have a relative
# error less than +epsilon+.
2008-10-09 21:18:03 -04:00
def assert_in_epsilon a , b , epsilon = 0 . 001 , msg = nil
2012-05-04 17:46:01 -04:00
assert_in_delta a , b , [ a . abs , b . abs ] . min * epsilon , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +collection+ includes +obj+.
2008-10-09 21:18:03 -04:00
def assert_includes collection , obj , msg = nil
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
" Expected #{ mu_pp ( collection ) } to include #{ mu_pp ( obj ) } "
}
2008-10-09 21:18:03 -04:00
assert_respond_to collection , :include?
assert collection . include? ( obj ) , msg
end
2010-03-27 21:37:07 -04:00
##
2011-10-19 16:34:21 -04:00
# Fails unless +obj+ is an instance of +cls+.
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def assert_instance_of cls , obj , msg = nil
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
" Expected #{ mu_pp ( obj ) } to be an instance of #{ cls } , not #{ obj . class } "
}
2008-12-16 08:52:17 -05:00
assert obj . instance_of? ( cls ) , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +obj+ is a kind of +cls+.
2008-10-09 21:18:03 -04:00
def assert_kind_of cls , obj , msg = nil # TODO: merge with instance_of
msg = message ( msg ) {
" Expected #{ mu_pp ( obj ) } to be a kind of #{ cls } , not #{ obj . class } " }
2010-01-06 17:58:07 -05:00
2008-10-09 21:18:03 -04:00
assert obj . kind_of? ( cls ) , msg
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# Fails unless +matcher+ <tt>=~</tt> +obj+.
2010-03-27 21:37:07 -04:00
2012-05-04 17:46:01 -04:00
def assert_match matcher , obj , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp matcher } to match #{ mu_pp obj } " }
2012-05-09 00:09:25 -04:00
assert_respond_to matcher , :" =~ "
matcher = Regexp . new Regexp . escape matcher if String === matcher
2012-05-04 17:46:01 -04:00
assert matcher =~ obj , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +obj+ is nil
2008-10-09 21:18:03 -04:00
def assert_nil obj , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp ( obj ) } to be nil " }
assert obj . nil? , msg
end
2010-03-27 21:37:07 -04:00
##
2011-10-19 16:34:21 -04:00
# For testing with binary operators.
2010-03-27 21:37:07 -04:00
#
# assert_operator 5, :<=, 4
2011-10-19 16:34:21 -04:00
def assert_operator o1 , op , o2 = UNDEFINED , msg = nil
return assert_predicate o1 , op , msg if UNDEFINED == o2
2008-10-09 21:18:03 -04:00
msg = message ( msg ) { " Expected #{ mu_pp ( o1 ) } to be #{ op } #{ mu_pp ( o2 ) } " }
assert o1 . __send__ ( op , o2 ) , msg
end
2010-09-01 04:40:53 -04:00
##
# Fails if stdout or stderr do not output the expected results.
# Pass in nil if you don't care about that streams output. Pass in
2012-09-05 19:24:58 -04:00
# "" if you require it to be silent. Pass in a regexp if you want
# to pattern match.
2010-09-01 04:40:53 -04:00
#
2012-11-28 22:23:11 -05:00
# NOTE: this uses #capture_io, not #capture_subprocess_io.
#
2010-09-01 04:40:53 -04:00
# See also: #assert_silent
def assert_output stdout = nil , stderr = nil
out , err = capture_io do
yield
end
2012-09-05 19:24:58 -04:00
err_msg = Regexp === stderr ? :assert_match : :assert_equal if stderr
out_msg = Regexp === stdout ? :assert_match : :assert_equal if stdout
y = send err_msg , stderr , err , " In stderr " if err_msg
x = send out_msg , stdout , out , " In stdout " if out_msg
2010-09-01 04:40:53 -04:00
( ! stdout || x ) && ( ! stderr || y )
end
2011-10-19 16:34:21 -04:00
##
# For testing with predicates.
#
# assert_predicate str, :empty?
#
# This is really meant for specs and is front-ended by assert_operator:
#
# str.must_be :empty?
def assert_predicate o1 , op , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp ( o1 ) } to be #{ op } " }
assert o1 . __send__ ( op ) , msg
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# Fails unless the block raises one of +exp+. Returns the
# exception matched so you can check the message, attributes, etc.
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def assert_raises * exp
2012-05-04 17:46:01 -04:00
msg = " #{ exp . pop } . \n " if String === exp . last
2011-06-01 01:12:55 -04:00
2008-10-09 21:18:03 -04:00
begin
yield
2010-09-23 17:27:19 -04:00
rescue MiniTest :: Skip = > e
2012-11-28 22:23:11 -05:00
return e if exp . include? MiniTest :: Skip
raise e
2008-10-09 21:18:03 -04:00
rescue Exception = > e
2012-11-28 22:23:11 -05:00
expected = exp . any? { | ex |
if ex . instance_of? Module then
e . kind_of? ex
else
e . instance_of? ex
end
}
assert expected , proc {
exception_details ( e , " #{ msg } #{ mu_pp ( exp ) } exception expected, not " )
}
2008-10-25 18:38:09 -04:00
2008-10-09 21:18:03 -04:00
return e
end
exp = exp . first if exp . size == 1
2012-11-28 22:23:11 -05:00
flunk " #{ msg } #{ mu_pp ( exp ) } expected but nothing was raised. "
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +obj+ responds to +meth+.
2008-10-09 21:18:03 -04:00
def assert_respond_to obj , meth , msg = nil
msg = message ( msg ) {
2010-12-01 00:33:32 -05:00
" Expected #{ mu_pp ( obj ) } ( #{ obj . class } ) to respond to # #{ meth } "
}
2008-10-09 21:18:03 -04:00
assert obj . respond_to? ( meth ) , msg
end
2010-03-27 21:37:07 -04:00
##
# Fails unless +exp+ and +act+ are #equal?
2008-10-09 21:18:03 -04:00
def assert_same exp , act , msg = nil
msg = message ( msg ) {
data = [ mu_pp ( act ) , act . object_id , mu_pp ( exp ) , exp . object_id ]
2010-03-27 21:37:07 -04:00
" Expected %s (oid=%d) to be the same as %s (oid=%d) " % data
2008-10-09 21:18:03 -04:00
}
assert exp . equal? ( act ) , msg
end
2010-03-27 21:37:07 -04:00
##
# +send_ary+ is a receiver, message and arguments.
#
# Fails unless the call returns a true value
2010-09-23 17:27:19 -04:00
# TODO: I should prolly remove this from specs
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def assert_send send_ary , m = nil
recv , msg , * args = send_ary
m = message ( m ) {
" Expected #{ mu_pp ( recv ) } . #{ msg } (* #{ mu_pp ( args ) } ) to return true " }
assert recv . __send__ ( msg , * args ) , m
end
2010-09-01 04:40:53 -04:00
##
# Fails if the block outputs anything to stderr or stdout.
#
# See also: #assert_output
def assert_silent
assert_output " " , " " do
yield
end
end
2010-03-27 21:37:07 -04:00
##
# Fails unless the block throws +sym+
2008-10-09 21:18:03 -04:00
def assert_throws sym , msg = nil
default = " Expected #{ mu_pp ( sym ) } to have been thrown "
caught = true
catch ( sym ) do
begin
yield
2012-11-28 22:23:11 -05:00
rescue ThreadError = > e # wtf?!? 1.8 + threads == suck
default += " , not \ : #{ e . message [ / uncaught throw \ `( \ w+?) \ ' / , 1 ] } "
2008-10-09 21:18:03 -04:00
rescue ArgumentError = > e # 1.9 exception
default += " , not #{ e . message . split ( / / ) . last } "
rescue NameError = > e # 1.8 exception
default += " , not #{ e . name . inspect } "
end
caught = false
end
assert caught , message ( msg ) { default }
end
2010-03-27 21:37:07 -04:00
##
# Captures $stdout and $stderr into strings:
#
# out, err = capture_io do
2012-11-28 22:23:11 -05:00
# puts "Some info"
2010-03-27 21:37:07 -04:00
# warn "You did a bad thing"
# end
#
2012-11-28 22:23:11 -05:00
# assert_match %r%info%, out
2010-03-27 21:37:07 -04:00
# assert_match %r%bad%, err
2012-11-28 22:23:11 -05:00
#
# NOTE: For efficiency, this method uses StringIO and does not
# capture IO for subprocesses. Use #capture_subprocess_io for
# that.
2010-03-27 21:37:07 -04:00
2008-11-19 17:28:24 -05:00
def capture_io
require 'stringio'
2008-10-09 21:18:03 -04:00
2008-11-19 17:28:24 -05:00
captured_stdout , captured_stderr = StringIO . new , StringIO . new
2008-10-09 21:18:03 -04:00
2012-11-28 22:23:11 -05:00
synchronize do
orig_stdout , orig_stderr = $stdout , $stderr
$stdout , $stderr = captured_stdout , captured_stderr
begin
yield
ensure
$stdout = orig_stdout
$stderr = orig_stderr
end
end
2008-10-09 21:18:03 -04:00
return captured_stdout . string , captured_stderr . string
2012-11-28 22:23:11 -05:00
end
##
# Captures $stdout and $stderr into strings, using Tempfile to
# ensure that subprocess IO is captured as well.
#
# out, err = capture_subprocess_io do
# system "echo Some info"
# system "echo You did a bad thing 1>&2"
# end
#
# assert_match %r%info%, out
# assert_match %r%bad%, err
#
# NOTE: This method is approximately 10x slower than #capture_io so
# only use it when you need to test the output of a subprocess.
def capture_subprocess_io
require 'tempfile'
captured_stdout , captured_stderr = Tempfile . new ( " out " ) , Tempfile . new ( " err " )
synchronize do
orig_stdout , orig_stderr = $stdout . dup , $stderr . dup
$stdout . reopen captured_stdout
$stderr . reopen captured_stderr
begin
yield
$stdout . rewind
$stderr . rewind
[ captured_stdout . read , captured_stderr . read ]
ensure
captured_stdout . unlink
captured_stderr . unlink
$stdout . reopen orig_stdout
$stderr . reopen orig_stderr
end
end
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Returns details for exception +e+
2008-10-09 21:18:03 -04:00
def exception_details e , msg
2011-06-01 01:12:55 -04:00
[
" #{ msg } " ,
" Class: < #{ e . class } > " ,
" Message: < #{ e . message . inspect } > " ,
" ---Backtrace--- " ,
" #{ MiniTest :: filter_backtrace ( e . backtrace ) . join ( " \n " ) } " ,
" --------------- " ,
] . join " \n "
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails with +msg+
2008-10-09 21:18:03 -04:00
def flunk msg = nil
msg || = " Epic Fail! "
assert false , msg
end
2010-03-27 21:37:07 -04:00
##
# Returns a proc that will output +msg+ along with the default message.
2011-06-01 18:34:09 -04:00
def message msg = nil , ending = " . " , & default
2008-10-09 21:18:03 -04:00
proc {
2011-06-01 01:12:55 -04:00
custom_message = " #{ msg } . \n " unless msg . nil? or msg . to_s . empty?
2011-06-01 18:34:09 -04:00
" #{ custom_message } #{ default . call } #{ ending } "
2008-10-09 21:18:03 -04:00
}
end
2010-03-27 21:37:07 -04:00
##
2008-10-09 21:18:03 -04:00
# used for counting assertions
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def pass msg = nil
assert true
end
2010-03-27 21:37:07 -04:00
##
# Fails if +test+ is a true value
2008-10-09 21:18:03 -04:00
def refute test , msg = nil
msg || = " Failed refutation, no message given "
not assert ( ! test , msg )
end
2010-03-27 21:37:07 -04:00
##
# Fails if +obj+ is empty.
2008-10-09 21:18:03 -04:00
def refute_empty obj , msg = nil
2011-08-23 17:47:25 -04:00
msg = message ( msg ) { " Expected #{ mu_pp ( obj ) } to not be empty " }
2008-10-09 21:18:03 -04:00
assert_respond_to obj , :empty?
refute obj . empty? , msg
end
2010-03-27 21:37:07 -04:00
##
# Fails if <tt>exp == act</tt>.
#
# For floats use refute_in_delta.
2008-10-09 21:18:03 -04:00
def refute_equal exp , act , msg = nil
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
" Expected #{ mu_pp ( act ) } to not be equal to #{ mu_pp ( exp ) } "
}
2008-10-09 21:18:03 -04:00
refute exp == act , msg
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# For comparing Floats. Fails if +exp+ is within +delta+ of +act+.
2010-03-27 21:37:07 -04:00
#
# refute_in_delta Math::PI, (22.0 / 7.0)
2008-10-09 21:18:03 -04:00
def refute_in_delta exp , act , delta = 0 . 001 , msg = nil
n = ( exp - act ) . abs
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
2012-05-04 17:46:01 -04:00
" Expected | #{ exp } - #{ act } | ( #{ n } ) to not be < #{ delta } "
2010-01-06 17:58:07 -05:00
}
2008-10-09 21:18:03 -04:00
refute delta > n , msg
end
2010-03-27 21:37:07 -04:00
##
# For comparing Floats. Fails if +exp+ and +act+ have a relative error
# less than +epsilon+.
2008-10-09 21:18:03 -04:00
def refute_in_epsilon a , b , epsilon = 0 . 001 , msg = nil
refute_in_delta a , b , a * epsilon , msg
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# Fails if +collection+ includes +obj+.
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def refute_includes collection , obj , msg = nil
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
" Expected #{ mu_pp ( collection ) } to not include #{ mu_pp ( obj ) } "
}
2008-10-09 21:18:03 -04:00
assert_respond_to collection , :include?
refute collection . include? ( obj ) , msg
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# Fails if +obj+ is an instance of +cls+.
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def refute_instance_of cls , obj , msg = nil
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
" Expected #{ mu_pp ( obj ) } to not be an instance of #{ cls } "
}
2008-12-27 15:51:23 -05:00
refute obj . instance_of? ( cls ) , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# Fails if +obj+ is a kind of +cls+.
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
def refute_kind_of cls , obj , msg = nil # TODO: merge with instance_of
msg = message ( msg ) { " Expected #{ mu_pp ( obj ) } to not be a kind of #{ cls } " }
refute obj . kind_of? ( cls ) , msg
end
2010-03-27 21:37:07 -04:00
##
2012-05-04 17:46:01 -04:00
# Fails if +matcher+ <tt>=~</tt> +obj+.
2010-03-27 21:37:07 -04:00
2012-05-04 17:46:01 -04:00
def refute_match matcher , obj , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp matcher } to not match #{ mu_pp obj } " }
2012-05-09 00:09:25 -04:00
assert_respond_to matcher , :" =~ "
matcher = Regexp . new Regexp . escape matcher if String === matcher
2012-05-04 17:46:01 -04:00
refute matcher =~ obj , msg
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Fails if +obj+ is nil.
2008-10-09 21:18:03 -04:00
def refute_nil obj , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp ( obj ) } to not be nil " }
refute obj . nil? , msg
end
2010-03-27 21:37:07 -04:00
##
2011-08-23 17:47:25 -04:00
# Fails if +o1+ is not +op+ +o2+. Eg:
2010-03-27 21:37:07 -04:00
#
# refute_operator 1, :>, 2 #=> pass
# refute_operator 1, :<, 2 #=> fail
2011-10-19 16:34:21 -04:00
def refute_operator o1 , op , o2 = UNDEFINED , msg = nil
return refute_predicate o1 , op , msg if UNDEFINED == o2
msg = message ( msg ) { " Expected #{ mu_pp ( o1 ) } to not be #{ op } #{ mu_pp ( o2 ) } " }
2008-10-09 21:18:03 -04:00
refute o1 . __send__ ( op , o2 ) , msg
end
2011-10-19 16:34:21 -04:00
##
# For testing with predicates.
#
# refute_predicate str, :empty?
#
# This is really meant for specs and is front-ended by refute_operator:
#
# str.wont_be :empty?
def refute_predicate o1 , op , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp ( o1 ) } to not be #{ op } " }
refute o1 . __send__ ( op ) , msg
end
2010-03-27 21:37:07 -04:00
##
# Fails if +obj+ responds to the message +meth+.
2008-10-09 21:18:03 -04:00
def refute_respond_to obj , meth , msg = nil
msg = message ( msg ) { " Expected #{ mu_pp ( obj ) } to not respond to #{ meth } " }
2010-01-06 17:58:07 -05:00
2008-10-09 21:18:03 -04:00
refute obj . respond_to? ( meth ) , msg
end
2010-03-27 21:37:07 -04:00
##
# Fails if +exp+ is the same (by object identity) as +act+.
2008-10-09 21:18:03 -04:00
def refute_same exp , act , msg = nil
2010-01-06 17:58:07 -05:00
msg = message ( msg ) {
2010-03-27 21:37:07 -04:00
data = [ mu_pp ( act ) , act . object_id , mu_pp ( exp ) , exp . object_id ]
" Expected %s (oid=%d) to not be the same as %s (oid=%d) " % data
2010-01-06 17:58:07 -05:00
}
2008-10-09 21:18:03 -04:00
refute exp . equal? ( act ) , msg
end
2010-03-27 21:37:07 -04:00
##
# Skips the current test. Gets listed at the end of the run but
# doesn't cause a failure exit code.
2008-10-25 18:38:09 -04:00
def skip msg = nil , bt = caller
2008-10-09 21:18:03 -04:00
msg || = " Skipped, no message given "
2008-10-25 18:38:09 -04:00
raise MiniTest :: Skip , msg , bt
2008-10-09 21:18:03 -04:00
end
2012-11-28 22:23:11 -05:00
##
# Takes a block and wraps it with the runner's shared mutex.
def synchronize
Minitest :: Unit . runner . synchronize do
yield
end
end
2008-10-09 21:18:03 -04:00
end
2012-05-04 17:46:01 -04:00
class Unit # :nodoc:
2012-11-28 22:23:11 -05:00
VERSION = " 4.3.2 " # :nodoc:
2008-10-09 21:18:03 -04:00
2010-03-27 21:37:07 -04:00
attr_accessor :report , :failures , :errors , :skips # :nodoc:
attr_accessor :test_count , :assertion_count # :nodoc:
attr_accessor :start_time # :nodoc:
2010-12-01 00:33:32 -05:00
attr_accessor :help # :nodoc:
attr_accessor :verbose # :nodoc:
attr_writer :options # :nodoc:
2012-05-04 17:46:01 -04:00
##
# Lazy accessor for options.
2010-12-01 00:33:32 -05:00
def options
@options || = { }
end
2008-10-09 21:18:03 -04:00
@@installed_at_exit || = false
@@out = $stdout
2012-05-04 17:46:01 -04:00
@@after_tests = [ ]
2008-10-09 21:18:03 -04:00
2010-12-01 00:33:32 -05:00
##
2012-08-20 20:39:57 -04:00
# A simple hook allowing you to run a block of code after _all_ of
# the tests are done. Eg:
2010-12-01 00:33:32 -05:00
#
# MiniTest::Unit.after_tests { p $debugging_info }
2012-05-04 17:46:01 -04:00
def self . after_tests & block
@@after_tests << block
2010-12-01 00:33:32 -05:00
end
2010-03-27 21:37:07 -04:00
##
# Registers MiniTest::Unit to run tests at process exit
2008-10-09 21:18:03 -04:00
def self . autorun
at_exit {
2012-11-28 22:23:11 -05:00
# don't run if there was a non-exit exception
next if $! and not $! . kind_of? SystemExit
2010-12-15 17:23:52 -05:00
# the order here is important. The at_exit handler must be
# installed before anyone else gets a chance to install their
# own, that way we can be assured that our exit will be last
# to run (at_exit stacks).
exit_code = nil
2012-05-04 17:46:01 -04:00
at_exit {
@@after_tests . reverse_each ( & :call )
exit false if exit_code && exit_code != 0
}
2010-12-15 17:23:52 -05:00
2010-12-01 00:33:32 -05:00
exit_code = MiniTest :: Unit . new . run ARGV
2008-10-09 21:18:03 -04:00
} unless @@installed_at_exit
2008-12-12 19:22:31 -05:00
@@installed_at_exit = true
2008-10-09 21:18:03 -04:00
end
2010-12-01 00:33:32 -05:00
##
# Returns the stream to use for output.
def self . output
@@out
end
2010-03-27 21:37:07 -04:00
##
# Sets MiniTest::Unit to write output to +stream+. $stdout is the default
# output
2008-10-09 21:18:03 -04:00
def self . output = stream
@@out = stream
end
2011-06-01 18:34:09 -04:00
##
# Tells MiniTest::Unit to delegate to +runner+, an instance of a
# MiniTest::Unit subclass, when MiniTest::Unit#run is called.
def self . runner = runner
@@runner = runner
end
##
# Returns the MiniTest::Unit subclass instance that will be used
# to run the tests. A MiniTest::Unit instance is the default
# runner.
def self . runner
@@runner || = self . new
end
2010-12-01 00:33:32 -05:00
##
# Return all plugins' run methods (methods that start with "run_").
def self . plugins
@@plugins || = ( [ " run_tests " ] +
public_instance_methods ( false ) .
grep ( / ^run_ / ) . map { | s | s . to_s } ) . uniq
end
2012-05-04 17:46:01 -04:00
##
# Return the IO for output.
2010-12-01 00:33:32 -05:00
def output
self . class . output
end
def puts * a # :nodoc:
output . puts ( * a )
end
def print * a # :nodoc:
output . print ( * a )
end
2012-05-04 17:46:01 -04:00
##
# Runner for a given +type+ (eg, test vs bench).
2010-12-01 00:33:32 -05:00
def _run_anything type
suites = TestCase . send " #{ type } _suites "
return if suites . empty?
start = Time . now
puts
puts " # Running #{ type } s: "
puts
@test_count , @assertion_count = 0 , 0
sync = output . respond_to? :" sync= " # stupid emacs
old_sync , output . sync = output . sync , true if sync
results = _run_suites suites , type
2010-12-25 00:01:32 -05:00
@test_count = results . inject ( 0 ) { | sum , ( tc , _ ) | sum + tc }
@assertion_count = results . inject ( 0 ) { | sum , ( _ , ac ) | sum + ac }
2010-12-01 00:33:32 -05:00
output . sync = old_sync if sync
t = Time . now - start
puts
puts
puts " Finished #{ type } s in %.6fs, %.4f tests/s, %.4f assertions/s. " %
[ t , test_count / t , assertion_count / t ]
report . each_with_index do | msg , i |
puts " \n %3d) %s " % [ i + 1 , msg ]
end
puts
status
end
2012-05-04 17:46:01 -04:00
##
2012-11-28 22:23:11 -05:00
# Runs all the +suites+ for a given +type+. Runs suites declaring
# a test_order of +:parallel+ in parallel, and everything else
# serial.
2012-05-04 17:46:01 -04:00
2010-12-01 00:33:32 -05:00
def _run_suites suites , type
2012-11-28 22:23:11 -05:00
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 }
2010-12-01 00:33:32 -05:00
end
2012-05-04 17:46:01 -04:00
##
# Run a single +suite+ for a given +type+.
2010-12-01 00:33:32 -05:00
def _run_suite suite , type
header = " #{ type } _suite_header "
puts send ( header , suite ) if respond_to? header
filter = options [ :filter ] || '/./'
filter = Regexp . new $1 if filter =~ / \/ (.*) \/ /
assertions = suite . send ( " #{ type } _methods " ) . grep ( filter ) . map { | method |
inst = suite . new method
inst . _assertions = 0
2010-12-13 21:42:40 -05:00
print " #{ suite } # #{ method } = " if @verbose
2010-12-15 17:23:52 -05:00
2012-11-28 22:23:11 -05:00
start_time = Time . now if @verbose
2010-12-01 00:33:32 -05:00
result = inst . run self
2012-11-28 22:23:11 -05:00
print " %.2f s = " % ( Time . now - start_time ) if @verbose
2010-12-01 00:33:32 -05:00
print result
puts if @verbose
inst . _assertions
}
return assertions . size , assertions . inject ( 0 ) { | sum , n | sum + n }
end
2012-05-04 17:46:01 -04:00
##
# Record the result of a single run. Makes it very easy to gather
# information. Eg:
#
# class StatisticsRecorder < MiniTest::Unit
# def record suite, method, assertions, time, error
# # ... record the results somewhere ...
# end
# end
#
# MiniTest::Unit.runner = StatisticsRecorder.new
def record suite , method , assertions , time , error
end
2010-03-27 21:37:07 -04:00
def location e # :nodoc:
2008-12-27 15:51:23 -05:00
last_before_assertion = " "
e . backtrace . reverse_each do | s |
2009-06-18 03:31:39 -04:00
break if s =~ / in .(assert|refute|flunk|pass|fail|raise|must|wont) /
2008-12-27 15:51:23 -05:00
last_before_assertion = s
end
last_before_assertion . sub ( / :in .*$ / , '' )
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
# Writes status for failed test +meth+ in +klass+ which finished with
# exception +e+
2008-10-09 21:18:03 -04:00
def puke klass , meth , e
e = case e
when MiniTest :: Skip then
@skips += 1
2011-06-01 01:12:55 -04:00
return " S " unless @verbose
2008-10-09 21:18:03 -04:00
" Skipped: \n #{ meth } ( #{ klass } ) [ #{ location e } ]: \n #{ e . message } \n "
when MiniTest :: Assertion then
@failures += 1
" Failure: \n #{ meth } ( #{ klass } ) [ #{ location e } ]: \n #{ e . message } \n "
else
@errors += 1
2010-12-01 00:33:32 -05:00
bt = MiniTest :: filter_backtrace ( e . backtrace ) . join " \n "
2008-10-09 21:18:03 -04:00
" Error: \n #{ meth } ( #{ klass } ): \n #{ e . class } : #{ e . message } \n #{ bt } \n "
end
@report << e
e [ 0 , 1 ]
end
2010-03-27 21:37:07 -04:00
def initialize # :nodoc:
2008-10-09 21:18:03 -04:00
@report = [ ]
@errors = @failures = @skips = 0
@verbose = false
2012-11-28 22:23:11 -05:00
@mutex = Mutex . new
end
def synchronize # :nodoc:
@mutex . synchronize { yield }
2008-10-09 21:18:03 -04:00
end
2012-05-04 17:46:01 -04:00
def process_args args = [ ] # :nodoc:
2010-03-27 21:37:07 -04:00
options = { }
2010-12-01 00:33:32 -05:00
orig_args = args . dup
2010-03-27 21:37:07 -04:00
OptionParser . new do | opts |
opts . banner = 'minitest options:'
opts . version = MiniTest :: Unit :: VERSION
opts . on '-h' , '--help' , 'Display this help.' do
puts opts
exit
end
opts . on '-s' , '--seed SEED' , Integer , " Sets random seed " do | m |
options [ :seed ] = m . to_i
end
opts . on '-v' , '--verbose' , " Verbose. Show progress processing files. " do
options [ :verbose ] = true
end
2012-05-04 17:46:01 -04:00
opts . on '-n' , '--name PATTERN' , " Filter test names on pattern (e.g. /foo/) " do | a |
2010-03-27 21:37:07 -04:00
options [ :filter ] = a
end
2010-12-01 00:33:32 -05:00
opts . parse! args
orig_args -= args
end
unless options [ :seed ] then
srand
options [ :seed ] = srand % 0xFFFF
orig_args << " --seed " << options [ :seed ] . to_s
2010-03-27 21:37:07 -04:00
end
2010-12-01 00:33:32 -05:00
srand options [ :seed ]
self . verbose = options [ :verbose ]
@help = orig_args . map { | s | s =~ / [ \ s|&<>$()] / ? s . inspect : s } . join " "
2010-03-27 21:37:07 -04:00
options
end
2008-10-09 21:18:03 -04:00
##
2011-06-01 18:34:09 -04:00
# Begins the full test run. Delegates to +runner+'s #_run method.
2008-10-09 21:18:03 -04:00
def run args = [ ]
2011-06-01 18:34:09 -04:00
self . class . runner . _run ( args )
end
##
# Top level driver, controls all output and filtering.
def _run args = [ ]
2010-12-01 00:33:32 -05:00
self . options = process_args args
2010-03-27 21:37:07 -04:00
2010-12-01 00:33:32 -05:00
puts " Run options: #{ help } "
2010-03-27 21:37:07 -04:00
2010-12-01 00:33:32 -05:00
self . class . plugins . each do | plugin |
send plugin
break unless report . empty?
2010-03-27 21:37:07 -04:00
end
2008-10-09 21:18:03 -04:00
return failures + errors if @test_count > 0 # or return nil...
2009-06-25 05:46:17 -04:00
rescue Interrupt
abort 'Interrupted'
end
2010-03-27 21:37:07 -04:00
##
2010-12-01 00:33:32 -05:00
# Runs test suites matching +filter+.
2010-03-27 21:37:07 -04:00
2010-12-01 00:33:32 -05:00
def run_tests
_run_anything :test
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
##
2010-12-01 00:33:32 -05:00
# Writes status to +io+
2010-03-27 21:37:07 -04:00
2010-12-01 00:33:32 -05:00
def status io = self . output
format = " %d tests, %d assertions, %d failures, %d errors, %d skips "
io . puts format % [ test_count , assertion_count , failures , errors , skips ]
2008-10-09 21:18:03 -04:00
end
2012-05-04 17:46:01 -04:00
##
# Provides a simple set of guards that you can use in your tests
# to skip execution if it is not applicable. These methods are
# mixed into TestCase as both instance and class methods so you
# can use them inside or outside of the test methods.
#
# def test_something_for_mri
# skip "bug 1234" if jruby?
# # ...
# end
#
# if windows? then
# # ... lots of test methods ...
# end
module Guard
##
# Is this running on jruby?
def jruby? platform = RUBY_PLATFORM
" java " == platform
end
##
# Is this running on mri?
def mri? platform = RUBY_DESCRIPTION
/ ^ruby / =~ platform
end
##
# Is this running on rubinius?
def rubinius? platform = defined? ( RUBY_ENGINE ) && RUBY_ENGINE
" rbx " == platform
end
##
# Is this running on windows?
def windows? platform = RUBY_PLATFORM
/ mswin|mingw / =~ platform
end
end
2012-08-20 20:39:57 -04:00
##
# Provides before/after hooks for setup and teardown. These are
# meant for library writers, NOT for regular test authors. See
# #before_setup for an example.
module LifecycleHooks
##
# Runs before every test, after setup. This hook is meant for
# libraries to extend minitest. It is not meant to be used by
# test developers.
#
# See #before_setup for an example.
def after_setup ; end
##
# Runs before every test, before setup. This hook is meant for
# libraries to extend minitest. It is not meant to be used by
# test developers.
#
# As a simplistic example:
#
# module MyMinitestPlugin
# def before_setup
# super
# # ... stuff to do before setup is run
# end
#
# def after_setup
# # ... stuff to do after setup is run
# super
# end
#
# def before_teardown
# super
# # ... stuff to do before teardown is run
# end
#
# def after_teardown
# # ... stuff to do after teardown is run
# super
# end
# end
#
# class MiniTest::Unit::TestCase
# include MyMinitestPlugin
# end
def before_setup ; end
##
# Runs after every test, before teardown. This hook is meant for
# libraries to extend minitest. It is not meant to be used by
# test developers.
#
# See #before_setup for an example.
def before_teardown ; end
##
# Runs after every test, after teardown. This hook is meant for
# libraries to extend minitest. It is not meant to be used by
# test developers.
#
# See #before_setup for an example.
def after_teardown ; end
end
module Deprecated # :nodoc:
2012-11-28 22:23:11 -05:00
##
# This entire module is deprecated and slated for removal on 2013-01-01.
2012-08-20 20:39:57 -04:00
module Hooks
2012-11-28 22:23:11 -05:00
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
2012-08-20 20:39:57 -04:00
##
# 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.
2012-11-28 22:23:11 -05:00
def add_setup_hook arg = nil , & block
2012-08-20 20:39:57 -04:00
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
2012-11-28 22:23:11 -05:00
def setup_hooks # :nodoc:
2012-08-20 20:39:57 -04:00
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.
2012-11-28 22:23:11 -05:00
def add_teardown_hook arg = nil , & block
2012-08-20 20:39:57 -04:00
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
2012-11-28 22:23:11 -05:00
def teardown_hooks # :nodoc:
2012-08-20 20:39:57 -04:00
if superclass . respond_to? :teardown_hooks then
superclass . teardown_hooks
else
[ ]
end + @teardown_hooks
end
end
end
2010-03-27 21:37:07 -04:00
##
2010-12-01 00:33:32 -05:00
# Subclass TestCase to create your own tests. Typically you'll want a
2010-03-27 21:37:07 -04:00
# TestCase subclass per implementation class.
2010-12-01 00:33:32 -05:00
#
# See MiniTest::Assertions
2010-03-27 21:37:07 -04:00
2008-10-09 21:18:03 -04:00
class TestCase
2012-08-20 20:39:57 -04:00
include LifecycleHooks
include Deprecated :: Hooks
2012-11-28 22:23:11 -05:00
extend Deprecated :: HooksCM # UGH... I can't wait 'til 2013!
2012-05-04 17:46:01 -04:00
include Guard
extend Guard
2010-03-27 21:37:07 -04:00
attr_reader :__name__ # :nodoc:
2008-10-09 21:18:03 -04:00
2010-03-27 21:37:07 -04:00
PASSTHROUGH_EXCEPTIONS = [ NoMemoryError , SignalException ,
Interrupt , SystemExit ] # :nodoc:
2009-06-25 05:46:17 -04:00
2010-03-27 21:37:07 -04:00
SUPPORTS_INFO_SIGNAL = Signal . list [ 'INFO' ] # :nodoc:
##
# Runs the tests reporting the status to +runner+
2009-06-25 05:46:17 -04:00
2008-10-09 21:18:03 -04:00
def run runner
2010-12-01 00:33:32 -05:00
trap " INFO " do
2012-05-04 17:46:01 -04:00
runner . report . each_with_index do | msg , i |
warn " \n %3d) %s " % [ i + 1 , msg ]
end
warn ''
2010-12-15 17:23:52 -05:00
time = runner . start_time ? Time . now - runner . start_time : 0
2012-05-04 17:46:01 -04:00
warn " Current Test: %s # %s %.2fs " % [ self . class , self . __name__ , time ]
2009-06-25 05:46:17 -04:00
runner . status $stderr
end if SUPPORTS_INFO_SIGNAL
2012-11-28 22:23:11 -05:00
start_time = Time . now
2010-12-01 00:33:32 -05:00
result = " "
2008-10-09 21:18:03 -04:00
begin
@passed = nil
2012-05-04 17:46:01 -04:00
self . before_setup
2008-10-09 21:18:03 -04:00
self . setup
2012-05-04 17:46:01 -04:00
self . after_setup
2011-11-17 18:02:16 -05:00
self . run_test self . __name__
2010-12-01 00:33:32 -05:00
result = " . " unless io?
2012-11-28 22:23:11 -05:00
time = Time . now - start_time
runner . record self . class , self . __name__ , self . _assertions , time , nil
2008-10-09 21:18:03 -04:00
@passed = true
2009-06-25 05:46:17 -04:00
rescue * PASSTHROUGH_EXCEPTIONS
raise
2008-10-09 21:18:03 -04:00
rescue Exception = > e
@passed = false
2012-11-28 22:23:11 -05:00
time = Time . now - start_time
runner . record self . class , self . __name__ , self . _assertions , time , e
2010-12-01 00:33:32 -05:00
result = runner . puke self . class , self . __name__ , e
2008-10-09 21:18:03 -04:00
ensure
2012-05-04 17:46:01 -04:00
%w{ before_teardown teardown after_teardown } . each do | hook |
begin
self . send hook
rescue * PASSTHROUGH_EXCEPTIONS
raise
rescue Exception = > e
2012-08-20 20:39:57 -04:00
@passed = false
2012-05-04 17:46:01 -04:00
result = runner . puke self . class , self . __name__ , e
end
2008-10-09 21:18:03 -04:00
end
2009-06-25 05:46:17 -04:00
trap 'INFO' , 'DEFAULT' if SUPPORTS_INFO_SIGNAL
2008-10-09 21:18:03 -04:00
end
result
end
2011-11-17 18:02:16 -05:00
alias :run_test :__send__
2010-03-27 21:37:07 -04:00
def initialize name # :nodoc:
2009-06-18 03:31:39 -04:00
@__name__ = name
2010-12-01 00:33:32 -05:00
@__io__ = nil
2008-10-09 21:18:03 -04:00
@passed = nil
2011-11-17 18:02:16 -05:00
@@current = self
end
def self . current # :nodoc:
@@current
2008-10-09 21:18:03 -04:00
end
2012-05-04 17:46:01 -04:00
##
# Return the output IO object
2010-12-01 00:33:32 -05:00
def io
@__io__ = true
MiniTest :: Unit . output
end
2012-05-04 17:46:01 -04:00
##
# Have we hooked up the IO yet?
2010-12-01 00:33:32 -05:00
def io?
@__io__
end
2010-03-27 21:37:07 -04:00
def self . reset # :nodoc:
2008-10-09 21:18:03 -04:00
@@test_suites = { }
end
reset
2011-08-23 17:47:25 -04:00
##
# Call this at the top of your tests when you absolutely
# positively need to have ordered tests. In doing so, you're
# admitting that you suck and your tests are weak.
def self . i_suck_and_my_tests_are_order_dependent!
class << self
2012-05-04 17:46:01 -04:00
undef_method :test_order if method_defined? :test_order
2011-08-23 17:47:25 -04:00
define_method :test_order do :alpha end
end
end
2012-11-28 22:23:11 -05:00
##
# Make diffs for this TestCase use #pretty_inspect so that diff
# in assert_equal can be more details. NOTE: this is much slower
# than the regular inspect but much more usable for complex
# objects.
def self . make_my_diffs_pretty!
require 'pp'
define_method :mu_pp do | o |
o . pretty_inspect
end
end
##
# Call this at the top of your tests when you want to run your
# tests in parallel. In doing so, you're admitting that you rule
# and your tests are awesome.
def self . parallelize_me!
class << self
undef_method :test_order if method_defined? :test_order
define_method :test_order do :parallel end
end
end
2010-03-27 21:37:07 -04:00
def self . inherited klass # :nodoc:
2008-10-09 21:18:03 -04:00
@@test_suites [ klass ] = true
2011-08-23 17:47:25 -04:00
klass . reset_setup_teardown_hooks
super
2008-10-09 21:18:03 -04:00
end
2011-08-23 17:47:25 -04:00
def self . test_order # :nodoc:
2008-10-09 21:18:03 -04:00
:random
end
2010-03-27 21:37:07 -04:00
def self . test_suites # :nodoc:
2010-12-01 00:33:32 -05:00
@@test_suites . keys . sort_by { | ts | ts . name . to_s }
2008-10-09 21:18:03 -04:00
end
2010-03-27 21:37:07 -04:00
def self . test_methods # :nodoc:
methods = public_instance_methods ( true ) . grep ( / ^test / ) . map { | m | m . to_s }
2008-10-09 21:18:03 -04:00
2010-03-27 21:37:07 -04:00
case self . test_order
2012-11-28 22:23:11 -05:00
when :parallel
max = methods . size
ParallelEach . new methods . sort . sort_by { rand max }
2010-03-27 21:37:07 -04:00
when :random then
2008-10-09 21:18:03 -04:00
max = methods . size
2010-12-01 00:33:32 -05:00
methods . sort . sort_by { rand max }
2010-03-27 21:37:07 -04:00
when :alpha , :sorted then
methods . sort
else
raise " Unknown test_order: #{ self . test_order . inspect } "
2008-10-09 21:18:03 -04:00
end
end
2010-03-27 21:37:07 -04:00
##
# Returns true if the test passed.
2008-10-09 21:18:03 -04:00
def passed?
@passed
end
2010-03-27 21:37:07 -04:00
##
2012-08-20 20:39:57 -04:00
# Runs before every test. Use this to set up before each test
# run.
2010-03-27 21:37:07 -04:00
def setup ; end
2012-05-04 17:46:01 -04:00
##
2012-08-20 20:39:57 -04:00
# Runs after every test. Use this to clean up after each test
# run.
2010-03-27 21:37:07 -04:00
def teardown ; end
2011-08-23 17:47:25 -04:00
def self . reset_setup_teardown_hooks # :nodoc:
2012-08-20 20:39:57 -04:00
# also deprecated... believe it.
2011-08-23 17:47:25 -04:00
@setup_hooks = [ ]
@teardown_hooks = [ ]
end
reset_setup_teardown_hooks
2008-10-09 21:18:03 -04:00
include MiniTest :: Assertions
end # class TestCase
2010-01-06 17:58:07 -05:00
end # class Unit
end # module MiniTest
2009-06-25 05:46:17 -04:00
2012-08-20 20:39:57 -04:00
Minitest = MiniTest # :nodoc: because ugh... I typo this all the time
2012-05-04 17:46:01 -04:00
2009-06-25 05:46:17 -04:00
if $DEBUG then
2010-03-27 21:37:07 -04:00
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
2009-06-25 05:46:17 -04:00
end
end
end