//! Fields in an ODT file update in WordCraft as they do in Word: a reference to a bookmark shows //! the bookmarked text (a bookmark over text, `text:bookmark-start`…`-end`, not a point). The //! ODT is original and synthetic, shaped like LibreOffice's. use serde_json::json; use wordcraft_engine::{Session, io}; const NS: &str = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0""#; fn odt(body: &str) -> Vec { let content = format!(r#"{body}"#); let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } z.finish().unwrap() } fn text(s: &Session) -> Vec { s.doc.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(wordcraft_doc::para::OBJ, "")).collect() } /// The displayed results of the fields in the body. fn results(s: &Session) -> Vec { s.doc .body .iter() .filter_map(|b| b.as_para()) .flat_map(|p| p.objects.iter()) .filter_map(|o| match o { wordcraft_doc::para::InlineObject::Field { result, .. } => Some(result.clone()), _ => None, }) .collect() } #[test] fn a_reference_to_a_bookmark_updates_to_its_text() { let doc = io::open_bytes( "refs.odt", &odt(r#"Planning aheadSee Planning."#), ) .unwrap(); let mut s = Session::new(doc); assert_eq!(results(&s), ["Planning"]); s.run("references.updateFields", &json!({"all": true})).unwrap(); assert_eq!(results(&s), ["Planning ahead"], "{:?}", text(&s)); // And after a save in ODT and opening again. let again = io::open_bytes("refs.odt", &io::save_bytes("refs.odt", &s.doc).unwrap()).unwrap(); let mut s2 = Session::new(again); s2.run("references.updateFields", &json!({"all": true})).unwrap(); assert_eq!(results(&s2), ["Planning ahead"]); } #[test] fn captions_chapters_and_dates_update_as_words() { // Stale results, as a file can hold them: they are recomputed. let doc = io::open_bytes( "fields.odt", &odt(r#"Methods Figure 7: a cat Figure 9: a dog See Figure 9 in Old."#), ) .unwrap(); let mut s = Session::new(doc); s.run("references.updateFields", &json!({"all": true})).unwrap(); assert_eq!(results(&s), ["1", "2", "Figure 2", "Methods"], "{:?}", text(&s)); } #[test] fn a_flat_document_opens_and_saves_by_its_extension() { let fodt = r#"Flat words."#; let doc = io::open_bytes("plain.fodt", fodt.as_bytes()).unwrap(); let s = Session::new(doc); assert_eq!(text(&s), ["Flat words."]); let saved = io::save_bytes("plain.fodt", &s.doc).unwrap(); assert!(saved.starts_with(b"By stale."#; let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content)] { z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } let mut s = Session::new(io::open_bytes("user.odt", &z.finish().unwrap()).unwrap()); s.run("references.updateFields", &json!({"all": true})).unwrap(); assert_eq!(results(&s), ["Ada"]); } #[test] fn a_table_formula_updates_as_a_word_formula() { // LibreOffice's sum of the cells above, its value out of date: Update Field works it out. let cell = |v: &str| { format!( r#"{v}"# ) }; let body = format!( r#"{}{}99"#, cell("1.5"), cell("3") ); let mut s = Session::new(io::open_bytes("sum.odt", &odt(&body)).unwrap()); let shown = |s: &Session| -> Vec<(String, String)> { let t = s.doc.body.iter().find_map(|b| b.as_table()).unwrap(); t.rows .iter() .flat_map(|r| r.cells.iter()) .flat_map(|c| c.blocks.iter().filter_map(|b| b.as_para())) .flat_map(|p| p.objects.iter()) .filter_map(|o| match o { wordcraft_doc::para::InlineObject::Field { instr, result, .. } => Some((instr.clone(), result.clone())), _ => None, }) .collect() }; assert_eq!(shown(&s), [("=SUM(A1:A2)".to_string(), "99".to_string())]); s.run("references.updateFields", &json!({"all": true})).unwrap(); assert_eq!(shown(&s), [("=SUM(A1:A2)".to_string(), "4.50".to_string())]); } #[test] fn odt_variables_update_as_set_and_ref() { // A variable set, displayed; set again, hidden; then shown: LibreOffice shows "Ada", nothing, // "Grace". Its results out of date here, Update Field works them out again. let body = r#"stale||stale"#; let mut s = Session::new(io::open_bytes("vars.odt", &odt(body)).unwrap()); s.run("references.updateFields", &json!({"all": true})).unwrap(); let r = results(&s); assert_eq!((r[1].as_str(), r[3].as_str()), ("Ada", "Grace")); }