2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-08-14 07:59:35 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "coverage"
|
2011-06-13 12:12:52 -04:00
|
|
|
require "tmpdir"
|
2010-08-14 07:59:35 -04:00
|
|
|
|
|
|
|
class TestCoverage < Test::Unit::TestCase
|
|
|
|
def test_result_without_start
|
|
|
|
assert_raise(RuntimeError) {Coverage.result}
|
|
|
|
end
|
2015-11-25 19:48:56 -05:00
|
|
|
|
|
|
|
def test_peek_result_without_start
|
|
|
|
assert_raise(RuntimeError) {Coverage.peek_result}
|
|
|
|
end
|
|
|
|
|
2010-08-14 07:59:35 -04:00
|
|
|
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
|
2011-07-04 11:55:06 -04:00
|
|
|
|
2015-02-13 10:53:47 -05:00
|
|
|
def test_coverage_snapshot
|
|
|
|
loaded_features = $".dup
|
|
|
|
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
|
|
|
File.open("test.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
2016-04-11 01:13:31 -04:00
|
|
|
def TestCoverage.coverage_test_snapshot
|
2015-02-13 10:53:47 -05:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
Coverage.start
|
|
|
|
require tmp + '/test.rb'
|
|
|
|
cov = Coverage.peek_result[tmp + '/test.rb']
|
2016-04-11 01:13:31 -04:00
|
|
|
TestCoverage.coverage_test_snapshot
|
2015-02-13 10:53:47 -05:00
|
|
|
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
|
|
|
|
|
2011-07-04 11:55:06 -04:00
|
|
|
def test_restarting_coverage
|
|
|
|
loaded_features = $".dup
|
|
|
|
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
|
|
|
File.open("test.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
2016-04-11 01:13:31 -04:00
|
|
|
def TestCoverage.coverage_test_restarting
|
2011-07-04 11:55:06 -04:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2015-11-25 19:51:03 -05:00
|
|
|
File.open("test2.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
2016-04-11 01:13:31 -04:00
|
|
|
itself
|
2015-11-25 19:51:03 -05:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2011-07-04 11:55:06 -04:00
|
|
|
Coverage.start
|
|
|
|
require tmp + '/test.rb'
|
2016-04-06 10:29:17 -04:00
|
|
|
cov = { "#{tmp}/test.rb" => [1, 0, nil] }
|
|
|
|
assert_equal cov, Coverage.result
|
2015-11-25 19:51:03 -05:00
|
|
|
|
|
|
|
# Restart coverage but '/test.rb' is required before restart,
|
|
|
|
# so coverage is not recorded.
|
2011-07-04 11:55:06 -04:00
|
|
|
Coverage.start
|
2016-04-11 01:13:31 -04:00
|
|
|
TestCoverage.coverage_test_restarting
|
2016-04-06 10:29:17 -04:00
|
|
|
assert_equal({}, Coverage.result)
|
2015-11-25 19:51:03 -05:00
|
|
|
|
|
|
|
# Restart coverage and '/test2.rb' is required after restart,
|
|
|
|
# so coverage is recorded.
|
|
|
|
Coverage.start
|
|
|
|
require tmp + '/test2.rb'
|
2016-04-06 10:29:17 -04:00
|
|
|
cov = { "#{tmp}/test2.rb" => [1] }
|
|
|
|
assert_equal cov, Coverage.result
|
2011-08-23 11:44:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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|
|
2013-08-08 12:27:08 -04:00
|
|
|
f.puts "__id__\n" * 10000
|
2011-08-23 11:44:26 -04:00
|
|
|
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
|
2011-07-04 11:55:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
$".replace loaded_features
|
|
|
|
end
|
2016-01-03 20:14:45 -05:00
|
|
|
end unless ENV['COVERAGE']
|