Cotton code
59 lines · 2.7 KB · SwiftRaw
// The architect's shared rules R2 (when the bar is shown) and R3 (who keeps// a record in step), note 27fcb084, held on the bundled definitions and on// the one input. Every record is invented.import CottonKitimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class BarAndKeptRulesTests: XCTestCase {    private static var resources: Bundle? {        Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))    }    private func bundled() throws -> [TableDefinition] {        FixtureDefinitions.load(from: try XCTUnwrap(Self.resources)).definitions    }    private func definition(_ id: String) throws -> TableDefinition {        try XCTUnwrap(try bundled().first { $0.id == id }, "no bundled \(id) definition")    }    private func decode(_ extra: String) throws -> TableDefinition {        try TableDefinition.decode(Data("""        {"id":"example","name":"Example","plural":"Examples","builtin":false,         "columns":[{"key":"title","type":"text"},{"key":"day","type":"date"},{"key":"keeper","type":"text"}],         "title":"title","date":"day","slip":{"title":"title"},"tile":{"face":"slip-list"},         "page":[{"columns":["title"]}]\(extra)}        """.utf8))    }    // ── R2: the bar ─────────────────────────────────────────────────────    /// RULE: add, append and kept_by decode and round-trip, a definition without them reads as before, and add false alone takes the bar off a list.    func testTheBarAndKeeperKeysDecode() throws {        let bare = try decode("")        XCTAssertNil(bare.add)        XCTAssertNil(bare.append)        XCTAssertNil(bare.keptBy)        let full = try decode(#","add":false,"append":true,"kept_by":{"when":"keeper","says":"Kept in step with Gmail"}"#)        XCTAssertEqual(full.add, false)        XCTAssertEqual(full.append, true)        XCTAssertEqual(full.keptBy, KeptBy(when: "keeper", says: "Kept in step with Gmail"))        XCTAssertEqual(try TableDefinition.decode(JSONEncoder().encode(full)), full)    }    // ── R3: kept in step ────────────────────────────────────────────────    /// RULE: Every keeper line is read from a column the definition has, and says something — the wording is data.    func testEveryKeeperLineNamesItsKeeper() throws {        for definition in try bundled() {            guard let kept = definition.keptBy else { continue }            XCTAssertNotNil(definition.column(kept.when), "\(definition.id) kept_by.when")            XCTAssertFalse(kept.says.isEmpty, "\(definition.id)")        }    }}