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

ruby-changes:64237

From: Nobuyoshi <ko1@a...>
Date: Thu, 17 Dec 2020 20:06:34 +0900 (JST)
Subject: [ruby-changes:64237] 9908177857 (master): test/ruby: Check warning messages at a finer granularity

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

From 9908177857a28633d6279c43a1ad4dfedcb98596 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 17 Dec 2020 20:06:18 +0900
Subject: test/ruby: Check warning messages at a finer granularity

Instead of suppressing all warnings wholly in each test scripts by
setting `$VERBOSE` to `nil` in `setup` methods.

diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 0ed97de..f2abb3d 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -7,7 +7,6 @@ require "rbconfig/sizeof" https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L7
 class TestArray < Test::Unit::TestCase
   def setup
     @verbose = $VERBOSE
-    $VERBOSE = nil
     @cls = Array
   end
 
@@ -662,7 +661,7 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L661
     assert_equal(5, a.count)
     assert_equal(2, a.count(1))
     assert_equal(3, a.count {|x| x % 2 == 1 })
-    assert_equal(2, a.count(1) {|x| x % 2 == 1 })
+    assert_equal(2, assert_warning(/given block not used/) {a.count(1) {|x| x % 2 == 1 }})
     assert_raise(ArgumentError) { a.count(0, 1) }
 
     bug8654 = '[ruby-core:56072]'
@@ -1100,7 +1099,7 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1099
     assert_nil(a.index('ca'))
     assert_nil(a.index([1,2]))
 
-    assert_equal(1, a.index(99) {|x| x == 'cat' })
+    assert_equal(1, assert_warn(/given block not used/) {a.index(99) {|x| x == 'cat' }})
   end
 
   def test_values_at
@@ -1111,42 +1110,42 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1110
   end
 
   def test_join
-    $, = ""
+    assert_deprecated_warning {$, = ""}
     a = @cls[]
-    assert_equal("", a.join)
+    assert_equal("", assert_warn(/non-nil value/) {a.join})
     assert_equal("", a.join(','))
-    assert_equal(Encoding::US_ASCII, a.join.encoding)
+    assert_equal(Encoding::US_ASCII, assert_warn(/non-nil value/) {a.join}.encoding)
 
-    $, = ""
+    assert_deprecated_warning {$, = ""}
     a = @cls[1, 2]
-    assert_equal("12", a.join)
-    assert_equal("12", a.join(nil))
+    assert_equal("12", assert_warn(/non-nil value/) {a.join})
+    assert_equal("12", assert_warn(/non-nil value/) {a.join(nil)})
     assert_equal("1,2", a.join(','))
 
-    $, = ""
+    assert_deprecated_warning {$, = ""}
     a = @cls[1, 2, 3]
-    assert_equal("123", a.join)
-    assert_equal("123", a.join(nil))
+    assert_equal("123", assert_warn(/non-nil value/) {a.join})
+    assert_equal("123", assert_warn(/non-nil value/) {a.join(nil)})
     assert_equal("1,2,3", a.join(','))
 
-    $, = ":"
+    assert_deprecated_warning {$, = ":"}
     a = @cls[1, 2, 3]
-    assert_equal("1:2:3", a.join)
-    assert_equal("1:2:3", a.join(nil))
+    assert_equal("1:2:3", assert_warn(/non-nil value/) {a.join})
+    assert_equal("1:2:3", assert_warn(/non-nil value/) {a.join(nil)})
     assert_equal("1,2,3", a.join(','))
 
-    $, = ""
+    assert_deprecated_warning {$, = ""}
 
     e = ''.force_encoding('EUC-JP')
     u = ''.force_encoding('UTF-8')
-    assert_equal(Encoding::US_ASCII, [[]].join.encoding)
-    assert_equal(Encoding::US_ASCII, [1, [u]].join.encoding)
-    assert_equal(Encoding::UTF_8, [u, [e]].join.encoding)
-    assert_equal(Encoding::UTF_8, [u, [1]].join.encoding)
-    assert_equal(Encoding::UTF_8, [Struct.new(:to_str).new(u)].join.encoding)
+    assert_equal(Encoding::US_ASCII, assert_warn(/non-nil value/) {[[]].join}.encoding)
+    assert_equal(Encoding::US_ASCII, assert_warn(/non-nil value/) {[1, [u]].join}.encoding)
+    assert_equal(Encoding::UTF_8, assert_warn(/non-nil value/) {[u, [e]].join}.encoding)
+    assert_equal(Encoding::UTF_8, assert_warn(/non-nil value/) {[u, [1]].join}.encoding)
+    assert_equal(Encoding::UTF_8, assert_warn(/non-nil value/) {[Struct.new(:to_str).new(u)].join}.encoding)
     bug5379 = '[ruby-core:39776]'
-    assert_equal(Encoding::US_ASCII, [[], u, nil].join.encoding, bug5379)
-    assert_equal(Encoding::UTF_8, [[], "\u3042", nil].join.encoding, bug5379)
+    assert_equal(Encoding::US_ASCII, assert_warn(/non-nil value/) {[[], u, nil].join}.encoding, bug5379)
+    assert_equal(Encoding::UTF_8, assert_warn(/non-nil value/) {[[], "\u3042", nil].join}.encoding, bug5379)
   ensure
     $, = nil
   end
@@ -1440,7 +1439,7 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1439
     assert_nil(a.rindex('ca'))
     assert_nil(a.rindex([1,2]))
 
-    assert_equal(3, a.rindex(99) {|x| x == [1,2,3] })
+    assert_equal(3, assert_warning(/given block not used/) {a.rindex(99) {|x| x == [1,2,3] }})
 
     bug15951 = "[Bug #15951]"
     o2 = Object.new
@@ -1734,19 +1733,19 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1733
   end
 
   def test_to_s
-    $, = ""
+    assert_deprecated_warning {$, = ""}
     a = @cls[]
     assert_equal("[]", a.to_s)
 
-    $, = ""
+    assert_deprecated_warning {$, = ""}
     a = @cls[1, 2]
     assert_equal("[1, 2]", a.to_s)
 
-    $, = ""
+    assert_deprecated_warning {$, = ""}
     a = @cls[1, 2, 3]
     assert_equal("[1, 2, 3]", a.to_s)
 
-    $, = ":"
+    assert_deprecated_warning {$, = ""}
     a = @cls[1, 2, 3]
     assert_equal("[1, 2, 3]", a.to_s)
   ensure
@@ -2403,13 +2402,13 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2402
 
   def test_initialize
     assert_nothing_raised { [].instance_eval { initialize } }
-    assert_nothing_raised { Array.new { } }
+    assert_warning(/given block not used/) { Array.new { } }
     assert_equal([1, 2, 3], Array.new([1, 2, 3]))
     assert_raise(ArgumentError) { Array.new(-1, 1) }
     assert_raise(ArgumentError) { Array.new(LONGP, 1) }
     assert_equal([1, 1, 1], Array.new(3, 1))
     assert_equal([1, 1, 1], Array.new(3) { 1 })
-    assert_equal([1, 1, 1], Array.new(3, 1) { 1 })
+    assert_equal([1, 1, 1], assert_warning(/block supersedes default value argument/) {Array.new(3, 1) { 1 }})
   end
 
   def test_aset_error
@@ -2462,7 +2461,7 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2461
   end
 
   def test_fetch
-    assert_equal(1, [].fetch(0, 0) { 1 })
+    assert_equal(1, assert_warning(/block supersedes default value argument/) {[].fetch(0, 0) { 1 }})
     assert_equal(1, [0, 1].fetch(-1))
     assert_raise(IndexError) { [0, 1].fetch(2) }
     assert_raise(IndexError) { [0, 1].fetch(-3) }
@@ -3305,7 +3304,6 @@ end https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3304
 class TestArraySubclass < TestArray
   def setup
     @verbose = $VERBOSE
-    $VERBOSE = nil
     @cls = Class.new(Array)
   end
 
diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb
index 434c5be..a240c8e 100644
--- a/test/ruby/test_bignum.rb
+++ b/test/ruby/test_bignum.rb
@@ -35,7 +35,6 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L35
 
   def setup
     @verbose = $VERBOSE
-    $VERBOSE = nil
     @fmax = Float::MAX.to_i
     @fmax2 = @fmax * 2
     @big = (1 << BIGNUM_MIN_BITS) - 1
@@ -214,9 +213,11 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L213
 
   def test_to_f
     assert_nothing_raised { T31P.to_f.to_i }
-    assert_raise(FloatDomainError) { (1024**1024).to_f.to_i }
-    assert_equal(1, (2**50000).to_f.infinite?)
-    assert_equal(-1, (-(2**50000)).to_f.infinite?)
+    assert_raise(FloatDomainError) {
+      assert_warning(/out of Float range/) {(1024**1024).to_f}.to_i
+    }
+    assert_equal(1, assert_warning(/out of Float range/) {(2**50000).to_f}.infinite?)
+    assert_equal(-1, assert_warning(/out of Float range/) {(-(2**50000)).to_f}.infinite?)
   end
 
   def test_cmp
@@ -415,7 +416,7 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L416
   def test_divide
     bug5490 = '[ruby-core:40429]'
     assert_raise(ZeroDivisionError, bug5490) {T1024./(0)}
-    assert_equal(Float::INFINITY, T1024./(0.0), bug5490)
+    assert_equal(Float::INFINITY, assert_warning(/out of Float range/) {T1024./(0.0)}, bug5490)
   end
 
   def test_div
@@ -466,8 +467,8 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L467
   def test_pow
     assert_equal(1.0, T32 ** 0.0)
     assert_equal(1.0 / T32, T32 ** -1)
-    assert_equal(1, (T32 ** T32).infinite?)
-    assert_equal(1, (T32 ** (2**30-1)).infinite?)
+    assert_equal(1, assert_warning(/may be too big/) {T32 ** T32}.infinite?)
+    assert_equal(1, assert_warning(/may be too big/) {T32 ** (2**30-1)}.infinite?)
 
     ### rational changes the behavior of Bignum#**
     #assert_raise(TypeError) { T32**"foo" }
@@ -505,39 +506,57 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L506
   end
 
   def test_and_with_float
-    assert_raise(TypeError) { T1024 & 1.5 }
+    assert_raise(TypeError) {
+      assert_warning(/out of Float range/) {T1024 & 1.5}
+    }
   end
 
   def test_and_with_rational
-    assert_raise(TypeError, "#1792") { T1024 & Rational(3, 2) }
+    assert_raise(TypeError, "#1792") {
+      assert_warn(/out of Float range/) {T1024 & Rational(3, 2)}
+    }
   end
 
   def test_and_with_nonintegral_numeric
-    assert_raise(TypeError, "#1792") { T1024 & DummyNumeric.new }
+    assert_raise(TypeError, "#1792") {
+      assert_warn(/out of Float range/) {T1024 & DummyNumeric.new}
+    }
   end
 
   def test_or_with_float
-    assert_raise(TypeError) { T1024 | 1.5 }
+    assert_raise(TypeError) {
+      assert_warn(/out of Float range/) {T1024 | 1.5}
+    }
   end
 
   def test_or_with_rational
-    assert_raise(TypeError, "#1792") { T1024 | Rational( (... truncated)

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

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