1import Foundation
2
3/// How a coverage percentage moved since the baseline run (DESIGN.md D6).
4enum CoverageChange: Equatable {
5 /// Not in the baseline.
6 case new
7 /// Percentage points gained (positive) or lost (negative).
8 case points(Double)
9
10 /// What the table shows: `new`, or points to one decimal with a sign, such as `+0.4`.
11 /// A move that rounds to nothing shows `0.0`.
12168 var text: String {
13168 switch self {
14168 case .new: "new"
15168 case .points: roundedPoints == 0 ? "0.0" : String(format: "%+.1f", roundedPoints)
16168 }
17168 }
18
19 /// The style the table gives the change: `new`, `up`, `down` or `flat`.
20146 var styleName: String {
21146 switch self {
22146 case .new: "new"
23146 case .points: roundedPoints > 0 ? "up" : roundedPoints < 0 ? "down" : "flat"
24146 }
25146 }
26
27347 private var roundedPoints: Double {
28347 guard case .points(let points) = self else { return 0 }
29347 return (points * 10).rounded() / 10
30347 }
31}
32
33/// The current run's modules and files set against an earlier run's `totals.json`.
34struct BaselineComparison {
35 private let baseline: RunTotals
36 private let total: RunTotals.Lines
37 private let modules: [String: RunTotals.Lines]
38 private let files: [String: RunTotals.Lines]
39 private let moduleRegions: [String: RunTotals.Lines]
40 private let fileRegions: [String: RunTotals.Lines]
41
4226 init(baseline: RunTotals) {
4326 self.baseline = baseline
4426 total = baseline.lines
4526 modules = Dictionary(uniqueKeysWithValues: baseline.modules.map { ($0.name, $0.lines) })
4626 files = Dictionary(
4727 uniqueKeysWithValues: baseline.modules.flatMap(\.files).map { ($0.path, $0.lines) })
4826 moduleRegions = Dictionary(
4926 uniqueKeysWithValues: baseline.modules.compactMap { module in
5026 module.regions.map { (module.name, $0) }
5126 })
5226 fileRegions = Dictionary(
5327 uniqueKeysWithValues: baseline.modules.flatMap(\.files).compactMap { file in
5427 file.regions.map { (file.path, $0) }
5527 })
5626 }
57
58 /// The whole run's line coverage, which the baseline always has, so never `.new`.
5923 func changeOfTotal(now: LineSummary) -> CoverageChange {
6023 Self.change(from: total, to: now)
6123 }
62
6334 func change(ofModule name: String, now: LineSummary) -> CoverageChange {
6434 Self.change(from: modules[name], to: now)
6534 }
66
67 /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`.
6838 func change(ofFile path: String, now: LineSummary) -> CoverageChange {
6938 Self.change(from: files[path], to: now)
7038 }
71
72 /// The change in the number of uncovered lines across the run.
7318 func missedLineChange(now: LineSummary) -> Int {
7418 (now.count - now.covered) - (total.total - total.covered)
7518 }
76
77 /// The change in the number of uncovered regions across the run; nil when the
78 /// baseline recorded none.
7910 func missedRegionChange(now: CoverageCount) -> Int? {
8010 baseline.regions.map { before in
816 (now.total - now.covered) - (before.total - before.covered)
826 }
8310 }
84
85 /// Files whose relative paths were absent from the baseline.
8618 func newFileCount(in current: [FileTotals]) -> Int {
8736 current.filter { files[$0.relativePath] == nil }.count
8818 }
89
9013 func regionChange(now: CoverageCount) -> CoverageChange {
9113 Self.change(
9213 from: baseline.regions, to: LineSummary(count: now.total, covered: now.covered))
9313 }
94
9513 func regionChange(ofModule name: String, now: CoverageCount) -> CoverageChange {
9613 Self.change(
9713 from: moduleRegions[name], to: LineSummary(count: now.total, covered: now.covered))
9813 }
99
100 /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`.
10110 func regionChange(ofFile path: String, now: CoverageCount) -> CoverageChange {
10210 Self.change(
10310 from: fileRegions[path], to: LineSummary(count: now.total, covered: now.covered))
10410 }
105
1062 func branchChange(now: CoverageCount) -> CoverageChange {
1072 Self.change(
1082 from: baseline.branches, to: LineSummary(count: now.total, covered: now.covered))
1092 }
110
1112 func conditionChange(now: CoverageCount) -> CoverageChange {
1122 Self.change(
1132 from: baseline.conditions, to: LineSummary(count: now.total, covered: now.covered))
1142 }
115
116 private static func change(from before: RunTotals.Lines?, to now: LineSummary) -> CoverageChange
117135 {
118135 guard let before else { return .new }
11992 return .points(
12092 now.percent - LineSummary(count: before.total, covered: before.covered).percent)
121135 }
122}