use super::{advance, number_ref, page_ref, picture_switch, property_name, style_ref};
use crate::numbering::ReferenceKind;
use f_office_units::Length;
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() {
let cases = [
(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")),
(r#"DOCPROPERTY "a \"b\"""#, Some(r"a \")),
(r#"STYLEREF "a \"b\"""#, None),
(r#"DATE \@ "a \"b\"""#, Some(r#"a "b""#)),
(r#"DOCPROPERTY "a b"#, Some("a b")),
(r#"STYLEREF "a b"#, None),
(r#"PAGEREF "a"#, Some("a")),
(r#"DATE \@ "a b"#, Some("a b")),
(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}");
}
}