Cotton code
183 lines · 8.3 KB · RustRaw
//! The one slip answer, over the shipped todo's definition.use super::*;fn todo() -> TableDefinition {    cotton_core::definitions::builtin_table_definition("todo").unwrap().clone()}fn now() -> Option<chrono::DateTime<chrono::Utc>> {    Some(chrono::DateTime::parse_from_rfc3339("2026-11-08T10:00:00Z").unwrap().with_timezone(&chrono::Utc))}fn said(definition: &TableDefinition, data: Value) -> Value {    let reading = Reading::new(definition, "UTC", now(), false, Words::shipped());    said_of(&reading, &json!({"id": "t1", "data": data}))}/// RULE: a record's slip names it three ways: `title` what heads the slip,/// `name` its own title, `spoken` never empty.#[test]fn a_titled_record_is_named_by_its_title() {    let said = said(&todo(), json!({"title": "Water the ferns"}));    assert_eq!(said["slip"]["title"], "Water the ferns");    assert_eq!(said["slip"]["name"], "Water the ferns");    assert_eq!(said["slip"]["spoken"], "Water the ferns");}/// RULE: a record with nothing to call it is spoken by its table's name, and/// answers no `name` a caller could mistake for its title.#[test]fn an_untitled_record_is_spoken_by_its_table() {    let definition = todo();    let said = said(&definition, json!({}));    assert!(said["slip"].get("name").is_none());    assert_eq!(said["slip"]["spoken"], definition.name.as_str());}/// RULE: the check's state, what a tap writes and the stamp are one answer/// with the words, wherever the record is drawn.#[test]fn a_done_todo_says_its_check_and_stamp() {    let definition = todo();    let toggle = definition.slip.toggle.clone().unwrap();    let said = said(&definition, json!({"title": "Post the letter", toggle.clone(): "done"}));    assert_eq!(said["toggled"], true);    assert_eq!(said["stamp"], "done");    assert_eq!(said["toggle"][toggle.as_str()], definition.column(&toggle).unwrap().choices.as_ref().unwrap()[0].as_str());}/// RULE: a record the core will not delete alone says so, though its table deletes.#[test]fn a_record_kept_from_deleting_says_it_does_not_delete() {    let definition = todo();    let reading = Reading::new(&definition, "UTC", now(), false, Words::shipped());    let said = said_of(&reading, &json!({"id": "t1", "deletable": false, "data": {"title": "A later occurrence"}}));    assert_eq!(said["deletes"], false);}/// RULE: a table every column of which is read-only takes nothing typed.#[test]fn an_engines_table_is_read_only() {    let mut definition = todo();    for column in &mut definition.columns { column.readonly = Some(true); }    let said = said(&definition, json!({"title": "Kept by its engine"}));    assert_eq!(said["read_only"], true);    assert_eq!(said["types_into"], false);    assert_eq!(said["deletes"], false);}/// RULE: a secret title is never said raw on a slip.#[test]fn a_secret_title_is_not_said() {    let mut definition = todo();    let title = definition.title.clone();    definition.columns.iter_mut().find(|column| column.key == title).unwrap().secret = Some(true);    let said = said(&definition, json!({"title": "hunter2"}));    assert!(said["slip"].get("name").is_none());    assert_ne!(said["slip"]["spoken"], "hunter2");}/// RULE: a cover's foot is said part by part, and says nothing that only/// repeats its title.#[test]fn a_cover_foot_that_repeats_the_title_says_nothing() {    let definition = todo();    let said = said(&definition, json!({"title": "Same", "@slip_subtitle": "Same"}));    assert!(said["slip"].get("cover_footer").is_none());    let parted = self::said(&definition, json!({"title": "A book", "@slip_subtitle": "PDF · 9 Oct"}));    assert_eq!(parted["slip"]["cover_footer"], json!(["PDF", "9 Oct"]));}/// RULE: a dated record with no title is headed by its day, and its foot/// says the date under the weekday rather than the weekday again.#[test]fn an_untitled_dated_record_is_headed_by_its_day() {    let said = said(&todo(), json!({"due_at": "2026-10-01", "@day_parts": {"due_at": {"weekday_name": "Thursday", "day_long": "1 October"}}}));    assert_eq!(said["slip"]["title"], "Thursday");    assert_eq!(said["slip"]["weekday"], "Thursday");    assert_eq!(said["slip"]["day_foot"], "1 October");}/// RULE: a record with its own title is never headed by its day.#[test]fn a_titled_dated_record_is_not_headed_by_its_day() {    let said = said(&todo(), json!({"title": "Call the plumber", "due_at": "2026-10-01", "@day_parts": {"due_at": {"weekday_name": "Thursday", "day_long": "1 October"}}}));    assert_eq!(said["slip"]["title"], "Call the plumber");    assert!(said["slip"].get("weekday").is_none());}/// RULE: away from a list a slip says its trailing column, in the core's words.#[test]fn a_slip_says_its_trailing_column() {    let said = said(&todo(), json!({"title": "Pay the rent", "due_at": "2026-10-01", "@column_words": {"due_at": "1 Oct"}}));    assert_eq!(said["slip"]["trailing"], "1 Oct");}/// RULE: an archived record does not stand on its table's own list.#[test]fn an_archived_record_is_not_listed() {    let mut definition = todo();    definition.archived = Some("trashed_at".to_string());    assert_eq!(said(&definition, json!({"title": "Put away", "trashed_at": "2026-10-01"}))["listed"], false);    assert_eq!(said(&definition, json!({"title": "Still here"}))["listed"], true);}/// RULE: a `slip_by` table says the record's own variant's words.#[test]fn a_typed_slip_says_its_variant() {    let mut definition = todo();    definition.slip_by = Some(serde_json::from_value(json!({"column": "status", "slips": {"done": {"title": "notes"}}})).unwrap());    let done = said(&definition, json!({"title": "Plant the bulbs", "notes": "All forty in", "status": "done"}));    assert_eq!(done["slip"]["title"], "All forty in");    let open = said(&definition, json!({"title": "Plant the bulbs", "notes": "All forty in", "status": "open"}));    assert_eq!(open["slip"]["title"], "Plant the bulbs");}/// RULE: a list narrowed to one link's records does not say that link on/// each slip: a store's orders do not each say the store's name.#[test]fn a_list_under_its_link_does_not_say_the_link() {    let mut definition = todo();    definition.slip.trailing = Some("project".to_string());    let plain = said(&definition, json!({"title": "Book the hall", "project": "Summer fair"}));    assert_eq!(plain["slip"]["trailing"], "Summer fair");    let narrowed = without_links(&definition, &["project".to_string()]);    let reading = Reading::new(&narrowed, "UTC", now(), false, Words::shipped()).narrowed();    let answer = said_of(&reading, &json!({"id": "t1", "data": {"title": "Book the hall", "project": "Summer fair"}}));    assert!(answer["slip"].get("trailing").is_none());}/// RULE: a slip typed by a choice answers the layout its variant draws;/// a table with no variants answers none, and its own slip stands.#[test]fn a_typed_slip_answers_its_layout() {    let mut definition = todo();    assert!(said(&definition, json!({"title": "Plain", "status": "done"})).get("layout").is_none());    definition.slip_by = Some(serde_json::from_value(json!({"column": "status", "slips": {"done": {"title": "notes"}}})).unwrap());    let said = said(&definition, json!({"title": "Typed", "status": "done"}));    assert_eq!(said["layout"]["title"], "notes");}/// RULE: a record names the columns whose value its `red_when` holds, and/// says nothing where none does.#[test]fn a_red_value_is_named_by_its_column() {    let mut definition = todo();    let at = definition.columns.iter().position(|column| column.key == "title").unwrap();    definition.columns[at].red_when = Some(vec!["Overdue".to_string()]);    assert_eq!(said(&definition, json!({"title": "Overdue"}))["red"], json!(["title"]));    assert!(said(&definition, json!({"title": "Fine"})).get("red").is_none());}/// RULE: units read only the row's own text words, never `{n}` and never a secret.#[test]fn unit_words_are_the_rows_own_text() {    let mut definition = todo();    let at = definition.columns.iter().position(|column| column.key == "title").unwrap();    definition.columns[at].units = serde_json::from_value(json!({"one": "{n} {title}", "other": "{n} {title}"})).unwrap();    let answer = said(&definition, json!({"title": "  ferns "}));    assert_eq!(answer["unit_words"], json!({"title": "ferns"}));    definition.columns[at].secret = Some(true);    assert!(said(&definition, json!({"title": "ferns"})).get("unit_words").is_none());}