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?
16147 ) {
17147 let others = [("regions", regions), ("branches", branches), ("conditions", conditions)]
18147 let shares =
19147 [("lines", lines.percent, lines.percentText)]
20441 + others.compactMap { name, count in
21441 count.map { count in
22155 let share = LineSummary(count: count.total, covered: count.covered)
23155 return (name, share.percent, share.percentText)
24155 }
25441 }
26302 let mean = shares.map(\.1).reduce(0, +) / Double(shares.count)
27147 percent = mean
28302 let named = shares.map { name, _, text in "\(name) \(text)" }.joined(separator: ", ")
29147 breakdown = "Overall \(Self.text(of: mean)): \(named)"
30147 }
31
3273 var percentText: String { Self.text(of: percent) }
33
3473 var tier: CoverageTier { CoverageTier(percent: percent) }
35
36220 private static func text(of percent: Double) -> String {
37220 String(format: "%.1f%%", percent)
38220 }
39}