From 71730b42434e3c2dce7e7f6488bd54fae52cae51 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 27 Sep 2015 06:35:10 +0000 Subject: [PATCH] test/unit/assertions.rb: all_assertions * test/lib/test/unit/assertions.rb (all_assertions): try all assertions and check if all passed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/lib/envutil.rb | 23 ++++++++++------------- test/lib/test/unit/assertions.rb | 22 ++++++++++++++++++++++ test/ruby/test_env.rb | 12 +++++------- test/ruby/test_yield.rb | 14 +++++--------- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/test/lib/envutil.rb b/test/lib/envutil.rb index 1e0cf61b95..723bfdda06 100644 --- a/test/lib/envutil.rb +++ b/test/lib/envutil.rb @@ -351,22 +351,19 @@ module Test raise "test_stderr ignored, use block only or without block" if test_stderr != [] yield(stdout.lines.map {|l| l.chomp }, stderr.lines.map {|l| l.chomp }, status) else - errs = [] - [[test_stdout, stdout], [test_stderr, stderr]].each do |exp, act| - begin - if exp.is_a?(Regexp) - assert_match(exp, act, message) - elsif exp.all? {|e| String === e} - assert_equal(exp, act.lines.map {|l| l.chomp }, message) - else - assert_pattern_list(exp, act, message) + all_assertions(message) do |a| + [["stdout", test_stdout, stdout], ["stderr", test_stderr, stderr]].each do |key, exp, act| + a.for(key) do + if exp.is_a?(Regexp) + assert_match(exp, act) + elsif exp.all? {|e| String === e} + assert_equal(exp, act.lines.map {|l| l.chomp }) + else + assert_pattern_list(exp, act) + end end - rescue MiniTest::Assertion => e - errs << e.message - message = nil end end - raise MiniTest::Assertion, errs.join("\n---\n") unless errs.empty? status end end diff --git a/test/lib/test/unit/assertions.rb b/test/lib/test/unit/assertions.rb index 727c54c9d5..6c36c4e8c5 100644 --- a/test/lib/test/unit/assertions.rb +++ b/test/lib/test/unit/assertions.rb @@ -442,6 +442,28 @@ EOT assert(failed.empty?, message(m) {failed.pretty_inspect}) end + class AllFailures + attr_reader :failures + + def initialize + @failures = {} + end + + def for(key) + yield + rescue Exception => e + @failures[key] = e + end + end + + def all_assertions(msg = nil) + all = AllFailures.new + yield all + ensure + failures = all.failures + assert(failures.empty?, message(msg) {mu_pp(failures)}) + end + def build_message(head, template=nil, *arguments) #:nodoc: template &&= template.chomp template.gsub(/\G((?:[^\\]|\\.)*?)(\\)?\?/) { $1 + ($2 ? "?" : mu_pp(arguments.shift)) } diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index 6effa5197c..070e10464e 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -10,15 +10,13 @@ class TestEnv < Test::Unit::TestCase ] def assert_invalid_env(msg = nil) - failed = {} - INVALID_ENVVARS.select do |v| - begin - assert_raise(ArgumentError) {yield v} - rescue MiniTest::Assertion => e - failed[v] = e + all_assertions(msg) do |a| + INVALID_ENVVARS.each do |v| + a.for(v) do + assert_raise(ArgumentError) {yield v} + end end end - assert(failed.empty?, message(msg) {mu_pp(failed)}) end def setup diff --git a/test/ruby/test_yield.rb b/test/ruby/test_yield.rb index e7d150a08f..5c98bb8850 100644 --- a/test/ruby/test_yield.rb +++ b/test/ruby/test_yield.rb @@ -356,16 +356,12 @@ class TestRubyYieldGen < Test::Unit::TestCase end def assert_all_sentences(syntax, *args) - fails = [] syntax = Sentence.expand_syntax(syntax) - Sentence.each(syntax, *args) {|t| - begin - yield t - rescue MiniTest::Assertion => e - fails << e.message - end - } - assert(fails.empty?, proc {fails.join("\n--------\n")}) + all_assertions do |a| + Sentence.each(syntax, *args) {|t| + a.for(t) {yield t} + } + end end def test_yield