import XCTest @testable import PetKit /// The other half of the contract with `pet-life`. Its JSON is pinned to exact /// bytes by a Rust test; this asserts Swift reads those same bytes. The wire /// format between the daemon and the collector drifted twice in this project, /// and both times a pinned fixture is what caught it. final class PetStateTests: XCTestCase { /// Verbatim from pet-life's own `the_json_contract_is_exact` test. let living = #""" {"alive":true,"name":"Ash","generation":2,"stage":"content","face":"(^ω^)","age_days":0.50,"quiet_days":0.50,"cemetery":[{"name":"Marble","generation":1,"lived_days":7.00}]} """# /// A pet that has died: name and generation are null, not absent. let dead = #""" {"alive":false,"name":null,"generation":null,"stage":"dead","face":"†","age_days":0.00,"quiet_days":31.00,"cemetery":[{"name":"Marble","generation":1,"lived_days":7.00}]} """# func testDecodesALivingPet() throws { let pet = try PetState.decode(Data(living.utf8)) XCTAssertTrue(pet.alive) XCTAssertEqual(pet.name, "Ash") XCTAssertEqual(pet.generation, 2) XCTAssertEqual(pet.stage, "content") XCTAssertEqual(pet.face, "(^ω^)") XCTAssertEqual(pet.age_days, 0.5, accuracy: 0.001) XCTAssertEqual(pet.cemetery.count, 1) XCTAssertEqual(pet.cemetery[0].name, "Marble") XCTAssertEqual(pet.cemetery[0].lived_days, 7.0, accuracy: 0.001) } /// The null case is the one a naive `String` rather than `String?` would /// break on, and it is exactly the state the app must render. func testDecodesADeadPet() throws { let pet = try PetState.decode(Data(dead.utf8)) XCTAssertFalse(pet.alive) XCTAssertNil(pet.name) XCTAssertNil(pet.generation) XCTAssertEqual(pet.stage, "dead") XCTAssertEqual(pet.cemetery.count, 1, "the dead are still remembered") } func testRejectsSomethingThatIsNotAPet() { XCTAssertThrowsError(try PetState.decode(Data(#"{"nope":1}"#.utf8))) } }