Cotton code
83 lines · 4.2 KB · SwiftRaw
// The album board: up to five of the album's prints in the board's five// places, the cover first; fewer leave the cork bare; the label types the// album's name and under it its count and its days; the name, the prints,// the count and the days are tagged; the board lies on the Photos tool's// bookcloth and is stepped by a cut.@testable import CottonFacesimport CottonPaperimport CottonValuesimport SwiftUIimport XCTest@MainActorfinal class AlbumBoardTests: XCTestCase {    private typealias Catalogue = AlbumBoardFaceCatalogue    /// RULE: The board shows the cover first, then the album's other prints as it holds them, each once, five at most.    func testTheCoverFirstAndFiveAtMost() {        XCTAssertEqual(BoardPrints.shown(Catalogue.weekend), ["kelmoor-lake", "kelmoor-jetty", "portrait", "verandah", "waterfall"])        XCTAssertEqual(BoardPrints.shown(Catalogue.two), ["verandah", "verandah-wide"])        var unread = Catalogue.weekend        unread.prints = nil        XCTAssertEqual(BoardPrints.shown(unread), ["kelmoor-lake"], "while the prints are read the cover stands alone")        XCTAssertEqual(BoardPlace.all.count, 5)        XCTAssertEqual(BoardPlace.stacking.sorted(), [0, 1, 2, 3, 4])        XCTAssertEqual(BoardPlace.stacking.last, 0, "the cover lies on top")        XCTAssertEqual(BoardPlace.all.map(\.width).max(), BoardPlace.all[0].width, "the cover is the largest")    }    /// RULE: Every place's pin stands clear of the prints laid over it, so the board shows all five pins, a bare place's in the cork.    func testEveryPinStandsClear() {        let board = CGSize(width: 330, height: 330 / AlbumBoardFaceKnobs.aspect)        for (at, index) in BoardPlace.stacking.enumerated() {            let pin = BoardPlace.all[index].pin(on: board)            XCTAssertTrue(BoardPlace.all[index].covers(pin, on: board), "place \(index)'s pin is off its print")            for over in BoardPlace.stacking[(at + 1)...] {                XCTAssertFalse(BoardPlace.all[over].covers(pin, on: board), "place \(index)'s pin is under place \(over)'s print")            }            XCTAssertTrue((0...board.width).contains(pin.x) && (0...board.height).contains(pin.y))        }    }    /// RULE: The label types the album's count in the shell's count line and its days, joined as facts are.    func testTheLabelSaysTheCountAndTheDays() {        XCTAssertEqual(BoardPrints.facts(Catalogue.weekend), "23 photos \u{00B7} Sun 26 Oct")        XCTAssertEqual(BoardPrints.facts(Catalogue.longName), "41 photos \u{00B7} Fri 2 Oct \u{2013} Sun 11 Oct")        var one = Catalogue.two        one.fields["count"] = .text("1")        one.fields["span"] = nil        XCTAssertEqual(BoardPrints.facts(one), "1 photo")        one.fields["count"] = nil        XCTAssertNil(BoardPrints.facts(one))    }    /// RULE: The board tags the album's name, its prints, its count and its days.    func testTheBoardTagsItsParts() {        XCTAssertEqual(tagged(Catalogue.weekend), [.title, .picture, .corner, .when])    }    private func tagged(_ card: DeckCard) -> Set<FacePart> {        final class Tagged { var parts: Set<FacePart> = [] }        let tagged = Tagged()        let drawn = AlbumBoardFace(card: card)            .frame(width: 340, height: 425)            .backgroundPreferenceValue(FacePartsKey.self) { regions in                let _ = tagged.parts.formUnion(regions.map(\.part))                Color.clear            }        XCTAssertNotNil(ImageRenderer(content: drawn).cgImage)        return tagged.parts    }    /// RULE: The album board lies bare on the Photos tool's bookcloth, draws its own sheet, and is stepped by a cut; its board, prints, pins and label are painted.    func testTheBoardLiesOnTheBookclothAndCuts() {        XCTAssertEqual(FaceRegistry.ground("album.board"), .bookcloth)        XCTAssertEqual(FaceRegistry.stock("album.board"), .own)        XCTAssertEqual(Catalogue.face.steps, .cut)        XCTAssertEqual(Catalogue.face.specimens.map(\.name), ["weekend", "two", "long-name"])        for name in ["album-board", "album-board-shadow", "album-instant", "album-pin", "album-label"] {            XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue")        }    }}