test: refine assertions
* test/test_prime.rb (test_eratosthenes_works_fine_after_timeout): use assert_raise to check timeout. * test/test_securerandom.rb: check if results are hexadecimal strings, and refine failure messages. * test/test_tracer.rb (test_tracer_with_option_r_without_gems): use assert_equal to compare. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
31637f00fc
commit
2c3656f5e8
@ -179,10 +179,8 @@ class TestPrime < Test::Unit::TestCase
|
|||||||
return n
|
return n
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
assert_raise(Timeout::Error) do
|
||||||
Timeout.timeout(0.5) { Prime.each(7*37){} }
|
Timeout.timeout(0.5) { Prime.each(7*37){} }
|
||||||
flunk("timeout expected")
|
|
||||||
rescue Timeout::Error
|
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
class << sieve
|
class << sieve
|
||||||
|
@ -104,10 +104,13 @@ end
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_s_hex
|
def test_s_hex
|
||||||
assert_equal(16 * 2, @it.hex.size)
|
s = @it.hex
|
||||||
|
assert_equal(16 * 2, s.size)
|
||||||
|
assert_match(/\A\h+\z/, s)
|
||||||
33.times do |idx|
|
33.times do |idx|
|
||||||
assert_equal(idx * 2, @it.hex(idx).size)
|
s = @it.hex(idx)
|
||||||
assert_equal(idx, @it.hex(idx).gsub(/(..)/) { [$1].pack('H*') }.size)
|
assert_equal(idx * 2, s.size)
|
||||||
|
assert_match(/\A\h*\z/, s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -128,23 +131,20 @@ end
|
|||||||
assert_not_match(safe, @it.urlsafe_base64(idx))
|
assert_not_match(safe, @it.urlsafe_base64(idx))
|
||||||
end
|
end
|
||||||
# base64 can include unsafe byte
|
# base64 can include unsafe byte
|
||||||
10001.times do |idx|
|
assert((0..10000).any? {|idx| safe =~ @it.base64(idx)}, "None of base64(0..10000) is url-safe")
|
||||||
return if safe =~ @it.base64(idx)
|
|
||||||
end
|
|
||||||
flunk
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_s_random_number_float
|
def test_s_random_number_float
|
||||||
101.times do
|
101.times do
|
||||||
v = @it.random_number
|
v = @it.random_number
|
||||||
assert(0.0 <= v && v < 1.0)
|
assert_in_range(0.0...1.0, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_s_random_number_float_by_zero
|
def test_s_random_number_float_by_zero
|
||||||
101.times do
|
101.times do
|
||||||
v = @it.random_number(0)
|
v = @it.random_number(0)
|
||||||
assert(0.0 <= v && v < 1.0)
|
assert_in_range(0.0...1.0, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -152,16 +152,14 @@ end
|
|||||||
101.times do |idx|
|
101.times do |idx|
|
||||||
next if idx.zero?
|
next if idx.zero?
|
||||||
v = @it.random_number(idx)
|
v = @it.random_number(idx)
|
||||||
assert(0 <= v && v < idx)
|
assert_in_range(0...idx, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_uuid
|
def test_uuid
|
||||||
uuid = @it.uuid
|
uuid = @it.uuid
|
||||||
assert_equal(36, uuid.size)
|
assert_equal(36, uuid.size)
|
||||||
uuid.unpack('a8xa4xa4xa4xa12').each do |e|
|
assert_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/, uuid)
|
||||||
assert_match(/^[0-9a-f]+$/, e)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def protect
|
def protect
|
||||||
@ -182,4 +180,7 @@ end
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def assert_in_range(range, result, mesg = nil)
|
||||||
|
assert(range.cover?(result), message(mesg) {"Expected #{result} to be in #{range}"})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,12 +18,7 @@ class TestTracer < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_tracer_with_option_r_without_gems
|
def test_tracer_with_option_r_without_gems
|
||||||
assert_in_out_err(%w[--disable-gems -rtracer -e 1]) do |(*lines),|
|
assert_in_out_err(%w[--disable-gems -rtracer -e 1]) do |(*lines),|
|
||||||
case lines.size
|
assert_equal 1, lines.size, "unexpected output from `ruby --disable-gems -rtracer -e 1`"
|
||||||
when 1
|
|
||||||
# do nothing
|
|
||||||
else
|
|
||||||
flunk "unexpected output from `ruby --disable-gems -rtracer -e 1`"
|
|
||||||
end
|
|
||||||
assert_equal "#0:-e:1::-: 1", lines.last
|
assert_equal "#0:-e:1::-: 1", lines.last
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user