use mdtablefix::wrap::wrap_text;
use rstest::rstest;
fn assert_date_snapshot(name: &str, input: &[String], width: usize) {
insta::with_settings!(
{
snapshot_path => "../snapshots",
prepend_module_to_snapshot => false,
},
{ insta::assert_snapshot!(name, wrap_text(input, width).join("\n")) }
);
}
#[rstest]
#[case("25th December 2025")]
#[case("19 March 2018")]
#[case("July 4, 2008")]
#[case("25th Dec 2025")]
#[case("Jul 4, 2008")]
#[case("july 4, 2008")]
#[case("JUL 4, 2008")]
#[case("July 4 2008")]
#[case("March 19 2018")]
#[case("25th December 2025.")]
#[case("July 4, 2008)")]
#[case("July 4, 2008,")]
fn wrap_text_keeps_date_sequence_intact(#[case] expected_date: &str) {
let input = lines_vec![format!(
"This paragraph has enough preceding prose to make {expected_date} a tempting wrap point."
)];
let output = wrap_text(&input, 48);
assert!(output.iter().any(|line| line.contains(expected_date)));
}
#[test]
fn wrap_text_handles_date_wider_than_width() {
let input = lines_vec!["Remember 25th December 2025 when wrapping."];
let output = wrap_text(&input, 10);
assert!(!output.is_empty());
assert!(
output
.iter()
.any(|line| line.contains("25th December 2025")),
"over-width date should fall back to long-token emission: {output:?}"
);
let input_with_following_punct = lines_vec!["On 25th December 2025, we met."];
let output_with_following_punct = wrap_text(&input_with_following_punct, 10);
assert!(!output_with_following_punct.is_empty());
assert!(
output_with_following_punct
.iter()
.any(|line| line.contains("25th December 2025")),
"over-width date with following punctuation should stay atomic: \
{output_with_following_punct:?}"
);
let input_at_line_start = lines_vec!["25th December 2025. We met again."];
let output_at_line_start = wrap_text(&input_at_line_start, 10);
assert!(!output_at_line_start.is_empty());
assert!(
output_at_line_start
.iter()
.any(|line| line.contains("25th December 2025")),
"over-width date at line start should stay atomic: {output_at_line_start:?}"
);
}
#[test]
fn wrap_text_date_at_exact_boundary() {
let input = lines_vec!["Then 25th December 2025 follows."];
let output = wrap_text(&input, 23);
assert!(output.iter().any(|line| line == "Then 25th December 2025"));
}
#[test]
fn wrap_text_keeps_footnote_reference_with_punctuated_date() {
let input = lines_vec![concat!(
"This sentence has enough preceding prose to make July 4, 2008.[^1] ",
"a tempting wrap point.",
)];
let output = wrap_text(&input, 48);
assert!(
output.iter().any(|line| line.contains("July 4, 2008.[^1]")),
"punctuated date and adjacent footnote should stay atomic: {output:?}"
);
}
#[test]
fn wrap_text_keeps_parenthesised_month_day_year_intact() {
let input = lines_vec!["See (July 4, 2008) for details."];
let output = wrap_text(&input, 16);
assert!(
output.iter().any(|line| line.contains("(July 4, 2008)")),
"parenthesised date should stay atomic: {output:?}"
);
assert_eq!(output.join(" "), input[0]);
}
#[test]
fn wrap_text_keeps_quoted_ordinal_date_intact() {
let input = lines_vec!["Mark \"25th December 2025 as important."];
let output = wrap_text(&input, 22);
assert!(
output
.iter()
.any(|line| line.contains("\"25th December 2025")),
"quoted ordinal date should stay atomic: {output:?}"
);
assert_eq!(output.join(" "), input[0]);
}
#[test]
fn wrap_text_partial_date_not_grouped() {
let input = lines_vec!["Plan around December 2025 carefully."];
let output = wrap_text(&input, 21);
assert!(!output.iter().any(|line| line.contains("December 2025")));
assert_eq!(output.join(" "), input[0]);
}
#[rstest]
#[case(
"date_ordinal_day_month_year_wrap",
lines_vec!["Remember 25th December 2025 when wrapping prose near a boundary."],
28
)]
#[case(
"date_month_day_year_punctuation_wrap",
lines_vec!["The record cites July 4, 2008, before the follow-up note."],
24
)]
#[case(
"date_parenthesised_and_footnote_wrap",
lines_vec!["See (July 4, 2008).[^1] before editing the release note."],
24
)]
fn wrap_text_snapshots_date_sequence_outputs(
#[case] name: &str,
#[case] input: Vec<String>,
#[case] width: usize,
) {
assert_date_snapshot(name, &input, width);
}