Skip to content

Commit

Permalink
Bump up the latest version of CoreAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Sep 11, 2021
1 parent 3ceafa6 commit 7820037
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end

task :sync_tool do
require 'fileutils'
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
end
64 changes: 34 additions & 30 deletions test/lib/core_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,8 @@ def message msg = nil, ending = nil, &default
end

module CoreAssertions
if defined?(MiniTest)
require_relative '../../envutil'
# for ruby core testing
include MiniTest::Assertions

# Compatibility hack for assert_raise
Test::Unit::AssertionFailedError = MiniTest::Assertion
else
module MiniTest
class Assertion < Exception; end
class Skip < Assertion; end
end

require 'pp'
require_relative 'envutil'
include Test::Unit::Assertions
end
require_relative 'envutil'
require 'pp'

def mu_pp(obj) #:nodoc:
obj.pretty_inspect.chomp
Expand Down Expand Up @@ -117,16 +102,16 @@ def assert_no_memory_leak(args, prepare, code, message=nil, limit: 2.0, rss: fal
# TODO: consider choosing some appropriate limit for MJIT and stop skipping this once it does not randomly fail
pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined?(RubyVM::JIT) && RubyVM::JIT.enabled?

require_relative '../../memory_status'
raise MiniTest::Skip, "unsupported platform" unless defined?(Memory::Status)
require_relative 'memory_status'
raise Test::Unit::PendedError, "unsupported platform" unless defined?(Memory::Status)

token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
token_dump = token.dump
token_re = Regexp.quote(token)
envs = args.shift if Array === args and Hash === args.first
args = [
"--disable=gems",
"-r", File.expand_path("../../../memory_status", __FILE__),
"-r", File.expand_path("../memory_status", __FILE__),
*args,
"-v", "-",
]
Expand Down Expand Up @@ -183,11 +168,11 @@ def assert_nothing_raised(*args)
end
begin
line = __LINE__; yield
rescue MiniTest::Skip
rescue Test::Unit::PendedError
raise
rescue Exception => e
bt = e.backtrace
as = e.instance_of?(MiniTest::Assertion)
as = e.instance_of?(Test::Unit::AssertionFailedError)
if as
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
bt.reject! {|ln| ans =~ ln}
Expand All @@ -199,7 +184,7 @@ def assert_nothing_raised(*args)
"Backtrace:\n" +
e.backtrace.map{|frame| " #{frame}"}.join("\n")
}
raise MiniTest::Assertion, msg.call, bt
raise Test::Unit::AssertionFailedError, msg.call, bt
else
raise
end
Expand Down Expand Up @@ -263,7 +248,7 @@ def separated_runner(out = nil)
include(*Test::Unit::TestCase.ancestors.select {|c| !c.is_a?(Class) })
out = out ? IO.new(out, 'w') : STDOUT
at_exit {
out.puts [Marshal.dump($!)].pack('m'), "assertions=\#{self._assertions}"
out.puts [Marshal.dump($!)].pack('m'), "assertions=#{self._assertions}"
}
Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true) if defined?(Test::Unit::Runner)
end
Expand All @@ -277,14 +262,14 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
capture_stdout = true
unless /mswin|mingw/ =~ RUBY_PLATFORM
capture_stdout = false
opt[:out] = MiniTest::Unit.output if defined?(MiniTest::Unit)
opt[:out] = Test::Unit::Runner.output if defined?(Test::Unit::Runner)
res_p, res_c = IO.pipe
opt[:ios] = [res_c]
end
src = <<eom
# -*- coding: #{line += __LINE__; src.encoding}; -*-
BEGIN {
require "test/unit";include Test::Unit::Assertions;require #{(__dir__ + "/core_assertions").dump};include Test::Unit::CoreAssertions
require "test/unit";include Test::Unit::Assertions;require #{__FILE__.dump};include Test::Unit::CoreAssertions
separated_runner #{res_c&.fileno}
}
#{line -= __LINE__; src}
Expand Down Expand Up @@ -402,8 +387,8 @@ def assert_raise(*exp, &b)

begin
yield
rescue MiniTest::Skip => e
return e if exp.include? MiniTest::Skip
rescue Test::Unit::PendedError => e
return e if exp.include? Test::Unit::PendedError
raise e
rescue Exception => e
expected = exp.any? { |ex|
Expand Down Expand Up @@ -478,7 +463,7 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
ex
end

MINI_DIR = File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), "minitest") #:nodoc:
MINI_DIR = File.join(File.dirname(File.expand_path(__FILE__)), "minitest") #:nodoc:

# :call-seq:
# assert(test, [failure_message])
Expand Down Expand Up @@ -624,6 +609,7 @@ def assert_deprecated_warn(mesg = /deprecated/)
end

class << (AssertFile = Struct.new(:failure_message).new)
include Assertions
include CoreAssertions
def assert_file_predicate(predicate, *args)
if /\Anot_/ =~ predicate
Expand Down Expand Up @@ -714,10 +700,20 @@ def assert_join_threads(threads, message = nil)
if message
msg = "#{message}\n#{msg}"
end
raise MiniTest::Assertion, msg
raise Test::Unit::AssertionFailedError, msg
end
end

def assert_all?(obj, m = nil, &blk)
failed = []
obj.each do |*a, &b|
unless blk.call(*a, &b)
failed << (a.size > 1 ? a : a[0])
end
end
assert(failed.empty?, message(m) {failed.pretty_inspect})
end

def assert_all_assertions(msg = nil)
all = AllFailures.new
yield all
Expand All @@ -726,6 +722,14 @@ def assert_all_assertions(msg = nil)
end
alias all_assertions assert_all_assertions

def assert_all_assertions_foreach(msg = nil, *keys, &block)
all = AllFailures.new
all.foreach(*keys, &block)
ensure
assert(all.pass?, message(msg) {all.message.chomp(".")})
end
alias all_assertions_foreach assert_all_assertions_foreach

def message(msg = nil, *args, &default) # :nodoc:
if Proc === msg
super(nil, *args) do
Expand Down

0 comments on commit 7820037

Please sign in to comment.