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
|
|
|
|
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
|
|
|
|
|
|
|
def test_restarting_coverage
|
|
|
|
loaded_features = $".dup
|
|
|
|
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
|
|
|
File.open("test.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
|
|
|
def coverage_test_method
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
Coverage.start
|
|
|
|
require tmp + '/test.rb'
|
2011-08-23 11:44:26 -04:00
|
|
|
assert_equal 3, Coverage.result[tmp + '/test.rb'].size
|
2011-07-04 11:55:06 -04:00
|
|
|
Coverage.start
|
|
|
|
coverage_test_method
|
2011-08-23 11:44:26 -04:00
|
|
|
assert_equal 0, Coverage.result[tmp + '/test.rb'].size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 "p\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
|
2011-07-04 11:55:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
$".replace loaded_features
|
|
|
|
end
|
2010-08-14 07:59:35 -04:00
|
|
|
end
|