Cotton code
130 lines · 7.6 KB · SwiftRaw
// The 7-inch single and the artist's crate divider: the single's label// carries the song's name, artist and length, its paper is its album's, and// a favourite wears the brass star; the divider stands the newest five// albums' spines, newest at the back, tints each from its cover or the grey,// and says the deck's counts. With `COTTON_FACE_SHOTS=<dir>` set, each is// written there in each state at three text sizes.import CottonFacesimport CottonPaperimport CottonSpecimensimport CottonValuesimport SwiftUIimport XCTest#if canImport(UIKit)    import UIKit#else    import AppKit#endif@MainActorfinal class SongSingleArtistDividerTests: XCTestCase {    // MARK: The single    /// RULE: The label carries the song's name, its artist in capitals and its length, in that order, from the card alone.    func testTheLabelCarriesNameArtistAndLength() {        XCTAssertEqual(SongSingle.label(SongSingleFaceCatalogue.harbour), ["Harbour Lights", "THE LOW TIDE", "4:02"])        XCTAssertEqual(SongSingle.label(DeckCard(face: .songSingle, fields: ["title": .text("Slow Train")])), ["Slow Train"])    }    /// RULE: The sleeve's paper is picked by the album's title, so an album's singles share it; by the song's own name without one; cream with neither; and both papers are worn.    func testThePaperIsTheAlbums() {        let song = { (title: String, album: String?) in            DeckCard(face: .songSingle, fields: ["title": .text(title)].merging(album.map { ["album": .text($0)] } ?? [:]) { $1 })        }        for album in ["Night Ferry", "Common Ground", "Paper Moons", "Blue Hour"] {            XCTAssertEqual(SongSingle.paper(song("Harbour Lights", album)), SongSingle.paper(song("Slow Train", album)), album)        }        XCTAssertEqual(SongSingle.paper(song("Slow Train", nil)), SongSingle.paper(DeckCard(face: .songSingle, fields: ["album": .text("Slow Train")])))        XCTAssertEqual(SongSingle.paper(DeckCard(face: .songSingle, fields: [:])), .cream)        let worn = Set(["Night Ferry", "Common Ground", "Paper Moons", "Blue Hour", "Harbour Lights", "Slow Train"].map { SongSingle.paper(song("", $0)) })        XCTAssertEqual(worn, [.cream, .blue], "the six invented albums wear both papers")        XCTAssertEqual(SongSingle.paper(song("", "Night Ferry")), SongSingle.paper(song("", "Night Ferry")), "the same every time")        for paper in SongSingle.Paper.allCases {            XCTAssertNotNil(CottonMedia.image(paper.painting), paper.painting)        }    }    /// RULE: A favourite wears the brass star — the card's `favourite` yes — and any other card none.    func testTheStarIsTheFavourite() {        XCTAssertTrue(SongSingle.favourite(SongSingleFaceCatalogue.favourite))        XCTAssertFalse(SongSingle.favourite(SongSingleFaceCatalogue.harbour))        XCTAssertFalse(SongSingle.favourite(DeckCard(face: .songSingle, fields: ["favourite": .bool(false)])))        XCTAssertNotEqual(ShellLines.line("faces", "single.favourite"), "faces.single.favourite", "the kit ships faces.single.favourite")    }    // MARK: The divider    /// RULE: The divider stands one spine per album, at most five — the newest five — oldest at the front and newest at the back.    func testTheSpinesAreTheNewestFiveNewestAtTheBack() {        XCTAssertEqual(ArtistDivider.spines(ArtistDividerFaceCatalogue.lowTide).map(\.title), ["Paper Moons", "Common Ground", "Night Ferry"])        let eight = ArtistDivider.spines(ArtistDividerFaceCatalogue.lanterns)        XCTAssertEqual(eight.count, 5)        XCTAssertEqual(eight.map(\.title), ["Night Ferry", "Common Ground", "Paper Moons", "Harbour Lights Live", "The Blue Hour Sessions"])        XCTAssertEqual(ArtistDivider.spines(ArtistDividerFaceCatalogue.maraVance), [ArtistDivider.Album(title: "Slow Train", photo: nil)])        XCTAssertEqual(ArtistDivider.spines(DeckCard(face: .artistDivider, fields: ["title": .text("Mara Vance")])), [])    }    /// RULE: A spine is its cover's colour, darkened no further than the cream title needs; with no cover's colour, the grey stock.    func testASpineIsItsCoversColourElseGrey() {        XCTAssertEqual(ArtistDivider.tint(nil), MusicMaterial.spine.colour)        let dark = Color(red: 0.2, green: 0.1, blue: 0.05)        XCTAssertEqual(ArtistDivider.tint(dark).resolve(in: EnvironmentValues()), dark.resolve(in: EnvironmentValues()), "a dark cover keeps its colour")        let light = ArtistDivider.tint(Color(red: 0.9, green: 0.85, blue: 0.7)).resolve(in: EnvironmentValues())        let lightness = 0.2126 * Double(light.red) + 0.7152 * Double(light.green) + 0.0722 * Double(light.blue)        XCTAssertEqual(lightness, 0.36, accuracy: 0.01, "a light cover is darkened to the lightest the title reads on")        XCTAssertGreaterThan(light.red, light.blue, "and keeps its hue")        let ferry = try? XCTUnwrap(FixturePictures.tint("poster-ferry"))        XCTAssertNotNil(ferry, "the fixtures answer a cover's colour")        XCTAssertNil(FixturePictures.tint("no-such-picture"))    }    /// RULE: The counts on the divider's foot are the deck's counts of the artist's albums and songs, joined as the cards join words; none read, none said.    func testTheCountsAreTheDecks() {        XCTAssertEqual(ArtistDivider.counts(ArtistDividerFaceCatalogue.lowTide), "3 albums" + ShellLines.line("cards", "join") + "31 songs")        XCTAssertEqual(ArtistDivider.counts(ArtistDividerFaceCatalogue.lowTide), "3 albums · 31 songs")        XCTAssertNil(ArtistDivider.counts(DeckCard(face: .artistDivider, fields: ["title": .text("Mara Vance")])))    }    // MARK: Both    /// RULE: The single and the divider are registered, printed on no stock and laid on the music's black tolex.    func testTheyLieOnTheTolex() {        for id in ["song.single", "artist.divider"] {            XCTAssertNotNil(FaceRegistry.all.first { $0.id == id }, id)            XCTAssertEqual(FaceRegistry.ground(id), .tolex, id)            XCTAssertEqual(FaceRegistry.stock(id), .own, id)        }        for name in ["single-record", "single-label-blank", "single-shadow", "crate", "divider", "spine-blank", "divider-shadow"] {            XCTAssertNotNil(CottonMedia.image(name), name)        }    }    /// RULE: Each draws every specimen from its card alone, at every text size.    func testTheyDrawInEveryState() throws {        let out = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"].flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) }        if let out { try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true) }        for face in [SongSingleFaceCatalogue.face, ArtistDividerFaceCatalogue.face] {            for size in [DynamicTypeSize.xSmall, .large, .accessibility5] {                for specimen in face.specimens {                    let renderer = ImageRenderer(content: SpecimenCard(face, specimen)                        .frame(width: 340, height: 451)                        .background(FaceGroundSurface(.tolex))                        .dynamicTypeSize(size))                    renderer.scale = 2                    let image = try XCTUnwrap(renderer.cgImage, "\(face.id) \(specimen.name) drew nothing")                    if let out { try write(image, to: out.appendingPathComponent("\(face.id)-\(specimen.name)-\(size).png")) }                }            }        }    }    private func write(_ image: CGImage, to url: URL) throws {        #if canImport(UIKit)            let data = UIImage(cgImage: image).pngData()        #else            let data = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])        #endif        try XCTUnwrap(data, "\(url.lastPathComponent) was not encoded").write(to: url)    }}