1import 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.
5public struct TestsPage: Sendable {
6 /// Beside the index, which links it from its headline.
7 public static let sitePath = "tests.html"
8
9 private let title: String
10 private let results: TestResults
11
12 /// - Parameter title: the site's name, shown as given in the page's title.
1312 public init(title: String, results: TestResults) {
1412 self.title = title
1512 self.results = results
1612 }
17
1812 public var html: String {
1912 """
2012 <!doctype html>
2112 <html lang="en">
2212 <head>
2312 <meta charset="utf-8">
2412 <meta name="viewport" content="width=device-width, initial-scale=1">
2512 <title>Tests · \(escapingHTML(title))</title>
2612 <link rel="stylesheet" href="\(SiteStylesheet.fileName)">
2712 </head>
2812 <body class="tests-page">
2912 <header class="page-header">
3012 \(SiteHeader(title: title, currentTab: .tests, hasTestsPage: true, rootPath: "").html)
3112 \(headlineMarkup)
3212 </header>
3312 <main>
3412 \(failuresMarkup)\(suitesMarkup)
3512 </main>
3612 </body>
3712 </html>
3812
3912 """
4012 }
41
4212 private var headlineMarkup: String {
4312 let counts = results.counts
4412 return #"<p class="totals">"#
4512 + "\(counts.totalText): \(groupingDigits(counts.passed)) passed, "
4612 + "\(groupingDigits(counts.failed)) failed, \(groupingDigits(counts.skipped)) skipped</p>"
4712 }
48
49 /// Empty when nothing failed.
5012 private var failuresMarkup: String {
5116 let failures = results.cases.enumerated().compactMap { index, testCase -> String? in
5216 guard case .failed(let message, let details) = testCase.outcome else { return nil }
534 let detailsMarkup = details.isEmpty ? "" : "<pre>\(escapingHTML(details))</pre>"
544 return #"<article class="failure" id="\#(Self.failureID(at: index))"><h3>"#
554 + "\(escapingHTML(testCase.suite)) · \(escapingHTML(testCase.name))</h3>"
564 + "<p>\(escapingHTML(message))</p>\(detailsMarkup)</article>"
5716 }
5812 guard !failures.isEmpty else { return "" }
594 return #"<section class="failures"><h2>Failures</h2>"# + failures.joined() + "</section>\n"
6012 }
61
62 /// One folded block per suite, in the order suites first appear; a suite with a
63 /// failure starts open.
6412 private var suitesMarkup: String {
6512 var order: [String] = []
6612 var casesBySuite: [String: [(index: Int, testCase: TestResults.TestCase)]] = [:]
6716 for (index, testCase) in results.cases.enumerated() {
6816 if casesBySuite[testCase.suite] == nil {
6912 order.append(testCase.suite)
7012 }
7116 casesBySuite[testCase.suite, default: []].append((index, testCase))
7216 }
7312 return order.map { suite in
7412 let cases = casesBySuite[suite] ?? []
7516 let hasFailure = cases.contains {
7616 if case .failed = $0.testCase.outcome { true } else { false }
7716 }
7812 return #"<details class="suite""# + (hasFailure ? " open" : "")
7912 + "><summary>\(escapingHTML(suite)) <span class=\"count\">\(cases.count)</span>"
8016 + "</summary><ul>" + cases.map(Self.caseMarkup).joined() + "</ul></details>"
8112 }.joined(separator: "\n")
8212 }
83
84 /// Names a failure by its position among all cases, so the list entry can link to it.
858 private static func failureID(at index: Int) -> String {
868 "failure-\(index)"
878 }
88
8916 private static func caseMarkup(index: Int, testCase: TestResults.TestCase) -> String {
9016 let name = escapingHTML(testCase.name)
9116 switch testCase.outcome {
9216 case .passed:
9310 return #"<li class="passed">\#(name)\#(timeMarkup(testCase.seconds))</li>"#
9416 case .failed:
954 let link = ##"<a href="#\##(failureID(at: index))">\##(name)</a>"##
964 return #"<li class="failed">\#(link)\#(timeMarkup(testCase.seconds))</li>"#
9716 case .skipped(let message):
982 let reason =
992 message.isEmpty ? "" : #" <span class="reason">\#(escapingHTML(message))</span>"#
1002 return #"<li class="skipped">\#(name)\#(reason)</li>"#
10116 }
10216 }
103
104 /// Under a second in whole milliseconds, longer in seconds with two decimals.
10514 private static func timeMarkup(_ seconds: Double?) -> String {
10614 guard let seconds else { return "" }
10711 let text =
10811 seconds < 1
10911 ? "\(Int((seconds * millisecondsPerSecond).rounded())) ms"
11011 : String(format: "%.2f s", seconds)
11111 return #" <span class="time">\#(text)</span>"#
11214 }
113
114 private static let millisecondsPerSecond = 1000.0
115}