f-office-docx 0.3.0

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

use super::{
    advance, file_name_case, number_ref, page_ref, picture_switch, property_name, style_ref,
};
use crate::body::TextCase;
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}");
    }
}

#[test]
fn reads_the_case_of_a_file_name_and_keeps_the_stored_result_with_the_path_or_another_switch() {
    let cases = [
        ("FILENAME", Some(TextCase::AsIs)),
        ("filename", Some(TextCase::AsIs)),
        (r"FILENAME \* MERGEFORMAT", Some(TextCase::AsIs)),
        (r"FILENAME \* charformat", Some(TextCase::AsIs)),
        (r"FILENAME \* Upper", Some(TextCase::Upper)),
        (r"FILENAME \* LOWER \* MERGEFORMAT", Some(TextCase::Lower)),
        (r"FILENAME \* FirstCap", Some(TextCase::FirstCap)),
        (r"FILENAME \* caps", Some(TextCase::Caps)),
        (r"FILENAME \p", None),
        (r"FILENAME \P \* MERGEFORMAT", None),
        (r"FILENAME \* roman", None),
        (r"FILENAME \*", None),
        ("FILENAME name", None),
    ];
    for (code, expected) in cases {
        assert_eq!(file_name_case(code), expected, "{code}");
    }
}

#[test]
fn puts_a_text_in_the_case_of_each_format() {
    let text = "my file 2b";
    let cases = [
        (TextCase::AsIs, "my file 2b"),
        (TextCase::Upper, "MY FILE 2B"),
        (TextCase::Lower, "my file 2b"),
        (TextCase::FirstCap, "My file 2b"),
        (TextCase::Caps, "My File 2b"),
    ];
    for (case, expected) in cases {
        assert_eq!(case.apply(text), expected, "{case:?}");
    }
    // A word that starts with a digit keeps its letters.
    assert_eq!(TextCase::Caps.apply("4ab cd"), "4ab Cd");
    assert_eq!(TextCase::FirstCap.apply("  ab"), "  Ab");
}