oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1@testable import BaseKit
2import SwiftUI
3import XCTest
4
5/// P7: the apps' palette is the web's. Reads internal/look/static/app.css
6/// (`:root` holds every token's dark value; `:root[data-theme="light"]`
7/// the light values over it) and fails on any token Palette mirrors that
8/// differs, or any colour token it doesn't mirror.
9final class PaletteTests: XCTestCase {
10 static let stylesheet = URL(fileURLWithPath: #filePath)
11 .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
12 .deletingLastPathComponent().deletingLastPathComponent()
13 .appendingPathComponent("internal/look/static/app.css")
14
15 struct Sheet {
16 var dark: [String: String] = [:]
17 var light: [String: String] = [:]
18 }
19
20 static func read() throws -> Sheet {
21 let css = try String(contentsOf: stylesheet, encoding: .utf8)
22 func declarations(_ selector: String) throws -> [String: String] {
23 guard let open = css.range(of: selector + " {"), let close = css[open.upperBound...].firstIndex(of: "}") else {
24 throw XCTSkip("no \(selector) block in app.css")
25 }
26 var body = String(css[open.upperBound..<close])
27 body = body.replacingOccurrences(of: #"/\*[\s\S]*?\*/"#, with: "", options: .regularExpression)
28 var out: [String: String] = [:]
29 let regex = try NSRegularExpression(pattern: #"--([a-z0-9-]+)\s*:\s*([^;]+);"#)
30 for match in regex.matches(in: body, range: NSRange(body.startIndex..., in: body)) {
31 out[String(body[Range(match.range(at: 1), in: body)!])] = String(body[Range(match.range(at: 2), in: body)!]).trimmingCharacters(in: .whitespaces)
32 }
33 return out
34 }
35 var sheet = Sheet()
36 sheet.dark = try declarations(":root")
37 sheet.light = sheet.dark.merging(try declarations(#":root[data-theme="light"]"#)) { $1 }
38 return sheet
39 }
40
41 static func parse(_ value: String) -> RGBA? {
42 let v = value.trimmingCharacters(in: .whitespaces)
43 if v.hasPrefix("#"), v.count == 7, let hex = Int(v.dropFirst(), radix: 16) {
44 return RGBA(hex >> 16 & 0xFF, hex >> 8 & 0xFF, hex & 0xFF, 1)
45 }
46 if v.hasPrefix("rgb"), let open = v.firstIndex(of: "("), let close = v.lastIndex(of: ")") {
47 let parts = v[v.index(after: open)..<close].split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
48 guard parts.count >= 3, let r = Int(parts[0]), let g = Int(parts[1]), let b = Int(parts[2]) else { return nil }
49 let a = parts.count == 4 ? Double(parts[3]) ?? -1 : 1
50 return RGBA(r, g, b, a)
51 }
52 return nil
53 }
54
55 func testEveryMirroredTokenMatchesTheStylesheet() throws {
56 let sheet = try Self.read()
57 XCTAssertGreaterThan(sheet.dark.count, 90, "app.css's :root looks too small to be the real one")
58 for token in Palette.Token.allCases {
59 for (scheme, values) in [(ColorScheme.dark, sheet.dark), (.light, sheet.light)] {
60 guard let raw = values[token.rawValue] else { XCTFail("--\(token.rawValue) is not in app.css"); continue }
61 XCTAssertEqual(Palette(scheme).rgba(token), Self.parse(raw), "--\(token.rawValue) (\(scheme)) is \(raw) on the web")
62 }
63 }
64 }
65
66 func testEveryColourTokenIsMirrored() throws {
67 let sheet = try Self.read()
68 let mirrored = Set(Palette.Token.allCases.map(\.rawValue))
69 let colours = sheet.dark.filter { Self.parse($0.value) != nil }.map(\.key)
70 XCTAssertEqual(Set(colours).subtracting(mirrored), [], "colour tokens the apps don't mirror")
71 }
72
73 func testTheNumbersMatchTheStylesheet() throws {
74 let sheet = try Self.read()
75 for (scheme, values) in [(ColorScheme.dark, sheet.dark), (.light, sheet.light)] {
76 let p = Palette(scheme)
77 XCTAssertEqual(p.skyStop * 100, Double(values["sky-stop"]!.dropLast())!, accuracy: 0.001)
78 XCTAssertEqual(p.fogOpacity, (1...4).map { Double(values["fog-\($0)-a"]!)! })
79 XCTAssertEqual(p.stateWash, Double(values["st-wash"]!)!)
80 XCTAssertEqual(p.glassLift, Double(values["glass-lift"]!.dropLast(2))!)
81 }
82 }
83}
84