use f_office_docx::testing;
use super::{EditOutcome, Made, NamedEdit, Operation, edits, once_word, other_parts_kept};
use crate::Options;
use crate::test_fonts::carlito;
const STYLES: &str =
r#"<w:style w:type="paragraph" w:styleId="Heading1"><w:name w:val="heading 1"/></w:style>"#;
const CONTENT_TYPES: &str = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/></Types>"#;
fn file(body: &str) -> Vec<u8> {
testing::docx(body)
.relationship(testing::DOCUMENT, "styles", "styles", "styles.xml")
.part("word/styles.xml", testing::word("styles", STYLES))
.part("[Content_Types].xml", CONTENT_TYPES)
.zip()
.unwrap()
}
fn outcomes(body: &str) -> Vec<(Operation, EditOutcome)> {
edits(&file(body), &carlito(), &Options::base()).unwrap()
}
fn paragraph(ppr: &str, text: &str) -> String {
format!("<w:p><w:pPr>{ppr}</w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>")
}
const MADE: Made = Made {
lays_out: true,
blocks_confirmed: true,
other_parts_kept: true,
note_kept: false,
};
#[test]
fn takes_a_word_that_the_text_holds_once_and_no_brace_touches() {
assert_eq!(once_word("The {PAGE} heading, heading Fees"), Some("Fees"));
assert_eq!(once_word("PAGE {PAGE}"), None);
assert_eq!(once_word("a bcd e"), None);
assert_eq!(once_word("früh Grüße"), Some("früh"));
}
fn table(columns: usize, rows: &[(&str, &[&str])]) -> String {
let mut xml = format!(
"<w:tbl><w:tblGrid>{}</w:tblGrid>",
r#"<w:gridCol w:w="2000"/>"#.repeat(columns)
);
for (properties, cells) in rows {
xml.push_str("<w:tr><w:trPr>");
xml.push_str(properties);
xml.push_str("</w:trPr>");
for text in *cells {
xml.push_str("<w:tc>");
xml.push_str(¶graph("", text));
xml.push_str("</w:tc>");
}
xml.push_str("</w:tr>");
}
xml + "</w:tbl>"
}
fn address(operation: Operation, docx: &[u8]) -> String {
let blocks = crate::Document::open(docx, &carlito(), &Options::base())
.unwrap()
.value
.text_blocks()
.to_vec();
match operation.edit(docx, &blocks).unwrap() {
NamedEdit::Replace { at, find, with } => format!("{at} {find} {with}"),
NamedEdit::Comment {
at,
find: Some(find),
..
} => format!("{at} {find}"),
NamedEdit::Insert { at, .. }
| NamedEdit::Delete { at }
| NamedEdit::SetText { at, .. }
| NamedEdit::InsertRow { at, .. }
| NamedEdit::Comment { at, .. }
| NamedEdit::InsertImage { at, .. } => at.to_string(),
NamedEdit::Style { at, style } => format!("{at} {style}"),
other => panic!("{other:?}"),
}
}
#[test]
fn makes_each_operation_at_the_block_of_its_rule_and_checks_the_new_file() {
let heading = r#"<w:pStyle w:val="Heading1"/>"#;
let body = [
"<w:p/>".to_owned(),
paragraph(heading, "Title"),
paragraph("", "Some text here"),
table(1, &[("", &["In cell"])]),
table(
2,
&[
(r#"<w:gridBefore w:val="0"/>"#, &["a", "b"]),
(r#"<w:gridBefore w:val="1"/>"#, &["c"]),
],
),
paragraph("", "Last words"),
]
.concat();
let docx = file(&body);
let addresses = [
"p2 Title [Title]",
"p2",
"p1 Heading1",
"p2",
"t1/r1/c1",
"t1/r1",
"t2/r2",
"p2",
"p2 Title",
"p2",
];
for (operation, expected) in Operation::ALL.into_iter().zip(addresses) {
assert_eq!(address(operation, &docx), expected, "{operation:?}");
}
let found = outcomes(&body);
let expected: Vec<_> = Operation::ALL
.into_iter()
.map(|operation| (operation, EditOutcome::Made(MADE)))
.collect();
assert_eq!(found, expected);
}
#[test]
fn counts_an_operation_without_a_block_apart_from_a_refused_edit() {
let found = outcomes("<w:p/>");
assert!(
found
.iter()
.all(|(_, outcome)| *outcome == EditOutcome::NoAddress),
"{found:?}"
);
let crossing = "<w:p><w:r><w:t>Fe</w:t></w:r><w:hyperlink><w:r><w:t>es</w:t></w:r></w:hyperlink></w:p><w:p/>";
assert_eq!(
outcomes(crossing)[0],
(
Operation::Replace,
EditOutcome::Refused("match crosses the edge of w:hyperlink".to_owned())
)
);
let tracked = format!(
r#"{}<w:p><w:ins w:id="1" w:author="a"><w:r><w:t>added</w:t></w:r></w:ins></w:p><w:p/>"#,
paragraph("", "Plain words")
);
assert_eq!(
outcomes(&tracked)[0],
(
Operation::Replace,
EditOutcome::Refused("tracked-changes".to_owned())
)
);
}
#[test]
fn takes_a_grid_after_a_row_and_finds_no_row_without_a_grid_edge() {
let after = table(2, &[(r#"<w:gridAfter w:val="1"/>"#, &["a"])]);
assert_eq!(address(Operation::GridRow, &file(&after)), "t1/r1");
let plain = file(&table(1, &[("", &["a"])]));
let blocks = crate::Document::open(&plain, &carlito(), &Options::base())
.unwrap()
.value
.text_blocks()
.to_vec();
assert_eq!(Operation::GridRow.edit(&plain, &blocks), None);
}
#[test]
fn keeps_other_parts_unless_the_operation_changes_or_adds_them() {
let before = file(¶graph("", "words"));
let more = testing::docx(¶graph("", "other words"))
.relationship(testing::DOCUMENT, "styles", "styles", "styles.xml")
.part("word/styles.xml", testing::word("styles", STYLES))
.part("[Content_Types].xml", CONTENT_TYPES)
.part("word/media/image1.png", "png")
.zip()
.unwrap();
assert!(other_parts_kept(Operation::Replace, &before, &before));
assert!(!other_parts_kept(Operation::Replace, &before, &more));
assert!(other_parts_kept(Operation::Image, &before, &more));
let restyled = testing::docx(¶graph("", "words"))
.relationship(testing::DOCUMENT, "styles", "styles", "styles.xml")
.part("word/styles.xml", testing::word("styles", ""))
.part("[Content_Types].xml", CONTENT_TYPES)
.zip()
.unwrap();
assert!(!other_parts_kept(Operation::Image, &before, &restyled));
}