use super::{Asked, sets};
use crate::cli::testkit::fixture;
use crate::judge::Config;
use crate::render::Format;
use std::path::Path;
const NONE: Asked<'static> = Asked {
names: &[],
locales: false,
containing: None,
};
fn shown(config: &Config, asked: &Asked<'_>) -> String {
sets(config, Format::Human, asked).unwrap_or_else(|why| why)
}
fn found(root: &Path) -> Config {
crate::cli::config::discovered(root)
}
fn argued(root: &Path, flags: &str) -> Config {
let words = std::iter::once("sets").chain(flags.split('|'));
let argv: Vec<String> = words.map(str::to_owned).collect();
crate::cli::config::from_argv(root, &argv).unwrap_or_default()
}
fn at(text: &str, needle: &str) -> usize {
text.find(needle).unwrap_or(usize::MAX)
}
#[test]
fn the_presets_lead_with_descriptions_and_the_locales_are_counted() {
let Some(root) = fixture("ctrm-sets-fixture", &[]) else {
return;
};
let said = shown(&found(&root), &NONE);
assert!(said.starts_with("ascii -- printable ASCII"), "{said}");
assert!(said.contains("caveman -- the logic and arrow"), "{said}");
assert!(said.contains("\n U+00A7 U+00AC"), "{said}");
assert!(said.contains(" CLDR locale sets: `ctrm sets --locales`"));
assert!(!said.contains("\nja -- "), "{said}");
}
#[test]
fn the_declared_sets_follow_the_presets() {
let files = [(".ctrm-sets", "house U+2261\n")];
let Some(root) = fixture("ctrm-sets-declared-fixture", &files) else {
return;
};
let said = shown(&found(&root), &NONE);
let house = "house -- declared by this repository\n U+2261";
assert!(said.contains(house), "{said}");
assert!(at(&said, "\nany -- ") < at(&said, "\nhouse -- "), "{said}");
}
#[test]
fn locales_list_only_when_asked_and_a_check_reads_only_the_named() {
let files = [(".ctrm", "*.md ascii+pl\n")];
let Some(root) = fixture("ctrm-sets-locale-fixture", &files) else {
return;
};
let config = found(&root);
let asked = Asked {
locales: true,
..NONE
};
let said = shown(&config, &asked);
assert!(said.contains("ja -- one locale's letters, from CLDR\n U+3005"));
assert!(said.contains("\npt-BR -- ") && !said.contains("caveman"));
let catalog = config.catalog().unwrap_or_default();
assert!(catalog.get("pl").is_some() && catalog.get("ja").is_none());
}
#[test]
fn a_locale_typography_set_says_it_is_punctuation() {
let Some(root) = fixture("ctrm-sets-punctuation-fixture", &[]) else {
return;
};
let config = found(&root);
let asked = naming(&["typography-pl", "pl"]);
let said = shown(&config, &asked);
assert!(said.starts_with("typography-pl -- one locale's punctuation"));
assert!(said.contains("\npl -- one locale's letters"), "{said}");
let json = sets(&config, Format::Json, &asked).unwrap_or_default();
assert!(json.contains(PUNCTUATION_JSON), "{json}");
assert!(json.contains(LETTERS_JSON), "{json}");
}
const PUNCTUATION_JSON: &str = concat!(
r#""name":"typography-pl","kind":"locale","#,
r#""description":"one locale's punctuation, from CLDR""#
);
const LETTERS_JSON: &str = r#""name":"pl","kind":"locale","description":null"#;
#[test]
fn names_filter_the_listing_and_an_unknown_one_is_refused() {
let Some(root) = fixture("ctrm-sets-named-fixture", &[]) else {
return;
};
let config = found(&root);
let said = shown(&config, &naming(&["pl", "typography"]));
assert!(said.starts_with("pl -- ") && said.contains("\ntypography -- "));
assert_eq!(said.lines().count(), 4, "{said}");
let typo = shown(&config, &naming(&["a.md"]));
assert_eq!(typo, "unknown set `a.md`");
}
fn naming(names: &[&str]) -> Asked<'static> {
let owned: Vec<String> = names.iter().map(|n| (*n).to_owned()).collect();
Asked {
names: Vec::leak(owned),
..NONE
}
}
#[test]
fn containing_lists_the_sets_holding_a_code_point() {
let Some(root) = fixture("ctrm-sets-containing-fixture", &[]) else {
return;
};
let ask = |what: &'static str| Asked {
containing: Some(what),
..NONE
};
let said = shown(&found(&root), &ask("U+22A5"));
assert!(said.starts_with("caveman -- "), "{said}");
assert!(said.contains("\nany -- ") && !said.contains("\nmath -- "));
assert!(!said.contains("CLDR locale sets"), "{said}");
assert_eq!(shown(&found(&root), &ask("\u{22A5}")), said);
let bad = shown(&found(&root), &ask("ab"));
assert!(bad.contains("one character"), "{bad}");
}
#[test]
fn the_json_carries_kind_description_and_the_locale_count() {
let Some(root) = fixture("ctrm-sets-json-fixture", &[]) else {
return;
};
let said = sets(&found(&root), Format::Json, &NONE).unwrap_or_default();
assert!(said.contains(r#""name":"caveman","kind":"preset","#));
assert!(said.contains(r#""unlisted_locales":2952"#), "{said}");
assert!(said.contains(r#""containing":null"#), "{said}");
}
#[test]
fn the_listing_resolves_at_the_family_the_rules_give() {
let Some(root) = fixture("ctrm-sets-fidelity-fixture", &[]) else {
return;
};
let text = shown(&found(&root), &NONE);
let emoji = shown(&argued(&root, "--fidelity|emoji"), &NONE);
assert!(
text.contains("U+2713") && emoji.contains("U+2705"),
"{emoji}"
);
assert_eq!(emoji, shown(&argued(&root, "--rule|* @emoji"), &NONE));
}