Cotton code
90 lines · 5.5 KB · RustRaw
use super::*;use serde_json::json;const HASH: &str = "aa30ee64d799b856e6219185544162815159ad9493bfb3e181c24898c805d0ec";fn photo() -> Value {    json!({"photo": format!("photo:{HASH}")})}// RULE: a fan tile reads as stock, type and pictograms; the type is courier where it says none, and courier is never written.#[test]fn a_fan_tile_reads_and_writes_as_data() {    let tile: FanTile = serde_json::from_value(json!({"stock": photo(), "type": "letterboard", "pictures": {"list": photo()},        "views": {"films": photo()}, "acts": {"play": photo()}, "doors": {"files": photo()}})).unwrap();    assert_eq!(tile.kind, TileType::Letterboard);    assert_eq!(tile.view("films"), Some(format!("photo:{HASH}").as_str()));    assert_eq!(tile.act("play"), tile.door("files"));    assert_eq!(tile.role("list"), tile.view("films"));    assert_eq!(tile.role("more"), None);    assert_eq!(tile.photos().len(), 1, "a picture named twice is one picture");    assert_eq!(tile.look(), json!({"stock": format!("photo:{HASH}"), "type": "letterboard", "pictures": {"list": format!("photo:{HASH}")}}));    let plain: FanTile = serde_json::from_value(json!({"stock": photo()})).unwrap();    assert_eq!(plain.kind, TileType::Courier);    assert!(!serde_json::to_value(&plain).unwrap().as_object().unwrap().contains_key("type"));    assert!(serde_json::from_value::<FanTile>(json!({"type": "neon"})).is_err(), "a type is one of the closed list");    let newer: FanTile = serde_json::from_value(json!({"stock": photo(), "colour": "red"})).unwrap();    assert_eq!(newer, plain, "a READ passes over a key a newer build wrote (tolerant); authoring refuses it below");    assert!(super::super::tolerant::authored::<FanTile>(&json!({"colour": "red"})).is_err(), "an AUTHORED tile is refused for a key this build does not name");}// RULE: every picture on a fan tile is a well-formed kit picture; its roles are the shell's closed list; its views are the tool's own.#[test]fn a_fan_tile_validates_its_pictures_roles_and_views() {    let tile = |value: Value| -> FanTile { serde_json::from_value(value).unwrap() };    assert!(tile(json!({"stock": photo(), "pictures": {"list": photo(), "new_list": photo(), "more": photo()}, "views": {"films": photo()}})).validate(&["films"]).is_ok());    for bad in [        json!({"stock": {"photo": "photo:abc"}}),        json!({"stock": {"photo": format!("photo:{}", HASH.to_uppercase())}}),        json!({"pictures": {"watchlist": photo()}}),        json!({"views": {"continue": photo()}}),        json!({"acts": {"Play!": photo()}}),        json!({"doors": {"files": {"photo": "pictures/files.png"}}}),    ] {        assert!(tile(bad.clone()).validate(&["films"]).is_err(), "{bad}");    }    let named = tile(json!({"acts": {"play": photo(), "group": photo(), "list_set_picture": photo()}, "doors": {"files": photo()}}));    assert!(named.validate_names(&["play"], &["files"]).is_ok());    assert!(named.validate_names(&[], &["files"]).is_err(), "play is no act of the tool");    assert!(named.validate_names(&["play"], &[]).is_err(), "files is no door of its tables");}// RULE: the shell's own acts a tile may picture are the choosers a held card offers and a list's own acts, by the ids the core answers them with.#[test]fn the_shell_acts_are_the_cores_own_ids() {    for chooser in ["when", "group", "repeat", "status", "face", "bin"] {        assert!(SHELL_ACTS.contains(&chooser), "{chooser}");    }}// RULE: a tile's words are data too: their ink and baselines read and write as the kit gives them, every key optional, and the look answers them only where the tile names them.#[test]fn a_fan_tile_s_words_are_its_ink_and_baselines() {    let words = json!({"ink": "#211a12", "name_baseline": 500, "count_baseline_one": 586, "count_baseline_two": 644});    let tile: FanTile = serde_json::from_value(json!({"stock": photo(), "type": "letterboard", "words": words})).unwrap();    assert_eq!(tile.words.as_ref().and_then(|words| words.ink.as_deref()), Some("#211a12"));    assert_eq!(tile.look()["words"], words, "the look answers the words as given");    assert_eq!(serde_json::to_value(&tile).unwrap()["words"], words, "and the kit writes them back as read");    assert!(tile.validate(&[]).is_ok());    let inked: FanTile = serde_json::from_value(json!({"stock": photo(), "words": {"ink": "#18243d"}})).unwrap();    assert_eq!(inked.look()["words"], json!({"ink": "#18243d"}), "a key left out is the type's own, and is not written");    let plain: FanTile = serde_json::from_value(json!({"stock": photo(), "type": "letterboard"})).unwrap();    assert!(plain.look().get("words").is_none(), "a tile naming no words answers as it did");    let tile = |words: Value| -> FanTile { serde_json::from_value(json!({"words": words})).unwrap() };    for bad in [        json!({"ink": "cream"}),        json!({"ink": "#21a12"}),        json!({"ink": "#21a12g"}),        json!({"name_baseline": 0}),        json!({"name_baseline": 700}),        json!({"name_baseline": 600, "count_baseline_one": 586}),        json!({"count_baseline_one": 644, "count_baseline_two": 586}),    ] {        assert!(tile(bad.clone()).validate(&[]).is_err(), "{bad}");    }    let newer: FanTile = serde_json::from_value(json!({"words": {"ink": "#211a12", "colour": "#211a12"}})).unwrap();    assert_eq!(newer.words.as_ref().and_then(|words| words.ink.as_deref()), Some("#211a12"), "a read keeps the words it knows and passes over the rest");    assert!(super::super::tolerant::authored::<FanTile>(&json!({"words": {"colour": "#211a12"}})).is_err(), "an authored key the words do not name is refused");}