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