use super::super::run;
use crate::cli::testkit::fixture;
use crate::render::Format;
fn report(name: &str, ctrm: &str, files: &[(&str, &str)]) -> String {
let mut tree = vec![(".ctrm", ctrm)];
tree.extend_from_slice(files);
let Some(root) = fixture(name, &tree) else {
return String::from("(fixture not written)");
};
let paths: Vec<String> = files
.iter()
.filter(|(path, _)| !path.starts_with('.'))
.map(|(path, _)| String::from(*path))
.collect();
let config = crate::cli::config::discovered(&root);
let found = run(&config, &paths, Format::Human);
found.map(|r| r.text).unwrap_or_else(|why| why)
}
#[test]
fn a_line_lowering_a_hazard_is_refused_at_its_origin() {
let lowering = [
["--rule", "* any !hazard=allow"],
["--rule", "* any !invisible=warn"],
["--rule", "* any !bidi-control=deny"],
["--set", "hazard-invisible U+0041"],
];
let here = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
for [flag, value] in lowering {
let words = crate::cli::testkit::argv(&["check", flag, value]);
let config = crate::cli::config::from_argv(here, &words);
let why = config.and_then(|c| c.validate()).err().unwrap_or_default();
assert!(why.contains("argv[3]"), "{value}: {why}");
}
}
#[test]
fn no_configuration_talks_a_hazard_down() {
let files = [
(".ctrm", "* any !hazard=forbid !allow\n"),
("smuggled.txt", "fine\u{200B}\n"),
];
let Some(root) = fixture("ctrm-hazard-forbid-fixture", &files) else {
return;
};
let paths = [String::from("smuggled.txt")];
let config = crate::cli::config::discovered(&root);
let report = run(&config, &paths, Format::Json);
let report = report.unwrap_or_else(|why| unreachable!("{why}"));
assert_eq!(report.code, 1, "{}", report.text);
assert!(report.text.contains("\"invisible\""), "{}", report.text);
assert!(report.text.contains("\"forbid\""), "{}", report.text);
}
#[test]
fn a_script_preset_excuses_its_own_joiner_and_any_does_not() {
let word =
"\u{0645}\u{06CC}\u{200C}\u{062E}\u{0648}\u{0627}\u{0647}\u{0645}\n";
let files = [("fa.md", word)];
let fa = report("ctrm-joiner-fa", "* ascii+persian\n", &files);
assert_eq!(fa, "");
let any = report("ctrm-joiner-any", "* any\n", &files);
assert!(any.contains("U+200C"), "{any}");
}
#[test]
fn a_script_preset_excuses_nothing_but_its_joiners() {
let bidi = [("fa.md", "\u{0645}\u{202E}\u{0645}\n")];
let fired = report("ctrm-joiner-bidi", "* ascii+persian\n", &bidi);
assert!(fired.contains("U+202E"), "{fired}");
let conjunct = [("hi.md", "\u{0915}\u{094D}\u{200D}\u{0937}\n")];
let hi = report("ctrm-joiner-hi", "* ascii+hindi\n", &conjunct);
assert_eq!(hi, "");
let fa = report("ctrm-joiner-zwj", "* ascii+persian\n", &conjunct);
assert!(fa.contains("U+200D"), "{fa}");
}
#[test]
fn a_redeclared_preset_excuses_no_joiner() {
let sets = (".ctrm-sets", "hindi ascii U+200C U+200D\n");
let files = [sets, ("a.md", "a\u{200C}b\n")];
let got = report("ctrm-joiner-redeclared", "* hindi\n", &files);
assert!(got.contains("U+200C"), "{got}");
}
#[test]
fn a_batch_file_needs_crlf_and_says_so_per_path() {
let files = [("build.bat", "@echo off\r\n"), ("notes.txt", "hi\r\n")];
let base = "* ascii+cr !pedantic=warn\n";
let on = report("ctrm-crlf-on", base, &files);
let both =
"build.bat:1:10 U+000D crlf (warn)\nnotes.txt:1:3 U+000D crlf (warn)";
assert_eq!(on, both);
let ctrm = format!("{base}*.bat !crlf=allow\n");
let off = report("ctrm-crlf-off", &ctrm, &files);
assert_eq!(off, "notes.txt:1:3 U+000D crlf (warn)");
}
#[test]
fn a_markdown_hard_break_is_trailing_whitespace_on_purpose() {
let files = [("a.md", "one \ntwo\n"), ("a.rs", "fn f() {} \n")];
let base = "* ascii !pedantic=warn\n";
let on = report("ctrm-trailing-on", base, &files);
let rs = "a.rs:1:10 U+0020 trailing-whitespace (warn)";
assert_eq!(
on,
format!("a.md:1:4 U+0020 trailing-whitespace (warn)\n{rs}")
);
let ctrm = format!("{base}*.md ascii !trailing-whitespace=allow\n");
assert_eq!(report("ctrm-trailing-off", &ctrm, &files), rs);
}
#[test]
fn a_byte_exact_golden_file_ends_without_a_newline() {
let golden = ("want.out", "a.rs:1:1 U+2014 ascii");
let files = [golden, ("lib.rs", "fn f() {}"), ("empty.txt", "")];
let base = "* ascii !pedantic=warn\n";
let on = report("ctrm-final-on", base, &files);
let rs = "lib.rs:1:9 U+007D final-newline (warn)";
assert_eq!(
on,
format!("{rs}\nwant.out:1:21 U+0069 final-newline (warn)")
);
let ctrm = format!("{base}*.out ascii !final-newline=allow\n");
assert_eq!(report("ctrm-final-off", &ctrm, &files), rs);
}
#[test]
fn a_character_two_walks_point_at_is_claimed_once() {
let trailing = [("f.txt", "a ")];
let got = report("ctrm-claim-trail", "* ascii !pedantic=warn\n", &trailing);
assert_eq!(got, "f.txt:1:2 U+0020 trailing-whitespace (warn)");
let mixed = [("f.txt", "a\u{03BB}")];
let got = report("ctrm-claim-mixed", "* any !pedantic=warn\n", &mixed);
assert_eq!(got, "f.txt:1:2 U+03BB final-newline (warn)");
}
#[test]
fn french_spacing_is_a_unicode_space_on_purpose() {
let fr = ("fr.md", "Prix\u{a0}: 5\nOui\u{202f}!\n");
let sets = (".ctrm-sets", "french U+00A0 U+202F\n");
let files = [sets, fr, ("a.md", "a\u{a0}b\n")];
let base = "* ascii+french !pedantic=warn\n";
let one = "a.md:1:2 U+00A0 unicode-space (warn)";
let on = report("ctrm-space-on", base, &files);
assert!(
on.starts_with(one) && on.contains("fr.md:2:4 U+202F"),
"{on}"
);
let ctrm = format!("{base}fr.md ascii+french !unicode-space=allow\n");
assert_eq!(report("ctrm-space-off", &ctrm, &files), one);
}
fn exempted(name: &str, files: &[(&str, &str)], exempt: &str) -> [String; 2] {
let base = "* any !pedantic=warn\n";
let on = report(&format!("{name}-on"), base, files);
let ctrm = format!("{base}{exempt}\n");
[on, report(&format!("{name}-off"), &ctrm, files)]
}
#[test]
fn a_macos_listing_is_decomposed_on_purpose() {
let files = [("ls.out", "cafe\u{301}.txt\n"), ("a.md", "cafe\u{301}\n")];
let [on, off] = exempted("ctrm-nfc", &files, "*.out !not-nfc=allow");
let md = "a.md:1:4 U+0065 not-nfc (warn)";
assert_eq!(on, format!("{md}\nls.out:1:4 U+0065 not-nfc (warn)"));
assert_eq!(off, md);
}
#[test]
fn a_unit_superscript_is_compatibility_on_purpose() {
let files = [("area.md", "50 m\u{b2}\n"), ("a.md", "x\u{fb01}\n")];
let exempt = "area.md !nfkc-compat=allow";
let [on, off] = exempted("ctrm-nfkc", &files, exempt);
let md = "a.md:1:2 U+FB01 nfkc-compat (warn)";
assert_eq!(on, format!("{md}\narea.md:1:5 U+00B2 nfkc-compat (warn)"));
assert_eq!(off, md);
}
#[test]
fn a_micrometre_mixes_scripts_on_purpose() {
let files = [("lab.md", "5 \u{3bc}m\n"), ("a.md", "p\u{3bb}y\n")];
let exempt = "lab.md !mixed-script=allow";
let [on, off] = exempted("ctrm-mixed", &files, exempt);
let md = "a.md:1:2 U+03BB mixed-script (warn)";
assert_eq!(on, format!("{md}\nlab.md:1:4 U+006D mixed-script (warn)"));
assert_eq!(off, md);
}
#[test]
fn russian_prose_is_confusable_on_purpose() {
let mir = "\u{43c}\u{438}\u{440}\n";
let files = [("ru.md", mir), ("a.md", "\u{440}\n")];
let exempt = "ru.md !confusable=allow";
let [on, off] = exempted("ctrm-confusable", &files, exempt);
let md = "a.md:1:1 U+0440 confusable (warn)";
assert_eq!(on, format!("{md}\nru.md:1:3 U+0440 confusable (warn)"));
assert_eq!(off, md);
}
fn granted_run(path: &str) -> (u8, String) {
let files = [
(".ctrm", "granted.txt ascii+hazard:tags\n"),
("granted.txt", "x\u{E0041}\n"),
("other.txt", "x\u{E0041}\n"),
("mixed.txt", "\u{E0041}\u{202E}\n"),
];
let Some(root) = fixture("ctrm-hazard-granted", &files) else {
return (9, String::new());
};
let config = crate::cli::config::discovered(&root);
let report = run(&config, &[String::from(path)], Format::Json);
let report = report.unwrap_or_else(|why| unreachable!("{why}"));
(report.code, report.text)
}
#[test]
fn a_named_path_may_hold_tag_characters_as_notes() {
let (code, text) = granted_run("granted.txt");
assert_eq!(code, 0, "{text}");
assert!(text.contains("\"tag-character\""), "{text}");
assert!(text.contains("\"warn\""), "{text}");
}
#[test]
fn the_same_tags_elsewhere_still_fail() {
let (code, text) = granted_run("other.txt");
assert_eq!(code, 1, "{text}");
assert!(text.contains("\"forbid\""), "{text}");
}
#[test]
fn another_hazard_beside_the_grant_still_fails() {
let (code, text) = granted_run("mixed.txt");
assert_eq!(code, 1, "{text}");
assert!(text.contains("\"bidi-control\""), "{text}");
}
#[test]
fn a_broad_or_wrong_hazard_grant_is_refused_at_its_origin() {
let lines = ["* ascii+hazard:tags", "a/b ascii+hazard:bidi"];
let here = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
for line in lines {
let words = crate::cli::testkit::argv(&["check", "--rule", line]);
let config = crate::cli::config::from_argv(here, &words);
let why = config.and_then(|c| c.validate()).err().unwrap_or_default();
assert!(why.contains("argv[3]"), "{line}: {why}");
}
}