Cotton code
133 lines · 8.9 KB · SwiftRaw
// The season's card: the tab is the film's season line from the number; the// title is the season's own unless it only repeats the tab, then the series';// the facts are the year and the episodes as the core said them; the watched// line and the strip count the episodes watched of its episodes; every// episode watched wears the stub, with no day; a posterless card shows its// number on the dark; the parts tagged; every word a line or the card's; the// card lies on the velvet and cuts.@testable import CottonFacesimport CottonPaperimport CottonSpecimensimport CottonValuesimport SwiftUIimport XCTest/// The parts a drawn face tagged.private final class SeasonPartsTagged {    var parts: Set<FacePart> = []}@MainActorfinal class SeasonCardTests: XCTestCase {    private typealias Catalogue = SeasonCardFaceCatalogue    private typealias Knobs = SeasonCardFaceKnobs    private let join = ShellLines.line("cards", "join")    /// RULE: The tab is the film's season line from the card's number, in capitals; the title is the season's own, set in Caslon at its own size when it fits; a title that only repeats the tab gives way to the series' title; no number, no tab.    func testTheTabAndTheTitle() {        XCTAssertEqual(SeasonCardSetting.tab(Catalogue.poster), ShellLines.line("cards", "film.season", ["n": "2"]).uppercased())        XCTAssertEqual(SeasonCardSetting.tab(Catalogue.poster), "SEASON 2")        XCTAssertEqual(SeasonCardSetting.title(Catalogue.poster), "The Winter Crossings")        XCTAssertEqual(SeasonCardSetting.title(Catalogue.noPoster), "The Fog Bell", "Season 1 repeats the tab: the series' title")        XCTAssertEqual(SeasonCardSetting.titleLines(Catalogue.poster).size, Knobs.titleSize)        XCTAssertEqual(SeasonCardSetting.titleLines(Catalogue.poster).lines, ["The Winter Crossings"])        XCTAssertNil(SeasonCardSetting.tab(Catalogue.season([:])))        XCTAssertNil(SeasonCardSetting.title(Catalogue.season([:])))        XCTAssertEqual(SeasonCardSetting.title(Catalogue.season(["title": .text("Season 1"), "number": .text("1")])), "Season 1",                       "no series kept: the season's own, though it repeats the tab")        XCTAssertEqual(SeasonCardSetting.numeral(Catalogue.noPoster), "1")    }    /// RULE: The longest title takes two lines inside its room before it is cut; one too long even at the least size is cut with an ellipsis.    func testTheTitleTakesTwoLinesThenCuts() {        let (lines, size) = SeasonCardSetting.titleLines(Catalogue.longestTitle)        XCTAssertEqual(lines.count, 2)        XCTAssertGreaterThanOrEqual(size, Knobs.titleLeast)        XCTAssertLessThanOrEqual(CGFloat(lines.count) * size * Knobs.titleStep, Knobs.title.height + 0.5)        for line in lines {            XCTAssertLessThanOrEqual(HighlightPassageSetting.wide(line, FilmPosterSetting.caslon(size)), Knobs.title.width + 0.5)        }        XCTAssertEqual(lines.last?.last, "\u{2026}", "the longest specimen is cut")    }    /// RULE: The facts are the year and the episodes as the core said them, joined as the cards join words; the watched line and the strip count the episodes watched of its episodes once any is watched; every episode watched wears the stub stamped in the film's line with no day.    func testTheEpisodesWatched() throws {        XCTAssertEqual(SeasonCardSetting.facts(Catalogue.poster), "2021" + join + "8 episodes")        XCTAssertEqual(SeasonCardSetting.facts(Catalogue.longestTitle), "104 episodes", "no year: the episodes alone")        XCTAssertNil(SeasonCardSetting.facts(Catalogue.season([:])))        XCTAssertNil(SeasonCardSetting.watched(Catalogue.poster), "none watched: no line")        XCTAssertNil(SeasonCardSetting.share(Catalogue.poster))        XCTAssertNil(SeasonCardSetting.stub(Catalogue.poster))        XCTAssertEqual(SeasonCardSetting.watched(Catalogue.partWatched), ShellLines.line("cards", "film.season_watched", ["n": "3", "of": "8"]))        XCTAssertEqual(SeasonCardSetting.watched(Catalogue.partWatched), "3 of 8 watched")        XCTAssertEqual(try XCTUnwrap(SeasonCardSetting.share(Catalogue.partWatched)), 0.375, accuracy: 0.0001)        XCTAssertNil(SeasonCardSetting.stub(Catalogue.partWatched))        XCTAssertEqual(SeasonCardSetting.share(Catalogue.watched), 1)        XCTAssertEqual(SeasonCardSetting.stub(Catalogue.watched), ShellLines.line("cards", "film.stub").uppercased())        XCTAssertNil(SeasonCardSetting.stub(Catalogue.season(["subtitle": .text("0 episodes"), "limit": .number(0), "used": .number(0)])),                     "a season of no episodes is never watched through")        XCTAssertEqual(SeasonCardSetting.share(Catalogue.season(["limit": .number(4), "used": .number(9)])), 1, "never past full")    }    /// RULE: Every word the season's card says is a line or the card's, never a literal typed or said.    func testItsWordsAreLines() throws {        for key in ["film.season", "film.season_watched", "film.stub", "join"] {            XCTAssertNotEqual(ShellLines.line("cards", key), "cards.\(key)", "the kit ships cards.\(key)")        }        let faces = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()            .appendingPathComponent("Sources/CottonFaces/Cotton")        let source = try String(contentsOf: faces.appendingPathComponent("SeasonCardFace.swift"), encoding: .utf8)        let face = source.components(separatedBy: "public enum SeasonCardFaceCatalogue")[0]        XCTAssertNil(face.range(of: #"Text\(""#, options: .regularExpression), "the face types no literal")        XCTAssertNil(face.range(of: #"accessibilityLabel\(""#, options: .regularExpression), "the face says no literal")    }    /// RULE: The face tags the poster's window as its picture, the title as its title and the facts as its body; a season with an episode watched its strip as its state, one watched through its stub as done.    func testTheCardTagsItsParts() {        let poster = FixturePictures.image("poster-ferry")        XCTAssertNotNil(poster)        func parts(_ card: DeckCard, poster: Image?) -> Set<FacePart> {            let tagged = SeasonPartsTagged()            let drawn = SeasonCardFace(card: card, poster: poster)                .frame(width: 340, height: 451)                .backgroundPreferenceValue(FacePartsKey.self) { regions in                    let _ = tagged.parts.formUnion(regions.map(\.part))                    Color.clear                }            XCTAssertNotNil(ImageRenderer(content: drawn).cgImage)            return tagged.parts        }        XCTAssertEqual(parts(Catalogue.poster, poster: poster), [.picture, .title, .body])        XCTAssertEqual(parts(Catalogue.noPoster, poster: nil), [.picture, .title, .body])        XCTAssertEqual(parts(Catalogue.partWatched, poster: poster), [.picture, .title, .body, .state])        XCTAssertEqual(parts(Catalogue.watched, poster: poster), [.picture, .title, .body, .state, .done])    }    /// RULE: The poster's window and every line of words stand on the card, clear of one another; the strip is laid on the card; the stub reaches past the card's corner only as far as the face keeps room for.    func testTheCardKeepsItsPartsOnIt() {        let card = CGRect(origin: .zero, size: Knobs.box)        for (name, rect) in [("poster", Knobs.poster), ("tab", Knobs.tab), ("title", Knobs.title), ("facts", Knobs.facts),                             ("watched", Knobs.watched), ("strip", Knobs.progress)] {            XCTAssertTrue(card.contains(rect), "\(name) is on the card")            if name != "poster" { XCTAssertFalse(rect.intersects(Knobs.poster), "\(name) is clear of the poster") }        }        for (upper, lower) in [(Knobs.tab, Knobs.title), (Knobs.title, Knobs.facts), (Knobs.facts, Knobs.watched), (Knobs.watched, Knobs.progress)] {            XCTAssertLessThanOrEqual(upper.maxY, lower.minY, "one line stands above the next")        }        XCTAssertTrue(SeasonCardSetting.reach.contains(Knobs.stub), "the face keeps room for the stub")    }    /// RULE: The season's card lies bare on the cinema's velvet under its house light, draws its own sheet and is stepped by a cut; its specimens show a season with the series' poster and without, part watched, watched through, the longest title and the fullest; every material it lays is in the catalogue.    func testTheCardLiesOnTheVelvetAndCuts() {        XCTAssertEqual(FaceRegistry.ground("season.card"), .velvet)        XCTAssertEqual(FaceRegistry.stock("season.card"), .own)        XCTAssertEqual(Catalogue.face.steps, .cut)        XCTAssertEqual(Catalogue.face.specimens.map(\.name), ["poster", "no-poster", "part-watched", "watched", "longest-title", "largest"])        XCTAssertNil(Catalogue.face.specimens.first { $0.name == "no-poster" }?.picture)        for name in ["season-card", "progress-strip", "stub", "stub-shadow", "velvet", "house-light"] {            XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue")        }    }}