mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
95d949d571
* ext/coverage/coverage.c (rb_coverage_running): add to quickly check if coverage is enabled. patched by Burke Libbey <burke AT libbey.me> in [ruby-core:81726]. [Feature #13667] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
167 lines
4.1 KiB
Ruby
167 lines
4.1 KiB
Ruby
# frozen_string_literal: false
|
|
require "test/unit"
|
|
require "coverage"
|
|
require "tmpdir"
|
|
require "envutil"
|
|
|
|
class TestCoverage < Test::Unit::TestCase
|
|
def test_result_without_start
|
|
assert_raise(RuntimeError) {Coverage.result}
|
|
end
|
|
|
|
def test_peek_result_without_start
|
|
assert_raise(RuntimeError) {Coverage.peek_result}
|
|
end
|
|
|
|
def test_result_with_nothing
|
|
Coverage.start
|
|
result = Coverage.result
|
|
assert_kind_of(Hash, result)
|
|
result.each do |key, val|
|
|
assert_kind_of(String, key)
|
|
assert_kind_of(Array, val)
|
|
end
|
|
end
|
|
|
|
def test_coverage_running?
|
|
refute Coverage.running?
|
|
Coverage.start
|
|
assert Coverage.running?
|
|
Coverage.peek_result
|
|
assert Coverage.running?
|
|
Coverage.result
|
|
refute Coverage.running?
|
|
end
|
|
|
|
def test_coverage_snapshot
|
|
loaded_features = $".dup
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
Dir.chdir(tmp) {
|
|
File.open("test.rb", "w") do |f|
|
|
f.puts <<-EOS
|
|
def TestCoverage.coverage_test_snapshot
|
|
:ok
|
|
end
|
|
EOS
|
|
end
|
|
|
|
Coverage.start
|
|
require tmp + '/test.rb'
|
|
cov = Coverage.peek_result[tmp + '/test.rb']
|
|
TestCoverage.coverage_test_snapshot
|
|
cov2 = Coverage.peek_result[tmp + '/test.rb']
|
|
assert_equal cov[1] + 1, cov2[1]
|
|
assert_equal cov2, Coverage.result[tmp + '/test.rb']
|
|
}
|
|
}
|
|
ensure
|
|
$".replace loaded_features
|
|
end
|
|
|
|
def test_restarting_coverage
|
|
loaded_features = $".dup
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
Dir.chdir(tmp) {
|
|
File.open("test.rb", "w") do |f|
|
|
f.puts <<-EOS
|
|
def TestCoverage.coverage_test_restarting
|
|
:ok
|
|
end
|
|
EOS
|
|
end
|
|
|
|
File.open("test2.rb", "w") do |f|
|
|
f.puts <<-EOS
|
|
itself
|
|
EOS
|
|
end
|
|
|
|
Coverage.start
|
|
require tmp + '/test.rb'
|
|
cov = { "#{tmp}/test.rb" => [1, 0, nil] }
|
|
assert_equal cov, Coverage.result
|
|
|
|
# Restart coverage but '/test.rb' is required before restart,
|
|
# so coverage is not recorded.
|
|
Coverage.start
|
|
TestCoverage.coverage_test_restarting
|
|
assert_equal({}, Coverage.result)
|
|
|
|
# Restart coverage and '/test2.rb' is required after restart,
|
|
# so coverage is recorded.
|
|
Coverage.start
|
|
require tmp + '/test2.rb'
|
|
cov = { "#{tmp}/test2.rb" => [1] }
|
|
assert_equal cov, Coverage.result
|
|
}
|
|
}
|
|
ensure
|
|
$".replace loaded_features
|
|
end
|
|
|
|
def test_big_code
|
|
loaded_features = $".dup
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
Dir.chdir(tmp) {
|
|
File.open("test.rb", "w") do |f|
|
|
f.puts "__id__\n" * 10000
|
|
f.puts "def ignore(x); end"
|
|
f.puts "ignore([1"
|
|
f.puts "])"
|
|
end
|
|
|
|
Coverage.start
|
|
require tmp + '/test.rb'
|
|
assert_equal 10003, Coverage.result[tmp + '/test.rb'].size
|
|
}
|
|
}
|
|
ensure
|
|
$".replace loaded_features
|
|
end
|
|
|
|
def test_nonpositive_linenumber
|
|
bug12517 = '[ruby-core:76141] [Bug #12517]'
|
|
Coverage.start
|
|
EnvUtil.suppress_warning do
|
|
assert_nothing_raised(ArgumentError, bug12517) do
|
|
RubyVM::InstructionSequence.compile(":ok", nil, "<compiled>", 0)
|
|
end
|
|
end
|
|
assert_include Coverage.result, "<compiled>"
|
|
end
|
|
|
|
def test_eval
|
|
bug13305 = '[ruby-core:80079] [Bug #13305]'
|
|
loaded_features = $".dup
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
Dir.chdir(tmp) {
|
|
File.open("test.rb", "w") do |f|
|
|
f.puts 'REPEATS = 400'
|
|
f.puts 'def add_method(target)'
|
|
f.puts ' REPEATS.times do'
|
|
f.puts ' target.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)'
|
|
f.puts ' def foo'
|
|
f.puts ' #{"\n" * rand(REPEATS)}'
|
|
f.puts ' end'
|
|
f.puts ' 1'
|
|
f.puts ' RUBY'
|
|
f.puts ' end'
|
|
f.puts 'end'
|
|
end
|
|
|
|
Coverage.start
|
|
require tmp + '/test.rb'
|
|
EnvUtil.suppress_warning do
|
|
add_method(Class.new)
|
|
end
|
|
assert_equal Coverage.result[tmp + "/test.rb"], [1, 1, 1, 400, nil, nil, nil, nil, nil, nil, nil], bug13305
|
|
}
|
|
}
|
|
ensure
|
|
$".replace loaded_features
|
|
end
|
|
end unless ENV['COVERAGE']
|