mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #4540 from dearblue/assert-nesting
Nested `assert` for mrbtest
This commit is contained in:
@@ -3,9 +3,11 @@ require 'open3'
|
||||
|
||||
def assert_mruby(exp_out, exp_err, exp_success, args)
|
||||
out, err, stat = Open3.capture3(cmd("mruby"), *args)
|
||||
assert_operator(exp_out, :===, out, "standard output")
|
||||
assert_operator(exp_err, :===, err, "standard error")
|
||||
assert_equal(exp_success, stat.success?, "exit success?")
|
||||
assert do
|
||||
assert_operator(exp_out, :===, out, "standard output")
|
||||
assert_operator(exp_err, :===, err, "standard error")
|
||||
assert_equal(exp_success, stat.success?, "exit success?")
|
||||
end
|
||||
end
|
||||
|
||||
assert('regression for #1564') do
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
def assert_complex(real, exp)
|
||||
assert_float real.real, exp.real
|
||||
assert_float real.imaginary, exp.imaginary
|
||||
assert do
|
||||
assert_float real.real, exp.real
|
||||
assert_float real.imaginary, exp.imaginary
|
||||
end
|
||||
end
|
||||
|
||||
assert 'Complex' do
|
||||
|
||||
+22
-20
@@ -4,25 +4,27 @@
|
||||
MRubyIOTestUtil.io_test_setup
|
||||
$cr, $crlf, $cmd = MRubyIOTestUtil.win? ? [1, "\r\n", "cmd /c "] : [0, "\n", ""]
|
||||
|
||||
assert_io_open = ->(meth) do
|
||||
fd = IO.sysopen($mrbtest_io_rfname)
|
||||
assert_equal Fixnum, fd.class
|
||||
io1 = IO.__send__(meth, fd)
|
||||
begin
|
||||
assert_equal IO, io1.class
|
||||
assert_equal $mrbtest_io_msg, io1.read
|
||||
ensure
|
||||
io1.close
|
||||
end
|
||||
|
||||
io2 = IO.__send__(meth, IO.sysopen($mrbtest_io_rfname))do |io|
|
||||
if meth == :open
|
||||
assert_equal $mrbtest_io_msg, io.read
|
||||
else
|
||||
flunk "IO.#{meth} does not take block"
|
||||
def assert_io_open(meth)
|
||||
assert do
|
||||
fd = IO.sysopen($mrbtest_io_rfname)
|
||||
assert_equal Fixnum, fd.class
|
||||
io1 = IO.__send__(meth, fd)
|
||||
begin
|
||||
assert_equal IO, io1.class
|
||||
assert_equal $mrbtest_io_msg, io1.read
|
||||
ensure
|
||||
io1.close
|
||||
end
|
||||
|
||||
io2 = IO.__send__(meth, IO.sysopen($mrbtest_io_rfname))do |io|
|
||||
if meth == :open
|
||||
assert_equal $mrbtest_io_msg, io.read
|
||||
else
|
||||
flunk "IO.#{meth} does not take block"
|
||||
end
|
||||
end
|
||||
io2.close unless meth == :open
|
||||
end
|
||||
io2.close unless meth == :open
|
||||
end
|
||||
|
||||
assert('IO.class', '15.2.20') do
|
||||
@@ -38,7 +40,7 @@ assert('IO.ancestors', '15.2.20.3') do
|
||||
end
|
||||
|
||||
assert('IO.open', '15.2.20.4.1') do
|
||||
assert_io_open.(:open)
|
||||
assert_io_open(:open)
|
||||
end
|
||||
|
||||
assert('IO#close', '15.2.20.5.1') do
|
||||
@@ -224,11 +226,11 @@ assert('IO#dup for writable') do
|
||||
end
|
||||
|
||||
assert('IO.for_fd') do
|
||||
assert_io_open.(:for_fd)
|
||||
assert_io_open(:for_fd)
|
||||
end
|
||||
|
||||
assert('IO.new') do
|
||||
assert_io_open.(:new)
|
||||
assert_io_open(:new)
|
||||
end
|
||||
|
||||
assert('IO gc check') do
|
||||
|
||||
@@ -2,8 +2,10 @@ PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
|
||||
|
||||
def assert_pack tmpl, packed, unpacked
|
||||
t = tmpl.inspect
|
||||
assert_equal packed, unpacked.pack(tmpl), "#{unpacked.inspect}.pack(#{t})"
|
||||
assert_equal unpacked, packed.unpack(tmpl), "#{packed.inspect}.unpack(#{t})"
|
||||
assert do
|
||||
assert_equal packed, unpacked.pack(tmpl), "#{unpacked.inspect}.pack(#{t})"
|
||||
assert_equal unpacked, packed.unpack(tmpl), "#{packed.inspect}.unpack(#{t})"
|
||||
end
|
||||
end
|
||||
|
||||
# pack & unpack 'm' (base64)
|
||||
|
||||
@@ -23,17 +23,21 @@ class ComplexLikeNumeric < UserDefinedNumeric
|
||||
end
|
||||
|
||||
def assert_rational(exp, real)
|
||||
assert_float exp.numerator, real.numerator
|
||||
assert_float exp.denominator, real.denominator
|
||||
assert do
|
||||
assert_float exp.numerator, real.numerator
|
||||
assert_float exp.denominator, real.denominator
|
||||
end
|
||||
end
|
||||
|
||||
def assert_equal_rational(exp, o1, o2)
|
||||
if exp
|
||||
assert_operator(o1, :==, o2)
|
||||
assert_not_operator(o1, :!=, o2)
|
||||
else
|
||||
assert_not_operator(o1, :==, o2)
|
||||
assert_operator(o1, :!=, o2)
|
||||
assert do
|
||||
if exp
|
||||
assert_operator(o1, :==, o2)
|
||||
assert_not_operator(o1, :!=, o2)
|
||||
else
|
||||
assert_not_operator(o1, :==, o2)
|
||||
assert_operator(o1, :!=, o2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+82
-11
@@ -15,6 +15,36 @@ unless RUBY_ENGINE == "mruby"
|
||||
end
|
||||
end
|
||||
|
||||
class Array
|
||||
def _assertion_join
|
||||
join("-")
|
||||
end
|
||||
end
|
||||
|
||||
class String
|
||||
def _assertion_indent(indent)
|
||||
indent = indent.to_s
|
||||
off = 0
|
||||
str = self
|
||||
while nl = index("\n", off)
|
||||
nl += 1
|
||||
nl += 1 while slice(nl) == "\n"
|
||||
break if nl >= size
|
||||
str = indent.dup if off == 0
|
||||
str += slice(off, nl - off) + indent
|
||||
off = nl
|
||||
end
|
||||
|
||||
if off == 0
|
||||
str = indent + self
|
||||
else
|
||||
str += slice(off..-1)
|
||||
end
|
||||
|
||||
str
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Create the assertion in a readable way
|
||||
def assertion_string(err, str, iso=nil, e=nil, bt=nil)
|
||||
@@ -22,14 +52,14 @@ def assertion_string(err, str, iso=nil, e=nil, bt=nil)
|
||||
msg += " [#{iso}]" if iso && !iso.empty?
|
||||
msg += " => #{e}" if e && !e.to_s.empty?
|
||||
msg += " (#{GEMNAME == 'mruby-test' ? 'core' : "mrbgems: #{GEMNAME}"})"
|
||||
if $mrbtest_assert && $mrbtest_assert.size > 0
|
||||
if $mrbtest_assert
|
||||
$mrbtest_assert.each do |idx, assert_msg, diff|
|
||||
msg += "\n - Assertion[#{idx}]"
|
||||
msg += " #{assert_msg}." if assert_msg && !assert_msg.empty?
|
||||
msg += "\n#{diff}" if diff && !diff.empty?
|
||||
end
|
||||
end
|
||||
msg += "\nbacktrace:\n\t#{bt.join("\n\t")}" if bt
|
||||
msg += "\nbacktrace:\n #{bt.join("\n ")}" if bt
|
||||
msg
|
||||
end
|
||||
|
||||
@@ -44,13 +74,35 @@ end
|
||||
def assert(str = 'Assertion failed', iso = '')
|
||||
t_print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose
|
||||
begin
|
||||
$mrbtest_child_noassert ||= [0]
|
||||
$mrbtest_child_noassert << 0
|
||||
parent_asserts = $asserts
|
||||
$asserts = []
|
||||
parent_mrbtest_assert = $mrbtest_assert
|
||||
$mrbtest_assert = []
|
||||
$mrbtest_assert_idx = 0
|
||||
|
||||
if $mrbtest_assert_idx && !$mrbtest_assert_idx.empty?
|
||||
$mrbtest_assert_idx[-1] += 1
|
||||
$mrbtest_assert_idx << 0
|
||||
else
|
||||
$mrbtest_assert_idx = [0]
|
||||
class << $mrbtest_assert_idx
|
||||
alias to_s _assertion_join
|
||||
end
|
||||
end
|
||||
|
||||
yield
|
||||
if($mrbtest_assert.size > 0)
|
||||
$asserts.push(assertion_string('Fail: ', str, iso))
|
||||
$ko_test += 1
|
||||
t_print('F')
|
||||
if $mrbtest_assert.size > 0
|
||||
if $mrbtest_assert.size == $mrbtest_child_noassert[-1]
|
||||
$asserts.push(assertion_string('Info: ', str, iso))
|
||||
$mrbtest_child_noassert[-2] += 1
|
||||
$ok_test += 1
|
||||
t_print('.')
|
||||
else
|
||||
$asserts.push(assertion_string('Fail: ', str, iso))
|
||||
$ko_test += 1
|
||||
t_print('F')
|
||||
end
|
||||
else
|
||||
$ok_test += 1
|
||||
t_print('.')
|
||||
@@ -58,6 +110,7 @@ def assert(str = 'Assertion failed', iso = '')
|
||||
rescue MRubyTestSkip => e
|
||||
$asserts.push(assertion_string('Skip: ', str, iso, e))
|
||||
$skip_test += 1
|
||||
$mrbtest_child_noassert[-2] += 1
|
||||
t_print('?')
|
||||
rescue Exception => e
|
||||
bt = e.backtrace if $mrbtest_verbose
|
||||
@@ -65,7 +118,25 @@ def assert(str = 'Assertion failed', iso = '')
|
||||
$kill_test += 1
|
||||
t_print('X')
|
||||
ensure
|
||||
$mrbtest_assert = nil
|
||||
if $mrbtest_assert_idx.size > 1
|
||||
$asserts.each do |mesg|
|
||||
idx = $mrbtest_assert_idx[0..-2]._assertion_join
|
||||
mesg = mesg._assertion_indent(" ")
|
||||
|
||||
# Give `mesg` as a `diff` argument to avoid adding extra periods.
|
||||
parent_mrbtest_assert << [idx, nil, mesg]
|
||||
end
|
||||
else
|
||||
parent_asserts.concat $asserts
|
||||
end
|
||||
$asserts = parent_asserts
|
||||
|
||||
$mrbtest_assert = parent_mrbtest_assert
|
||||
$mrbtest_assert_idx.pop
|
||||
$mrbtest_assert_idx = nil if $mrbtest_assert_idx.empty?
|
||||
$mrbtest_child_noassert.pop
|
||||
|
||||
nil
|
||||
end
|
||||
t_print("\n") if $mrbtest_verbose
|
||||
end
|
||||
@@ -76,11 +147,11 @@ def assertion_diff(exp, act)
|
||||
end
|
||||
|
||||
def assert_true(obj, msg = nil, diff = nil)
|
||||
if $mrbtest_assert
|
||||
$mrbtest_assert_idx += 1
|
||||
if $mrbtest_assert_idx && $mrbtest_assert_idx.size > 0
|
||||
$mrbtest_assert_idx[-1] += 1
|
||||
unless obj == true
|
||||
diff ||= " Expected #{obj.inspect} to be true."
|
||||
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
|
||||
$mrbtest_assert.push([$mrbtest_assert_idx.to_s, msg, diff])
|
||||
end
|
||||
end
|
||||
obj
|
||||
|
||||
Reference in New Issue
Block a user