beffroi-tools/crates/midra-layout/tests/import.rs

212 lines
7.2 KiB
Rust

use midra_layout::import::{parse_svg, parse_values};
use midra_layout::{Geometry, Layout};
fn svg(body: &str) -> String {
format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080">{body}</svg>"#
)
}
fn close(a: f64, b: f64) {
assert!((a - b).abs() < 1e-7, "{a} != {b}");
}
#[test]
fn svg_detects_rectangles_background_names_transforms_and_outlines() {
let doc=parse_svg(&svg(r#"<rect id="background" width="100%" height="100%" fill="black"/><g transform="translate(12 20) scale(2 3)" stroke="white" stroke-width="4"><rect id="presenter" x="10" y="30" width="100" height="50"/><rect id="slides" x="150" y="30" width="100" height="50"/></g>"#)).unwrap();
assert_eq!(doc.shapes.len(), 3);
assert!(doc.shapes[0].background);
assert!(!doc.shapes[1].background);
assert_eq!(doc.shapes[1].id, "presenter");
assert_eq!(
doc.shapes[1].geometry,
Geometry {
x: 32.0,
y: 110.0,
width: 200.0,
height: 150.0
}
);
assert_eq!(doc.shapes[1].outline, [8.0, 12.0]);
let g = doc.shapes[1]
.paste_geometry(doc.shapes[1].geometry, true, true)
.unwrap();
assert_eq!(
g,
Geometry {
x: 28.0,
y: 104.0,
width: 208.0,
height: 162.0
}
);
}
#[test]
fn svg_units_viewbox_letterboxing_and_preserve_aspect_none_are_applied() {
let d=parse_svg(r#"<svg width="2in" height="25.4mm" viewBox="10 20 200 100"><rect x="10" y="20" width="200" height="100"/></svg>"#).unwrap();
let g = d.shapes[0].geometry;
close(g.x, 0.0);
close(g.y, 0.0);
close(g.width, 192.0);
close(g.height, 96.0);
let d=parse_svg(r#"<svg width="200" height="200" viewBox="10 20 200 100"><rect x="10" y="20" width="200" height="100"/></svg>"#).unwrap();
assert_eq!(
d.shapes[0].geometry,
Geometry {
x: 0.0,
y: 50.0,
width: 200.0,
height: 100.0
}
);
let d=parse_svg(r#"<svg width="200" height="200" viewBox="10 20 200 100" preserveAspectRatio="none"><rect x="10" y="20" width="200" height="100"/></svg>"#).unwrap();
assert_eq!(
d.shapes[0].geometry,
Geometry {
x: 0.0,
y: 0.0,
width: 200.0,
height: 200.0
}
);
assert!(d.shapes[0].background);
}
#[test]
fn rectangular_paths_polygons_hidden_nodes_and_image_background_are_handled() {
let doc=parse_svg(&svg(r#"<image width="1920" height="1080" href="https://example.invalid/no-fetch.png"/><g style="display:none"><circle r="50"/></g><path id="path" d="M12 30 h100 v50 h-100 z"/><polygon points="200,30 300,30 300,80 200,80"/>"#)).unwrap();
assert_eq!(doc.shapes.len(), 3);
assert!(doc.shapes[0].background_only);
assert!(
doc.shapes[0]
.paste_geometry(doc.shapes[0].geometry, true, false)
.is_err()
);
assert_eq!(
doc.shapes[1].geometry,
Geometry {
x: 12.0,
y: 30.0,
width: 100.0,
height: 50.0
}
);
assert_eq!(
doc.shapes[2].geometry,
Geometry {
x: 200.0,
y: 30.0,
width: 100.0,
height: 50.0
}
);
let d = parse_svg(&svg(
r#"<g visibility="hidden"><rect visibility="visible" width="40" height="30"/></g>"#,
))
.unwrap();
assert_eq!(d.shapes.len(), 1);
}
#[test]
fn unsupported_or_ambiguous_svg_is_refused_without_partial_shapes() {
for body in [
r#"<rect width="40" height="30"/><circle r="5"/>"#,
r#"<g transform="rotate(30)"><rect width="40" height="30"/></g>"#,
r#"<rect width="40" height="30" rx="5"/>"#,
r#"<rect width="40" height="30" clip-path="url(#c)"/>"#,
r#"<style>rect {width:400px}</style><rect width="40" height="30"/>"#,
r#"<rect width="40" height="30" style="transform:scale(2)"/>"#,
r#"<path d="M0 0 C0 10 10 10 10 0 Z"/>"#,
r#"<path d="M0 0 H10 V10 H0"/>"#,
r#"<polygon points="0,0 10,10 20,0 10,-10"/>"#,
r#"<polygon points="0,0 10,0 10,10 0,10 garbage"/>"#,
r#"<polygon points="0,0 10,0 10,10 0,10 42"/>"#,
r##"<use href="#shape"/>"##,
r#"<image width="30" height="20"/>"#,
r#"<rect width="0" height="20"/>"#,
] {
assert!(parse_svg(&svg(body)).is_err(), "accepted {body}");
}
assert!(
parse_svg(r#"<svg width="100%" height="100%"><rect width="30" height="20"/></svg>"#)
.is_err()
);
assert!(parse_svg("not xml").is_err());
assert!(parse_values(&"x".repeat(1_048_577)).is_err());
}
#[test]
fn batch_import_is_one_transaction_and_refuses_count_duplicates_and_invalid_geometry() {
let mut l = Layout::default();
l.set_selection([true; 2]);
let before = *l.layers();
let formats = *l.formats();
let a = Geometry {
x: 12.0,
y: 271.0,
width: 945.0,
height: 531.58,
};
let b = Geometry {
x: 1000.0,
y: 60.0,
width: 300.0,
height: 250.0,
};
assert!(
l.apply_import(&[(0, a)])
.unwrap_err()
.contains("count mismatch")
);
assert!(l.apply_import(&[(0, a), (0, b)]).is_err());
assert!(
l.apply_import(&[(0, a), (1, Geometry { width: -1.0, ..b })])
.is_err()
);
assert_eq!(*l.layers(), before);
assert_eq!(*l.formats(), formats);
l.apply_import(&[(1, a), (0, b)]).unwrap();
assert_eq!(*l.layers(), [b, a]);
l.undo();
assert_eq!(*l.layers(), before);
assert_eq!(*l.formats(), formats);
l.redo();
assert_eq!(*l.layers(), [b, a]);
l.set_locked(1, true);
assert!(l.apply_import(&[(0, a), (1, b)]).is_err());
}
#[test]
fn drawio_auto_detection_and_svg_text_preserve_fractional_bounds() {
let d = parse_values(include_str!("fixtures/drawio-rectangle.txt")).unwrap();
assert!(!d.svg);
assert_eq!(d.shapes[0].geometry.height, 531.58);
let d = parse_values(&svg(
r#"<rect x="12.25" y="20.5" width="30.75" height="40.25"/>"#,
))
.unwrap();
assert!(d.svg);
assert_eq!(d.shapes[0].geometry.x, 12.25);
}
#[test]
fn midra_values_convert_centers_and_reject_incomplete_or_invalid_batches() {
let one = "01 / Presenter — visible box (device container unverified)\nHorizontal: -10 px\nVertical: 200 px\nWidth: 301 px\nHeight: 251 px";
let doc = parse_values(&format!("{one}\r\n\r\n{one}")).unwrap();
assert_eq!(doc.shapes.len(), 2);
assert_eq!(
doc.shapes[0].geometry,
Geometry {
x: -160.5,
y: 74.5,
width: 301.0,
height: 251.0
}
);
for bad in [
one.replace("Width: 301 px", "Width: NaN px"),
one.replace("Height: 251 px", "Height: -1 px"),
one.replace("Vertical: 200 px", "Vertical: inf px"),
one.replace("Width: 301 px", "Width: 301 mm"),
one.replace("Width: 301 px", "Horizontal: 301 px"),
one.replace("Height: 251 px", ""),
format!("{one}\n\nunexpected content"),
] {
assert!(parse_values(&bad).is_err(), "accepted {bad}");
assert!(parse_values(&format!("{one}\n\n{bad}")).is_err());
}
}