expose assert_raise and assert_join_threads
This commit is contained in:
parent
55de0282da
commit
15606963de
Notes:
git
2019-10-01 22:19:45 +09:00
@ -48,53 +48,6 @@ module Test
|
|||||||
assert yield, *msgs
|
assert yield, *msgs
|
||||||
end
|
end
|
||||||
|
|
||||||
# :call-seq:
|
|
||||||
# assert_raise( *args, &block )
|
|
||||||
#
|
|
||||||
#Tests if the given block raises an exception. Acceptable exception
|
|
||||||
#types may be given as optional arguments. If the last argument is a
|
|
||||||
#String, it will be used as the error message.
|
|
||||||
#
|
|
||||||
# assert_raise do #Fails, no Exceptions are raised
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# assert_raise NameError do
|
|
||||||
# puts x #Raises NameError, so assertion succeeds
|
|
||||||
# end
|
|
||||||
def assert_raise(*exp, &b)
|
|
||||||
case exp.last
|
|
||||||
when String, Proc
|
|
||||||
msg = exp.pop
|
|
||||||
end
|
|
||||||
|
|
||||||
begin
|
|
||||||
yield
|
|
||||||
rescue MiniTest::Skip => e
|
|
||||||
return e if exp.include? MiniTest::Skip
|
|
||||||
raise e
|
|
||||||
rescue Exception => e
|
|
||||||
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, message(msg) {"#{mu_pp(exp)} exception expected, not"}.call)
|
|
||||||
}
|
|
||||||
|
|
||||||
return e
|
|
||||||
ensure
|
|
||||||
unless e
|
|
||||||
exp = exp.first if exp.size == 1
|
|
||||||
|
|
||||||
flunk(message(msg) {"#{mu_pp(exp)} expected but nothing was raised"})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def assert_raises(*exp, &b)
|
def assert_raises(*exp, &b)
|
||||||
raise NoMethodError, "use assert_raise", caller
|
raise NoMethodError, "use assert_raise", caller
|
||||||
end
|
end
|
||||||
@ -611,38 +564,6 @@ EOT
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# threads should respond to shift method.
|
|
||||||
# Array can be used.
|
|
||||||
def assert_join_threads(threads, message = nil)
|
|
||||||
errs = []
|
|
||||||
values = []
|
|
||||||
while th = threads.shift
|
|
||||||
begin
|
|
||||||
values << th.value
|
|
||||||
rescue Exception
|
|
||||||
errs << [th, $!]
|
|
||||||
th = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
values
|
|
||||||
ensure
|
|
||||||
if th&.alive?
|
|
||||||
th.raise(Timeout::Error.new)
|
|
||||||
th.join rescue errs << [th, $!]
|
|
||||||
end
|
|
||||||
if !errs.empty?
|
|
||||||
msg = "exceptions on #{errs.length} threads:\n" +
|
|
||||||
errs.map {|t, err|
|
|
||||||
"#{t.inspect}:\n" +
|
|
||||||
err.full_message(highlight: false, order: :top)
|
|
||||||
}.join("\n---\n")
|
|
||||||
if message
|
|
||||||
msg = "#{message}\n#{msg}"
|
|
||||||
end
|
|
||||||
raise MiniTest::Assertion, msg
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def assert_all_assertions_foreach(msg = nil, *keys, &block)
|
def assert_all_assertions_foreach(msg = nil, *keys, &block)
|
||||||
all = AllFailures.new
|
all = AllFailures.new
|
||||||
all.foreach(*keys, &block)
|
all.foreach(*keys, &block)
|
||||||
@ -655,24 +576,6 @@ EOT
|
|||||||
template &&= template.chomp
|
template &&= template.chomp
|
||||||
template.gsub(/\G((?:[^\\]|\\.)*?)(\\)?\?/) { $1 + ($2 ? "?" : mu_pp(arguments.shift)) }
|
template.gsub(/\G((?:[^\\]|\\.)*?)(\\)?\?/) { $1 + ($2 ? "?" : mu_pp(arguments.shift)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def message(msg = nil, *args, &default) # :nodoc:
|
|
||||||
if Proc === msg
|
|
||||||
super(nil, *args) do
|
|
||||||
ary = [msg.call, (default.call if default)].compact.reject(&:empty?)
|
|
||||||
if 1 < ary.length
|
|
||||||
ary[0...-1] = ary[0...-1].map {|str| str.sub(/(?<!\.)\z/, '.') }
|
|
||||||
end
|
|
||||||
begin
|
|
||||||
ary.join("\n")
|
|
||||||
rescue Encoding::CompatibilityError
|
|
||||||
ary.map(&:b).join("\n")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,34 +2,40 @@
|
|||||||
|
|
||||||
module Test
|
module Test
|
||||||
module Unit
|
module Unit
|
||||||
|
module Assertions
|
||||||
|
def _assertions= n # :nodoc:
|
||||||
|
@_assertions = n
|
||||||
|
end
|
||||||
|
|
||||||
|
def _assertions # :nodoc:
|
||||||
|
@_assertions ||= 0
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns a proc that will output +msg+ along with the default message.
|
||||||
|
|
||||||
|
def message msg = nil, ending = nil, &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 || "."}"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module CoreAssertions
|
module CoreAssertions
|
||||||
if defined?(MiniTest)
|
if defined?(MiniTest)
|
||||||
require_relative '../../envutil'
|
require_relative '../../envutil'
|
||||||
# for ruby core testing
|
# for ruby core testing
|
||||||
include MiniTest::Assertions
|
include MiniTest::Assertions
|
||||||
else
|
else
|
||||||
|
module MiniTest
|
||||||
|
class Skip; end
|
||||||
|
end
|
||||||
|
|
||||||
require 'pp'
|
require 'pp'
|
||||||
require_relative 'envutil'
|
require_relative 'envutil'
|
||||||
include Test::Unit::Assertions
|
include Test::Unit::Assertions
|
||||||
|
|
||||||
def _assertions= n # :nodoc:
|
|
||||||
@_assertions = n
|
|
||||||
end
|
|
||||||
|
|
||||||
def _assertions # :nodoc:
|
|
||||||
@_assertions ||= 0
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Returns a proc that will output +msg+ along with the default message.
|
|
||||||
|
|
||||||
def message msg = nil, ending = nil, &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 || "."}"
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def mu_pp(obj) #:nodoc:
|
def mu_pp(obj) #:nodoc:
|
||||||
@ -172,6 +178,53 @@ eom
|
|||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# :call-seq:
|
||||||
|
# assert_raise( *args, &block )
|
||||||
|
#
|
||||||
|
#Tests if the given block raises an exception. Acceptable exception
|
||||||
|
#types may be given as optional arguments. If the last argument is a
|
||||||
|
#String, it will be used as the error message.
|
||||||
|
#
|
||||||
|
# assert_raise do #Fails, no Exceptions are raised
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# assert_raise NameError do
|
||||||
|
# puts x #Raises NameError, so assertion succeeds
|
||||||
|
# end
|
||||||
|
def assert_raise(*exp, &b)
|
||||||
|
case exp.last
|
||||||
|
when String, Proc
|
||||||
|
msg = exp.pop
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
rescue MiniTest::Skip => e
|
||||||
|
return e if exp.include? MiniTest::Skip
|
||||||
|
raise e
|
||||||
|
rescue Exception => e
|
||||||
|
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, message(msg) {"#{mu_pp(exp)} exception expected, not"}.call)
|
||||||
|
}
|
||||||
|
|
||||||
|
return e
|
||||||
|
ensure
|
||||||
|
unless e
|
||||||
|
exp = exp.first if exp.size == 1
|
||||||
|
|
||||||
|
flunk(message(msg) {"#{mu_pp(exp)} expected but nothing was raised"})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# :call-seq:
|
# :call-seq:
|
||||||
# assert_raise_with_message(exception, expected, msg = nil, &block)
|
# assert_raise_with_message(exception, expected, msg = nil, &block)
|
||||||
#
|
#
|
||||||
@ -301,6 +354,38 @@ eom
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# threads should respond to shift method.
|
||||||
|
# Array can be used.
|
||||||
|
def assert_join_threads(threads, message = nil)
|
||||||
|
errs = []
|
||||||
|
values = []
|
||||||
|
while th = threads.shift
|
||||||
|
begin
|
||||||
|
values << th.value
|
||||||
|
rescue Exception
|
||||||
|
errs << [th, $!]
|
||||||
|
th = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
values
|
||||||
|
ensure
|
||||||
|
if th&.alive?
|
||||||
|
th.raise(Timeout::Error.new)
|
||||||
|
th.join rescue errs << [th, $!]
|
||||||
|
end
|
||||||
|
if !errs.empty?
|
||||||
|
msg = "exceptions on #{errs.length} threads:\n" +
|
||||||
|
errs.map {|t, err|
|
||||||
|
"#{t.inspect}:\n" +
|
||||||
|
err.full_message(highlight: false, order: :top)
|
||||||
|
}.join("\n---\n")
|
||||||
|
if message
|
||||||
|
msg = "#{message}\n#{msg}"
|
||||||
|
end
|
||||||
|
raise MiniTest::Assertion, msg
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def assert_all_assertions(msg = nil)
|
def assert_all_assertions(msg = nil)
|
||||||
all = AllFailures.new
|
all = AllFailures.new
|
||||||
yield all
|
yield all
|
||||||
@ -309,6 +394,23 @@ eom
|
|||||||
end
|
end
|
||||||
alias all_assertions assert_all_assertions
|
alias all_assertions assert_all_assertions
|
||||||
|
|
||||||
|
def message(msg = nil, *args, &default) # :nodoc:
|
||||||
|
if Proc === msg
|
||||||
|
super(nil, *args) do
|
||||||
|
ary = [msg.call, (default.call if default)].compact.reject(&:empty?)
|
||||||
|
if 1 < ary.length
|
||||||
|
ary[0...-1] = ary[0...-1].map {|str| str.sub(/(?<!\.)\z/, '.') }
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
ary.join("\n")
|
||||||
|
rescue Encoding::CompatibilityError
|
||||||
|
ary.map(&:b).join("\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user