Cotton code
110 lines · 3.4 KB · RustRaw
//! The vocabulary is a declaration, so these hold the PROPERTIES a declaration//! has to have — one spelling per word, no duplicates, alphabetical — rather//! than pinning the list, which would make every future word a two-file edit//! for no reader's benefit.use super::*;#[test]fn every_word_is_one_lowercase_english_token() {    for word in EMOTIONS {        assert!(!word.is_empty(), "an empty slug");        assert!(            word.chars()                .all(|character| character.is_ascii_lowercase()),            "`{word}` is not one lowercase ASCII word — the slug IS the display word, and a \             surface that lowercased it would stop matching the declaration"        );    }}#[test]fn the_list_is_alphabetical_and_holds_no_word_twice() {    let mut sorted: Vec<&str> = EMOTIONS.to_vec();    sorted.sort_unstable();    assert_eq!(        EMOTIONS.to_vec(),        sorted,        "the vocabulary is alphabetical on purpose: a declared order running from bad to good \         would rebuild the 1-to-5 scale under another name"    );    sorted.dedup();    assert_eq!(sorted.len(), EMOTIONS.len(), "a word is declared twice");}/// The migration converts a stored score into one of these five, so a build/// that dropped one would leave a converted row carrying a word nothing else/// recognises.#[test]fn the_five_words_the_faces_used_are_still_in_the_vocabulary() {    for word in ["awful", "low", "okay", "good", "great"] {        assert!(is_emotion(word), "`{word}` — migration 18 writes it");    }}#[test]fn a_word_outside_the_set_is_not_an_emotion() {    for word in ["anxiety", "Anxious", "meh", "", "tired!"] {        assert!(!is_emotion(word), "`{word}`");    }}#[test]fn normalise_trims_and_lowercases_and_answers_the_declarations_own_bytes() {    assert_eq!(normalise("  Anxious "), Some("anxious"));    assert_eq!(normalise("TIRED"), Some("tired"));    assert_eq!(normalise("anxiety"), None);    assert_eq!(normalise(""), None);}#[test]fn reads_a_stored_array_back_as_declared_words() {    assert_eq!(        parse_emotions(r#"["tired","hopeful"]"#),        vec!["tired".to_string(), "hopeful".to_string()]    );    assert_eq!(parse_emotions("[]"), Vec::<String>::new());}/// A word nothing else can match is worse than no word: no chip highlights it,/// no filter finds it and no tally counts it.#[test]fn drops_a_stored_word_that_is_not_in_the_vocabulary() {    assert_eq!(        parse_emotions(r#"["tired","anxiety",7,null]"#),        vec!["tired".to_string()]    );}#[test]fn keeps_a_word_once_however_many_times_it_was_stored() {    assert_eq!(        parse_emotions(r#"["tired","Tired","tired"]"#),        vec!["tired".to_string()]    );}#[test]fn a_column_that_is_not_an_array_reads_as_no_emotions_rather_than_throwing() {    for stored in ["", "null", "{}", "not json", "\"tired\""] {        assert_eq!(parse_emotions(stored), Vec::<String>::new(), "{stored:?}");    }}#[test]fn encodes_the_array_a_row_stores_and_says_empty_as_empty() {    assert_eq!(        encode_emotions(&["tired".to_string(), "hopeful".to_string()]),        r#"["tired","hopeful"]"#    );    assert_eq!(encode_emotions(&[]), "[]");}#[test]fn renders_the_words_the_way_a_person_reads_them() {    assert_eq!(        render_emotions(&["tired".to_string(), "hopeful".to_string()]),        "tired, hopeful"    );    assert_eq!(render_emotions(&[]), "");}