Cotton code
61 lines · 2.9 KB · SwiftRaw
// A group deck's face (Money's By person): one card per person, as the core// deals it — their name, what the group comes to, its latest lines.import CottonPaperimport CottonValuesimport SwiftUIimport XCTest@testable import CottonFaces@testable import CottonUI@MainActorfinal class GroupFaceTests: XCTestCase {    private static func line(_ title: String, _ amount: String?, _ date: String?) -> RecordValue {        var said: [String: RecordValue] = ["title": .text(title)]        if let amount { said["amount"] = .text(amount) }        if let date { said["date"] = .text(date) }        return .object(said)    }    /// RULE: a group card's lines are the core's, in its order, each its title, amount and day; a line with nothing to say is left out.    func testAGroupCardReadsItsLinesInTheCoresOrder() {        let card = DeckCard(face: .group, fields: [            "title": .text("Invented Asha"),            "net": .list([.text("₹1,800 between you")]),            "lines": .list([                Self.line("Invented dinner", "₹600", "Tue 20 Oct"),                .object([:]),                Self.line("Invented tickets", "₹1,500", nil),            ]),            "more": .text("+1"),        ])        XCTAssertEqual(GroupLine.of(card), [            GroupLine(id: 0, title: "Invented dinner", amount: "₹600", date: "Tue 20 Oct"),            GroupLine(id: 2, title: "Invented tickets", amount: "₹1,500", date: nil),        ])        XCTAssertEqual(GroupLine.of(DeckCard(face: .group, fields: ["title": .text("Invented Ravi")])), [], "no lines, none drawn")    }    /// RULE: the deck draws a group card on its own face, on the cream card a person stands on.    func testTheDeckDrawsAGroupOnTheCreamCard() {        let card = DeckCard(face: .group, fields: ["title": .text("Invented Asha")])        XCTAssertTrue(DeckFace.draws(card))    }    /// RULE: a partitioned view is read by its name, as a ring's view is drawn.    func testAPartitionedViewReadsUnderItsName() throws {        var table = try XCTUnwrap(FixtureDefinitions.load().definitions.first { $0.id == "todo" })        table.card = try JSONDecoder().decode(DefinitionCardFace.self, from: Data(            #"{"face":"group","fields":[{"slot":"people","column":"person_id"},{"slot":"title","column":"title"}]}"#.utf8))        var tool = ToolDefinition(            id: "synthetic-money", name: "Money", tables: [table.id], instructions: "",            automations: [], author: .init(device: "test", name: "Test"), signature: nil)        var with = DefinitionToolView(_generated: (), id: "with_person", label: "With person", read: ["table": .string(table.id)])        with.partition = "person_id"        with.face = "cards"        tool.views = [with]        let read = try XCTUnwrap(GroupOpening.read(named: "synthetic-money/with_person", tables: [], tools: [tool]))        XCTAssertEqual(read.name, "synthetic-money/with_person")        XCTAssertEqual(read.face, "cards")    }}