1import Foundation
2
3/// How a module or file stands across every metric it has: the mean of their shares, so
4/// each metric counts equally however many items it has (DESIGN.md D4).
5struct OverallCoverage {
6 /// 0 to 100.
7 let percent: Double
8 /// The overall share and each metric's, such as
9 /// `Overall 75.0%: lines 80.0%, regions 70.0%`.
10 let breakdown: String
11
12 /// A metric that is nil stays out of the mean.
13 init(
14 lines: LineSummary, regions: CoverageCount?, branches: CoverageCount?,
15 conditions: CoverageCount?
16151 ) {
17151 let others = [("regions", regions), ("branches", branches), ("conditions", conditions)]
18151 let shares =
19151 [("lines", lines.percent, lines.percentText)]
20453 + others.compactMap { name, count in
21453 count.map { count in
22163 let share = LineSummary(count: count.total, covered: count.covered)
23163 return (name, share.percent, share.percentText)
24163 }
25453 }
26314 let mean = shares.map(\.1).reduce(0, +) / Double(shares.count)
27151 percent = mean
28314 let named = shares.map { name, _, text in "\(name) \(text)" }.joined(separator: ", ")
29151 breakdown = "Overall \(Self.text(of: mean)): \(named)"
30151 }
31
3275 var percentText: String { Self.text(of: percent) }
33
3475 var tier: CoverageTier { CoverageTier(percent: percent) }
35
36226 private static func text(of percent: Double) -> String {
37226 String(format: "%.1f%%", percent)
38226 }
39}