| 1 | | import Foundation |
| 2 | | |
| 3 | | /// How a module's or file's line coverage moved since the baseline run (DESIGN.md D6). |
| 4 | | enum LineChange: 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 | 47 | var text: String { |
| 13 | 47 | switch self { |
| 14 | 47 | case .new: "new" |
| 15 | 47 | case .points: roundedPoints == 0 ? "0.0" : String(format: "%+.1f", roundedPoints) |
| 16 | 47 | } |
| 17 | 47 | } |
| 18 | | |
| 19 | | /// The style the table gives the change: `new`, `up`, `down` or `flat`. |
| 20 | 37 | var styleName: String { |
| 21 | 37 | switch self { |
| 22 | 37 | case .new: "new" |
| 23 | 37 | case .points: roundedPoints > 0 ? "up" : roundedPoints < 0 ? "down" : "flat" |
| 24 | 37 | } |
| 25 | 37 | } |
| 26 | | |
| 27 | 68 | private var roundedPoints: Double { |
| 28 | 68 | guard case .points(let points) = self else { return 0 } |
| 29 | 68 | return (points * 10).rounded() / 10 |
| 30 | 68 | } |
| 31 | | } |
| 32 | | |
| 33 | | /// The current run's modules and files set against an earlier run's `totals.json`. |
| 34 | | struct BaselineComparison { |
| 35 | | private let total: RunTotals.Lines |
| 36 | | private let modules: [String: RunTotals.Lines] |
| 37 | | private let files: [String: RunTotals.Lines] |
| 38 | | |
| 39 | 10 | init(baseline: RunTotals) { |
| 40 | 10 | total = baseline.lines |
| 41 | 10 | modules = Dictionary(uniqueKeysWithValues: baseline.modules.map { ($0.name, $0.lines) }) |
| 42 | 10 | files = Dictionary( |
| 43 | 11 | uniqueKeysWithValues: baseline.modules.flatMap(\.files).map { ($0.path, $0.lines) }) |
| 44 | 10 | } |
| 45 | | |
| 46 | | /// The whole run's line coverage, which the baseline always has, so never `.new`. |
| 47 | 8 | func changeOfTotal(now: LineSummary) -> LineChange { |
| 48 | 8 | Self.change(from: total, to: now) |
| 49 | 8 | } |
| 50 | | |
| 51 | 18 | func change(ofModule name: String, now: LineSummary) -> LineChange { |
| 52 | 18 | Self.change(from: modules[name], to: now) |
| 53 | 18 | } |
| 54 | | |
| 55 | | /// - Parameter path: below the sources folder, as in `FileTotals.relativePath`. |
| 56 | 22 | func change(ofFile path: String, now: LineSummary) -> LineChange { |
| 57 | 22 | Self.change(from: files[path], to: now) |
| 58 | 22 | } |
| 59 | | |
| 60 | 48 | private static func change(from before: RunTotals.Lines?, to now: LineSummary) -> LineChange { |
| 61 | 48 | guard let before else { return .new } |
| 62 | 25 | return .points( |
| 63 | 25 | now.percent - LineSummary(count: before.total, covered: before.covered).percent) |
| 64 | 48 | } |
| 65 | | } |