f-office 0.2.2

Renders Office Open XML documents the way Microsoft Office does.
Documentation
//! Tests of the span check.

use f_office_docx::testing::{self, WordParts as _};

use super::{SaveCheck, Saved, SpanCounts, save, spans};

#[test]
fn every_span_of_a_document_reads_back() {
    let body = r#"<w:p><w:pPr><w:jc w:val="center"/></w:pPr><w:r><w:t xml:space="preserve"> a &amp; b </w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p/></w:tc></w:tr></w:tbl>"#;
    let docx = testing::docx(body).plain_styles().zip().unwrap();
    let counts = spans(&docx).unwrap();
    assert!(counts.parts >= 3, "{counts:?}");
    assert!(counts.elements > 10, "{counts:?}");
    assert_eq!(
        (counts.unreadable, counts.not_utf8, counts.not_read_back()),
        (0, 0, 0)
    );
}

#[test]
fn counts_the_parts_that_do_not_read_or_are_not_in_utf_8() {
    let utf16: Vec<u8> = [0xFF, 0xFE]
        .into_iter()
        .chain("<a/>".encode_utf16().flat_map(u16::to_le_bytes))
        .collect();
    let docx = testing::docx("<w:p/>")
        .part("word/broken.xml", "<a>")
        .part(
            "word/latin.xml",
            r#"<?xml version="1.0" encoding="ISO-8859-1"?><a/>"#,
        )
        .part("word/wide.xml", utf16)
        .part("word/media/image.png", "not XML")
        .zip()
        .unwrap();
    let counts = spans(&docx).unwrap();
    assert_eq!((counts.unreadable, counts.not_utf8), (1, 2), "{counts:?}");
    assert_eq!(counts.not_read_back(), 0);
    let mut twice = counts;
    twice += counts;
    assert_eq!(
        twice,
        SpanCounts {
            parts: counts.parts * 2,
            unreadable: 2,
            not_utf8: 4,
            elements: counts.elements * 2,
            undeclared: 0,
            unparsed: 0,
            differing: 0,
        }
    );
    assert!(spans(b"not a package").is_err());
}

#[test]
fn counts_apart_the_spans_with_a_prefix_that_the_part_does_not_declare() {
    // The element of the undeclared prefix and its two ancestors hold it; the sibling does not.
    let docx = testing::docx("<w:p/>")
        .part("word/odd.xml", "<a><b><p:c/></b><d/></a>")
        .zip()
        .unwrap();
    let counts = spans(&docx).unwrap();
    assert_eq!(
        (counts.undeclared, counts.unparsed, counts.differing),
        (3, 0, 0),
        "{counts:?}"
    );
    assert_eq!(counts.not_read_back(), 3);
}

#[test]
fn saves_a_document_as_its_own_bytes_and_as_its_own_parts() {
    let docx = testing::docx("<w:p><w:r><w:t>a</w:t></w:r></w:p>")
        .plain_styles()
        .zip()
        .unwrap();
    assert_eq!(
        save(&docx).unwrap(),
        SaveCheck {
            saved: Saved::Same,
            rewritten: Some(true),
        }
    );
    assert!(save(b"not a package").is_err());
}

#[test]
fn counts_a_package_of_zip64_apart() {
    use std::io::{Cursor, Write as _};
    let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
    let options = zip::write::SimpleFileOptions::default().large_file(true);
    zip.start_file("word/document.xml", options).unwrap();
    zip.write_all(b"<w:document/>").unwrap();
    let docx = zip.finish().unwrap().into_inner();
    assert_eq!(
        save(&docx).unwrap(),
        SaveCheck {
            saved: Saved::Zip64,
            rewritten: None,
        }
    );
}