2008-12-11 05:40:24 -05:00
|
|
|
require 'minitest/unit'
|
|
|
|
require 'pp'
|
|
|
|
|
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
module Assertions
|
|
|
|
include MiniTest::Assertions
|
|
|
|
|
|
|
|
def mu_pp(obj)
|
|
|
|
obj.pretty_inspect.chomp
|
|
|
|
end
|
|
|
|
|
2010-04-30 11:26:34 -04:00
|
|
|
def assert(test, msg = (nomsg = true; nil))
|
|
|
|
unless nomsg or msg.instance_of?(String) or msg.instance_of?(Proc) or
|
2010-05-01 09:41:10 -04:00
|
|
|
(bt = caller).first.rindex(MiniTest::MINI_DIR, 0)
|
2010-05-01 09:27:49 -04:00
|
|
|
bt.delete_if {|s| s.rindex(MiniTest::MINI_DIR, 0)}
|
2010-04-30 11:26:34 -04:00
|
|
|
raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2008-12-11 05:40:24 -05:00
|
|
|
def assert_raise(*args, &b)
|
|
|
|
assert_raises(*args, &b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_nothing_raised(*args)
|
2008-12-22 07:11:43 -05:00
|
|
|
self._assertions += 1
|
2008-12-11 05:40:24 -05:00
|
|
|
if Module === args.last
|
|
|
|
msg = nil
|
|
|
|
else
|
|
|
|
msg = args.pop
|
|
|
|
end
|
|
|
|
begin
|
2009-01-12 03:23:07 -05:00
|
|
|
line = __LINE__; yield
|
2010-09-27 02:28:59 -04:00
|
|
|
rescue MiniTest::Skip
|
|
|
|
raise
|
2008-12-11 05:40:24 -05:00
|
|
|
rescue Exception => e
|
2009-01-12 03:23:07 -05:00
|
|
|
bt = e.backtrace
|
|
|
|
as = e.instance_of?(MiniTest::Assertion)
|
|
|
|
if as
|
|
|
|
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
|
2009-01-30 10:09:17 -05:00
|
|
|
bt.reject! {|ln| ans =~ ln}
|
2009-01-12 03:23:07 -05:00
|
|
|
end
|
|
|
|
if ((args.empty? && !as) ||
|
2008-12-11 05:40:24 -05:00
|
|
|
args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
|
|
|
|
msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
|
2009-01-12 03:23:07 -05:00
|
|
|
raise MiniTest::Assertion, msg.call, bt
|
2008-12-11 05:40:24 -05:00
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_nothing_thrown(msg=nil)
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue ArgumentError => error
|
|
|
|
raise error if /\Auncaught throw (.+)\z/m !~ error.message
|
|
|
|
msg = message(msg) { "<#{$1}> was thrown when nothing was expected" }
|
|
|
|
flunk(msg)
|
|
|
|
end
|
|
|
|
assert(true, "Expected nothing to be thrown")
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_equal(exp, act, msg = nil)
|
|
|
|
msg = message(msg) {
|
|
|
|
exp_str = mu_pp(exp)
|
|
|
|
act_str = mu_pp(act)
|
|
|
|
exp_comment = ''
|
|
|
|
act_comment = ''
|
|
|
|
if exp_str == act_str
|
2008-12-23 04:34:08 -05:00
|
|
|
if (exp.is_a?(String) && act.is_a?(String)) ||
|
|
|
|
(exp.is_a?(Regexp) && act.is_a?(Regexp))
|
2008-12-11 05:40:24 -05:00
|
|
|
exp_comment = " (#{exp.encoding})"
|
|
|
|
act_comment = " (#{act.encoding})"
|
2008-12-23 04:34:08 -05:00
|
|
|
elsif exp.is_a?(Float) && act.is_a?(Float)
|
|
|
|
exp_str = "%\#.#{Float::DIG+2}g" % exp
|
|
|
|
act_str = "%\#.#{Float::DIG+2}g" % act
|
2008-12-11 05:40:24 -05:00
|
|
|
elsif exp.is_a?(Time) && act.is_a?(Time)
|
2009-09-29 09:22:27 -04:00
|
|
|
if exp.subsec * 1000_000_000 == exp.nsec
|
|
|
|
exp_comment = " (#{exp.nsec}[ns])"
|
|
|
|
else
|
|
|
|
exp_comment = " (subsec=#{exp.subsec})"
|
|
|
|
end
|
|
|
|
if act.subsec * 1000_000_000 == act.nsec
|
|
|
|
act_comment = " (#{act.nsec}[ns])"
|
|
|
|
else
|
|
|
|
act_comment = " (subsec=#{act.subsec})"
|
|
|
|
end
|
2009-01-31 08:51:46 -05:00
|
|
|
elsif exp.class != act.class
|
|
|
|
# a subclass of Range, for example.
|
|
|
|
exp_comment = " (#{exp.class})"
|
|
|
|
act_comment = " (#{act.class})"
|
2008-12-11 05:40:24 -05:00
|
|
|
end
|
|
|
|
elsif !Encoding.compatible?(exp_str, act_str)
|
|
|
|
if exp.is_a?(String) && act.is_a?(String)
|
|
|
|
exp_str = exp.dump
|
|
|
|
act_str = act.dump
|
|
|
|
exp_comment = " (#{exp.encoding})"
|
|
|
|
act_comment = " (#{act.encoding})"
|
|
|
|
else
|
|
|
|
exp_str = exp_str.dump
|
|
|
|
act_str = act_str.dump
|
|
|
|
end
|
|
|
|
end
|
|
|
|
"<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
|
|
|
|
}
|
|
|
|
assert(exp == act, msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_nil(exp, msg=nil)
|
|
|
|
msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
|
|
|
|
assert(!exp.nil?, msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_equal(exp, act, msg=nil)
|
|
|
|
msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
|
|
|
|
assert(exp != act, msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_match(regexp, string, msg=nil)
|
|
|
|
assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
|
2009-01-07 10:33:17 -05:00
|
|
|
self._assertions -= 1
|
2008-12-11 05:40:24 -05:00
|
|
|
msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" }
|
|
|
|
assert(regexp !~ string, msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_same(expected, actual, message="")
|
|
|
|
msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
|
|
|
|
<?>
|
|
|
|
with id <?> expected to not be equal\\? to
|
|
|
|
<?>
|
|
|
|
with id <?>.
|
|
|
|
EOT
|
|
|
|
assert(!actual.equal?(expected), msg)
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:03:32 -05:00
|
|
|
# get rid of overcounting
|
|
|
|
def assert_respond_to obj, meth, msg = nil
|
|
|
|
super if !caller[0].rindex(MiniTest::MINI_DIR, 0) || !obj.respond_to?(meth)
|
|
|
|
end
|
|
|
|
|
2009-02-27 08:10:07 -05:00
|
|
|
ms = instance_methods(true).map {|sym| sym.to_s }
|
|
|
|
ms.grep(/\Arefute_/) do |m|
|
|
|
|
mname = ('assert_not_' << m.to_s[/.*?_(.*)/, 1])
|
|
|
|
alias_method(mname, m) unless ms.include? mname
|
2009-02-15 07:22:50 -05:00
|
|
|
end
|
|
|
|
|
2008-12-11 05:40:24 -05:00
|
|
|
def build_message(head, template=nil, *arguments)
|
|
|
|
template &&= template.chomp
|
|
|
|
template.gsub(/\?/) { mu_pp(arguments.shift) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|