use super::*; /// A page of `n` one-word blocks, each in its own size and colour. fn varied_page(n: usize) -> Page { let blocks = (0..n) .map(|i| Block { text: "word".into(), rect: [72.0, i as f64, 100.0, i as f64 + 1.0], size: 4.0 + i as f64 * 0.5, bold: false, italic: false, color: [(i % 256) as f64 / 255.0, (i / 256 % 256) as f64 / 255.0, 0.5], rotation: 0.0, space_around: [false; 2], }) .collect(); Page { width: 612.0, height: 792.0, blocks, images: Vec::new(), background: None, rules: Vec::new() } } fn lookup_steps(f: impl FnOnce()) -> usize { LOOKUP_STEPS.with(|c| c.set(0)); f(); LOOKUP_STEPS.with(|c| c.get()) } #[test] fn many_distinct_sizes_are_counted_in_constant_steps_each() { let pages = [varied_page(3000)]; let steps = lookup_steps(|| { body_size(&pages); }); assert!(steps <= 2 * 3000, "{steps} steps"); } #[test] fn many_distinct_colours_take_constant_steps_each() { let pages = [varied_page(3000)]; let mut out = String::new(); let steps = lookup_steps(|| out = rtf(&pages)); assert!(steps <= 4 * 3000, "{steps} steps"); assert!(out.contains("\\cf3000 ") || out.contains("\\cf3000\\"), "the last colour has the last slot"); } fn page() -> Page { let b = |t: &str, y: f64, size: f64, bold: bool| Block { text: t.into(), rect: [72.0, y, 500.0, y + size], size, bold, italic: false, color: [0.0; 3], rotation: 0.0, space_around: [false; 2], }; Page { width: 612.0, height: 792.0, blocks: vec![ b("Body text after the picture, which is long enough to be the main text size of the page.", 400.0, 11.0, false), b("Annual Report", 700.0, 24.0, true), b("Overview", 650.0, 14.0, true), b("Some
text & more of it so that eleven points is the body size here.", 620.0, 11.0, false), ], images: vec![Image { ext: "png", bytes: b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR\0\0\0\x02\0\0\0\x03".as_slice().into(), rect: [72.0, 450.0, 272.0, 600.0], size: [200.0, 150.0], rotation: 0.0, }], background: None, rules: Vec::new(), } } #[test] fn html_has_headings_paragraphs_and_images_in_order() { let h = html(&[page(), page()], "Report"); let order: VecSome <body> text &", "Body text after"]
.iter()
.map(|x| h.find(x).unwrap_or_else(|| panic!("{x} missing in {h}")))
.collect();
assert!(order.windows(2).all(|w| w[0] < w[1]), "{order:?}");
assert_eq!(h.matches("