2008-10-16 09:55:09 -04:00
|
|
|
# test/unit compatibility layer using minitest.
|
2008-10-10 02:15:29 -04:00
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
require 'minitest/unit'
|
|
|
|
require 'pp'
|
2008-10-10 02:15:29 -04:00
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
module Test
|
2008-10-10 02:15:29 -04:00
|
|
|
module Unit
|
2008-10-16 09:55:09 -04:00
|
|
|
TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
|
|
|
|
|
|
|
|
def self.setup_argv(original_argv=ARGV)
|
|
|
|
argv = []
|
|
|
|
files = nil
|
|
|
|
reject = []
|
|
|
|
original_argv = original_argv.dup
|
|
|
|
while arg = original_argv.shift
|
|
|
|
case arg
|
|
|
|
when '-v'
|
|
|
|
argv << '-v'
|
|
|
|
when '-n', '--name'
|
|
|
|
argv << arg
|
|
|
|
argv << original_argv.shift
|
|
|
|
when '-x'
|
|
|
|
reject << original_argv.shift
|
|
|
|
else
|
|
|
|
files ||= []
|
|
|
|
if File.directory? arg
|
|
|
|
files.concat Dir["#{arg}/**/test_*.rb"]
|
|
|
|
elsif File.file? arg
|
|
|
|
files << arg
|
|
|
|
else
|
|
|
|
raise ArgumentError, "file not found: #{arg}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if files == nil
|
|
|
|
files = Dir["test/**/test_*.rb"]
|
|
|
|
end
|
|
|
|
|
|
|
|
reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
|
|
|
|
files.reject! {|f| reject_pat =~ f }
|
|
|
|
|
|
|
|
files.each {|f|
|
|
|
|
d = File.dirname(File.expand_path(f))
|
|
|
|
unless $:.include? d
|
|
|
|
$: << d
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
require f
|
|
|
|
rescue LoadError
|
|
|
|
puts $!
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
argv.concat files
|
|
|
|
|
|
|
|
ARGV.replace argv
|
|
|
|
end
|
|
|
|
|
|
|
|
module Assertions
|
|
|
|
include MiniTest::Assertions
|
|
|
|
|
|
|
|
def mu_pp(obj)
|
|
|
|
obj.pretty_inspect.chomp
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_raise(*args, &b)
|
|
|
|
assert_raises(*args, &b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_nothing_raised(*args)
|
|
|
|
if Module === args.last
|
|
|
|
msg = nil
|
|
|
|
else
|
|
|
|
msg = args.pop
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue Exception => e
|
|
|
|
if ((args.empty? && !e.instance_of?(MiniTest::Assertion)) ||
|
|
|
|
args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
|
|
|
|
msg = message(msg) { "Exception raised:\n<#{mu_pp(act)}>" }
|
|
|
|
assert(false, msg)
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_equal(exp, act, msg = nil)
|
2008-10-16 10:24:08 -04:00
|
|
|
msg = message(msg) {
|
|
|
|
exp_str = mu_pp(exp)
|
|
|
|
act_str = mu_pp(act)
|
|
|
|
exp_comment = ''
|
|
|
|
act_comment = ''
|
|
|
|
if exp_str == act_str
|
|
|
|
if exp.is_a?(String) && act.is_a?(String)
|
|
|
|
exp_comment = " (#{exp.encoding})"
|
|
|
|
act_comment = " (#{act.encoding})"
|
|
|
|
elsif exp.is_a?(Time) && act.is_a?(Time)
|
|
|
|
exp_comment = " (nsec=#{exp.nsec})"
|
|
|
|
act_comment = " (nsec=#{act.nsec})"
|
|
|
|
end
|
2008-10-16 10:28:06 -04:00
|
|
|
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
|
2008-10-16 10:24:08 -04:00
|
|
|
end
|
|
|
|
"<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
|
|
|
|
}
|
2008-10-16 09:55:09 -04:00
|
|
|
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.")
|
|
|
|
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
|
|
|
|
|
|
|
|
def build_message(head, template=nil, *arguments)
|
|
|
|
template &&= template.chomp
|
|
|
|
template.gsub(/\?/) { mu_pp(arguments.shift) }
|
|
|
|
end
|
2008-10-10 02:15:29 -04:00
|
|
|
end
|
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
class TestCase < MiniTest::Unit::TestCase
|
|
|
|
include Assertions
|
2008-10-10 02:15:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
MiniTest::Unit.autorun
|