Cotton code
102 lines · 5.9 KB · SwiftRaw
// A tool tile with no object of its own types its name on a slip// (`ToolObject` → `TypedCard`), and the slip's words are a tile's words:// never broken inside a word (362 walk, 6 Nov 2026: Downloads' slip read// "Download s" at the largest text size, as Books' tier tiles read "Add /// a fi…" before e02f0633c1).#if os(iOS)import CottonPaperimport SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class TileSlipWordsTests: XCTestCase {    private let theme = Theme.paper    /// Every tool name the shipped kits carry, and long synthetic ones.    private func names() throws -> [String] {        let kits = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-core/kits")        var names: Set<String> = ["Subscriptions", "Membership", "Bartholomew Featherstonehaugh", "Synthetic extraordinarily long tool"]        let walker = try XCTUnwrap(FileManager.default.enumerator(at: kits, includingPropertiesForKeys: nil))        for case let url as URL in walker where url.lastPathComponent == "manifest.json" && url.path.contains("/tools/") {            let manifest = try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any]            if let name = manifest?["name"] as? String { names.insert(name) }        }        XCTAssertTrue(names.contains("Downloads"), "the kits' tools are read")        return names.sorted()    }    /// The slip's side on the fan's tool tile: the card's object square less the typed card's padding.    private var fanSlip: CGFloat {        let tile = CGFloat(FanGeometry(scale: theme.scale).tileWidth)        return ToolCardMetrics(scale: theme.scale).objectSide(width: tile) - 2 * ToolObjectKnobs.toolObjectTypedPadding    }    /// RULE: a slip's name stands in whole words on at most two lines at every slip size from the fan's tile down — a word wider than the slip's line even at its floor is set smaller on a line of its own, never split ("Downloads", not "Download s"); one rule for every tool, no tool named.    func testASlipsNameIsNeverBrokenInsideAWord() throws {        let typed = TypedCard.Print(scale: theme.scale)        let face = theme.type.body        for name in try names() {            for side in stride(from: fanSlip, through: 36, by: -4) {                let set = typed.name(name, side: side, face: face, monogram: false)                let width = typed.line(side: side)                let laid = InkFit.laid(set.words, face: face, size: set.size, width: width)                XCTAssertNotNil(laid, "\(name) at \(side): a word is broken at \(set.size)")                if !name.contains(" ") {                    XCTAssertEqual(laid?.count, 1, "\(name) at \(side): one word, one line")                }                for line in set.words.split(separator: "\n") where set.words != name {                    XCTAssertTrue(InkFit.fits(String(line), face: face, size: set.size, width: width, lines: 1),                                  "\(name) at \(side): the given line '\(line)' stands whole")                }            }        }    }    /// RULE: a printed tool card's caption under its object stands whole on its one line at the fan's tile and every card width down to a small one — a word longer than the line ("Subscriptions", drawn "Subscription" on the 362 walk) is set smaller, never cut inside; one rule for every tool, no tool named.    func testAPrintedCardsCaptionIsNeverCutInsideAWord() throws {        let face = theme.type.title        let tile = CGFloat(FanGeometry(scale: theme.scale).tileWidth)        let metrics = ToolCardMetrics(scale: theme.scale)        for name in try names() {            for width in stride(from: tile, through: 60, by: -8) {                let set = ToolCard.printedName(name, theme: theme, width: width)                let inner = width * (1 - 2 * metrics.contentInset)                XCTAssertTrue(InkFit.fits(set.words, face: face, size: set.size, width: inner, lines: 1),                              "\(name) at \(width): '\(set.words)' at \(set.size) overruns its line")                if !name.contains(" ") { XCTAssertEqual(set.words, name, "\(name) at \(width): one word stands whole") }            }        }    }    /// RULE: a name that stands whole at the slip's own size is struck at it: nothing shrinks that need not.    func testANameThatFitsIsNotShrunk() {        let typed = TypedCard.Print(scale: theme.scale)        let full = typed.nameSizes(side: 160, monogram: false)[0]        XCTAssertEqual(typed.name("Todos", side: 160, face: theme.type.body, monogram: false).size, full)    }    /// RULE: the slip sets its words by the tile's rule, never by a Text's own wrapping across a size `ViewThatFits` gave up on.    func testTheSlipMeasuresItsWords() throws {        let source = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Paper/TypedCard.swift", ownedBy: "paper"))        XCTAssertFalse(source.contains("ViewThatFits"))        XCTAssertTrue(source.contains("InkFit.wholeWords("))        XCTAssertTrue(source.contains("InkFit.broken("))    }    /// RULE: the fan's Downloads tile draws at Accessibility XXXL with its slip set by the rule.    func testTheDownloadsTileDrawsAtTheLargestTextSize() throws {        let tile = FanGeometry(scale: theme.scale).tileWidth        let card = ToolCard(key: "downloads", name: "Downloads", picture: nil, stock: .cream, width: tile)            .environment(\.theme, theme)            .dynamicTypeSize(.accessibility5)        let renderer = ImageRenderer(content: card)        renderer.scale = 3        XCTAssertGreaterThan(try XCTUnwrap(renderer.cgImage).width, 0)        let set = TypedCard.Print(scale: theme.scale).name("Downloads", side: fanSlip, face: theme.type.body, monogram: false)        XCTAssertEqual(set.words, "Downloads")        XCTAssertEqual(InkFit.laid(set.words, face: theme.type.body, size: set.size, width: TypedCard.Print(scale: theme.scale).line(side: fanSlip))?.count, 1)    }}#endif