From 0df82313342b80d2db97dbdeb3ef8ed1d31a6798 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Fri, 9 Oct 2026 05:00:01 +0700 Subject: [PATCH] Tests: format round trips find their objects by content, not by count #606 adds a second area type (Shrink Text to Fit) to the rich fixture: the vertical-alignment test now picks the centred area type by its text and strips verticalAlign from every area for its old-file case, and the composer test checks the Single-line text and the rest at the default. Co-Authored-By: Claude Opus 5.5 --- crates/format/tests/native_v3.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/format/tests/native_v3.rs b/crates/format/tests/native_v3.rs index 5919897e..dd6f0a83 100644 --- a/crates/format/tests/native_v3.rs +++ b/crates/format/tests/native_v3.rs @@ -53,8 +53,10 @@ fn area_type_vertical_alignment_round_trips() { let area = |d: &Document| { let mut found = None; d.walk(|n| { + // The fixture's centred area type, by its text (the fixture has other area type). if let NodeKind::Text(t) = &n.kind && matches!(t.kind, vectorcraft_doc::TextKind::Area { .. }) + && t.runs.iter().map(|r| r.text.as_str()).collect::() == "Centred area type" { found = Some(t.area.vertical_align); } @@ -63,7 +65,20 @@ fn area_type_vertical_alignment_round_trips() { }; assert_eq!(area(&back), vectorcraft_doc::VerticalAlign::Center); // Files from before the option open top-aligned. - let old = text.replacen("\"verticalAlign\":\"center\",", "", 1).replacen(",\"verticalAlign\":\"center\"", "", 1); + // (Every area type the fixture has writes its options, so the key goes from all of them.) + fn strip(v: &mut Value) { + match v { + Value::Object(m) => { + m.remove("verticalAlign"); + m.values_mut().for_each(strip); + } + Value::Array(a) => a.iter_mut().for_each(strip), + _ => {} + } + } + let mut v = json_of(text.as_bytes()); + strip(&mut v); + let old = serde_json::to_string(&v).unwrap(); assert!(!old.contains("verticalAlign")); assert_eq!(area(&load(old.as_bytes()).unwrap()), vectorcraft_doc::VerticalAlign::Top); } @@ -149,11 +164,12 @@ fn text_composer_round_trips() { }); v }; - // The point type set to Single-line, then the area type left at the default. - assert_eq!(composers(&d), [Composer::SingleLine, Composer::EveryLine]); + // The point type set to Single-line comes first; the fixture's other type keeps the default. + let single_then_default = |c: &[Composer]| c.len() > 1 && c[0] == Composer::SingleLine && c[1..].iter().all(|c| *c == Composer::EveryLine); + assert!(single_then_default(&composers(&d)), "{:?}", composers(&d)); let bytes = save(&d, false); assert!(String::from_utf8_lossy(&bytes).contains("\"composer\":\"singleLine\"")); - assert_eq!(composers(&load(&bytes).unwrap()), [Composer::SingleLine, Composer::EveryLine]); + assert_eq!(composers(&load(&bytes).unwrap()), composers(&d)); // Files without the key (older files, Every-line text) read as Every-line. fn strip(v: &mut Value) { match v { @@ -168,7 +184,7 @@ fn text_composer_round_trips() { let mut v = json_of(&bytes); strip(&mut v); let d2 = load(&serde_json::to_vec(&v).unwrap()).unwrap(); - assert_eq!(composers(&d2), [Composer::EveryLine, Composer::EveryLine]); + assert!(composers(&d2).iter().all(|c| *c == Composer::EveryLine), "{:?}", composers(&d2)); assert!(!String::from_utf8_lossy(&save(&d2, false)).contains("composer")); }