Cotton code
50 lines · 2.5 KB · RustRaw
use super::*;use serde_json::json;fn song(id: &str, title: &str, seconds: f64, photo: Option<&str>) -> Member {    Member {        record: id.into(), title: Some(title.into()), subtitle: Some("The Night Ferries".into()),        photo: photo.map(str::to_string), seconds, play: Some(format!("media:{id}")),    }}// RULE: a list's card holds its songs in the order given, each its record,// words, length and recording; counts them and adds their lengths as a clock.#[test]fn a_lists_card_holds_its_songs_in_order_counted_and_timed() {    let members = [song("s2", "Slow Train", 302.0, None), song("s1", "Harbour Lights", 221.0, Some("photo:cover1"))];    let card = places("Night Drives", None, &members, &[]);    assert_eq!(card["title"], "Night Drives");    let songs = card["songs"].as_array().unwrap();    assert_eq!(songs.iter().map(|song| song["record"].as_str().unwrap()).collect::<Vec<_>>(), ["s2", "s1"]);    assert_eq!(songs[0], json!({"record": "s2", "title": "Slow Train", "subtitle": "The Night Ferries", "duration": 302.0, "play": "media:s2"}));    assert_eq!(card["count"], "2 songs");    assert_eq!(card["length"], "8:43");}// RULE: the card's picture is the list's own, else the first member's that// has one; a list of none and no picture has none.#[test]fn a_lists_photo_is_its_own_else_its_first_covered_song() {    let members = [song("s1", "Harbour Lights", 221.0, None), song("s2", "Slow Train", 302.0, Some("photo:cover2"))];    assert_eq!(places("Night Drives", Some("photo:own"), &members, &[])["photo"], "photo:own");    assert_eq!(places("Night Drives", Some("  "), &members, &[])["photo"], "photo:cover2");    assert!(places("Night Drives", None, &[song("s1", "Harbour Lights", 221.0, None)], &[]).get("photo").is_none());}// RULE: one song is "1 song"; an empty list says "0 songs" and no length; a// song with no recording is unavailable; an hour and more is said in hours.#[test]fn a_lists_count_length_and_unplayable_songs() {    let mut lone = song("s1", "Harbour Lights", 4360.0, None);    lone.play = None;    let card = places("Long", None, &[lone], &[]);    assert_eq!(card["count"], "1 song");    assert_eq!(card["length"], "1:12:40");    assert_eq!(card["songs"][0]["unavailable"], true);    assert!(card["songs"][0].get("play").is_none());    let empty = places("For the new flat", None, &[], &[]);    assert_eq!(empty["count"], "0 songs");    assert!(empty.get("length").is_none());    assert_eq!(empty["songs"], json!([]));}