use crate::layout::testing::{on_base_with, p, pass_with, rects, table};
use crate::numbering::Numbering;
use crate::quirks::BorderDashes;
fn lines(style: &str, dashes: BorderDashes) -> Vec<(i64, i64, i64, i64)> {
let side = |name: &str| format!(r#"<w:{name} w:val="{style}" w:sz="8"/>"#);
let borders = format!(
"<w:tblBorders>{}</w:tblBorders>",
["top", "left", "bottom", "right", "insideH", "insideV"]
.map(side)
.concat()
);
let body = table(&borders, &[2880, 2880], &[("", &[&p("a"), &p("b")])]) + &p("c");
let target = on_base_with(|p| p.with_variant(dashes));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
rects(&list, 0)
}
#[test]
fn draws_a_dash_style_solid_where_the_target_draws_no_dashes() {
assert_eq!(
lines("dotted", BorderDashes::DrawsSolid),
lines("single", BorderDashes::DrawsSolid)
);
}
#[test]
fn draws_a_solid_style_alike_on_both_variants() {
assert_eq!(
lines("single", BorderDashes::DashesCellPieces),
lines("single", BorderDashes::DrawsSolid)
);
}
type Box = (i64, i64, i64, i64);
fn inside((x, y, w, h): Box, (ox, oy, ow, oh): Box) -> bool {
x >= ox && y >= oy && x + w <= ox + ow && y + h <= oy + oh
}
fn thickness(solid: &[Box]) -> i64 {
solid.iter().map(|&(_, _, w, h)| w.min(h)).min().unwrap()
}
#[test]
fn draws_each_dot_inside_a_solid_piece_and_no_longer_than_the_line_is_wide() {
let solid = lines("single", BorderDashes::DrawsSolid);
let dotted = lines("dotted", BorderDashes::DashesCellPieces);
assert!(dotted.len() > solid.len() * 10);
let thick = thickness(&solid);
assert!(dotted.iter().all(|&(_, _, w, h)| w <= thick && h <= thick));
assert!(
dotted
.iter()
.all(|&dot| solid.iter().any(|&piece| inside(dot, piece)))
);
}
#[test]
fn starts_each_piece_of_the_top_line_with_a_dot_at_its_left_end() {
let solid = lines("single", BorderDashes::DrawsSolid);
let dotted = lines("dotted", BorderDashes::DashesCellPieces);
let thick = thickness(&solid);
let top = solid.iter().map(|&(_, y, _, _)| y).min().unwrap();
let mut on_top: Vec<Box> = dotted
.into_iter()
.filter(|&(_, y, _, _)| y == top)
.collect();
on_top.sort_unstable();
let starts: Vec<i64> = solid
.iter()
.filter(|&&(_, y, w, h)| y == top && w > h)
.map(|&(x, ..)| x)
.collect();
assert_eq!(starts.len(), 2);
for left in starts {
let first = on_top.iter().position(|&(x, ..)| x == left).unwrap();
assert_eq!(on_top[first].2, thick);
assert_eq!(on_top[first + 1].0, left + 2 * thick);
}
}
fn limits(pass: &crate::layout::Pass) -> Vec<(crate::warnings::Limit, u32)> {
pass.warnings
.iter()
.filter_map(|(warning, count)| match warning {
crate::Warning::LimitReached { limit, .. } => Some((*limit, count)),
_ => None,
})
.collect()
}
#[test]
fn draws_a_dashed_line_whole_where_its_dashes_would_pass_the_limit_of_its_page() {
use crate::params::MAX_DASHED_PAGE_ITEMS;
use crate::warnings::Limit;
let side = |name: &str| format!(r#"<w:{name} w:val="dotted" w:sz="8"/>"#);
let borders = format!(
"<w:tblBorders>{}{}</w:tblBorders>",
side("top"),
side("bottom")
);
let body = table(&borders, &[50_000_000], &[("", &[&p("a")])]) + &p("c");
let target = on_base_with(|p| p.with_variant(BorderDashes::DashesCellPieces));
let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
assert!(rects(&pass.list, 0).len() <= MAX_DASHED_PAGE_ITEMS);
let reached = limits(&pass);
assert!(!reached.is_empty(), "{reached:?}");
assert!(
reached
.iter()
.all(|(limit, _)| *limit == Limit::TableDashes)
);
let body = table(&borders, &[2880], &[("", &[&p("a")])]) + &p("c");
let target = on_base_with(|p| p.with_variant(BorderDashes::DashesCellPieces));
let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
assert_eq!(limits(&pass), Vec::new());
}
fn top_dots_without_lines_down(dashes: BorderDashes) -> (Vec<i64>, i64) {
let side = |name: &str, style: &str| format!(r#"<w:{name} w:val="{style}" w:sz="8"/>"#);
let borders = format!(
"<w:tblBorders>{}{}{}{}{}</w:tblBorders>",
side("top", "dotted"),
side("bottom", "dotted"),
side("left", "none"),
side("right", "none"),
side("insideV", "none")
);
let body = table(&borders, &[2880, 2900], &[("", &[&p("a"), &p("b")])]) + &p("c");
let target = on_base_with(|p| p.with_variant(dashes));
let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
let lines = rects(&list, 0);
let top = lines.iter().map(|&(_, y, _, _)| y).min().unwrap();
let mut on_top: Vec<Box> = lines.into_iter().filter(|&(_, y, _, _)| y == top).collect();
on_top.sort_unstable();
let thick = on_top.iter().map(|&(_, _, _, h)| h).min().unwrap();
(on_top.into_iter().map(|(x, ..)| x).collect(), thick)
}
#[test]
fn starts_the_dots_again_in_each_cell_where_no_line_down_divides_the_line() {
let (dots, thick) = top_dots_without_lines_down(BorderDashes::DashesCellPieces);
let breaks: Vec<usize> = (1..dots.len())
.filter(|&i| dots[i] - dots[i - 1] != 2 * thick)
.collect();
assert_eq!(breaks.len(), 1, "{dots:?}");
assert!(breaks[0] > 1 && breaks[0] < dots.len() - 1, "{dots:?}");
}