Cotton code
109 lines · 3.9 KB · RustRaw
//! Recipe declarations and substitution behaviour; wording remains editable data.use super::{recipe_text, Recipe, RECIPES};#[test]fn names_are_unique_and_kebab_case() {    let pattern = regex::Regex::new(r"^[a-z][a-z0-9]*(-[a-z0-9]+)*$").expect("compiles");    let mut seen: Vec<&str> = Vec::new();    for recipe in RECIPES.iter() {        assert!(!seen.contains(&recipe.name), "duplicate {}", recipe.name);        seen.push(recipe.name);        assert!(pattern.is_match(recipe.name), "{}", recipe.name);    }}/// Every slot is declared, and every declared argument is used — an unused/// argument would advertise a knob that changes nothing.#[test]fn body_slots_and_declared_arguments_agree() {    let slot = regex::Regex::new(r"\{\{([a-z][a-z0-9-]*)\}\}").expect("compiles");    for recipe in RECIPES.iter() {        let mut slots: Vec<String> = slot            .captures_iter(recipe.body)            .map(|caps| caps[1].to_string())            .collect();        slots.sort();        slots.dedup();        let mut declared: Vec<String> = recipe            .arguments            .iter()            .map(|argument| argument.name.to_string())            .collect();        declared.sort();        declared.dedup();        assert_eq!(slots, declared, "{}", recipe.name);    }}fn by_name(name: &str) -> &'static Recipe {    RECIPES        .iter()        .find(|recipe| recipe.name == name)        .unwrap_or_else(|| panic!("no recipe called {name}"))}#[test]fn recipe_text_substitutes_a_given_argument() {    let text = recipe_text(by_name("harvest-project"), &[("project", "Meri Wedding")]);    assert!(text.contains("Meri Wedding"));    assert!(!text.contains("{{"));}#[test]fn recipe_text_falls_back_when_an_argument_is_absent_or_blank() {    for args in [        &[][..],        &[("project", "  "), ("person", "  ")][..],        &[("unrelated", "x")][..],    ] {        for recipe in RECIPES.iter() {            let text = recipe_text(recipe, args);            assert!(!text.contains("{{"), "{}", recipe.name);            for argument in recipe.arguments {                assert!(                    text.contains(argument.fallback),                    "{}.{}",                    recipe.name,                    argument.name                );            }        }    }}/// JavaScript's trim, not Rust's: a slot filled with a lone BOM is blank on/// both sides and falls back, and a lone NEL is a one-character value that/// wins. The two classes disagree on exactly these two characters, and the/// substitution is what a person would see.#[test]fn a_bom_only_argument_is_blank_and_a_nel_is_not() {    let recipe = by_name("harvest-project");    let bom = recipe_text(recipe, &[("project", "\u{feff}")]);    let fallback = recipe.arguments.iter().find(|argument| argument.name == "project").unwrap().fallback;    assert!(bom.contains(fallback));    let nel = recipe_text(recipe, &[("project", "\u{85}")]);    assert!(nel.contains("\u{85}"));    assert!(!nel.contains(fallback));}/// RULE: Recipe interpolation trims supplied values, uses declared fallbacks and ignores undeclared arguments, so prompt edits do not change substitution semantics.#[test]fn interpolation_obeys_arguments_without_pinning_prompt_words() {    let recipe = Recipe {        name: "synthetic",        title: "Synthetic",        description: "A parser fixture",        arguments: &[super::RecipeArgument { name: "value", description: "A value", fallback: "fallback" }],        body: "before {{value}} after {{value}}",    };    for (args, expected) in [        (vec![], "before fallback after fallback"),        (vec![("value", "")], "before fallback after fallback"),        (vec![("value", "  ")], "before fallback after fallback"),        (vec![("value", " padded ")], "before padded after padded"),        (vec![("unknown", "ignored")], "before fallback after fallback"),    ] {        assert_eq!(recipe_text(&recipe, &args), expected);    }}