use flowmark::ListSpacing;
use flowmark::formatter::filling::fill_markdown;
use std::collections::BTreeSet;
const FRAGMENTS: &[&str] = &[
"$",
"$$",
"`",
"``",
"```",
"\\(",
"\\)",
"\\[",
"\\]",
"\\begin{align}",
"\\end{align}",
"|",
":::",
"+++",
"---",
"> ",
"- ",
"1. ",
" ",
"\n",
"\n\n",
"x",
" ",
"[!NOTE]",
"[!WARNING]",
"{#id}",
"[[w]]",
"{r}`c`",
"<div>",
"</div>",
"<v>",
": def",
"Term",
"+---+",
"+===+",
"~~~",
"\t",
"\\$",
"\\`",
"\\\\",
"é",
"漢",
"\u{feff}",
"\u{f0000}",
"\u{f0001}",
"\u{f0002}",
];
struct Lcg(u64);
impl Lcg {
fn next(&mut self) -> u64 {
self.0 =
self.0.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407);
self.0
}
fn below(&mut self, bound: usize) -> usize {
usize::try_from(self.next() >> 33).expect("32-bit value fits usize") % bound
}
}
fn generate(seed: u64, max_fragments: usize) -> String {
let mut rng = Lcg(seed);
let count = rng.below(max_fragments) + 1;
(0..count).map(|_| FRAGMENTS[rng.below(FRAGMENTS.len())]).collect()
}
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Copy)]
struct Mode {
name: &'static str,
width: usize,
semantic: bool,
cleanups: bool,
smartquotes: bool,
ellipses: bool,
}
const MODES: &[Mode] = &[
Mode {
name: "default",
width: 88,
semantic: false,
cleanups: false,
smartquotes: false,
ellipses: false,
},
Mode {
name: "semantic",
width: 88,
semantic: true,
cleanups: false,
smartquotes: false,
ellipses: false,
},
Mode {
name: "cleanups",
width: 88,
semantic: false,
cleanups: true,
smartquotes: false,
ellipses: false,
},
Mode {
name: "typography",
width: 88,
semantic: true,
cleanups: true,
smartquotes: true,
ellipses: true,
},
Mode {
name: "nowrap",
width: 0,
semantic: false,
cleanups: false,
smartquotes: false,
ellipses: false,
},
];
fn format_at_width(width: usize, input: &str) -> String {
fill_markdown(input, false, width, false, false, false, false, None, ListSpacing::Preserve)
}
fn format_with(mode: Mode, input: &str) -> String {
fill_markdown(
input,
false,
mode.width,
mode.semantic,
mode.cleanups,
mode.smartquotes,
mode.ellipses,
None,
ListSpacing::Preserve,
)
}
#[test]
fn generated_documents_never_abort() {
const CASES: u64 = 2_000;
for seed in 0..CASES {
let input = generate(seed, 24);
for mode in MODES {
let once = format_with(*mode, &input);
let _ = format_with(*mode, &once);
}
}
}
#[test]
fn generated_documents_satisfy_the_output_normalization_contract() {
const CASES: u64 = 2_000;
for seed in 0..CASES {
let input = generate(seed, 24);
for mode in MODES {
let output = format_with(*mode, &input);
assert!(
output.ends_with('\n') && !output.ends_with("\n\n"),
"seed {seed} mode {} must end with exactly one LF: {output:?}",
mode.name
);
assert!(
!output.contains('\r'),
"seed {seed} mode {} must not emit CR: {output:?}",
mode.name
);
}
}
}
#[test]
fn generated_documents_match_the_known_fixed_point_ledger() {
const CASES: u64 = 2_000;
const KNOWN_FAILURE_SEEDS: &[u64] = &[610, 702, 1_316, 1_688];
let expected: BTreeSet<(u64, &str)> = KNOWN_FAILURE_SEEDS
.iter()
.flat_map(|seed| MODES.iter().map(move |mode| (*seed, mode.name)))
.collect();
let mut actual = BTreeSet::new();
let mut diagnostics = Vec::new();
for seed in 0..CASES {
let input = generate(seed, 24);
for mode in MODES {
let once = format_with(*mode, &input);
let twice = format_with(*mode, &once);
if once != twice {
actual.insert((seed, mode.name));
diagnostics.push(format!(
"seed {seed} mode {}\n input: {input:?}\n once: {once:?}\n twice: {twice:?}",
mode.name
));
}
}
}
assert_eq!(
actual,
expected,
"generated fixed-point ledger changed across {} checks:\n{}",
CASES * MODES.len() as u64,
diagnostics.join("\n")
);
}
#[test]
fn reviewed_regression_shapes_stay_fixed() {
let shapes = [
"> [!NOTE]<v>",
"> [!NOTE]`x`",
"> [!NOTE]$x$",
"> [!NOTE][[w]]",
"> [!WARNING]<b>t</b>",
"```\\$`$",
"```\\`",
"```a\\`b",
"```\n ",
];
for shape in shapes {
for mode in MODES {
let once = format_with(*mode, shape);
let twice = format_with(*mode, &once);
assert_eq!(once, twice, "{shape:?} in mode {} must be a fixed point", mode.name);
}
}
}
#[test]
fn synthetic_block_boundaries_leave_list_spacing_as_authored() {
let tight = concat!(
"- Bead: fmr-hr43 | Scope: Phase 8.5/8.6 | Repo: playbook\n",
"- Depends on: WI-1, WI-4\n",
"- Findings: F1-F12 (12 lessons + anti-patterns)\n",
);
let wrapped = format_at_width(40, tight);
assert_eq!(
wrapped,
concat!(
"- Bead: fmr-hr43 | Scope: Phase 8.5/8.6\n",
" | Repo: playbook\n",
"- Depends on: WI-1, WI-4\n",
"- Findings: F1-F12 (12 lessons +\n",
" anti-patterns)\n",
),
"wrapping must not loosen a tight list around a protected line block"
);
assert_eq!(format_at_width(40, &wrapped), wrapped, "the wrapped form must be a fixed point");
let loose = concat!(
"- Bead: fmr-hr43 | Scope: Phase 8.5/8.6\n",
" | Repo: playbook\n",
"\n",
"- Depends on: WI-1, WI-4\n",
"\n",
"- Findings: F1-F12\n",
);
assert_eq!(format_at_width(40, loose), loose, "an authored loose list must stay loose");
}
#[test]
fn escaped_backslash_in_fence_info_string_is_a_known_shared_divergence() {
let once = format_with(MODES[0], "~~~\\\\[");
let twice = format_with(MODES[0], &once);
assert_eq!(once, "~~~\\[\n~~~\n", "first pass changed; re-check fmr-c6xs");
assert_eq!(twice, "~~~[\n~~~\n", "second pass changed; re-check fmr-c6xs");
assert_ne!(once, twice, "fmr-c6xs looks fixed: promote this to a fixed-point assertion");
}
#[test]
fn interior_bom_with_leading_whitespace_is_a_known_shared_divergence() {
let once = format_with(MODES[0], " \u{feff}\t\\(");
let twice = format_with(MODES[0], &once);
assert_eq!(once, " \\(\n", "first pass changed; re-check fmr-uao3");
assert_eq!(twice, "\\(\n", "second pass changed; re-check fmr-uao3");
assert_ne!(once, twice, "fmr-uao3 looks fixed: promote this to a fixed-point assertion");
}