| 1 | | import Foundation |
| 2 | | |
| 3 | | /// How a coverage percentage moved since the baseline run (DESIGN.md D6). |
| 4 | | enum 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`. |
| 12 | 72 | var text: String { |
| 13 | 72 | switch self { |
| 14 | 72 | case .new: "new" |
| 15 | 72 | case .points: roundedPoints == 0 ? "0.0" : String(format: "%+.1f", roundedPoints) |
| 16 | 72 | } |
| 17 | 72 | } |
| 18 | | |
| 19 | | /// The style the table gives the change: `new`, `up`, `down` or `flat`. |
| 20 | 62 | var styleName: String { |
| 21 | 62 | switch self { |
| 22 | 62 | case .new: "new" |
| 23 | 62 | case .points: roundedPoints > 0 ? "up" : roundedPoints < 0 ? "down" : "flat" |
| 24 | 62 | } |
| 25 | 62 | } |
| 26 | | |
| 27 | 132 | private var roundedPoints: Double { |
| 28 | 132 | guard case .points(let points) = self else { return 0 } |
| 29 | 132 | return (points * 10).rounded() / 10 |
| 30 | 132 | } |
| 31 | | } |
| 32 | | |
| 33 | | /// The current run's modules and files set against an earlier run's `totals.json`. |
| 34 | | struct 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 | | |
| 40 | 13 | init(baseline: RunTotals) { |
| 41 | 13 | self.baseline = baseline |
| 42 | 13 | total = baseline.lines |
| 43 | 13 | modules = Dictionary(uniqueKeysWithValues: baseline.modules.map { ($0.name, $0.lines) }) |
| 44 | 13 | files = Dictionary( |
| 45 | 14 | uniqueKeysWithValues: baseline.modules.flatMap(\.files).map { ($0.path, $0.lines) }) |
| 46 | 13 | } |
| 47 | | |
| 48 | | /// The whole run's line coverage, which the baseline always has, so never `.new`. |
| 49 | 10 | func changeOfTotal(now: LineSummary) -> CoverageChange { |
| 50 | 10 | Self.change(from: total, to: now) |
| 51 | 10 | } |
| 52 | | |
| 53 | 21 | func change(ofModule name: String, now: LineSummary) -> CoverageChange { |
| 54 | 21 | Self.change(from: modules[name], to: now) |
| 55 | 21 | } |
| 56 | | |
| 57 | | /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`. |
| 58 | 27 | func change(ofFile path: String, now: LineSummary) -> CoverageChange { |
| 59 | 27 | Self.change(from: files[path], to: now) |
| 60 | 27 | } |
| 61 | | |
| 62 | | /// The change in the number of uncovered lines across the run. |
| 63 | 8 | func missedLineChange(now: LineSummary) -> Int { |
| 64 | 8 | (now.count - now.covered) - (total.total - total.covered) |
| 65 | 8 | } |
| 66 | | |
| 67 | | /// Files whose relative paths were absent from the baseline. |
| 68 | 8 | func newFileCount(in current: [FileTotals]) -> Int { |
| 69 | 25 | current.filter { files[$0.relativePath] == nil }.count |
| 70 | 8 | } |
| 71 | | |
| 72 | 2 | func branchChange(now: CoverageCount) -> CoverageChange { |
| 73 | 2 | Self.change( |
| 74 | 2 | from: baseline.branches, to: LineSummary(count: now.total, covered: now.covered)) |
| 75 | 2 | } |
| 76 | | |
| 77 | 2 | func conditionChange(now: CoverageCount) -> CoverageChange { |
| 78 | 2 | Self.change( |
| 79 | 2 | from: baseline.conditions, to: LineSummary(count: now.total, covered: now.covered)) |
| 80 | 2 | } |
| 81 | | |
| 82 | | private static func change(from before: RunTotals.Lines?, to now: LineSummary) -> CoverageChange |
| 83 | 62 | { |
| 84 | 62 | guard let before else { return .new } |
| 85 | 34 | return .points( |
| 86 | 34 | now.percent - LineSummary(count: before.total, covered: before.covered).percent) |
| 87 | 62 | } |
| 88 | | } |