f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! Tests for field code argument and switch parsing across field readers.

use super::{advance, number_ref, page_ref, picture_switch, property_name, style_ref};
use crate::numbering::ReferenceKind;
use f_office_units::Length;

/// What a reader gives for a code, as text: the bookmark, style, name or picture, with
/// the number kind of `REF` and the `\l` of `STYLEREF`; `None` when the field keeps its stored
/// result.
fn read(code: &str) -> Option<String> {
    let kind = code.split_whitespace().next().unwrap_or_default();
    match kind {
        "REF" => number_ref(code).map(|field| match field.kind {
            Some(kind) => format!("{} {kind:?}", field.bookmark),
            None => format!("{} text", field.bookmark),
        }),
        "PAGEREF" => page_ref(code).map(|field| field.bookmark),
        "STYLEREF" => style_ref(code).map(|field| format!("{} last={}", field.style, field.last)),
        "DOCPROPERTY" => property_name(code),
        _ => picture_switch(code),
    }
}

#[test]
fn reads_each_code_where_the_readings_differ_as_its_reader_did() {
    // (code, what its reader gives)
    let cases = [
        // A quoted argument with a space: a word argument keeps its first word, and the rest
        // of the quote is the next word, no switch of REF; a quoted argument keeps the space.
        (r#"REF "a b" \n"#, None),
        (r#"PAGEREF "a b""#, Some("a")),
        (r#"STYLEREF "a b""#, Some("a b last=false")),
        (r#"DOCPROPERTY "a b""#, Some("a b")),
        // An escaped quote: the quoted argument ends at it, and STYLEREF reads the rest as a
        // switch; the escaped argument reads past it.
        (r#"DOCPROPERTY "a \"b\"""#, Some(r"a \")),
        (r#"STYLEREF "a \"b\"""#, None),
        (r#"DATE \@ "a \"b\"""#, Some(r#"a "b""#)),
        // A quote that the code does not close.
        (r#"DOCPROPERTY "a b"#, Some("a b")),
        (r#"STYLEREF "a b"#, None),
        (r#"PAGEREF "a"#, Some("a")),
        (r#"DATE \@ "a b"#, Some("a b")),
        // `\@` inside a quoted argument: the name keeps it, and the picture starts after it.
        (r#"DOCPROPERTY "x \@ y""#, Some(r"x \@ y")),
        (r#"TIME "x \@ y""#, Some(r#"y""#)),
    ];
    for (code, expected) in cases {
        assert_eq!(read(code).as_deref(), expected, "{code}");
    }
}

#[test]
fn reads_the_switches_of_ref_in_their_own_case_and_the_others_in_any_case() {
    let pattern = format!("a {:?}", ReferenceKind::Pattern);
    let cases = [
        (r"REF a \n", Some(pattern.as_str())),
        (r"REF a \N", None),
        (r"REF a \n \H", None),
        (r"REF a \n \h \* mergeformat", Some(pattern.as_str())),
        (r"REF a \n \* CHARFORMAT", None),
        (r"REF a \h", Some("a text")),
        (r"REF a \* upper", None),
        (r"PAGEREF a \P", None),
        (r"PAGEREF a \p", None),
        (r"STYLEREF a \L", Some("a last=true")),
        (r"STYLEREF a \* charformat", Some("a last=false")),
        (r"STYLEREF a \* x", None),
    ];
    for (code, expected) in cases {
        assert_eq!(read(code).as_deref(), expected, "{code}");
    }
}

#[test]
fn reads_the_moves_of_advance_right_and_left_in_any_case() {
    let points =
        |thousandths: i64| Length::from_units(thousandths * Length::UNITS_PER_POINT / 1_000);
    let cases = [
        (r"ADVANCE \r 36", Some(points(36_000))),
        (r"ADVANCE \R 994.20", Some(points(994_200))),
        (r"ADVANCE \l 18", Some(points(-18_000))),
        (r"ADVANCE \r 10 \L 4", Some(points(6_000))),
        (r"ADVANCE \x 200", None),
        (r"ADVANCE \d 6", None),
        (r"ADVANCE \y 100", None),
        (r"ADVANCE \r", None),
        ("ADVANCE", None),
    ];
    for (code, expected) in cases {
        assert_eq!(advance(code), expected, "{code}");
    }
}