Skip to content

Commit

Permalink
test/ruby: Check warning messages at a finer granularity
Browse files Browse the repository at this point in the history
Instead of suppressing all warnings wholly in each test scripts by
setting `$VERBOSE` to `nil` in `setup` methods.
  • Loading branch information
nobu committed Dec 17, 2020
1 parent d597d7a commit 9908177
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 114 deletions.
62 changes: 30 additions & 32 deletions test/ruby/test_array.rb
Expand Up @@ -7,7 +7,6 @@
class TestArray < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
$VERBOSE = nil
@cls = Array
end

Expand Down Expand Up @@ -662,7 +661,7 @@ def test_count
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]'
Expand Down Expand Up @@ -1100,7 +1099,7 @@ def test_index
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
Expand All @@ -1111,42 +1110,42 @@ def test_values_at
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
Expand Down Expand Up @@ -1440,7 +1439,7 @@ def test_rindex
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
Expand Down Expand Up @@ -1734,19 +1733,19 @@ def o.to_ary
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
Expand Down Expand Up @@ -2403,13 +2402,13 @@ def test_try_convert

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
Expand Down Expand Up @@ -2462,7 +2461,7 @@ def test_aref
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) }
Expand Down Expand Up @@ -3305,7 +3304,6 @@ def need_continuation
class TestArraySubclass < TestArray
def setup
@verbose = $VERBOSE
$VERBOSE = nil
@cls = Class.new(Array)
end

Expand Down
51 changes: 35 additions & 16 deletions test/ruby/test_bignum.rb
Expand Up @@ -35,7 +35,6 @@ class TestBignum < Test::Unit::TestCase

def setup
@verbose = $VERBOSE
$VERBOSE = nil
@fmax = Float::MAX.to_i
@fmax2 = @fmax * 2
@big = (1 << BIGNUM_MIN_BITS) - 1
Expand Down Expand Up @@ -214,9 +213,11 @@ def test_to_s2

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
Expand Down Expand Up @@ -415,7 +416,7 @@ def test_divrem
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
Expand Down Expand Up @@ -466,8 +467,8 @@ def test_quo
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" }
Expand Down Expand Up @@ -505,39 +506,57 @@ def to_int
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(3, 2) }
assert_raise(TypeError, "#1792") {
assert_warn(/out of Float range/) {T1024 | Rational(3, 2)}
}
end

def test_or_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_xor_with_float
assert_raise(TypeError) { T1024 ^ 1.5 }
assert_raise(TypeError) {
assert_warn(/out of Float range/) {T1024 ^ 1.5}
}
end

def test_xor_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_xor_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_shift2
Expand Down
1 change: 0 additions & 1 deletion test/ruby/test_dir.rb
Expand Up @@ -8,7 +8,6 @@ class TestDir < Test::Unit::TestCase

def setup
@verbose = $VERBOSE
$VERBOSE = nil
@root = File.realpath(Dir.mktmpdir('__test_dir__'))
@nodir = File.join(@root, "dummy")
@dirs = []
Expand Down
9 changes: 4 additions & 5 deletions test/ruby/test_enum.rb
Expand Up @@ -27,7 +27,6 @@ def each(&block)
end
end
@verbose = $VERBOSE
$VERBOSE = nil
end

def teardown
Expand Down Expand Up @@ -88,7 +87,7 @@ def test_count
assert_equal(5, @obj.count)
assert_equal(2, @obj.count(1))
assert_equal(3, @obj.count {|x| x % 2 == 1 })
assert_equal(2, @obj.count(1) {|x| x % 2 == 1 })
assert_equal(2, assert_warning(/given block not used/) {@obj.count(1) {|x| x % 2 == 1 }})
assert_raise(ArgumentError) { @obj.count(0, 1) }

if RUBY_ENGINE == "ruby"
Expand Down Expand Up @@ -116,7 +115,7 @@ def test_find_index
assert_equal(1, @obj.find_index {|x| x % 2 == 0 })
assert_equal(nil, @obj.find_index {|x| false })
assert_raise(ArgumentError) { @obj.find_index(0, 1) }
assert_equal(1, @obj.find_index(2) {|x| x == 1 })
assert_equal(1, assert_warning(/given block not used/) {@obj.find_index(2) {|x| x == 1 }})
end

def test_find_all
Expand Down Expand Up @@ -227,7 +226,7 @@ def test_inject
assert_equal(48, @obj.inject {|z, x| z * 2 + x })
assert_equal(12, @obj.inject(:*))
assert_equal(24, @obj.inject(2) {|z, x| z * x })
assert_equal(24, @obj.inject(2, :*) {|z, x| z * x })
assert_equal(24, assert_warning(/given block not used/) {@obj.inject(2, :*) {|z, x| z * x }})
assert_equal(nil, @empty.inject() {9})
end

Expand Down Expand Up @@ -437,7 +436,7 @@ def test_all
assert_equal(false, [true, true, false].all?)
assert_equal(true, [].all?)
assert_equal(true, @empty.all?)
assert_equal(true, @obj.all?(Fixnum))
assert_equal(true, @obj.all?(Integer))
assert_equal(false, @obj.all?(1..2))
end

Expand Down
1 change: 0 additions & 1 deletion test/ruby/test_env.rb
Expand Up @@ -22,7 +22,6 @@ def assert_invalid_env(msg = nil)

def setup
@verbose = $VERBOSE
$VERBOSE = nil
@backup = ENV.to_hash
ENV.delete('test')
ENV.delete('TEST')
Expand Down
1 change: 0 additions & 1 deletion test/ruby/test_fixnum.rb
Expand Up @@ -4,7 +4,6 @@
class TestFixnum < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
$VERBOSE = nil
end

def teardown
Expand Down
5 changes: 3 additions & 2 deletions test/ruby/test_hash.rb
Expand Up @@ -86,7 +86,6 @@ def setup
'nil' => nil
]
@verbose = $VERBOSE
$VERBOSE = nil
end

def teardown
Expand Down Expand Up @@ -947,7 +946,7 @@ def test_rehash2
end

def test_fetch2
assert_equal(:bar, @h.fetch(0, :foo) { :bar })
assert_equal(:bar, assert_warning(/block supersedes default value argument/) {@h.fetch(0, :foo) { :bar }})
end

def test_default_proc
Expand Down Expand Up @@ -1112,6 +1111,7 @@ def test_equal2
def o.to_hash; @cls[]; end
def o.==(x); true; end
assert_equal({}, o)
o.singleton_class.remove_method(:==)
def o.==(x); false; end
assert_not_equal({}, o)

Expand All @@ -1128,6 +1128,7 @@ def test_eql
def o.to_hash; @cls[]; end
def o.eql?(x); true; end
assert_send([@cls[], :eql?, o])
o.singleton_class.remove_method(:eql?)
def o.eql?(x); false; end
assert_not_send([@cls[], :eql?, o])
end
Expand Down

0 comments on commit 9908177

Please sign in to comment.