Cotton code
73 lines · 3.8 KB · SwiftRaw
#if canImport(UIKit)import CottonValuesimport ImageIOimport SwiftUIimport UniformTypeIdentifiersimport XCTest@testable import CottonUI/// A capture utility, not a guard: with `EVENT_NOTE_OUT=<dir>` set (through/// xcodebuild, `TEST_RUNNER_EVENT_NOTE_OUT`) it draws the event face on the/// numbers of the house references — each on its stock, a 360 x 450 card at/// scale 3 — into `<dir>`, so they are laid beside the references. Each card/// carries only what the core's card block sends for an event (title, date,/// place, people, notes); every/// name is invented and the people's pictures are the gallery's own. Without/// the out variable it skips.@MainActorfinal class EventNoteDesignShotsTests: XCTestCase {    private static func person(_ name: String, _ picture: String?) -> RecordValue {        var entry: [String: RecordValue] = ["name": .text(name)]        if let picture { entry["picture"] = .text(picture) }        return .object(entry)    }    func testDrawTheEventAndNoteDesigns() throws {        guard let dir = ProcessInfo.processInfo.environment["EVENT_NOTE_OUT"], !dir.isEmpty else {            throw XCTSkip("set EVENT_NOTE_OUT=<dir> to draw the event and note designs")        }        let out = URL(fileURLWithPath: dir, isDirectory: true)        try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)        let events: [(String, DeckCard)] = [            ("event-dentist", DeckCard(face: .event, corner: "in 6 days", fields: [                "title": .text("Dentist"), "date": .text("Thu 29 Oct \u{00B7} 4:30 pm"), "ends": .text("5:15 pm"), "place": .text("Indiranagar"),                "people": .list([Self.person("Meera Iyer", "guest-2")]),            ])),            ("event-coorg", DeckCard(face: .event, fields: [                "title": .text("Coorg weekend"), "date": .text("Sat 7 Nov"), "ends": .text("Sun 8 Nov"), "place": .text("Madikeri"),                "people": .list([Self.person("Priya Example", "guest-0"), Self.person("Arjun Rao", "guest-1"), Self.person("Meera Iyer", "guest-2")]),            ])),            ("event-rent", DeckCard(face: .event, fields: [                "title": .text("Pay rent"), "date": .text("Mon 2 Nov"),            ])),            ("event-dinner", DeckCard(face: .event, corner: "Today", fields: [                "title": .text("Dinner at Toit"), "date": .text("Fri 23 Oct \u{00B7} 7:30 pm"), "place": .text("Indiranagar"),                "people": .list([Self.person("Arjun Rao", "guest-1"), Self.person("Meera Iyer", "guest-2")]),            ])),            ("event-yoga", DeckCard(face: .event, fields: [                "title": .text("Yoga"), "date": .text("Tue 27 Oct \u{00B7} 7:00 am"), "repeats": .text("Every Tuesday and Thursday"),                "place": .text("Cubbon Park"),            ])),        ]        for (name, card) in events { shoot(name, card, EventFace(card: card), to: out) }    }    private func shoot(_ name: String, _ card: DeckCard, _ face: some View, to out: URL) {        let theme = Theme.paper        let renderer = ImageRenderer(content: face            .environment(\.theme, theme)            .environment(\.colorScheme, .light)            .environment(\.facePictureDrawer, DeckFace.pictures)            .environment(\.facePictures) { key in FixturePictures.image(key) }            .frame(width: 360, height: 450)            .background(theme.colors.paper.color))        renderer.scale = 3        guard let image = renderer.cgImage,              let dest = CGImageDestinationCreateWithURL(out.appendingPathComponent("\(name).png") as CFURL, UTType.png.identifier as CFString, 1, nil)        else { return XCTFail("\(name) drew nothing") }        CGImageDestinationAddImage(dest, image, nil)        XCTAssertTrue(CGImageDestinationFinalize(dest), "\(name) was not written")    }}#endif