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`.
1272 var text: String {
1372 switch self {
1472 case .new: "new"
1572 case .points: roundedPoints == 0 ? "0.0" : String(format: "%+.1f", roundedPoints)
1672 }
1772 }
18
19 /// The style the table gives the change: `new`, `up`, `down` or `flat`.
2062 var styleName: String {
2162 switch self {
2262 case .new: "new"
2362 case .points: roundedPoints > 0 ? "up" : roundedPoints < 0 ? "down" : "flat"
2462 }
2562 }
26
27132 private var roundedPoints: Double {
28132 guard case .points(let points) = self else { return 0 }
29132 return (points * 10).rounded() / 10
30132 }
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
4013 init(baseline: RunTotals) {
4113 self.baseline = baseline
4213 total = baseline.lines
4313 modules = Dictionary(uniqueKeysWithValues: baseline.modules.map { ($0.name, $0.lines) })
4413 files = Dictionary(
4514 uniqueKeysWithValues: baseline.modules.flatMap(\.files).map { ($0.path, $0.lines) })
4613 }
47
48 /// The whole run's line coverage, which the baseline always has, so never `.new`.
4910 func changeOfTotal(now: LineSummary) -> CoverageChange {
5010 Self.change(from: total, to: now)
5110 }
52
5321 func change(ofModule name: String, now: LineSummary) -> CoverageChange {
5421 Self.change(from: modules[name], to: now)
5521 }
56
57 /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`.
5827 func change(ofFile path: String, now: LineSummary) -> CoverageChange {
5927 Self.change(from: files[path], to: now)
6027 }
61
62 /// The change in the number of uncovered lines across the run.
638 func missedLineChange(now: LineSummary) -> Int {
648 (now.count - now.covered) - (total.total - total.covered)
658 }
66
67 /// Files whose relative paths were absent from the baseline.
688 func newFileCount(in current: [FileTotals]) -> Int {
6925 current.filter { files[$0.relativePath] == nil }.count
708 }
71
722 func branchChange(now: CoverageCount) -> CoverageChange {
732 Self.change(
742 from: baseline.branches, to: LineSummary(count: now.total, covered: now.covered))
752 }
76
772 func conditionChange(now: CoverageCount) -> CoverageChange {
782 Self.change(
792 from: baseline.conditions, to: LineSummary(count: now.total, covered: now.covered))
802 }
81
82 private static func change(from before: RunTotals.Lines?, to now: LineSummary) -> CoverageChange
8362 {
8462 guard let before else { return .new }
8534 return .points(
8634 now.percent - LineSummary(count: before.total, covered: before.covered).percent)
8762 }
88}