Cotton code
95 lines · 4.7 KB · SwiftRaw
// Cotton Health's instruments: a day's measure as a brass pedometer, a// workout as a nickel stopwatch, a measurement as a brass tag, each on the// honed slate. Every word is the core's; each cuts.@testable import CottonFacesimport CottonPaperimport CottonValuesimport SwiftUIimport XCTest@MainActorfinal class HealthFacesTests: XCTestCase {    typealias Pedometer = HealthPedometerFaceCatalogue    typealias Stopwatch = HealthStopwatchFaceCatalogue    typealias Tag = HealthTagFaceCatalogue    /// RULE: A day's pedometer reads the core's words: the measure, its value in its unit, the usual, the dial's top, and where the hand and the usual's tick stand (each held to the dial); a day the core said nothing of falls back to the card's own title.    func testAPedometerReadsTheDaysWords() {        let steps = HealthReading(Pedometer.steps)        XCTAssertEqual(steps.label, "Steps")        XCTAssertEqual(steps.said, "8,123")        XCTAssertEqual(steps.unit, "steps")        XCTAssertEqual(steps.usual, "usual 10,233")        XCTAssertEqual(steps.top, "20,000")        XCTAssertEqual(steps.dial ?? 0, 0.406, accuracy: 1e-9)        XCTAssertEqual(steps.usualDial ?? 0, 0.512, accuracy: 1e-9)        XCTAssertNil(steps.flow)        var wild = Pedometer.steps        wild.fields["reading"] = .object(["dial": .number(3), "usual_dial": .number(-1)])        XCTAssertEqual(HealthReading(wild).dial, 1)        XCTAssertEqual(HealthReading(wild).usualDial, 0)        let bare = HealthReading(Pedometer.bare)        XCTAssertEqual(bare.label, "Cardio fitness")        XCTAssertNil(bare.dial)    }    /// RULE: The night's asleep row carries its stages deep to awake as shares of the night, and a period day carries its flow for the rim's mark; any other day carries neither.    func testANightCarriesItsStagesAndAPeriodDayItsMark() {        let night = HealthReading(Pedometer.night)        XCTAssertEqual(night.stages.map(\.stage), ["deep", "core", "rem"])        XCTAssertEqual(night.stages.map(\.share).reduce(0, +), 1, accuracy: 1e-9)        XCTAssertEqual(night.inBed, "7 h 10 min")        XCTAssertNotEqual(HealthReading.tone("deep"), HealthReading.tone("awake"))        XCTAssertEqual(HealthReading(Pedometer.periodDay).flow, "Light")        XCTAssertTrue(HealthReading(Pedometer.heart).stages.isEmpty)    }    /// RULE: A workout's stopwatch says its kind, minutes and start on the dial, its distance in the register and its energy and heart chalked under it; a tag says its kind, its reading (blood pressure upper over lower), its unit, its meal and when.    func testAStopwatchAndATagSayTheirWords() {        let run = HealthWorkoutWords(Stopwatch.run)        XCTAssertEqual(run.kind, "Running")        XCTAssertEqual(run.minutes, "33 min")        XCTAssertEqual(run.distance, "5.2 km")        XCTAssertEqual(run.foot, "310 kcal · 152 avg · 174 high bpm")        XCTAssertNil(HealthWorkoutWords(Stopwatch.yoga).foot)        let pressure = HealthTagWords(Tag.bloodPressure)        XCTAssertEqual(pressure.said, "118/76")        XCTAssertEqual(pressure.unit, "mmHg")        XCTAssertEqual(HealthTagWords(Tag.glucose).context, "Before a meal")    }    /// RULE: Each instrument tags its parts: the pedometer its measure, value and day; the stopwatch its kind, minutes and start; the tag its kind, reading and when.    func testTheInstrumentsTagTheirParts() {        XCTAssertEqual(tagged(HealthPedometerFace(card: Pedometer.steps)), [.title, .amount, .when])        XCTAssertEqual(tagged(HealthStopwatchFace(card: Stopwatch.run)), [.title, .amount, .when])        XCTAssertEqual(tagged(HealthTagFace(card: Tag.weight)), [.title, .amount, .when])    }    /// RULE: Health's three faces lie on the slate, each on its own stock, and cut when stepped; the slate is dark, so the words beside an instrument are chalk.    func testTheyLieOnTheSlateAndCut() {        for face in [Pedometer.face, Stopwatch.face, Tag.face] {            XCTAssertEqual(FaceRegistry.ground(face.id), .slate, face.id)            XCTAssertEqual(FaceRegistry.stock(face.id), .own, face.id)        }        XCTAssertTrue(FaceGround.slate.dark)        XCTAssertEqual(FaceGround.slate.tile, "health-slate")    }    private func tagged(_ face: some View) -> Set<FacePart> {        let tagged = HealthTaggedParts()        let drawn = face            .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    }}/// The parts a drawn face tagged, gathered while it renders.private final class HealthTaggedParts {    var parts: Set<FacePart> = []}