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`.
12100 var text: String {
13100 switch self {
14100 case .new: "new"
15100 case .points: roundedPoints == 0 ? "0.0" : String(format: "%+.1f", roundedPoints)
16100 }
17100 }
18
19 /// The style the table gives the change: `new`, `up`, `down` or `flat`.
2087 var styleName: String {
2187 switch self {
2287 case .new: "new"
2387 case .points: roundedPoints > 0 ? "up" : roundedPoints < 0 ? "down" : "flat"
2487 }
2587 }
26
27208 private var roundedPoints: Double {
28208 guard case .points(let points) = self else { return 0 }
29208 return (points * 10).rounded() / 10
30208 }
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
4016 init(baseline: RunTotals) {
4116 self.baseline = baseline
4216 total = baseline.lines
4316 modules = Dictionary(uniqueKeysWithValues: baseline.modules.map { ($0.name, $0.lines) })
4416 files = Dictionary(
4517 uniqueKeysWithValues: baseline.modules.flatMap(\.files).map { ($0.path, $0.lines) })
4616 }
47
48 /// The whole run's line coverage, which the baseline always has, so never `.new`.
4913 func changeOfTotal(now: LineSummary) -> CoverageChange {
5013 Self.change(from: total, to: now)
5113 }
52
5324 func change(ofModule name: String, now: LineSummary) -> CoverageChange {
5424 Self.change(from: modules[name], to: now)
5524 }
56
57 /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`.
5829 func change(ofFile path: String, now: LineSummary) -> CoverageChange {
5929 Self.change(from: files[path], to: now)
6029 }
61
62 /// The change in the number of uncovered lines across the run.
6310 func missedLineChange(now: LineSummary) -> Int {
6410 (now.count - now.covered) - (total.total - total.covered)
6510 }
66
67 /// Files whose relative paths were absent from the baseline.
6810 func newFileCount(in current: [FileTotals]) -> Int {
6927 current.filter { files[$0.relativePath] == nil }.count
7010 }
71
723 func regionChange(now: CoverageCount) -> CoverageChange {
733 Self.change(
743 from: baseline.regions, to: LineSummary(count: now.total, covered: now.covered))
753 }
76
772 func branchChange(now: CoverageCount) -> CoverageChange {
782 Self.change(
792 from: baseline.branches, to: LineSummary(count: now.total, covered: now.covered))
802 }
81
822 func conditionChange(now: CoverageCount) -> CoverageChange {
832 Self.change(
842 from: baseline.conditions, to: LineSummary(count: now.total, covered: now.covered))
852 }
86
87 private static func change(from before: RunTotals.Lines?, to now: LineSummary) -> CoverageChange
8873 {
8973 guard let before else { return .new }
9044 return .points(
9144 now.percent - LineSummary(count: before.total, covered: before.covered).percent)
9273 }
93}