| 1 | | import Foundation |
| 2 | | |
| 3 | | /// The run's test results: failures first, open, with their messages, then every |
| 4 | | /// suite folded, listing its cases. Readable with scripts off; it has none. |
| 5 | | public struct TestsPage: Sendable { |
| 6 | | /// Beside the index, which links it from its headline. |
| 7 | | public static let sitePath = "tests.html" |
| 8 | | |
| 9 | | private let run: ReportHeader |
| 10 | | private let title: String |
| 11 | | private let results: TestResults |
| 12 | | |
| 13 | | /// - Parameters: |
| 14 | | /// - title: the site's name, shown as given in the page's title. |
| 15 | | /// - run: shared commit, subject and optional History destination. |
| 16 | 14 | public init(title: String, results: TestResults, run: ReportHeader = ReportHeader()) { |
| 17 | 14 | self.run = run |
| 18 | 14 | self.title = title |
| 19 | 14 | self.results = results |
| 20 | 14 | } |
| 21 | | |
| 22 | 14 | public var html: String { |
| 23 | 14 | """ |
| 24 | 14 | <!doctype html> |
| 25 | 14 | <html lang="en"> |
| 26 | 14 | <head> |
| 27 | 14 | <meta charset="utf-8"> |
| 28 | 14 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 29 | 14 | <title>Tests · \(escapingHTML(title))</title> |
| 30 | 14 | <link rel="stylesheet" href="\(SiteStylesheet.fileName)"> |
| 31 | 14 | </head> |
| 32 | 14 | <body class="tests-page"> |
| 33 | 14 | <header class="page-header"> |
| 34 | 14 | \(SiteHeader(title: title, currentTab: .tests, hasTestsPage: true, rootPath: "", run: run).html) |
| 35 | 14 | \(headlineMarkup) |
| 36 | 14 | </header> |
| 37 | 14 | <main> |
| 38 | 14 | \(failuresMarkup)\(suitesMarkup) |
| 39 | 14 | </main> |
| 40 | 14 | </body> |
| 41 | 14 | </html> |
| 42 | 14 | |
| 43 | 14 | """ |
| 44 | 14 | } |
| 45 | | |
| 46 | 14 | private var headlineMarkup: String { |
| 47 | 14 | let counts = results.counts |
| 48 | 14 | return #"<p class="totals">"# |
| 49 | 14 | + "\(counts.totalText): \(groupingDigits(counts.passed)) passed, " |
| 50 | 14 | + "\(groupingDigits(counts.failed)) failed, \(groupingDigits(counts.skipped)) skipped</p>" |
| 51 | 14 | } |
| 52 | | |
| 53 | | /// Empty when nothing failed. |
| 54 | 14 | private var failuresMarkup: String { |
| 55 | 18 | let failures = results.cases.enumerated().compactMap { index, testCase -> String? in |
| 56 | 18 | guard case .failed(let message, let details) = testCase.outcome else { return nil } |
| 57 | 5 | let detailsMarkup = details.isEmpty ? "" : "<pre>\(escapingHTML(details))</pre>" |
| 58 | 5 | return #"<article class="failure" id="\#(Self.failureID(at: index))"><h3>"# |
| 59 | 5 | + "\(escapingHTML(testCase.suite)) · \(escapingHTML(testCase.name))</h3>" |
| 60 | 5 | + "<p>\(escapingHTML(message))</p>\(detailsMarkup)</article>" |
| 61 | 18 | } |
| 62 | 14 | guard !failures.isEmpty else { return "" } |
| 63 | 5 | return #"<section class="failures"><h2>Failures</h2>"# + failures.joined() + "</section>\n" |
| 64 | 14 | } |
| 65 | | |
| 66 | | /// One folded block per suite, in the order suites first appear; a suite with a |
| 67 | | /// failure starts open. |
| 68 | 14 | private var suitesMarkup: String { |
| 69 | 14 | var order: [String] = [] |
| 70 | 14 | var casesBySuite: [String: [(index: Int, testCase: TestResults.TestCase)]] = [:] |
| 71 | 18 | for (index, testCase) in results.cases.enumerated() { |
| 72 | 18 | if casesBySuite[testCase.suite] == nil { |
| 73 | 13 | order.append(testCase.suite) |
| 74 | 13 | } |
| 75 | 18 | casesBySuite[testCase.suite, default: []].append((index, testCase)) |
| 76 | 18 | } |
| 77 | 14 | return order.map { suite in |
| 78 | 13 | let cases = casesBySuite[suite] ?? [] |
| 79 | 18 | let hasFailure = cases.contains { |
| 80 | 18 | if case .failed = $0.testCase.outcome { true } else { false } |
| 81 | 18 | } |
| 82 | 13 | return #"<details class="suite""# + (hasFailure ? " open" : "") |
| 83 | 13 | + "><summary>\(escapingHTML(suite)) <span class=\"count\">\(cases.count)</span>" |
| 84 | 18 | + "</summary><ul>" + cases.map(Self.caseMarkup).joined() + "</ul></details>" |
| 85 | 13 | }.joined(separator: "\n") |
| 86 | 14 | } |
| 87 | | |
| 88 | | /// Names a failure by its position among all cases, so the list entry can link to it. |
| 89 | 10 | private static func failureID(at index: Int) -> String { |
| 90 | 10 | "failure-\(index)" |
| 91 | 10 | } |
| 92 | | |
| 93 | 18 | private static func caseMarkup(index: Int, testCase: TestResults.TestCase) -> String { |
| 94 | 18 | let name = escapingHTML(testCase.name) |
| 95 | 18 | switch testCase.outcome { |
| 96 | 18 | case .passed: |
| 97 | 11 | return #"<li class="passed">\#(name)\#(timeMarkup(testCase.seconds))</li>"# |
| 98 | 18 | case .failed: |
| 99 | 5 | let link = ##"<a href="#\##(failureID(at: index))">\##(name)</a>"## |
| 100 | 5 | return #"<li class="failed">\#(link)\#(timeMarkup(testCase.seconds))</li>"# |
| 101 | 18 | case .skipped(let message): |
| 102 | 2 | let reason = |
| 103 | 2 | message.isEmpty ? "" : #" <span class="reason">\#(escapingHTML(message))</span>"# |
| 104 | 2 | return #"<li class="skipped">\#(name)\#(reason)</li>"# |
| 105 | 18 | } |
| 106 | 18 | } |
| 107 | | |
| 108 | | /// Under a second in whole milliseconds, longer in seconds with two decimals. |
| 109 | 16 | private static func timeMarkup(_ seconds: Double?) -> String { |
| 110 | 16 | guard let seconds else { return "" } |
| 111 | 11 | let text = |
| 112 | 11 | seconds < 1 |
| 113 | 11 | ? "\(Int((seconds * millisecondsPerSecond).rounded())) ms" |
| 114 | 11 | : String(format: "%.2f s", seconds) |
| 115 | 11 | return #" <span class="time">\#(text)</span>"# |
| 116 | 16 | } |
| 117 | | |
| 118 | | private static let millisecondsPerSecond = 1000.0 |
| 119 | | } |