| 1 | | import 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). |
| 5 | | struct 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? |
| 16 | 147 | ) { |
| 17 | 147 | let others = [("regions", regions), ("branches", branches), ("conditions", conditions)] |
| 18 | 147 | let shares = |
| 19 | 147 | [("lines", lines.percent, lines.percentText)] |
| 20 | 441 | + others.compactMap { name, count in |
| 21 | 441 | count.map { count in |
| 22 | 155 | let share = LineSummary(count: count.total, covered: count.covered) |
| 23 | 155 | return (name, share.percent, share.percentText) |
| 24 | 155 | } |
| 25 | 441 | } |
| 26 | 302 | let mean = shares.map(\.1).reduce(0, +) / Double(shares.count) |
| 27 | 147 | percent = mean |
| 28 | 302 | let named = shares.map { name, _, text in "\(name) \(text)" }.joined(separator: ", ") |
| 29 | 147 | breakdown = "Overall \(Self.text(of: mean)): \(named)" |
| 30 | 147 | } |
| 31 | | |
| 32 | 73 | var percentText: String { Self.text(of: percent) } |
| 33 | | |
| 34 | 73 | var tier: CoverageTier { CoverageTier(percent: percent) } |
| 35 | | |
| 36 | 220 | private static func text(of percent: Double) -> String { |
| 37 | 220 | String(format: "%.1f%%", percent) |
| 38 | 220 | } |
| 39 | | } |