// The index card: a todo's title typed on the card's first rule, one line or // two and no further, the description on the rules after it, the day it was // made on the last rule the tray's lip leaves showing, the due date on a tab // (red once the day is behind, none for no date), the DONE ring ticked and // the title struck through once it is done; the parts the shell writes are // tagged; the tray stands on the todos' steel and is stepped by a cut. @testable import CottonFaces import CottonPaper import CottonValues import CoreText import SwiftUI import XCTest @MainActor final class TodoCardTests: XCTestCase { private typealias Catalogue = TodoCardFaceCatalogue private typealias Knobs = TodoCardFaceKnobs /// A phone's index card: the tray 96% of a 350-point card's width, the front card across its stack. private var size: CGSize { let tray = 350 * Knobs.fillWidth let width = (Knobs.stackRight - Knobs.stackLeft + 2 * Knobs.overhang) * tray return CGSize(width: width, height: width / Knobs.cardAspect) } private func todo(_ fields: [String: RecordValue], pressing: Bool = false) -> DeckCard { DeckCard(face: "todo.card", corner: nil, pressing: pressing, pace: nil, plate: nil, fields: fields) } /// The words of `text`, in order, whitespace let go. private func words(_ text: String) -> [Substring] { text.split(whereSeparator: { $0.isWhitespace }) } /// RULE: The title is typed on the card's first rule, the description on the rules after it, the day it was made on the last rule showing; the due date on the tab in capitals, and the ring's word under it. func testTheWordsAreTypedOnTheRules() { let card = IndexCardSetting.set(Catalogue.open, size: size) XCTAssertEqual(card.title, [TornLine("Renew the car insurance")]) XCTAssertEqual(card.titleRules, 0..<1) XCTAssertEqual(card.bodyRule, 1) XCTAssertEqual(card.body.flatMap { words($0.words) }, words("Policy ends 3 Nov; ask for the no-claims letter"), "a short description is typed whole") XCTAssertEqual(card.made, "made Thu 22 Oct") XCTAssertEqual(card.madeRule, Knobs.shown - 1) XCTAssertEqual(card.tab, "FRI 30 OCT") XCTAssertFalse(card.late) XCTAssertFalse(card.done) XCTAssertEqual(card.stampWord, "DONE") } /// RULE: The rules the card types on all stand above the tray's lip, and the next one is behind it. func testTheRulesTypedOnShowAboveTheLip() { let tray = CGSize(width: 340, height: 340 / Knobs.trayAspect) let cardHeight = (Knobs.stackRight - Knobs.stackLeft + 2 * Knobs.overhang) * tray.width / Knobs.cardAspect let rule = { (index: Int) in Knobs.top * tray.height + Knobs.rules[index] * cardHeight } XCTAssertLessThan(rule(Knobs.shown - 1) + Knobs.bodySize * cardHeight * 0.3, Knobs.lip * tray.height, "the made day's descenders clear the lip") XCTAssertGreaterThan(rule(Knobs.shown), Knobs.lip * tray.height, "the next rule is behind the lip") } /// RULE: The description is the body's first words in order and stops at the last whole word the card holds: nothing cut inside a word, nothing added, no ellipsis, no "and more". func testTheDescriptionStopsAtTheLastWholeWord() throws { let card = IndexCardSetting.set(Catalogue.long, size: size) let body = try XCTUnwrap(Catalogue.long.text("body")) let typed = card.body.flatMap { words($0.words) } XCTAssertLessThan(typed.count, words(body).count, "the long todo's description is longer than its card") XCTAssertEqual(Array(words(body).prefix(typed.count)), typed, "the description's first words, whole") XCTAssertFalse(card.body.contains { $0.words.contains("\u{2026}") }) let column = IndexCardSetting.column(size).width let font = IndexCardSetting.fonts(height: size.height).body for line in card.body { let set = NSAttributedString(string: line.words, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font]) let width = CGFloat(CTLineGetTypographicBounds(CTLineCreateWithAttributedString(set), nil, nil, nil)) XCTAssertLessThanOrEqual(width, column + 0.5, "\"\(line.words)\" runs past the card's writing room") } XCTAssertEqual(card.bodyRules.upperBound, card.madeRule, "the description fills the card down to the made day") } /// RULE: A long title takes two lines and no more, stopping at the second's last whole word, and the description moves down under it. func testALongTitleTakesTwoLinesAndNoMore() throws { let card = IndexCardSetting.set(Catalogue.long, size: size) XCTAssertEqual(card.title.count, 2) XCTAssertEqual(card.bodyRule, 2) let title = try XCTUnwrap(Catalogue.long.text("title")) let typed = card.title.flatMap { words($0.words) } XCTAssertLessThan(typed.count, words(title).count, "the long title wants a third line") XCTAssertEqual(Array(words(title).prefix(typed.count)), typed, "the title stops at a whole word") XCTAssertFalse(card.title.contains { $0.words.contains("\u{2026}") }) } /// RULE: A todo past its day takes the red tab; a todo with no date has no tab; a done one is ticked and struck through, and is not late; a dropped one says nothing of done. func testTheTabAndTheRingSayTheState() { let overdue = IndexCardSetting.set(Catalogue.overdue, size: size) XCTAssertTrue(overdue.late) XCTAssertEqual(overdue.tab, "TUE 27 OCT") XCTAssertNil(IndexCardSetting.set(Catalogue.noDate, size: size).tab, "the core's \"Add date\" is no date") let done = IndexCardSetting.set(Catalogue.done, size: size) XCTAssertTrue(done.done) XCTAssertFalse(done.late) let dropped = IndexCardSetting.set(todo(["check": .bool(true), "title": .text("Paint the gate"), "state": .text("Dropped")]), size: size) XCTAssertFalse(dropped.done) let bare = IndexCardSetting.set(todo(["title": .text("Paint the gate")]), size: size) XCTAssertNil(bare.made, "no made place, no made line") XCTAssertNil(bare.stampWord, "no label for the check, no word under the ring") XCTAssertEqual(bare.body, []) } func testTheStampUsesTheCardsOwnLabel() { var card = Catalogue.done card.labels = CardLabels(bySlot: ["check": "Filed"]) XCTAssertEqual(IndexCardSetting.set(card, size: size).stampWord, "FILED") card.labels = CardLabels() XCTAssertNil(IndexCardSetting.set(card, size: size).stampWord) } /// The parts tagged when `card` is drawn. private func parts(_ card: DeckCard) -> Set { final class Tagged { var parts: Set = [] } let tagged = Tagged() let drawn = TodoCardFace(card: card) .frame(width: 350, height: 437) .backgroundPreferenceValue(FacePartsKey.self) { regions in let _ = tagged.parts.formUnion(regions.map(\.part)) Color.clear } XCTAssertNotNil(ImageRenderer(content: drawn).cgImage) return tagged.parts } /// RULE: The title, the description, the tab and the ring are the parts the shell writes from the card; a todo with no date offers the tab's bare place as its corner; the made day is no part. func testThePartsAreTagged() { XCTAssertEqual(parts(Catalogue.open), [.title, .body, .when, .done]) XCTAssertEqual(parts(Catalogue.noDate), [.title, .body, .corner, .done]) XCTAssertFalse(Catalogue.face.tagsNoPart) } /// RULE: The index card's tray stands bare on the todos' dark teal baize, laid with its painted tile, and is stepped by a cut. func testTheTrayStandsOnTheBaizeAndCuts() { XCTAssertEqual(FaceRegistry.ground("todo.card"), .baizeTeal) XCTAssertEqual(FaceRegistry.stock("todo.card"), .own) XCTAssertTrue(FaceGround.baizeTeal.dark) XCTAssertEqual(FaceGround.baizeTeal.tile, "todos-baize") XCTAssertEqual(Catalogue.face.steps, .inPlace) XCTAssertEqual(Catalogue.face.specimens.map(\.name), ["opened", "open", "done", "overdue", "no-date", "long"]) for name in ["todo-tray", "todo-tray-front", "todo-tray-shadow", "todo-card", "todo-tab", "todo-tab-late", "todos-baize"] { XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue") } } }