Cotton code
56 lines · 2.6 KB · SwiftRaw
// The tap map (owner, 28 Oct 2026: a face has one state, view, plus a tap// map): every face tags its parts, so the shell routes a tap on a card by// what it shows — and a face drawn with no shell draws its parts as they are.import CottonFacesimport CottonSpecimensimport SwiftUIimport XCTest@MainActorfinal class FacePartsTests: XCTestCase {    /// The parts a view tagged, read off its preference as it is drawn.    private final class Tagged {        var parts: Set<FacePart> = []    }    private func parts(_ face: Face, _ specimen: Specimen) -> Set<FacePart> {        let tagged = Tagged()        let drawn = SpecimenCard(face, specimen)            .frame(width: 340, height: 451)            .backgroundPreferenceValue(FacePartsKey.self) { regions in                let _ = tagged.parts.formUnion(regions.map(\.part))                Color.clear            }        _ = ImageRenderer(content: drawn).cgImage        return tagged.parts    }    /// RULE: Every registered face tags at least its title or its picture, in one of the specimens it is reviewed in — save a face whose whole page is the deck's to tap, which tags nothing at all.    func testEveryFaceTagsItsTitleOrItsPicture() {        XCTAssertFalse(FaceRegistry.all.isEmpty, "the registry holds no face, so this proved nothing")        for face in FaceRegistry.all {            let tagged = face.specimens.reduce(into: Set<FacePart>()) { $0.formUnion(parts(face, $1)) }            // A face whose whole page is the deck's to tap declares it            // (`tagsNoPart`): a note's torn page, stepped by its side thirds            // and read by its middle (owner, 29 Oct 2026).            if face.tagsNoPart {                XCTAssertEqual(tagged, [], "\(face.id)'s page is the deck's to tap, yet it tags \(tagged.map(\.rawValue).sorted())")                continue            }            XCTAssertFalse(tagged.isDisjoint(with: [.title, .picture]), "\(face.id) tags neither its title nor its picture (tags \(tagged.map(\.rawValue).sorted()))")        }    }    /// RULE: A tagged part draws as it is with no shell installed, and with one the shell is handed that part.    func testTheShellIsHandedThePart() {        let plain = ImageRenderer(content: Text("Rice").facePart(.title)).cgImage        XCTAssertNotNil(plain)        var handed: [FacePart] = []        _ = ImageRenderer(content: Text("Rice").facePart(.title).environment(\.facePartTaps) { part, content in            MainActor.assumeIsolated { handed.append(part) }            return content        }).cgImage        XCTAssertEqual(handed, [.title])    }}