Files
cadcraft/crates/io/tests/hard_owned_objects_kept.rs
T
dotCooCoo f81c52b979 Dependencies: take craft-libs main at 3af2280ee3
Every craft-libs git dependency (Cargo.toml, its [patch.crates-io]
entries and apps/cadcraft-web/Cargo.toml) moves from eleven different
revisions to craft-libs main at 3af2280ee3434a2c860012df2f440652621c2c81.
Cargo.lock keeps one copy of each craft-libs crate and adds harfrust
0.13.3 and read-fonts 0.43.3, which craft-font-catalog now needs.

craft-dwg at this revision refuses to convert DWG when no worker process
is configured (storytold/craft-libs#1345). The app and the CLI configure
one at startup and are unaffected. The tests that read or write DWG in
their own process now call cadcraft_io::dwg_worker::allow_in_process(),
which cadcraft-io re-exports. dwg_worker.rs now checks that a worker
that cannot start is an error instead of a fallback to this process.
2026-11-16 12:49:47 -08:00

207 lines
7.4 KiB
Rust

//! An object of another application whose owner is null, but which another kept object holds by a
//! hard owner reference (360), is kept with it, as one application's proxy object held by another.
//! In DXF and in DWG, save after save.
use cadcraft_io::{read, read_dxf, write, write_dxf};
/// A named object dictionary entry `OTHER_APP` (a dictionary) with an XRECORD that holds a second
/// XRECORD by a 360 reference; the second one's owner is null. An object nothing keeps names the
/// second XRECORD by a 360 first.
fn source() -> String {
let groups: &[(i32, &str)] = &[
(0, "SECTION"),
(2, "ENTITIES"),
(0, "LINE"),
(5, "30"),
(8, "0"),
(10, "0"),
(20, "0"),
(11, "10"),
(21, "0"),
(0, "ENDSEC"),
(0, "SECTION"),
(2, "OBJECTS"),
(0, "DICTIONARY"),
(5, "C"),
(330, "0"),
(100, "AcDbDictionary"),
(3, "OTHER_APP"),
(350, "50"),
// An object nothing keeps (null owner, no holder) that also names the held record by a
// 360, before the holder does: it is dropped, and the held record stays with the holder.
(0, "XRECORD"),
(5, "60"),
(330, "0"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "stray"),
(360, "52"),
(0, "DICTIONARY"),
(5, "50"),
(330, "C"),
(100, "AcDbDictionary"),
(281, "1"),
(3, "HOLDER"),
(360, "51"),
(0, "XRECORD"),
(5, "51"),
(102, "{ACAD_REACTORS"),
(330, "50"),
(102, "}"),
(330, "50"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "holder"),
(360, "52"),
(0, "XRECORD"),
(5, "52"),
(102, "{ACAD_REACTORS"),
(330, "51"),
(102, "}"),
(330, "0"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "held"),
(0, "ENDSEC"),
(0, "EOF"),
];
groups.iter().map(|(c, v)| format!("{c}\n{v}\n")).collect()
}
fn check(text: &str, label: &str) {
let lines: Vec<&str> = text.lines().map(str::trim).collect();
let pairs: Vec<(&str, &str)> = lines.chunks(2).filter_map(|c| Some((*c.first()?, *c.get(1)?))).collect();
// Each record from its `0` group on, as (code, value) pairs.
let records: Vec<&[(&str, &str)]> = pairs
.iter()
.enumerate()
.filter(|(_, (c, _))| *c == "0")
.map(|(i, _)| {
let n = pairs[i + 1..].iter().position(|(c, _)| *c == "0").map_or(pairs.len(), |n| i + 1 + n);
&pairs[i..n]
})
.collect();
let with = |text: &str| records.iter().find(|r| r.contains(&("1", text))).copied().unwrap_or_else(|| panic!("{label}: no record {text}"));
let holder = with("holder");
let Some((_, held_handle)) = holder.iter().find(|(c, _)| *c == "360") else { panic!("{label}: the holder holds nothing: {holder:?}") };
let held = with("held");
assert!(held.contains(&("5", *held_handle)), "{label}: the holder's 360 names the held record: {holder:?} {held:?}");
assert!(!records.iter().any(|r| r.contains(&("1", "stray"))), "{label}: the object nothing keeps is dropped");
}
#[test]
fn an_object_held_by_a_kept_one_is_kept() {
cadcraft_io::dwg_worker::allow_in_process();
let mut d = read_dxf(source().as_bytes()).expect("read");
for generation in 1..=2 {
let text = write_dxf(&d);
check(&text, &format!("DXF {generation}"));
d = read_dxf(text.as_bytes()).expect("reread");
}
#[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))]
for generation in 1..=2 {
let dwg = write(&d, "held.dwg").expect("DWG");
d = read(&dwg, "held.dwg").expect("DWG read");
check(&write_dxf(&d), &format!("DWG {generation}"));
}
}
/// Mutual hard owners must not erase the live path through a retained dictionary entry.
#[test]
fn cyclic_hard_owners_with_a_live_holder_stay_kept() {
cadcraft_io::dwg_worker::allow_in_process();
let groups = [
(0, "SECTION"),
(2, "OBJECTS"),
(0, "DICTIONARY"),
(5, "C"),
(330, "0"),
(100, "AcDbDictionary"),
(3, "OTHER_APP"),
(350, "50"),
(0, "XRECORD"),
(5, "60"),
(330, "0"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "stray"),
(360, "54"),
(0, "XRECORD"),
(5, "52"),
(330, "0"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "first"),
(360, "53"),
(360, "54"),
(0, "XRECORD"),
(5, "53"),
(330, "0"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "second"),
(360, "52"),
(0, "XRECORD"),
(5, "54"),
(330, "0"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "child"),
(0, "DICTIONARY"),
(5, "50"),
(330, "C"),
(100, "AcDbDictionary"),
(281, "1"),
(3, "HOLDER"),
(360, "51"),
(0, "XRECORD"),
(5, "51"),
(330, "50"),
(100, "AcDbXrecord"),
(280, "1"),
(1, "holder"),
(360, "52"),
(360, "53"),
(0, "ENDSEC"),
(0, "EOF"),
];
let text = groups.iter().map(|(code, value)| format!("{code}\n{value}\n")).collect::<String>();
let mut drawing = read_dxf(text.as_bytes()).unwrap();
let mut names = vec!["copy.dxf", "again.dxf"];
if cfg!(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64")))) {
names.extend(["copy.dwg", "again.dwg"]);
}
for name in names {
let bytes = write(&drawing, name).unwrap();
drawing = read(&bytes, name).unwrap();
let text = write_dxf(&drawing);
let tags = craft_dxf::parse(text.as_bytes()).unwrap();
let objects = craft_dxf::sections(&tags).into_iter().find(|s| s.name == "OBJECTS").unwrap();
let records = craft_dxf::records(&objects.tags);
let record = |label: &str| records.iter().find(|(_, tags)| tags.iter().any(|t| t.code == 1 && t.str() == label)).unwrap().1.clone();
let first = record("first");
let second = record("second");
let holder = record("holder");
let child = record("child");
assert!(!records.iter().any(|(_, tags)| tags.iter().any(|t| t.code == 1 && t.str() == "stray")), "{name}");
let handle = |tags: &[craft_dxf::Tag]| tags.iter().find(|t| t.code == 5).unwrap().str();
assert!(first.iter().any(|t| t.code == 360 && t.str() == handle(&second)), "{name}");
assert!(second.iter().any(|t| t.code == 360 && t.str() == handle(&first)), "{name}");
assert!(first.iter().any(|t| t.code == 360 && t.str() == handle(&child)), "{name}");
for object in [&first, &second] {
assert!(holder.iter().any(|t| t.code == 360 && t.str() == handle(object)), "{name}");
}
}
}
/// A real 330 owner takes precedence, even when a retained object also names the record by 360.
#[test]
fn a_real_owner_is_not_replaced_by_a_hard_owner() {
let text = source().replace("330\n0\n100\nAcDbXrecord\n280\n1\n1\nheld", "330\nDEAD\n100\nAcDbXrecord\n280\n1\n1\nheld");
assert!(text.contains("330\nDEAD"));
let drawing = read_dxf(text.as_bytes()).unwrap();
let output = write_dxf(&drawing);
assert!(output.lines().any(|line| line.trim() == "holder"));
assert!(!output.lines().any(|line| line.trim() == "held"));
}