spicycode-rcov 0.8.1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ a = 1
2
+ if a == 2
3
+ b = 1
4
+ else
5
+ b = 2
6
+ end
7
+ 10.times{ b += 1 }
@@ -0,0 +1,5 @@
1
+ module Rcov; module Test; module Temporary; module Sample02
2
+ def self.foo(a,b)
3
+ a + b
4
+ end
5
+ end end end end
@@ -0,0 +1,20 @@
1
+
2
+ module Rcov; module Test; module Temporary; class Sample03
3
+ def f1 # MUST NOT CHANGE the position or the tests will break
4
+ 10.times { f2 }
5
+ end
6
+
7
+ def f2; 1 end
8
+
9
+ def f3
10
+ 10.times{ f1 }
11
+ 100.times{ f2 }
12
+ end
13
+
14
+ def self.g1
15
+ 10.times{ g2 }
16
+ end
17
+
18
+ def self.g2; 1 end
19
+ # safe from here ...
20
+ end end end end
@@ -0,0 +1,10 @@
1
+ $: << File.dirname(__FILE__)
2
+ require 'sample_03'
3
+
4
+ klass = Rcov::Test::Temporary::Sample03
5
+ obj = klass.new
6
+ obj.f1
7
+ obj.f2
8
+ obj.f3
9
+ #klass.g1 uncovered
10
+ klass.g2
@@ -0,0 +1,17 @@
1
+ def d(x)
2
+ 4*x
3
+ end
4
+
5
+ def a
6
+ b 10
7
+ end
8
+
9
+ def b(x)
10
+ x*10
11
+ end
12
+
13
+ def c(x)
14
+ 3*x
15
+ end
16
+
17
+ a()
@@ -0,0 +1,13 @@
1
+ def a
2
+ b 10
3
+ end
4
+
5
+ def b(x)
6
+ x*10
7
+ end
8
+
9
+ def c(x)
10
+ 3*x
11
+ end
12
+
13
+ a()
@@ -0,0 +1,17 @@
1
+ def d(x)
2
+ 4*x
3
+ end
4
+
5
+ def a
6
+ b 10
7
+ end
8
+
9
+ def b(x)
10
+ x*10
11
+ end
12
+
13
+ def c(x)
14
+ 3*x
15
+ end
16
+
17
+ a()
@@ -0,0 +1,207 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestCallSiteAnalyzer < Test::Unit::TestCase
4
+
5
+ sample_file = File.join(File.dirname(__FILE__), "assets/sample_03.rb")
6
+ load sample_file
7
+
8
+ def setup
9
+ @a = Rcov::CallSiteAnalyzer.new
10
+ @o = Rcov::Test::Temporary::Sample03.new
11
+ end
12
+
13
+ def verify_callsites_equal(expected, actual)
14
+ callsites = expected.inject({}) do |s,(backtrace, count)|
15
+ unless $".any?{|x| %r{\brcovrt\b} =~ x}
16
+ backtrace = backtrace.map{|_, mid, file, line| [nil, mid, file, line] }
17
+ end
18
+ backtrace[0][2] = File.expand_path(backtrace[0][2])
19
+ s[Rcov::CallSiteAnalyzer::CallSite.new(backtrace)] = count
20
+ s
21
+ end
22
+
23
+ act_callsites = actual.inject({}) do |s, (key, value)|
24
+ #wow thats obtuse. In a callsite we have an array of arrays. We have to deep copy them because
25
+ # if we muck with the actual backtrace it messes things up for accumulation type tests.
26
+ # we have to muck with backtrace so that we can normalize file names (it's an issue between MRI and JRuby)
27
+ backtrace = key.backtrace.inject([]) {|y, val| y << val.inject([]) {|z, v2| z<< v2}}
28
+ backtrace[0][2] = File.expand_path(key.backtrace[0][2])
29
+ s[Rcov::CallSiteAnalyzer::CallSite.new(backtrace)] = value
30
+ s
31
+ end
32
+ assert_equal(callsites.to_s, act_callsites.to_s)
33
+ end
34
+
35
+ def verify_defsite_equal(expected, actual)
36
+ defsite = Rcov::CallSiteAnalyzer::DefSite.new(*expected)
37
+ defsite.file = File.expand_path defsite.file
38
+ actual.file = File.expand_path actual.file
39
+ assert_equal(defsite.to_s, actual.to_s)
40
+ end
41
+
42
+ def test_callsite_compute_raw_difference
43
+ src = [
44
+ { ["Foo", "foo"] => {"bar" => 1},
45
+ ["Foo", "bar"] => {"baz" => 10} },
46
+ { ["Foo", "foo"] => ["foo.rb", 10] }
47
+ ]
48
+ dst = [
49
+ { ["Foo", "foo"] => {"bar" => 1, "fubar" => 10},
50
+ ["Foo", "baz"] => {"baz" => 10} },
51
+ { ["Foo", "foo"] => ["fooredef.rb", 10],
52
+ ["Foo", "baz"] => ["foo.rb", 20]}
53
+ ]
54
+ expected = [
55
+ { ["Foo", "foo"] => {"fubar" => 10},
56
+ ["Foo", "baz"] => {"baz" => 10} },
57
+ { ["Foo", "foo"] => ["fooredef.rb", 10],
58
+ ["Foo", "baz"] => ["foo.rb", 20] }
59
+ ]
60
+
61
+ assert_equal(expected,
62
+ @a.instance_eval{ compute_raw_data_difference(src, dst) } )
63
+ end
64
+
65
+ def test_return_values_when_no_match
66
+ @a.run_hooked{ @o.f1 }
67
+ assert_equal(nil, @a.defsite("Foobar#bogus"))
68
+ assert_equal(nil, @a.defsite("Foobar", "bogus"))
69
+ assert_equal(nil, @a.callsites("Foobar", "bogus"))
70
+ assert_equal(nil, @a.callsites("Foobar.bogus"))
71
+ assert_equal(nil, @a.callsites("<Class:Foobar>", "bogus"))
72
+ end
73
+
74
+ def test_basic_defsite_recording
75
+ @a.run_hooked{ @o.f1 }
76
+ verify_defsite_equal(["./test/assets/sample_03.rb", 3],
77
+ @a.defsite("Rcov::Test::Temporary::Sample03", "f1"))
78
+ verify_defsite_equal(["./test/assets/sample_03.rb", 7],
79
+ @a.defsite("Rcov::Test::Temporary::Sample03", "f2"))
80
+ verify_defsite_equal(["./test/assets/sample_03.rb", 7],
81
+ @a.defsite("Rcov::Test::Temporary::Sample03#f2"))
82
+ end
83
+
84
+ def test_basic_callsite_recording
85
+ @a.run_hooked{ @o.f1 }
86
+ assert(@a.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
87
+ assert_equal(%w[f1 f2], @a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
88
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1, "./test/assets/sample_03.rb", 4]] => 10},
89
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
90
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1, "./test/assets/sample_03.rb", 4]] => 10},
91
+ @a.callsites("Rcov::Test::Temporary::Sample03#f2"))
92
+ #verify_callsites_equal({["./test/sample_03.rb:4:in `f1'"] => 10},
93
+ # @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
94
+ #verify_callsites_equal({["./test/sample_03.rb:4:in `f1'"] => 10},
95
+ # @a.callsites("Rcov::Test::Temporary::Sample03#f2"))
96
+ end
97
+
98
+ def test_basic_callsite_recording_API
99
+ @a.run_hooked{ @o.f1 }
100
+ assert(@a.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
101
+ assert_equal(%w[f1 f2], @a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
102
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
103
+ "./test/assets/sample_03.rb", 4]] => 10},
104
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
105
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1, "./test/assets/sample_03.rb", 4]] => 10},
106
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
107
+ callsites = @a.callsites("Rcov::Test::Temporary::Sample03", "f2")
108
+ callsite = callsites.keys[0]
109
+ #expand path is used here to compensate for differences between JRuby and MRI
110
+ assert_equal(File.expand_path("./test/assets/sample_03.rb"), File.expand_path(callsite.file))
111
+ assert_equal(4, callsite.line)
112
+ assert_equal(:f1, callsite.calling_method)
113
+ end
114
+
115
+
116
+ def test_basic_callsite_recording_with_singleton_classes
117
+ @a.run_hooked{ @o.class.g1 }
118
+ assert(@a.analyzed_classes.include?("#<Class:Rcov::Test::Temporary::Sample03>"))
119
+ assert_equal(%w[g1 g2], @a.analyzed_methods("#<Class:Rcov::Test::Temporary::Sample03>"))
120
+ verify_callsites_equal({[[class << Rcov::Test::Temporary::Sample03; self end,
121
+ :g1, "./test/assets/sample_03.rb", 15]] => 10},
122
+ @a.callsites("Rcov::Test::Temporary::Sample03.g2"))
123
+ verify_callsites_equal({[[class << Rcov::Test::Temporary::Sample03; self end,
124
+ :g1, "./test/assets/sample_03.rb", 15]] => 10},
125
+ @a.callsites("#<Class:Rcov::Test::Temporary::Sample03>","g2"))
126
+ end
127
+
128
+
129
+ def test_differential_callsite_recording
130
+ @a.run_hooked{ @o.f1 }
131
+ assert(@a.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
132
+ assert_equal(%w[f1 f2], @a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
133
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
134
+ "./test/assets/sample_03.rb", 4]] => 10},
135
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
136
+
137
+ @a.run_hooked{ @o.f1 }
138
+ assert(@a.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
139
+ assert_equal(%w[f1 f2], @a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
140
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
141
+ "./test/assets/sample_03.rb", 4]] => 20},
142
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
143
+
144
+ @a.run_hooked{ @o.f3 }
145
+ assert_equal(%w[f1 f2 f3], @a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
146
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
147
+ "./test/assets/sample_03.rb", 4]] => 120,
148
+ [[Rcov::Test::Temporary::Sample03, :f3,
149
+ "./test/assets/sample_03.rb", 11]]=>100 },
150
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
151
+ end
152
+
153
+ def test_reset
154
+ @a.run_hooked do
155
+ 10.times{ @o.f1 }
156
+ @a.reset
157
+ @o.f1
158
+ end
159
+ assert(@a.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
160
+ assert_equal(%w[f1 f2], @a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
161
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
162
+ "./test/assets/sample_03.rb", 4]] => 10},
163
+ @a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
164
+
165
+ end
166
+
167
+ def test_nested_callsite_recording
168
+ a = Rcov::CallSiteAnalyzer.new
169
+ b = Rcov::CallSiteAnalyzer.new
170
+ a.run_hooked do
171
+ b.run_hooked { @o.f1 }
172
+ assert(b.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
173
+ assert_equal(%w[f1 f2], b.analyzed_methods("Rcov::Test::Temporary::Sample03"))
174
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
175
+ "./test/assets/sample_03.rb", 4]] => 10},
176
+ b.callsites("Rcov::Test::Temporary::Sample03", "f2"))
177
+
178
+ @o.f1
179
+ assert_equal(%w[f1 f2], b.analyzed_methods("Rcov::Test::Temporary::Sample03"))
180
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
181
+ "./test/assets/sample_03.rb", 4]] => 10},
182
+ b.callsites("Rcov::Test::Temporary::Sample03", "f2"))
183
+
184
+ assert(a.analyzed_classes.include?("Rcov::Test::Temporary::Sample03"))
185
+ assert_equal(%w[f1 f2], a.analyzed_methods("Rcov::Test::Temporary::Sample03"))
186
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
187
+ "./test/assets/sample_03.rb", 4]] => 20},
188
+ a.callsites("Rcov::Test::Temporary::Sample03", "f2"))
189
+ end
190
+ b.run_hooked{ @o.f3 }
191
+ assert_equal(%w[f1 f2 f3], b.analyzed_methods("Rcov::Test::Temporary::Sample03"))
192
+ verify_callsites_equal({[[Rcov::Test::Temporary::Sample03, :f1,
193
+ "./test/assets/sample_03.rb", 4]] => 110,
194
+ [[Rcov::Test::Temporary::Sample03, :f3,
195
+ "./test/assets/sample_03.rb", 11]]=>100 },
196
+ b.callsites("Rcov::Test::Temporary::Sample03", "f2"))
197
+ end
198
+
199
+ def test_expand_name
200
+ assert_equal(["Foo", "foo"], @a.instance_eval{ expand_name("Foo#foo") })
201
+ assert_equal(["Foo", "foo"], @a.instance_eval{ expand_name("Foo", "foo") })
202
+ assert_equal(["#<Class:Foo>", "foo"],
203
+ @a.instance_eval{ expand_name("Foo.foo") })
204
+ assert_equal(["#<Class:Foo>", "foo"],
205
+ @a.instance_eval{ expand_name("#<Class:Foo>", "foo") })
206
+ end
207
+ end
@@ -0,0 +1,186 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestCodeCoverageAnalyzer < Test::Unit::TestCase
4
+ LINES = <<-EOF.to_a
5
+ puts 1
6
+ if foo
7
+ bar
8
+ baz
9
+ end
10
+ 5.times do
11
+ foo
12
+ bar if baz
13
+ end
14
+ EOF
15
+
16
+ def setup
17
+ if defined? Rcov::Test::Temporary
18
+ Rcov::Test::Temporary.constants.each do |name|
19
+ Rcov::Test::Temporary.module_eval{ remove_const(name) }
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_refine_coverage_info
25
+ analyzer = Rcov::CodeCoverageAnalyzer.new
26
+ cover = [1, 1, nil, nil, 0, 5, 5, 5, 0]
27
+ line_info, marked_info,
28
+ count_info = analyzer.instance_eval{ refine_coverage_info(LINES, cover) }
29
+ assert_equal(LINES, line_info)
30
+ assert_equal([true] * 2 + [false] * 3 + [true] * 3 + [false], marked_info)
31
+ assert_equal([1, 1, 0, 0, 0, 5, 5, 5, 0], count_info)
32
+ end
33
+
34
+ def test_analyzed_files_no_analysis
35
+ analyzer = Rcov::CodeCoverageAnalyzer.new
36
+ assert_equal([], analyzer.analyzed_files)
37
+ end
38
+
39
+ def test_raw_coverage_info
40
+ sample_file = File.join(File.dirname(__FILE__), "assets/sample_01.rb")
41
+ lines = File.readlines(sample_file)
42
+ analyzer = Rcov::CodeCoverageAnalyzer.new
43
+ analyzer.run_hooked{ load sample_file }
44
+
45
+ assert_equal(lines, SCRIPT_LINES__[sample_file][0, lines.size])
46
+ assert(analyzer.analyzed_files.include?(sample_file))
47
+ line_info, cov_info, count_info = analyzer.data(sample_file)
48
+ assert_equal(lines, line_info)
49
+ assert_equal([true, true, false, false, true, false, true], cov_info)
50
+ assert_equal([1, 2, 0, 0, 1, 0, 11], count_info) unless PLATFORM =~ /java/
51
+ # JRUBY reports an if x==blah as hitting this type of line once, JRUBY also optimizes this stuff so you'd have to run with --debug to get "extra" information. MRI hits it twice.
52
+ assert_equal([1, 1, 0, 0, 1, 0, 11], count_info) if PLATFORM =~ /java/
53
+ analyzer.reset
54
+ assert_equal(nil, analyzer.data(sample_file))
55
+ assert_equal([], analyzer.analyzed_files)
56
+ end
57
+
58
+ def test_script_lines_workaround_detects_correctly
59
+ analyzer = Rcov::CodeCoverageAnalyzer.new
60
+ lines = ["puts a", "foo", "bar"] * 3
61
+ coverage = [true] * 3 + [false] * 6
62
+ counts = [1] * 3 + [0] * 6
63
+ nlines, ncoverage, ncounts = analyzer.instance_eval do
64
+ script_lines_workaround(lines, coverage, counts)
65
+ end
66
+
67
+ assert_equal(["puts a", "foo", "bar"], nlines)
68
+ assert_equal([true, true, true], ncoverage)
69
+ assert_equal([1, 1, 1], ncounts)
70
+ end
71
+
72
+ def test_script_lines_workaround_no_false_positives
73
+ analyzer = Rcov::CodeCoverageAnalyzer.new
74
+ lines = ["puts a", "foo", "bar"] * 2 + ["puts a", "foo", "baz"]
75
+ coverage = [true] * 9
76
+ counts = [1] * 9
77
+ nlines, ncoverage, ncounts = analyzer.instance_eval do
78
+ script_lines_workaround(lines, coverage, counts)
79
+ end
80
+ assert_equal(lines, nlines)
81
+ assert_equal(coverage, ncoverage)
82
+ assert_equal(counts, ncounts)
83
+ end
84
+
85
+ def test_differential_coverage_data
86
+ sample_file = File.join(File.dirname(__FILE__), "assets/sample_01.rb")
87
+ lines = File.readlines(sample_file)
88
+ analyzer = Rcov::CodeCoverageAnalyzer.new
89
+ analyzer.run_hooked{ load sample_file }
90
+ line_info, cov_info, count_info = analyzer.data(sample_file)
91
+ assert_equal([1, 2, 0, 0, 1, 0, 11], count_info) unless PLATFORM =~ /java/
92
+ # JRUBY reports an if x==blah as hitting this type of line once, JRUBY also optimizes this stuff so you'd have to run with --debug to get "extra" information. MRI hits it twice.
93
+ assert_equal([1, 1, 0, 0, 1, 0, 11], count_info) if PLATFORM =~ /java/
94
+
95
+ analyzer.reset
96
+ #set_trace_func proc { |event, file, line, id, binding, classname| printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname if (file =~ /sample_02.rb/) }
97
+
98
+ sample_file = File.join(File.dirname(__FILE__), "assets/sample_02.rb")
99
+ analyzer.run_hooked{ load sample_file }
100
+ line_info, cov_info, count_info = analyzer.data(sample_file)
101
+ assert_equal([8, 1, 0, 0, 0], count_info)
102
+
103
+ analyzer.reset
104
+ assert_equal([], analyzer.analyzed_files)
105
+ analyzer.run_hooked{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
106
+ line_info, cov_info, count_info = analyzer.data(sample_file)
107
+ assert_equal([0, 1, 1, 1, 0], count_info)
108
+ analyzer.run_hooked do
109
+ 10.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
110
+ end
111
+ line_info, cov_info, count_info = analyzer.data(sample_file)
112
+ assert_equal([0, 11, 11, 11, 0], count_info)
113
+ 10.times{ analyzer.run_hooked{ Rcov::Test::Temporary::Sample02.foo(1, 1) } }
114
+ line_info, cov_info, count_info = analyzer.data(sample_file)
115
+ assert_equal([0, 21, 21, 21, 0], count_info)
116
+
117
+ count_info2 = nil
118
+ 10.times do |i|
119
+ analyzer.run_hooked do
120
+ Rcov::Test::Temporary::Sample02.foo(1, 1)
121
+ line_info, cov_info, count_info = analyzer.data(sample_file) if i == 3
122
+ line_info2, cov_info2, count_info2 = analyzer.data(sample_file)
123
+ end
124
+ end
125
+ assert_equal([0, 25, 25, 25, 0], count_info)
126
+ assert_equal([0, 31, 31, 31, 0], count_info2)
127
+ end
128
+
129
+ def test_nested_analyzer_blocks
130
+ a1 = Rcov::CodeCoverageAnalyzer.new
131
+ a2 = Rcov::CodeCoverageAnalyzer.new
132
+
133
+ sample_file = File.join(File.dirname(__FILE__), "assets/sample_02.rb")
134
+ load sample_file
135
+
136
+ a1.run_hooked do
137
+ 100.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
138
+ a2.run_hooked do
139
+ 10.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
140
+ end
141
+ 100.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
142
+ end
143
+
144
+ a2.run_hooked do
145
+ 100.times{ Rcov::Test::Temporary::Sample02.foo(1, 1) }
146
+ 10.times{ a1.run_hooked { Rcov::Test::Temporary::Sample02.foo(1, 1) } }
147
+ end
148
+
149
+ a1.install_hook
150
+ Rcov::Test::Temporary::Sample02.foo(1, 1)
151
+ a1.remove_hook
152
+
153
+ a2.install_hook
154
+ Rcov::Test::Temporary::Sample02.foo(1, 1)
155
+ a2.remove_hook
156
+
157
+ _, _, counts1 = a1.data(sample_file)
158
+ _, _, counts2 = a2.data(sample_file)
159
+ assert_equal([0, 221, 221, 221, 0], counts1)
160
+ assert_equal([0, 121, 121, 121, 0], counts2)
161
+ end
162
+
163
+ def test_reset
164
+ a1 = Rcov::CodeCoverageAnalyzer.new
165
+
166
+ sample_file = File.join(File.dirname(__FILE__), "assets/sample_02.rb")
167
+ load sample_file
168
+
169
+ a1.run_hooked do
170
+ 100.times do |i|
171
+ Rcov::Test::Temporary::Sample02.foo(1, 1)
172
+ a1.reset if i == 49
173
+ end
174
+ end
175
+
176
+ assert_equal([0, 50, 50, 50, 0], a1.data(sample_file)[2])
177
+ end
178
+
179
+ def test_compute_raw_difference
180
+ first = {"a" => [1,1,1,1,1]}
181
+ last = {"a" => [2,1,5,2,1], "b" => [1,2,3,4,5]}
182
+ a = Rcov::CodeCoverageAnalyzer.new
183
+ assert_equal({"a" => [1,0,4,1,0], "b" => [1,2,3,4,5]},
184
+ a.instance_eval{ compute_raw_data_difference(first, last)} )
185
+ end
186
+ end