use super::{
end_space_fit, inline_picture_cut, kept_end_spaces, property_date, single_underline,
top_band_rows,
};
use crate::dates::DateTime;
use crate::grid::Device;
use crate::lines::EndSpaceFit;
use crate::profile::Profile;
use crate::quirks::{
CellTopBand, EastAsianUnderline, InlinePictureClip, PropertyDateZone, RightToLeftEndSpace,
};
use f_office_units::Length;
#[test]
fn counts_kept_rtl_end_spaces_in_the_fit_in_a_left_to_right_paragraph() {
let base = Profile::base();
let kept = base
.with_variant(RightToLeftEndSpace::RunLevelBesideLeftToRight)
.quirks();
let paragraph = base
.with_variant(RightToLeftEndSpace::ParagraphLevel)
.quirks();
let fit = |quirks, bidi| end_space_fit(quirks, bidi) == EndSpaceFit::WithRightToLeftSpaces;
assert!(fit(&kept, false));
assert!(!fit(&kept, true));
assert!(!fit(¶graph, false));
assert_eq!(kept_end_spaces(&kept, || true, [true, true, false]), 2);
assert_eq!(kept_end_spaces(&kept, || false, [true]), 0);
assert_eq!(kept_end_spaces(¶graph, || true, [true]), 0);
}
#[test]
fn writes_a_date_property_in_utc_or_as_its_local_date() {
let late = DateTime {
year: 2026,
month: 1,
day: 15,
hour: 23,
minute: 30,
second: 0,
};
let base = Profile::base();
let utc = base.with_variant(PropertyDateZone::Utc).quirks();
let local = base.with_variant(PropertyDateZone::LocalDate).quirks();
let picture = Some("yyyy-MM-dd HH:mm");
assert_eq!(
property_date(&utc, 480, late, picture).as_deref(),
Some("2026-01-15 23:30")
);
assert_eq!(property_date(&utc, 480, late, None), None);
assert_eq!(
property_date(&local, 480, late, picture).as_deref(),
Some("2026-01-16 00:00")
);
assert_eq!(
property_date(&local, 480, late, None).as_deref(),
Some("16/1/2026")
);
assert_eq!(
property_date(&local, -300, late, None).as_deref(),
Some("15/1/2026")
);
}
#[test]
fn paints_a_top_band_from_the_line_also_for_a_merged_cell_that_starts_a_page() {
let pt = Length::from_units;
let from_line = || Some((pt(12), pt(13)));
let below_margin = || Some((pt(13), pt(37)));
let base = Profile::base();
let margin = base.with_variant(CellTopBand::BelowMargin).quirks();
let line = base.with_variant(CellTopBand::FromLineEdge).quirks();
let band = |quirks, merged| top_band_rows(quirks, merged, below_margin, from_line);
assert_eq!(band(&margin, (false, false)), below_margin());
assert_eq!(band(&line, (false, false)), from_line());
assert_eq!(band(&margin, (true, false)), None);
assert_eq!(band(&line, (true, false)), from_line());
assert_eq!(band(&margin, (true, true)), None);
assert_eq!(band(&line, (true, true)), None);
}
#[test]
fn paints_no_top_band_of_no_height_and_one_below_the_margin_without_a_top_line() {
let pt = Length::from_units;
let below_margin = || Some((pt(13), pt(37)));
let line = Profile::base()
.with_variant(CellTopBand::FromLineEdge)
.quirks();
let flat = || Some((pt(12), pt(12)));
assert_eq!(
top_band_rows(&line, (false, false), below_margin, flat),
None
);
assert_eq!(
top_band_rows(&line, (false, false), below_margin, || None),
below_margin()
);
}
#[test]
fn cuts_an_inline_picture_only_on_a_target_that_clips_it() {
let base = Profile::base();
let band = (Length::from_units(254_000), Length::from_units(762_000));
let clips = base.with_variant(InlinePictureClip::LineBox).quirks();
assert_eq!(inline_picture_cut(&clips, || Some(band)), Some(band));
assert_eq!(
inline_picture_cut(&clips, || None::<(Length, Length)>),
None
);
let whole = base.with_variant(InlinePictureClip::Unknown).quirks();
assert_eq!(inline_picture_cut(&whole, || Some(band)), None);
}
#[test]
fn places_an_east_asian_underline_from_the_pixel_depth_where_the_target_does() {
let base = Profile::base();
let pixels = base.with_variant(EastAsianUnderline::PixelDescent).quirks();
let post = base.with_variant(EastAsianUnderline::PostTable).quirks();
let device = Device::per_inch(600).unwrap();
let dot = device.dots(1);
let size = Length::from_half_points(21).unwrap();
let from_post = || (-(dot * 6), dot * 2);
let depth = |pels: u16| (pels == 88).then_some(12);
assert_eq!(
single_underline(&pixels, &device, (size, true), from_post, depth),
(-(dot * 12), dot * 5)
);
assert_eq!(
single_underline(&pixels, &device, (size, false), from_post, depth),
from_post()
);
assert_eq!(
single_underline(&post, &device, (size, true), from_post, depth),
from_post()
);
assert_eq!(
single_underline(&pixels, &device, (size, true), from_post, |_| None),
from_post()
);
}