| 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 | 168 | var text: String { |
| 13 | 168 | switch self { |
| 14 | 168 | case .new: "new" |
| 15 | 168 | case .points: roundedPoints == 0 ? "0.0" : String(format: "%+.1f", roundedPoints) |
| 16 | 168 | } |
| 17 | 168 | } |
| 18 | | |
| 19 | | /// The style the table gives the change: `new`, `up`, `down` or `flat`. |
| 20 | 146 | var styleName: String { |
| 21 | 146 | switch self { |
| 22 | 146 | case .new: "new" |
| 23 | 146 | case .points: roundedPoints > 0 ? "up" : roundedPoints < 0 ? "down" : "flat" |
| 24 | 146 | } |
| 25 | 146 | } |
| 26 | | |
| 27 | 347 | private var roundedPoints: Double { |
| 28 | 347 | guard case .points(let points) = self else { return 0 } |
| 29 | 347 | return (points * 10).rounded() / 10 |
| 30 | 347 | } |
| 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 | | private let moduleRegions: [String: RunTotals.Lines] |
| 40 | | private let fileRegions: [String: RunTotals.Lines] |
| 41 | | |
| 42 | 26 | init(baseline: RunTotals) { |
| 43 | 26 | self.baseline = baseline |
| 44 | 26 | total = baseline.lines |
| 45 | 26 | modules = Dictionary(uniqueKeysWithValues: baseline.modules.map { ($0.name, $0.lines) }) |
| 46 | 26 | files = Dictionary( |
| 47 | 27 | uniqueKeysWithValues: baseline.modules.flatMap(\.files).map { ($0.path, $0.lines) }) |
| 48 | 26 | moduleRegions = Dictionary( |
| 49 | 26 | uniqueKeysWithValues: baseline.modules.compactMap { module in |
| 50 | 26 | module.regions.map { (module.name, $0) } |
| 51 | 26 | }) |
| 52 | 26 | fileRegions = Dictionary( |
| 53 | 27 | uniqueKeysWithValues: baseline.modules.flatMap(\.files).compactMap { file in |
| 54 | 27 | file.regions.map { (file.path, $0) } |
| 55 | 27 | }) |
| 56 | 26 | } |
| 57 | | |
| 58 | | /// The whole run's line coverage, which the baseline always has, so never `.new`. |
| 59 | 23 | func changeOfTotal(now: LineSummary) -> CoverageChange { |
| 60 | 23 | Self.change(from: total, to: now) |
| 61 | 23 | } |
| 62 | | |
| 63 | 34 | func change(ofModule name: String, now: LineSummary) -> CoverageChange { |
| 64 | 34 | Self.change(from: modules[name], to: now) |
| 65 | 34 | } |
| 66 | | |
| 67 | | /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`. |
| 68 | 38 | func change(ofFile path: String, now: LineSummary) -> CoverageChange { |
| 69 | 38 | Self.change(from: files[path], to: now) |
| 70 | 38 | } |
| 71 | | |
| 72 | | /// The change in the number of uncovered lines across the run. |
| 73 | 18 | func missedLineChange(now: LineSummary) -> Int { |
| 74 | 18 | (now.count - now.covered) - (total.total - total.covered) |
| 75 | 18 | } |
| 76 | | |
| 77 | | /// The change in the number of uncovered regions across the run; nil when the |
| 78 | | /// baseline recorded none. |
| 79 | 10 | func missedRegionChange(now: CoverageCount) -> Int? { |
| 80 | 10 | baseline.regions.map { before in |
| 81 | 6 | (now.total - now.covered) - (before.total - before.covered) |
| 82 | 6 | } |
| 83 | 10 | } |
| 84 | | |
| 85 | | /// Files whose relative paths were absent from the baseline. |
| 86 | 18 | func newFileCount(in current: [FileTotals]) -> Int { |
| 87 | 36 | current.filter { files[$0.relativePath] == nil }.count |
| 88 | 18 | } |
| 89 | | |
| 90 | 13 | func regionChange(now: CoverageCount) -> CoverageChange { |
| 91 | 13 | Self.change( |
| 92 | 13 | from: baseline.regions, to: LineSummary(count: now.total, covered: now.covered)) |
| 93 | 13 | } |
| 94 | | |
| 95 | 13 | func regionChange(ofModule name: String, now: CoverageCount) -> CoverageChange { |
| 96 | 13 | Self.change( |
| 97 | 13 | from: moduleRegions[name], to: LineSummary(count: now.total, covered: now.covered)) |
| 98 | 13 | } |
| 99 | | |
| 100 | | /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`. |
| 101 | 10 | func regionChange(ofFile path: String, now: CoverageCount) -> CoverageChange { |
| 102 | 10 | Self.change( |
| 103 | 10 | from: fileRegions[path], to: LineSummary(count: now.total, covered: now.covered)) |
| 104 | 10 | } |
| 105 | | |
| 106 | 2 | func branchChange(now: CoverageCount) -> CoverageChange { |
| 107 | 2 | Self.change( |
| 108 | 2 | from: baseline.branches, to: LineSummary(count: now.total, covered: now.covered)) |
| 109 | 2 | } |
| 110 | | |
| 111 | 2 | func conditionChange(now: CoverageCount) -> CoverageChange { |
| 112 | 2 | Self.change( |
| 113 | 2 | from: baseline.conditions, to: LineSummary(count: now.total, covered: now.covered)) |
| 114 | 2 | } |
| 115 | | |
| 116 | | private static func change(from before: RunTotals.Lines?, to now: LineSummary) -> CoverageChange |
| 117 | 135 | { |
| 118 | 135 | guard let before else { return .new } |
| 119 | 92 | return .points( |
| 120 | 92 | now.percent - LineSummary(count: before.total, covered: before.covered).percent) |
| 121 | 135 | } |
| 122 | | } |