Skip to content

Commit ddccb3c

Browse files
committed
add specs
1 parent 6420688 commit ddccb3c

File tree

4 files changed

+163
-17
lines changed

4 files changed

+163
-17
lines changed

spec/fixtures/coverer.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22

33
require "coverage"
44
Coverage.start(:all)
5-
require_relative "uneven_nocovs"
6-
7-
UnevenNocov.call(42)
8-
5+
require_relative "methods"
96
p Coverage.result

spec/fixtures/methods.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class A
2+
def method1
3+
puts "hello from method1"
4+
method2
5+
end
6+
7+
private
8+
9+
def method2
10+
puts "hello from method2"
11+
end
12+
13+
def method3
14+
puts "hello from method3"
15+
end
16+
end
17+
18+
A.new.method1

spec/source_file/method_spec.rb

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,48 @@
33
require "helper"
44

55
describe SimpleCov::SourceFile::Method do
6-
# TODO[@tycooon]: add specs
6+
subject { described_class.new(source_file, info, coverage) }
7+
8+
let(:source_file) do
9+
SimpleCov::SourceFile.new(source_fixture("methods.rb"), lines: {})
10+
end
11+
12+
let(:info) { ["A", :method1, 2, 2, 5, 5] }
13+
let(:coverage) { 1 }
14+
15+
it "is covered" do
16+
expect(subject.covered?).to eq(true)
17+
end
18+
19+
it "is not skipped" do
20+
expect(subject.skipped?).to eq(false)
21+
end
22+
23+
it "is not missed" do
24+
expect(subject.missed?).to eq(false)
25+
end
26+
27+
it "has 4 lines" do
28+
expect(subject.lines.size).to eq(4)
29+
end
30+
31+
it "converts to string properly" do
32+
expect(subject.to_s).to eq("A#method1")
33+
end
34+
35+
context "uncovered method" do
36+
let(:coverage) { 0 }
37+
38+
it "is not covered" do
39+
expect(subject.covered?).to eq(false)
40+
end
41+
42+
it "is not skipped" do
43+
expect(subject.skipped?).to eq(false)
44+
end
45+
46+
it "is missed" do
47+
expect(subject.missed?).to eq(true)
48+
end
49+
end
750
end

spec/source_file_spec.rb

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
end
7070
end
7171

72-
# TODO[@tycooon]: add method cov
7372
describe "branch coverage" do
7473
it "has total branches count 0" do
7574
expect(subject.total_branches.size).to eq(0)
@@ -91,23 +90,43 @@
9190
expect(subject.branches_report).to eq({})
9291
end
9392
end
93+
94+
describe "method coverage" do
95+
it "has total methods count 0" do
96+
expect(subject.total_methods.size).to eq(0)
97+
end
98+
99+
it "has covered methods count 0" do
100+
expect(subject.covered_methods.size).to eq(0)
101+
end
102+
103+
it "has missed methods count 0" do
104+
expect(subject.missed_methods.size).to eq(0)
105+
end
106+
107+
it "is considered 100% methods covered" do
108+
expect(subject.methods_coverage_percent).to eq(100.0)
109+
end
110+
end
94111
end
95112

96113
context "file with branches" do
97-
COVERAGE_FOR_BRANCHES_RB = {
98-
lines: [1, 1, 1, nil, 1, nil, 1, 0, nil, 1, nil, nil, nil],
99-
branches: {
100-
[:if, 0, 3, 4, 3, 21] =>
101-
{[:then, 1, 3, 4, 3, 10] => 0, [:else, 2, 3, 4, 3, 21] => 1},
102-
[:if, 3, 5, 4, 5, 26] =>
103-
{[:then, 4, 5, 16, 5, 20] => 1, [:else, 5, 5, 23, 5, 26] => 0},
104-
[:if, 6, 7, 4, 11, 7] =>
105-
{[:then, 7, 8, 6, 8, 10] => 0, [:else, 8, 10, 6, 10, 9] => 1}
114+
let(:coverage_for_branches_rb) do
115+
{
116+
lines: [1, 1, 1, nil, 1, nil, 1, 0, nil, 1, nil, nil, nil],
117+
branches: {
118+
[:if, 0, 3, 4, 3, 21] =>
119+
{[:then, 1, 3, 4, 3, 10] => 0, [:else, 2, 3, 4, 3, 21] => 1},
120+
[:if, 3, 5, 4, 5, 26] =>
121+
{[:then, 4, 5, 16, 5, 20] => 1, [:else, 5, 5, 23, 5, 26] => 0},
122+
[:if, 6, 7, 4, 11, 7] =>
123+
{[:then, 7, 8, 6, 8, 10] => 0, [:else, 8, 10, 6, 10, 9] => 1}
124+
}
106125
}
107-
}.freeze
126+
end
108127

109128
subject do
110-
SimpleCov::SourceFile.new(source_fixture("branches.rb"), COVERAGE_FOR_BRANCHES_RB)
129+
SimpleCov::SourceFile.new(source_fixture("branches.rb"), coverage_for_branches_rb)
111130
end
112131

113132
describe "branch coverage" do
@@ -164,6 +183,60 @@
164183
end
165184
end
166185

186+
context "file with methods" do
187+
let(:coverage_for_methods_rb) do
188+
{
189+
lines: [1, 1, 1, 1, nil, nil, 1, nil, 1, 1, nil, nil, 1, 0, nil, nil, nil, 1],
190+
branches: {},
191+
methods: {
192+
["A", :method3, 13, 2, 15, 5] => 0,
193+
["A", :method2, 9, 2, 11, 5] => 1,
194+
["A", :method1, 2, 2, 5, 5] => 1
195+
}
196+
}
197+
end
198+
199+
subject do
200+
SimpleCov::SourceFile.new(source_fixture("methods.rb"), coverage_for_methods_rb)
201+
end
202+
203+
describe "method coverage" do
204+
it "has total methods count 0" do
205+
expect(subject.total_methods.size).to eq(3)
206+
end
207+
208+
it "has covered methods count 0" do
209+
expect(subject.covered_methods.size).to eq(2)
210+
end
211+
212+
it "has missed methods count 0" do
213+
expect(subject.missed_methods.size).to eq(1)
214+
end
215+
216+
it "is considered 66.(6)% methods covered" do
217+
expect(subject.methods_coverage_percent).to eq(66.66666666666667)
218+
end
219+
end
220+
221+
describe "line coverage" do
222+
it "has line coverage" do
223+
expect(subject.covered_percent).to eq 90.0
224+
end
225+
226+
it "has 9 covered lines" do
227+
expect(subject.covered_lines.size).to eq 9
228+
end
229+
230+
it "has 1 missed line" do
231+
expect(subject.missed_lines.size).to eq 1
232+
end
233+
234+
it "has 10 relevant lines" do
235+
expect(subject.relevant_lines).to eq 10
236+
end
237+
end
238+
end
239+
167240
context "simulating potential Ruby 1.9 defect -- see Issue #56" do
168241
subject do
169242
SimpleCov::SourceFile.new(source_fixture("sample.rb"), COVERAGE_FOR_SAMPLE_RB_WITH_MORE_LINES)
@@ -234,6 +307,10 @@
234307
it "has 100.0 branch coverage" do
235308
expect(subject.branches_coverage_percent).to eq(100.00)
236309
end
310+
311+
it "has 100.0 method coverage" do
312+
expect(subject.methods_coverage_percent).to eq(100.00)
313+
end
237314
end
238315

239316
context "a file where nothing is ever executed mixed with skipping #563" do
@@ -327,6 +404,17 @@
327404
expect(subject.covered_branches.size).to eq 0
328405
end
329406
end
407+
408+
describe "method coverage" do
409+
it "has no methods" do
410+
expect(subject.total_methods.size).to eq 0
411+
end
412+
413+
it "does has neither covered nor missed methods" do
414+
expect(subject.missed_methods.size).to eq 0
415+
expect(subject.covered_methods.size).to eq 0
416+
end
417+
end
330418
end
331419

332420
context "a file with more complex skipping" do

0 commit comments

Comments
 (0)