// The standing hardback: the ribbon runs as far down the front as the// reader is, a tab at the head before they begin and past the foot once// they finish; the band says where the reader is and the page they are on,// or the day finished and the stars; the slip says where and when a book// was put down; a book wanting reading says nothing; a book kept without a// cover sets its title balanced on the blank jacket; the cover is mapped to// the turned board's corners; the parts tagged; the book stands on the navy// felt and cuts.@testable importCottonFacesimportCottonPaperimportCottonSpecimensimportCottonValuesimportSwiftUIimport XCTest/// The parts a drawn face tagged.privatefinalclassHardbackPartsTagged {var parts: Set<FacePart> = []}@MainActorfinalclassBookHardbackTests: XCTestCase {privatetypealiasCatalogue = BookHardbackFaceCatalogueprivatetypealiasKnobs = BookHardbackFaceKnobsprivatefunc book(_ status: BookHardbackStatus, _ fields: [String: RecordValue]) -> DeckCard {Catalogue.book(status, ["state": .text(status.rawValue)].merging(fields) { _, given in given }) }/// RULE: The ribbon is never a stub: a book not begun hangs it a sixth of the front's height (the shortest, one rule), the share read runs it on from there to the front's foot at the whole (41 % to y664), and a finished book's hangs its swallowtail past the book's foot.func testTheRibbonRunsAsFarAsTheReaderIs() {let shortest = (Knobs.ribbonFront + Knobs.ribbonFrontHeight / 6).rounded() XCTAssertEqual(Knobs.ribbonShortest, 1.0 / 6) XCTAssertEqual(BookHardbackSetting.ribbonEnd(book(.wantToRead, [:])), shortest) XCTAssertEqual(BookHardbackSetting.ribbonEnd(book(.reading, ["used": .number(0)])), shortest) XCTAssertEqual(shortest, 263, "a sixth of the 1172 px front below its head at y68") XCTAssertEqual(BookHardbackSetting.ribbonEnd(Catalogue.reading), 664, "41 % of the way from the shortest to the foot") XCTAssertEqual(BookHardbackSetting.ribbonEnd(book(.reading, ["used": .number(100)])), Knobs.ribbonFront + Knobs.ribbonFrontHeight) XCTAssertEqual(BookHardbackSetting.ribbonEnd(Catalogue.stopped), (Knobs.ribbonFront + Knobs.ribbonFrontHeight * (1.0 / 6 + 5.0 / 6 * 0.23)).rounded())let finished = BookHardbackSetting.ribbonEnd(Catalogue.finished) XCTAssertGreaterThan(finished - Knobs.ribbonTail, Knobs.box.height, "the whole swallowtail hangs below the foot") XCTAssertEqual(BookHardbackSetting.ribbonEnd(book(.finished, [:])), finished, "finished hangs whatever the share says") }/// RULE: The ribbon comes out of the head a little way in from the spine, over the cover's left margin: cover shows on its spine side (at least 40 px of the jacket) and on its other, and its cloth stands clear of the blank jacket's title.func testTheRibbonHangsInFromTheSpine() {let cloth = (Knobs.ribbon.minX + Knobs.ribbonCloth.lowerBound)...(Knobs.ribbon.minX + Knobs.ribbonCloth.upperBound) XCTAssertGreaterThanOrEqual(cloth.lowerBound, Knobs.jacket.minX + Knobs.ribbonInFrom, "cover shows on the spine side") XCTAssertLessThan(cloth.upperBound, Knobs.title.minX, "clear of the blank jacket's title") XCTAssertLessThan(cloth.upperBound, Knobs.jacket.midX, "on the cover's left margin") }/// RULE: The band says the state and the share while reading, the state and the day (bare of its weekday) once finished with the stars beside; a book wanted or stopped wears no band, and a stopped book's slip says the share it stopped at and the day last read.func testTheStatusSaysItselfOnTheBandOrTheSlip() {let percent = { (share: Double) in share.formatted(.percent.precision(.fractionLength(0))) } XCTAssertEqual(BookHardbackSetting.band(Catalogue.reading),ShellLines.line("cards", "book.band_reading", ["state": "Reading", "share": percent(0.41)])) XCTAssertEqual(BookHardbackSetting.band(Catalogue.reading), "Reading \u{00B7} \(percent(0.41))") XCTAssertEqual(BookHardbackSetting.band(Catalogue.finished), "Finished 12 Oct") XCTAssertEqual(BookHardbackSetting.stars(Catalogue.finished), 4.5) XCTAssertNil(BookHardbackSetting.stars(Catalogue.reading), "only a finished book's band carries stars") XCTAssertEqual(BookHardbackSetting.band(book(.finished, ["date": .text(ShellLines.line("cards", "date.add"))])), "finished","no day kept: the state alone") XCTAssertNil(BookHardbackSetting.band(Catalogue.want)) XCTAssertNil(BookHardbackSetting.band(Catalogue.stopped)) XCTAssertEqual(BookHardbackSetting.slip(Catalogue.stopped), ["Stopped at \(percent(0.23))", "4 Nov"]) XCTAssertEqual(BookHardbackSetting.slip(book(.stopped, [:])), ["stopped"], "no share, no day: the state alone") XCTAssertEqual(BookHardbackSetting.slip(Catalogue.reading), []) XCTAssertEqual(BookHardbackSetting.slip(Catalogue.want), []) XCTAssertNil(BookHardbackStatus(DeckCard(face: "book.hardback", corner: nil, pressing: false, pace: nil, plate: nil, fields: [:])),"no stored state, no status: the book alone") }/// RULE: While reading a book whose pages are known, the band types the page reached of them — the pages times the share, rounded — in the deck's own line; no pages, or not reading, no line.func testThePagesAreThePagesTimesTheShare() { XCTAssertEqual(BookHardbackSetting.pages(Catalogue.reading), ShellLines.line("cards", "deck.position", ["at": "124", "of": "303"])) XCTAssertEqual(BookHardbackSetting.pages(Catalogue.reading), "124 / 303") XCTAssertEqual(BookHardbackSetting.pages(Catalogue.readingNoCover), "114 / 190") XCTAssertEqual(BookHardbackSetting.pages(book(.reading, ["pages": .number(303)])), "0 / 303", "not begun: page nought") XCTAssertNil(BookHardbackSetting.pages(book(.reading, ["used": .number(41)])), "no pages known") XCTAssertNil(BookHardbackSetting.pages(Catalogue.finished)) XCTAssertNil(BookHardbackSetting.pages(Catalogue.stopped)) }/// RULE: A book kept without a cover sets its title on the blank jacket in at most three lines, balanced — no line much wider than another, each within the room — taken down in size only when it must, and past the least size cut with an ellipsis.func testTheBlankJacketSetsItsTitleBalanced() {let (lines, size) = BookHardbackSetting.title(Catalogue.readingNoCover) XCTAssertEqual(lines.joined(separator: " "), "A Field Guide to Small Weather") XCTAssertLessThanOrEqual(lines.count, Knobs.titleLines)let font = HighlightPassageSetting.caslon(size)let widths = lines.map { HighlightPassageSetting.wide($0, font) }for width in widths { XCTAssertLessThanOrEqual(width, Knobs.title.width + 0.5) }let greedy = HighlightPassageSetting.lines("A Field Guide to Small Weather", font: font, width: Knobs.title.width, limit: .max) XCTAssertEqual(lines.count, greedy.count, "balancing never adds a line") XCTAssertLessThanOrEqual(widths.max()! - widths.min()!, greedy.map { HighlightPassageSetting.wide($0, font) }.max()! - greedy.map { HighlightPassageSetting.wide($0, font) }.min()!)let rain = BookHardbackSetting.title(book(.reading, ["title": .text("Rain")])) XCTAssertEqual(rain.lines, ["Rain"]) XCTAssertEqual(rain.size, Knobs.titleSize, "a title that fits is set at the jacket's own size")let (long, least) = BookHardbackSetting.title(Catalogue.largest) XCTAssertEqual(long.count, Knobs.titleLines) XCTAssertEqual(long.last?.last, "\u{2026}", "too long even at the least size: cut") XCTAssertGreaterThanOrEqual(least, Knobs.titleLeast) XCTAssertEqual(BookHardbackSetting.title(book(.reading, [:])).lines, []) }/// RULE: The blank jacket's title wraps at word boundaries and is taken down in size before any word is split: every line is whole words of the title within the room, a long word shrinks the title rather than breaking ("Featherstonehaugh" whole, never "Featherstoneha / ugh"); only a word wider than the room even at the least size splits, there, with an explicit hyphen — the rolodex's rule (`NameFit`), not a second one.func testTheBlankJacketNeverBreaksAWordInTwo() {let titles = ["Featherstonehaugh", "The Featherstonehaugh Inheritance", "Notes from Thistlewaite Hall","A Field Guide to Small Weather", "Harbour Test", "Unquestionably Extraordinary Circumstances", ]for title in titles {let (lines, size) = BookHardbackSetting.title(book(.reading, ["title": .text(title)])) XCTAssertEqual(lines.joined(separator: " "), title, "\(title): every word whole, in order") XCTAssertLessThanOrEqual(lines.count, Knobs.titleLines, title) XCTAssertGreaterThanOrEqual(size, Knobs.titleLeast, title)let font = HighlightPassageSetting.caslon(size)for line in lines { XCTAssertLessThanOrEqual(HighlightPassageSetting.wide(line, font), Knobs.title.width + 0.5, "\(title): \(line)") } }let (long, size) = BookHardbackSetting.title(book(.reading, ["title": .text("Featherstonehaugh")])) XCTAssertEqual(long, ["Featherstonehaugh"]) XCTAssertEqual(BookHardbackSetting.title(Catalogue.longWord).lines.joined(separator: " "), "The Featherstonehaugh Inheritance", "the specimen") XCTAssertLessThan(size, Knobs.titleSize, "a word wider than the room at the jacket's size takes the title down")let word = "Pneumonoultramicroscopicsilicovolcanoconiosis"let (split, least) = BookHardbackSetting.title(book(.reading, ["title": .text(word)])) XCTAssertGreaterThan(split.count, 1) XCTAssertEqual(least, BookHardbackSetting.title(Catalogue.largest).size, "split only at the least size")for line in split.dropLast() { XCTAssertTrue(line.hasSuffix("-"), "\(line): a split is hyphenated") } XCTAssertEqual(split.map { $0.hasSuffix("-") ? String($0.dropLast()) : $0 }.joined(), word) }/// RULE: A plain cover's title that is a file name, one run with no spaces, breaks after its underscores, hyphens and dots and at its capitals, every character kept in order, every line inside the jacket, in at most three lines; a long one is cut with an ellipsis, never set as three raw lines split anywhere.func testAFileNameTitleBreaksSensiblyInsideTheCover() { XCTAssertEqual(BookHardbackSetting.fileNamePieces("the_lamp_keepers.epub"), ["the_", "lamp_", "keepers.", "epub"]) XCTAssertEqual(BookHardbackSetting.fileNamePieces("TheLampKeeper2"), ["The", "Lamp", "Keeper", "2"]) XCTAssertEqual(BookHardbackSetting.fileNamePieces("The Lamp_Keeper"), ["The Lamp_Keeper"], "a title with a space is words")let (lines, size) = BookHardbackSetting.title(Catalogue.fileName)let font = HighlightPassageSetting.caslon(size) XCTAssertLessThanOrEqual(lines.count, Knobs.titleLines) XCTAssertGreaterThan(lines.count, 1)for line in lines { XCTAssertLessThanOrEqual(HighlightPassageSetting.wide(line, font), Knobs.title.width + 0.5, line) }let set = lines.joined()let title = "the_lamp_keepers_daughter_annotated_edition.epub"if set.hasSuffix("\u{2026}") { XCTAssertTrue(title.hasPrefix(String(set.dropLast())), set) } else { XCTAssertEqual(set, title) }for line in lines.dropLast() { XCTAssertTrue(line.hasSuffix("_") || line.hasSuffix("."), "\(line) ends at a break") }let short = BookHardbackSetting.title(book(.wantToRead, ["title": .text("harbour_lamp.pdf")])) XCTAssertEqual(short.lines.joined(), "harbour_lamp.pdf", "kept whole, at most broken at its marks") XCTAssertEqual(short.size, Knobs.titleSize, "a short file name is set at the jacket's own size") }/// RULE: The cover is mapped onto the turned board: the projection lays the cover's four corners on the cut-out's four, clockwise from the top left.func testTheCoverIsMappedToTheBoardsCorners() {let size = CGSize(width: Knobs.jacket.width, height: Knobs.jacket.height)let m = BookHardbackSetting.projection(size, onto: Knobs.jacketCorners)func mapped(_ point: CGPoint) -> CGPoint {let w = m.m13 * point.x + m.m23 * point.y + m.m33return CGPoint(x: (m.m11 * point.x + m.m21 * point.y + m.m31) / w, y: (m.m12 * point.x + m.m22 * point.y + m.m32) / w) }let corners = [CGPoint.zero, CGPoint(x: size.width, y: 0), CGPoint(x: size.width, y: size.height), CGPoint(x: 0, y: size.height)]for (corner, onto) in zip(corners, Knobs.jacketCorners) {let at = mapped(corner) XCTAssertEqual(at.x, onto.x, accuracy: 0.001) XCTAssertEqual(at.y, onto.y, accuracy: 0.001) } XCTAssertEqual(BookHardbackSetting.spread(Knobs.jacketCorners, by: 2).first, CGPoint(x: 66, y: 65), "run under the rim") }/// RULE: The hardback tags its jacket as its picture (and, on the blank jacket, its title and author), the band as the state while reading and the day once finished, the stars, and the slip as the state; a book wanted tags its jacket alone; a stopped book lying tags its warped jacket (or its blank jacket's title and author) and its slip.func testTheBookTagsItsParts() {let cover = FixturePictures.image("cover-harbour") XCTAssertNotNil(cover)func parts<Drawn: View>(_ face: Drawn) -> Set<FacePart> {let tagged = HardbackPartsTagged()let drawn = face .frame(width: 340, height: 451) .backgroundPreferenceValue(FacePartsKey.self) { regions inlet _ = tagged.parts.formUnion(regions.map(\.part))Color.clear } XCTAssertNotNil(ImageRenderer(content: drawn).cgImage)return tagged.parts } XCTAssertEqual(parts(BookHardbackFace(card: Catalogue.reading, cover: cover)), [.picture, .state]) XCTAssertEqual(parts(BookHardbackFace(card: Catalogue.finished, cover: cover)), [.picture, .when, .stars]) XCTAssertEqual(parts(BookHardbackFace(card: Catalogue.stopped, cover: cover)), [.picture, .state]) XCTAssertEqual(parts(BookHardbackFace<Image>(card: Catalogue.stoppedNoCover)), [.picture, .title, .subtitle, .state]) XCTAssertEqual(parts(BookHardbackFace(card: Catalogue.want, cover: cover)), [.picture]) XCTAssertEqual(parts(BookHardbackFace<Image>(card: Catalogue.readingNoCover)), [.picture, .title, .subtitle, .state]) }/// RULE: The hardback stands bare on the books' navy felt, laid with its painted tile, draws its own sheet, and is stepped by a cut; every material it lays is in the catalogue.func testTheBookStandsOnTheNavyFeltAndCuts() { XCTAssertEqual(FaceRegistry.ground("book.hardback"), .feltNavy) XCTAssertEqual(FaceRegistry.stock("book.hardback"), .own) XCTAssertEqual(FaceGround.feltNavy.tile, "books-felt") XCTAssertEqual(Catalogue.face.steps, .cut) XCTAssertEqual(Catalogue.face.specimens.map(\.name), ["reading", "reading-no-cover", "want", "finished", "stopped", "largest", "long-word", "file-name", "stopped-no-cover"]) XCTAssertNil(Catalogue.face.specimens.first { $0.name == "reading-no-cover" }?.picture)for name in ["book-boards", "book-shadow", "book-jacket-blank", "book-ribbon", "book-band", "book-band-shadow", "books-felt","book-lying", "book-lying-shadow", "book-lying-slip", "book-lying-slip-shadow", "book-lying-gloss", "book-lying-ribbon"] { XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue") } }/// RULE: The books' felt lies under its warm lamp, the painted light-only overlay the screen's size (the glow from the upper left and its faint pool lower right), laid from the ground's upper left at its full strength, its opacity baked into the painting; the painting is in the catalogue.func testTheFeltHasItsLamp() { XCTAssertEqual(FaceGround.feltNavy.lamp, FaceGroundLamp(name: "books-lamp", rect: CGRect(x: 0, y: 0, width: 1290, height: 2796), strength: 1)) XCTAssertNotNil(CottonMedia.image("books-lamp", scale: 3), "books-lamp is not in the catalogue") }/// RULE: A stopped book lies on the felt in the painted three-quarter pose: every layer laid back at its bounds on the 1290 x 1600 canvas, all of it inside the canvas the face fits to the card; the cover warped onto the cover's quad (its four corners onto the quad's, clockwise from the top left), the slip's words onto the slip's quad, which stands out past the book's fore-edge a third of the way down.func testAStoppedBookLiesTurned() {for rect in [Knobs.lyingBook, Knobs.lyingShadow, Knobs.lyingSlip, Knobs.lyingSlipShadow, Knobs.lyingGloss, Knobs.lyingRibbon] { XCTAssertTrue(Knobs.lying.contains(rect), "\(rect) inside the canvas the face fits") }let size = CGSize(width: Knobs.jacket.width, height: Knobs.jacket.height)let m = BookHardbackSetting.projection(size, onto: Knobs.coverQuad)for (corner, onto) in zip([CGPoint.zero, CGPoint(x: size.width, y: 0), CGPoint(x: size.width, y: size.height), CGPoint(x: 0, y: size.height)], Knobs.coverQuad) {let w = corner.x * m.m13 + corner.y * m.m23 + m.m33 XCTAssertEqual((corner.x * m.m11 + corner.y * m.m21 + m.m31) / w, onto.x, accuracy: 0.01) XCTAssertEqual((corner.x * m.m12 + corner.y * m.m22 + m.m32) / w, onto.y, accuracy: 0.01) }let fore = Knobs.coverQuad[1].x XCTAssertGreaterThan(Knobs.slipQuad.map(\.x).max()!, fore, "the slip stands out past the fore-edge")let middle = Knobs.slipQuad.map(\.y).reduce(0, +) / 4let head = Knobs.coverQuad[1].y, foot = Knobs.coverQuad[2].y XCTAssertEqual((middle - head) / (foot - head), 1.0 / 3, accuracy: 0.12, "about a third of the way down") XCTAssertEqual(BookHardbackSetting.slip(Catalogue.stopped), ["Stopped at 23%", "4 Nov"]) }}