// A studio's faces (Sharmila's boards): an event card with a picture is a// class on its photograph; a studio's trainers, clients, place, classes and// memberships wear the faces its kit names; a trainer with no photograph is// drawn on their initials, as a client is.importCottonFacesimportCottonPaperimportCottonValuesimportSwiftUIimport XCTest@testable importCottonUI@MainActorfinalclassStudioFacesTests: XCTestCase {privatelet join = ShellLines.line("cards", "join")func testAnEventWithAPictureIsAClass() { XCTAssertTrue(StudioReading.isClass(DeckCard(face: .event, fields: ["title": .text("Invented flow"), "photo": .text("k")]))) XCTAssertFalse(StudioReading.isClass(DeckCard(face: .event, fields: ["title": .text("Invented dinner")])),"a calendar event keeps its clock") }func testTheTimeIsLargeAndItsLengthBesideIt() throws {let when = try XCTUnwrap(StudioReading.time("7:00 am\(join)60 min")) XCTAssertEqual(when.time, "7:00 am") XCTAssertEqual(when.rest, "60 min") XCTAssertNil(StudioReading.time("7:00 am")?.rest) XCTAssertNil(StudioReading.time(nil)) }func testSeatsAreTakenOfTheRoomOnlyWhenTheCardNamesBoth() throws {let seats = try XCTUnwrap(StudioReading.seats(DeckCard(face: .event, fields: ["used": .number(7), "limit": .number(12)]))) XCTAssertEqual(seats.taken, 7) XCTAssertEqual(seats.of, 12) XCTAssertEqual(StudioReading.seats(DeckCard(face: .event, fields: ["used": .number(15), "limit": .number(12)]))?.taken, 12,"an overbooked room is full, never past full") XCTAssertNil(StudioReading.seats(DeckCard(face: .event, fields: ["used": .number(7)]))) XCTAssertNil(StudioReading.seats(DeckCard(face: .event, fields: ["used": .number(0), "limit": .number(0)]))) }/// RULE: a class's time is drawn as its figures large and the clock's word small after them; words that are no clock (an event's kind) are no figures.func testTheClockIsItsFiguresAndItsWord() throws {let morning = try XCTUnwrap(StudioReading.clock("11:30 am")) XCTAssertEqual(morning.figures, "11:30") XCTAssertEqual(morning.suffix, "am")let evening = try XCTUnwrap(StudioReading.clock("19:30")) XCTAssertEqual(evening.figures, "19:30") XCTAssertNil(evening.suffix) XCTAssertNil(StudioReading.clock("Workshop"), "an event's kind is a line, not figures") }/// RULE: the core's words for a number ("148 sessions attended") are drawn as the figure large and its words under it; words that open on no figure are no figure.func testABigNumberIsItsFigureAndItsWords() throws {let sessions = try XCTUnwrap(StudioReading.figure("148 sessions attended")) XCTAssertEqual(sessions.figure, "148") XCTAssertEqual(sessions.words, "sessions attended") XCTAssertEqual(StudioReading.figure("1,204 sessions")?.figure, "1,204") XCTAssertNil(StudioReading.figure("18")?.words, "a bare figure has no words") XCTAssertNil(StudioReading.figure("Quarterly")) XCTAssertNil(StudioReading.figure(nil)) }/// RULE: the classes of a client's period are said with their share as a whole percent, rounded, never past a hundred.func testTheClassesOfThePeriodAreAPercent() { XCTAssertEqual(StudioReading.percent(taken: 2, of: 12), 17) XCTAssertEqual(StudioReading.percent(taken: 12, of: 12), 100) XCTAssertEqual(StudioReading.percent(taken: 14, of: 12), 100) XCTAssertEqual(StudioReading.percent(taken: 0, of: 0), 0) }/// RULE: a card's facts are each the core's label and words, in the core's order; a fact with no words is left out.func testACardsFactsAreItsLabelsAndWords() {let card = DeckCard(face: .sharmilaClient, fields: ["facts": .list([ .object(["label": .text("Ends"), "said": .text("12 Nov")]), .object(["label": .text("Usually"), "said": .text("")]), .object(["said": .text("Mon 9:15 AM")]), ])])let facts = StudioReading.facts(card) XCTAssertEqual(facts.map(\.said), ["12 Nov", "Mon 9:15 AM"]) XCTAssertEqual(facts.map(\.label), ["Ends", nil]) }/// RULE: the membership's ring is drawn round as far as the classes attended of those bought, never below empty or past full.func testTheSessionsRingIsTheShareAttended() { XCTAssertEqual(StudioReading.ringShare(taken: 18, of: 24), 0.75, accuracy: 0.0001) XCTAssertEqual(StudioReading.ringShare(taken: 30, of: 24), 1, "an extended pack is a full ring") XCTAssertEqual(StudioReading.ringShare(taken: 3, of: 0), 0, "no classes bought, no ring") XCTAssertEqual(StudioReading.ringShare(taken: -1, of: 10), 0) }/// RULE: a studio's cards wear the faces its kit names — a trainer, a client and its place (or a class of its catalogue) with the pages their cards turn to, a membership on its own sheet, each from Loom's registry — each printed on the stock of the card it stood for (a person's cream, a note's ornament); no app's flavour chooses them.func testAStudiosCardsAreTheFacesItsKitNames() throws {let theme = Theme.paperfor face in [DefinitionCardKind.sharmilaTrainer, .sharmilaClient, .sharmilaStudio] { XCTAssertNotNil(FaceRegistry.face(face.rawValue), "\(face.rawValue) is no face Loom registers") XCTAssertTrue(DeckFace.draws(DeckCard(face: face, fields: ["title": .text("Invented")]))) } XCTAssertTrue(DeckFace.draws(DeckCard(face: .sharmilaMembership, fields: [:]))) XCTAssertEqual(FaceRegistry.stock(DefinitionCardKind.sharmilaMembership.rawValue), .own, "a membership draws its own sheet")for file in ["Deck/DeckCardFill.swift", "Deck/DeckFace.swift"] {let source = SourceSweep.withoutComments(trySourceSweep.mustRead("apps/cotton/Sources/CottonUI/\(file)", ownedBy: "loom")) XCTAssertFalse(source.contains("FlavourLook"), "\(file) chooses a face by the flavour") } } #if canImport(UIKit)/// RULE: a trainer whose card names no photograph is drawn on their initials, exactly as a client is — the face carries that itself, so the kit names one face for every trainer.func testATrainerWithNoPhotographIsDrawnAsAClient() throws {let look = (theme: Theme.paper, name: "paper")let fields: [String: RecordValue] = ["title": .text("Invented Trainer"), "subtitle": .text("Yoga Instructor"), "body": .text("An invented line."), ]func png(_ view: someView) throws -> Data {let renderer = ImageRenderer(content: view.environment(\.theme, look.theme).frame(width: 340, height: 460) .background(look.theme.colors.paper.color)) renderer.scale = 2returntry XCTUnwrap(renderer.uiImage?.pngData(), "draws") }let bare = DeckCard(face: .sharmilaTrainer, fields: fields) XCTAssertEqual(try png(TrainerFace(card: bare, photo: Rectangle().fill(Color.gray))), try png(ClientFace(card: bare)),"a trainer with no photograph is not the client's face")var pictured = fields pictured["photo"] = .text("k")let trainer = DeckCard(face: .sharmilaTrainer, fields: pictured) XCTAssertNotEqual(try png(TrainerFace(card: trainer, photo: Rectangle().fill(Color.gray))), try png(ClientFace(card: trainer)),"a trainer with a photograph is drawn on it") } #endif/// RULE: a person with no picture is drawn as their initials (the first letters of their first and last words), never a stock face.func testAPersonWithNoPictureIsTheirMonogram() { XCTAssertEqual(CardPerson.initials("Invented Member"), "IM") XCTAssertEqual(CardPerson.initials("Asha"), "A") XCTAssertNil(CardPerson.initials("")) } #if canImport(UIKit)/// The two studio faces on Cotton's paper, drawn to files for a person to look at.func testTheStudioFacesDraw() throws {let look = (theme: Theme.paper, name: "paper")let session = DeckCard(face: .event, fields: ["photo": .text("k"), "title": .text("Invented Morning Flow"), "date": .text("Sat 24 Oct"),"subtitle": .text("7:00 am\(join)60 min"), "used": .number(7), "limit": .number(12),"people": .list([.object(["name": .text("Invented Trainer")])]), ])let pack = DeckCard(face: .sharmilaMembership, fields: ["title": .text("Ten classes"), "subtitle": .text("Invented Member"), "state": .text("Active"),"price": .text("₹3,500"), "used": .number(7), "limit": .number(10),"date": .text("Thu 22 Oct"), "next_charge": .text("Sat 21 Nov"), ])let photo = Rectangle().fill(Color.gray)for (name, view) in [ ("class", AnyView(ClassFace(card: session, photo: photo))), ("membership", AnyView(MembershipFace(card: pack, photo: Optional<EmptyView>.none))), ] {let renderer = ImageRenderer(content: view.environment(\.theme, look.theme).frame(width: 340, height: 460) .background(look.theme.colors.paper.color)) renderer.scale = 2let image = try XCTUnwrap(renderer.uiImage, "\(name) draws")iflet folder = ProcessInfo.processInfo.environment["STUDIO_FACES_OUT"] {try image.pngData()?.write(to: URL(fileURLWithPath: folder).appendingPathComponent("\(name).png")) } } } #endif}