Skip to content

Commit

Permalink
Rename CoverageData to CoverageStatistics
Browse files Browse the repository at this point in the history
*Data was an unfortunate name, as in SourceFile that's what the
attribute holding the well... actual coverage data emitted from
the coverage library. All in all it's statistics for display
and that should be helpful and neat, and not clash with other
names.
  • Loading branch information
PragTob committed Jan 26, 2020
1 parent 30deeeb commit 81b1402
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 87 deletions.
2 changes: 1 addition & 1 deletion lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def result_with_not_loaded_files
require "forwardable"
require "simplecov/configuration"
SimpleCov.extend SimpleCov::Configuration
require "simplecov/coverage_data"
require "simplecov/coverage_statistics"
require "simplecov/exit_codes"
require "simplecov/profiles"
require "simplecov/source_file/line"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ module SimpleCov
# * missed - how many of the coverables are missed
# * percent - percentage as covered/missed
# * strength - average hits per/coverable (will not exist for one shot lines format)
class CoverageData
class CoverageStatistics
attr_reader :total, :covered, :missed, :strength, :percent

def self.from(coverage_data)
def self.from(coverage_statistics)
sum_covered, sum_missed, sum_total_strength =
coverage_data.reduce([0, 0, 0.0]) do |(covered, missed, total_strength), file_coverage_data|
coverage_statistics.reduce([0, 0, 0.0]) do |(covered, missed, total_strength), file_coverage_statistics|
[
covered + file_coverage_data.covered,
missed + file_coverage_data.missed,
covered + file_coverage_statistics.covered,
missed + file_coverage_statistics.missed,
# gotta remultiply with loc because files have different strenght and loc
# giving them a different "weight" in total
total_strength + (file_coverage_data.strength * file_coverage_data.total)
total_strength + (file_coverage_statistics.strength * file_coverage_statistics.total)
]
end

Expand Down
8 changes: 4 additions & 4 deletions lib/simplecov/file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def branch_covered_percent
private

def compute_coverage
total_coverage_data = @files.each_with_object(line: [], branch: []) do |file, together|
total_coverage_statistics = @files.each_with_object(line: [], branch: []) do |file, together|
together[:line] << file.coverage[:line]
together[:branch] << file.coverage[:branch] if SimpleCov.branch_coverage?
end

coverage_data = {line: CoverageData.from(total_coverage_data[:line])}
coverage_data[:branch] = CoverageData.from(total_coverage_data[:branch]) if SimpleCov.branch_coverage?
coverage_data
coverage_statistics = {line: CoverageStatistics.from(total_coverage_statistics[:line])}
coverage_statistics[:branch] = CoverageStatistics.from(total_coverage_statistics[:branch]) if SimpleCov.branch_coverage?
coverage_statistics
end
end
end
4 changes: 2 additions & 2 deletions lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def build_branch(branch_data, hit_count, condition_start_line)

def line_coverage
{
line: CoverageData.new(
line: CoverageStatistics.new(
total_strength: lines_strength,
covered: covered_lines.size,
missed: missed_lines.size
Expand All @@ -290,7 +290,7 @@ def line_coverage

def branch_coverage
{
branch: CoverageData.new(
branch: CoverageStatistics.new(
covered: covered_branches.size,
missed: missed_branches.size
)
Expand Down
74 changes: 0 additions & 74 deletions spec/coverage_data_spec.rb

This file was deleted.

74 changes: 74 additions & 0 deletions spec/coverage_statistics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

require "helper"

RSpec.describe SimpleCov::CoverageStatistics do
describe ".new" do
it "retains statistics and computes new ones" do
statistics = described_class.new(covered: 4, missed: 6, total_strength: 14)

expect(statistics.covered).to eq 4
expect(statistics.missed).to eq 6

expect(statistics.total).to eq 10
expect(statistics.percent).to eq 40.0
expect(statistics.strength).to eq 1.4
end

it "can omit the total strength defaulting to 0.0" do
statistics = described_class.new(covered: 4, missed: 6, total_strength: 0.0)

expect(statistics.strength).to eq 0.0
end

it "can deal with it if everything is 0" do
statistics = described_class.new(covered: 0, missed: 0, total_strength: 0.0)

expect_all_empty(statistics)
end
end

describe ".from" do
it "returns an all 0s coverage statistics if there is no statistics" do
statistics = described_class.from([])

expect_all_empty(statistics)
end

it "returns all empty statistics when initialized with a couple of empty results" do
statistics = described_class.from([empty_statistics, empty_statistics])

expect_all_empty(statistics)
end

it "produces sensible total results" do
statistics = described_class.from(
[
described_class.new(covered: 3, missed: 4, total_strength: 54),
described_class.new(covered: 0, missed: 13, total_strength: 0),
described_class.new(covered: 37, missed: 0, total_strength: 682)
]
)

expect(statistics.covered).to eq 40
expect(statistics.missed).to eq 17
expect(statistics.total).to eq 57
expect(statistics.percent).to be_within(0.01).of(70.18)
expect(statistics.strength).to be_within(0.01).of(12.91)
end
end

def empty_statistics
described_class.new(covered: 0, missed: 0, total_strength: 0.0)
end

def expect_all_empty(statistics)
expect(statistics.covered).to eq 0
expect(statistics.missed).to eq 0

expect(statistics.total).to eq 0
# might be counter intuitive but think of it as "we covered everything we could"
expect(statistics.percent).to eq 100.0
expect(statistics.strength).to eq 0.0
end
end

0 comments on commit 81b1402

Please sign in to comment.