use f_office_paint::DisplayList;
use crate::layout::testing::{
NORMAL, du, filler, find, find_all, marked, on_base_with, p, pass_with, rects, table,
};
use crate::numbering::Numbering;
use crate::quirks::SpacedTableBreaks::{self, AroundPagePart, AroundTable};
const SPACED: &str = r#"<w:tblCellSpacing w:w="40" w:type="dxa"/><w:tblBorders><w:top w:val="single" w:sz="12"/><w:left w:val="single" w:sz="6"/><w:bottom w:val="single" w:sz="8"/><w:right w:val="single" w:sz="6"/><w:insideH w:val="single" w:sz="4"/><w:insideV w:val="single" w:sz="4"/></w:tblBorders>"#;
fn spaced_table(
(lead, after): (usize, i64),
(second, lines): (&str, usize),
breaks: SpacedTableBreaks,
) -> DisplayList {
let space = format!(r#"<w:spacing w:after="{after}"/>"#);
let body = filler(lead - 1) + &marked(&space, "q");
let long: String = (0..lines).map(|_| p("d")).collect();
let rows: &[(&str, &[&str])] = &[
("", &[&p("a"), &p("b")]),
(second, &[&p("c"), &long]),
("", &[&p("e"), &p("f")]),
];
let body = body + &table(SPACED, &[2000, 4000], rows) + &p("z");
let target = on_base_with(|profile| profile.with_variant(breaks));
pass_with(&body, (NORMAL, None), None, Numbering::default(), target).list
}
fn across(list: &DisplayList, page: usize) -> Vec<(i64, i64, i64, i64)> {
rects(list, page)
.into_iter()
.filter(|rect| rect.2 > du(5500))
.collect()
}
#[test]
fn opens_the_part_at_a_page_top_with_the_table_s_top_line_around_each_page_part() {
let whole = |breaks| spaced_table((58, 0), ("<w:cantSplit/>", 10), breaks);
let (page, table) = (whole(AroundPagePart), whole(AroundTable));
assert_eq!((find(&page, 'c').0, find(&table, 'c').0), (1, 1));
assert!((find(&page, 'c').2 - find(&table, 'c').2 - du(10)).abs() <= 1);
let tops = |list: &DisplayList| across(list, 1).iter().filter(|r| r.1 == du(1440)).count();
assert_eq!((tops(&page), tops(&table)), (1, 0));
assert_eq!(across(&page, 0).len(), across(&table, 0).len() + 1);
}
#[test]
fn ends_the_part_of_a_split_row_with_its_boxes_and_the_spacing_around_each_page_part() {
let split = |breaks| spaced_table((58, 0), ("", 20), breaks);
let (page, table) = (split(AroundPagePart), split(AroundTable));
assert_eq!((find(&page, 'e').0, find(&table, 'e').0), (1, 1));
assert!((find(&page, 'e').2 - find(&table, 'e').2 - du(60)).abs() <= 1);
assert_eq!(across(&page, 0).len(), across(&table, 0).len() + 1);
assert_eq!(across(&page, 1).len(), across(&table, 1).len() + 1);
}
fn on_first_page(list: &DisplayList, ch: char) -> usize {
find_all(list, ch).iter().filter(|g| g.0 == 0).count()
}
#[test]
fn keeps_a_line_of_a_split_row_only_with_room_for_its_box_and_the_closing_line() {
let counts: Vec<(usize, usize)> = (0..=200)
.step_by(2)
.map(|after| {
let lines = |breaks| on_first_page(&spaced_table((58, after), ("", 20), breaks), 'd');
(lines(AroundPagePart), lines(AroundTable))
})
.collect();
assert!(
counts.iter().all(|(page, table)| page <= table),
"{counts:?}"
);
assert!(
counts.iter().any(|(page, table)| page < table),
"{counts:?}"
);
}
#[test]
fn keeps_a_whole_row_at_the_foot_with_room_for_the_closing_line_alone() {
let counts: Vec<(usize, usize)> = (0..=200)
.step_by(2)
.map(|after| {
let rows = |breaks| {
let list = spaced_table((61, after), ("<w:cantSplit/>", 1), breaks);
on_first_page(&list, 'c') + on_first_page(&list, 'e')
};
(rows(AroundPagePart), rows(AroundTable))
})
.collect();
assert!(
counts.iter().all(|(page, table)| page >= table),
"{counts:?}"
);
assert!(
counts.iter().any(|(page, table)| page > table),
"{counts:?}"
);
}