[前][次][番号順一覧][スレッド一覧]

ruby-changes:57014

From: SHIBATA <ko1@a...>
Date: Wed, 14 Aug 2019 07:44:46 +0900 (JST)
Subject: [ruby-changes:57014] SHIBATA Hiroshi: 216d230080 (master): Move some assertions to CoreAssertions. (#2354)

https://git.ruby-lang.org/ruby.git/commit/?id=216d230080

From 216d23008098c358d3a57cddc6f5d44e0b2f1602 Mon Sep 17 00:00:00 2001
From: SHIBATA Hiroshi <hsbt@r...>
Date: Wed, 14 Aug 2019 07:44:26 +0900
Subject: Move some assertions to CoreAssertions. (#2354)

They are used by default gems like forwardable.

    * assert_raise_with_message
    * assert_warning
    * assert_warn

diff --git a/tool/lib/test/unit/assertions.rb b/tool/lib/test/unit/assertions.rb
index 33f31fe..7e5b8d5 100644
--- a/tool/lib/test/unit/assertions.rb
+++ b/tool/lib/test/unit/assertions.rb
@@ -100,56 +100,6 @@ module Test https://github.com/ruby/ruby/blob/trunk/tool/lib/test/unit/assertions.rb#L100
       end
 
       # :call-seq:
-      #   assert_raise_with_message(exception, expected, msg = nil, &block)
-      #
-      #Tests if the given block raises an exception with the expected
-      #message.
-      #
-      #    assert_raise_with_message(RuntimeError, "foo") do
-      #      nil #Fails, no Exceptions are raised
-      #    end
-      #
-      #    assert_raise_with_message(RuntimeError, "foo") do
-      #      raise ArgumentError, "foo" #Fails, different Exception is raised
-      #    end
-      #
-      #    assert_raise_with_message(RuntimeError, "foo") do
-      #      raise "bar" #Fails, RuntimeError is raised but the message differs
-      #    end
-      #
-      #    assert_raise_with_message(RuntimeError, "foo") do
-      #      raise "foo" #Raises RuntimeError with the message, so assertion succeeds
-      #    end
-      def assert_raise_with_message(exception, expected, msg = nil, &block)
-        case expected
-        when String
-          assert = :assert_equal
-        when Regexp
-          assert = :assert_match
-        else
-          raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
-        end
-
-        ex = m = nil
-        EnvUtil.with_default_internal(expected.encoding) do
-          ex = assert_raise(exception, msg || proc {"Exception(#{exception}) with message matches to #{expected.inspect}"}) do
-            yield
-          end
-          m = ex.message
-        end
-        msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"}
-
-        if assert == :assert_equal
-          assert_equal(expected, m, msg)
-        else
-          msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp m}" }
-          assert expected =~ m, msg
-          block.binding.eval("proc{|_|$~=_}").call($~)
-        end
-        ex
-      end
-
-      # :call-seq:
       #   assert_nothing_raised( *args, &block )
       #
       #If any exceptions are given as arguments, the assertion will
@@ -522,22 +472,6 @@ EOT https://github.com/ruby/ruby/blob/trunk/tool/lib/test/unit/assertions.rb#L472
         assert !status.signaled?, FailDesc[status, message, out]
       end
 
-      def assert_warning(pat, msg = nil)
-        result = nil
-        stderr = EnvUtil.with_default_internal(pat.encoding) {
-          EnvUtil.verbose_warning {
-            result = yield
-          }
-        }
-        msg = message(msg) {diff pat, stderr}
-        assert(pat === stderr, msg)
-        result
-      end
-
-      def assert_warn(*args)
-        assert_warning(*args) {$VERBOSE = false; yield}
-      end
-
       def assert_no_warning(pat, msg = nil)
         result = nil
         stderr = EnvUtil.verbose_warning {
diff --git a/tool/lib/test/unit/core_assertions.rb b/tool/lib/test/unit/core_assertions.rb
index 1801b91..38201ca 100644
--- a/tool/lib/test/unit/core_assertions.rb
+++ b/tool/lib/test/unit/core_assertions.rb
@@ -174,6 +174,72 @@ eom https://github.com/ruby/ruby/blob/trunk/tool/lib/test/unit/core_assertions.rb#L174
         ret
       end
 
+      # :call-seq:
+      #   assert_raise_with_message(exception, expected, msg = nil, &block)
+      #
+      #Tests if the given block raises an exception with the expected
+      #message.
+      #
+      #    assert_raise_with_message(RuntimeError, "foo") do
+      #      nil #Fails, no Exceptions are raised
+      #    end
+      #
+      #    assert_raise_with_message(RuntimeError, "foo") do
+      #      raise ArgumentError, "foo" #Fails, different Exception is raised
+      #    end
+      #
+      #    assert_raise_with_message(RuntimeError, "foo") do
+      #      raise "bar" #Fails, RuntimeError is raised but the message differs
+      #    end
+      #
+      #    assert_raise_with_message(RuntimeError, "foo") do
+      #      raise "foo" #Raises RuntimeError with the message, so assertion succeeds
+      #    end
+      def assert_raise_with_message(exception, expected, msg = nil, &block)
+        case expected
+        when String
+          assert = :assert_equal
+        when Regexp
+          assert = :assert_match
+        else
+          raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
+        end
+
+        ex = m = nil
+        EnvUtil.with_default_internal(expected.encoding) do
+          ex = assert_raise(exception, msg || proc {"Exception(#{exception}) with message matches to #{expected.inspect}"}) do
+            yield
+          end
+          m = ex.message
+        end
+        msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"}
+
+        if assert == :assert_equal
+          assert_equal(expected, m, msg)
+        else
+          msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp m}" }
+          assert expected =~ m, msg
+          block.binding.eval("proc{|_|$~=_}").call($~)
+        end
+        ex
+      end
+
+      def assert_warning(pat, msg = nil)
+        result = nil
+        stderr = EnvUtil.with_default_internal(pat.encoding) {
+          EnvUtil.verbose_warning {
+            result = yield
+          }
+        }
+        msg = message(msg) {diff pat, stderr}
+        assert(pat === stderr, msg)
+        result
+      end
+
+      def assert_warn(*args)
+        assert_warning(*args) {$VERBOSE = false; yield}
+      end
+
       class << (AssertFile = Struct.new(:failure_message).new)
         include CoreAssertions
         def assert_file_predicate(predicate, *args)
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]