1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
require 'test/unit'
require 'rcov'
class Test_CodeCoverageAnalyzer < Test::Unit::TestCase
LINES = <<-EOF.to_a
puts 1
if foo
bar
baz
end
5.times do
foo
bar if baz
end
EOF
def setup
if defined? Rcov::Test::Temporary
Rcov::Test::Temporary.constants.each do |name|
Rcov::Test::Temporary.module_eval{ remove_const(name) }
end
end
end
def test_refine_coverage_info
analyzer = Rcov::CodeCoverageAnalyzer.new
cover = [1, 1, nil, nil, 0, 5, 5, 5, 0]
line_info, marked_info,
count_info = analyzer.instance_eval{ refine_coverage_info(LINES, cover) }
assert_equal(LINES, line_info)
assert_equal([true] * 2 + [false] * 3 + [true] * 3 + [false], marked_info)
assert_equal([1, 1, 0, 0, 0, 5, 5, 5, 0], count_info)
end
def test_analyzed_files_no_analysis
analyzer = Rcov::CodeCoverageAnalyzer.new
assert_equal([], analyzer.analyzed_files)
end
def test_raw_coverage_info
sample_file = File.join(File.dirname(__FILE__), "sample_01.rb")
lines = File.readlines(sample_file)
analyzer = Rcov::CodeCoverageAnalyzer.new
analyzer.run_hooked{ load sample_file }
assert_equal(lines, SCRIPT_LINES__[sample_file][0, lines.size])
assert(analyzer.analyzed_files.include?(sample_file))
line_info, cov_info, count_info = analyzer.data(sample_file)
assert_equal(lines, line_info)
assert_equal([true, true, false, false, true, false, true], cov_info)
assert_equal([1, 2, 0, 0, 1, 0, 11], count_info)
analyzer.reset
assert_equal(nil, analyzer.data(sample_file))
assert_equal([], analyzer.analyzed_files)
end
def test_script_lines_workaround_detects_correctly
analyzer = Rcov::CodeCoverageAnalyzer.new
lines = ["puts a", "foo", "bar"] * 3
coverage = [true] * 3 + [false] * 6
counts = [1] * 3 + [0] * 6
nlines, ncoverage, ncounts = analyzer.instance_eval do
script_lines_workaround(lines, coverage, counts)
end
assert_equal(["puts a", "foo", "bar"], nlines)
assert_equal([true, true, true], ncoverage)
assert_equal([1, 1, 1], ncounts)
end
def test_script_lines_workaround_no_false_positives
analyzer = Rcov::CodeCoverageAnalyzer.new
lines = ["puts a", "foo", "bar"] * 2 + ["puts a", "foo", "baz"]
coverage = [true] * 9
counts = [1] * 9
nlines, ncoverage, ncounts = analyzer.instance_eval do
script_lines_workaround(lines, coverage, counts)
end
assert_equal(lines, nlines)
assert_equal(coverage, ncoverage)
assert_equal(counts, ncounts)
end
def test_differential_coverage_data
sample_file = File.join(File.dirname(__FILE__), "sample_01.rb")
lines = File.readlines(sample_file)
analyzer = Rcov::CodeCoverageAnalyzer.new
analyzer.run_hooked{ load sample_file }
line_info, cov_info, count_info = analyzer.data(sample_file)
assert_equal([1, 2, 0, 0, 1, 0, 11], count_info)
analyzer.reset
sample_file = File.join(File.dirname(__FILE__), "sample_02.rb")
analyzer.run_hooked{ load sample_file }
line_info, cov_info, count_info = analyzer.data(sample_file)
assert_equal([8, 1, 0, 0, 0], count_info)
analyzer.reset
assert_equal([], analyzer.analyzed_files)
analyzer.run_hooked{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
line_info, cov_info, count_info = analyzer.data(sample_file)
assert_equal([0, 1, 1, 1, 0], count_info)
analyzer.run_hooked do
10.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
end
line_info, cov_info, count_info = analyzer.data(sample_file)
assert_equal([0, 11, 11, 11, 0], count_info)
10.times{ analyzer.run_hooked{ Rcov::Test::Temporary::Sample02.foo(1, 1) } }
line_info, cov_info, count_info = analyzer.data(sample_file)
assert_equal([0, 21, 21, 21, 0], count_info)
count_info2 = nil
10.times do |i|
analyzer.run_hooked do
Rcov::Test::Temporary::Sample02.foo(1, 1)
line_info, cov_info, count_info = analyzer.data(sample_file) if i == 3
line_info2, cov_info2, count_info2 = analyzer.data(sample_file)
end
end
assert_equal([0, 25, 25, 25, 0], count_info)
assert_equal([0, 31, 31, 31, 0], count_info2)
end
def test_nested_analyzer_blocks
a1 = Rcov::CodeCoverageAnalyzer.new
a2 = Rcov::CodeCoverageAnalyzer.new
sample_file = File.join(File.dirname(__FILE__), "sample_02.rb")
load sample_file
a1.run_hooked do
100.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
a2.run_hooked do
10.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
end
100.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
end
a2.run_hooked do
100.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
10.times{ a1.run_hooked { Rcov::Test::Temporary::Sample02.foo(1, 1) } }
end
a1.install_hook
Rcov::Test::Temporary::Sample02.foo(1, 1)
a1.remove_hook
a2.install_hook
Rcov::Test::Temporary::Sample02.foo(1, 1)
a2.remove_hook
_, _, counts1 = a1.data(sample_file)
_, _, counts2 = a2.data(sample_file)
assert_equal([0, 221, 221, 221, 0], counts1)
assert_equal([0, 121, 121, 121, 0], counts2)
end
def test_reset
a1 = Rcov::CodeCoverageAnalyzer.new
sample_file = File.join(File.dirname(__FILE__), "sample_02.rb")
load sample_file
a1.run_hooked do
100.times do |i|
Rcov::Test::Temporary::Sample02.foo(1, 1)
a1.reset if i == 49
end
end
assert_equal([0, 50, 50, 50, 0], a1.data(sample_file)[2])
end
def test_compute_raw_difference
first = {"a" => [1,1,1,1,1]}
last = {"a" => [2,1,5,2,1], "b" => [1,2,3,4,5]}
a = Rcov::CodeCoverageAnalyzer.new
assert_equal({"a" => [1,0,4,1,0], "b" => [1,2,3,4,5]},
a.instance_eval{ compute_raw_data_difference(first, last)} )
end
end
|