Cotton code
75 lines · 4.0 KB · SwiftRaw
// The dish face (Nancy's kitchen): a plain card with a picture and a sum is a// dish on its plate; the columns its card turns to are its pages; it reads// its stars and its kind's mark.import CottonValuesimport SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class DishFaceTests: XCTestCase {    /// An invented dish, its card as the kitchen package names it.    static func dish() throws -> TableDefinition {        let json = """        {"id":"kitchen_dish","name":"Dish","plural":"Dishes","builtin":false,         "columns":[{"key":"name","name":"Dish","type":"text"},                    {"key":"kind","name":"Kind","type":"choice","options":["veg","egg","non_veg","sweet"]},                    {"key":"price","name":"Price a plate","type":"amount"},                    {"key":"photo","name":"Photo","type":"photo"},                    {"key":"description","name":"Description","type":"longtext"},                    {"key":"spice","name":"Spice","type":"choice","options":["mild","medium","spicy"]},                    {"key":"rating","name":"Rating","type":"number"},                    {"key":"reviews","name":"Reviews","type":"number"},                    {"key":"review_summary","name":"What buyers say","type":"longtext"}],         "title":"name","date":null,"project":null,"order":null,         "slip":{"title":"name"},"tile":{"face":"slip-list"},"page":[],         "card":{"face":"plain","fields":[            {"slot":"photo","column":"photo"},{"slot":"title","column":"name"},            {"slot":"subtitle","column":"kind","also":["spice"]},{"slot":"body","column":"description"},            {"slot":"amount","column":"price"},{"slot":"count","column":"reviews"},            {"slot":"facts","column":"price","also":["rating","reviews"]}],          "pages":["description","review_summary"]},         "tables":[]}        """        return try JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8))    }    private let row = TableRecord(id: "dish-1", values: [        "name": .text("Invented smoked pork"), "kind": .text("non_veg"), "rating": .number(4.5),        "description": .text("An invented dish."), "review_summary": .text("  "),    ])    func testThePagesAreTheCardsColumnsThatHoldWordsInOrder() throws {        let definition = try Self.dish()        XCTAssertEqual(DeckCardFill.pages(definition, record: row), [CardPage(label: "Description", text: "An invented dish.")],                       "an empty column turns to no page")        var said = row        said.values["review_summary"] = .text("Invented buyers liked it.")        XCTAssertEqual(DeckCardFill.pages(definition, record: said).map(\.label), ["Description", "What buyers say"])        XCTAssertEqual(DeckCardFill.pages(nil, record: said), [], "with no definition the card is its one page")    }    func testADishReadsItsStarsAndItsKindsMark() throws {        let definition = try Self.dish()        XCTAssertEqual(DeckCardFill.stars(definition, record: row), 4.5)        XCTAssertNil(DeckCardFill.stars(definition, record: TableRecord(id: "d", values: ["rating": .number(9)])),                     "a number past five is no rating")        XCTAssertEqual(DishKindMark.ink(DeckCardFill.stored(definition, record: row)["subtitle"]), .red)        XCTAssertEqual(DishKindMark.ink("veg"), .green)        XCTAssertNil(DishKindMark.ink("sweet"))    }    func testChipsAreThePartsTheCoreJoined() {        let join = ShellLines.line("cards", "join")        XCTAssertEqual(DeckCard.chips("Non-veg\(join)Spicy"), ["Non-veg", "Spicy"])        XCTAssertEqual(DeckCard.chips(nil), [])    }    func testAPictureAndASumMakeADish() {        let dish = DeckCard(face: .plain, fields: ["title": .text("Invented dal"), "photo": .text("k"), "amount": .text("₹180")])        XCTAssertTrue(dish.isDish)        let chef = DeckCard(face: .plain, fields: ["title": .text("Invented chef"), "photo": .text("k")])        XCTAssertFalse(chef.isDish, "a chef with a picture is drawn plainly")    }}