//! Section vertical alignment (`w:vAlign` in `w:sectPr`, ECMA-376 Part 1 ยง17.6.23, values from //! ST_VerticalJc). Fixtures are hand-written from the spec. use std::io::{Read, Write}; use wordcraft_doc::section::PageVAlign; fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); zw.write_all(bytes).unwrap(); } zw.finish().unwrap().into_inner() } fn document_xml(bytes: &[u8]) -> String { let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); let mut s = String::new(); z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); s } /// #1320: "both" (Justified) reads as Justify and writes back as "both"; the others keep theirs. #[test] fn section_vertical_alignment_round_trips_with_justified() { let sect = |v: &str| format!(r#""#); let body = format!( r#"{}A{}B{}C{}DE{}"#, sect("both"), sect("center"), sect("bottom"), sect("top"), sect("sideways") ); let d = wordcraft_docx::read(&docx(&body)).unwrap(); let got: Vec = d.sections().iter().map(|(_, s)| s.valign).collect(); let want = vec![PageVAlign::Justify, PageVAlign::Center, PageVAlign::Bottom, PageVAlign::Top, PageVAlign::Top]; assert_eq!(got, want); let out = wordcraft_docx::write(&d).unwrap(); let xml = document_xml(&out); assert_eq!(xml.matches(r#""#).count(), 1, "{xml}"); assert_eq!(xml.matches(r#""#).count(), 1, "{xml}"); assert_eq!(xml.matches(r#""#).count(), 1, "{xml}"); assert_eq!(xml.matches("w:vAlign").count(), 3, "top writes nothing: {xml}"); let back = wordcraft_docx::read(&out).unwrap(); assert_eq!(back.sections().iter().map(|(_, s)| s.valign).collect::>(), want); }