Files
vectorcraft/crates/engine/src/tests/strokegeom.rs
T
dotCooCoo cd6c575ae6 Tests: engine tests assert results instead of only success
Engine tests in crates/engine/src/tests that passed on a broken result
now check the result:

- cancel: Export PDF under a canceled token stops, and outside it the
  whole file is written.
- ids_without_selection: each command refuses with nothing selected,
  then changes the object given in one undo step.
- pathops: every bad input is refused and changes nothing.
- plugins: the folder's plug-in is listed as loaded, nothing is loaded
  once the folder is cleared, and it loads again when set again.
- brushlib: the pasted brush and its pattern equal the copied ones.
- build: Image Trace keeps a preset's settings passed back, for one
  preset with an enhanced version and one without, in place of a loop
  over every preset.
- strokegeom: two width profiles and two arrowheads are checked by
  value, in place of a round trip over every preset and arrowhead.
- script_xmp: the ACL test fails instead of returning when the default
  ACL cannot be set.
- Tests that checked every item of a list now also check its length, so
  an empty list fails (bboxrotate, build, clippaint, colormgmt,
  constrain_new, dynamic_symbols, freeform, gradient).

Removed: a manual timing probe in areafit, covered by
auto_height_in_columns_lays_out_the_columns_twice_a_keystroke, and two
plug-in file generators in plugins that ran only with an environment
variable set, covered by live_effects_are_given_their_object_s_paints
and filters_set_effects_and_stroke_styles.

object.path.simplify now refuses a negative tolerance and a level
outside 0 to 100, which it clamped before.
2026-12-01 18:29:53 -08:00

91 lines
4.6 KiB
Rust

//! Stroke geometry from the Stroke panel's commands (`cmd/stroke.rs`): arrowhead alignment and
//! the width-profile presets.
use serde_json::json;
use vectorcraft_doc::{ArrowAlign, Arrowhead, StrokeLayer, WidthProfile};
use crate::*;
fn session_with_line() -> (Session, NodeId) {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
let r = s.execute("shape.line", &json!({"x1": 10, "y1": 100, "x2": 150, "y2": 100})).unwrap();
(s, NodeId(r["id"].as_u64().unwrap()))
}
fn stroke(s: &Session, id: NodeId) -> StrokeLayer {
s.doc().unwrap().doc.node(id).unwrap().appearance.stroke().unwrap().clone()
}
#[test]
fn arrow_align_is_set_per_stroke_and_validated() {
let (mut s, id) = session_with_line();
assert_eq!(stroke(&s, id).arrow_align, ArrowAlign::Extend);
s.execute("stroke.set", &json!({"endArrow": "TriangleOpen", "arrowAlign": "tip"})).unwrap();
let st = stroke(&s, id);
assert_eq!((st.end_arrow, st.arrow_align), (Some(vectorcraft_doc::Arrowhead::TriangleOpen), ArrowAlign::Tip));
s.execute("stroke.set", &json!({"arrowAlign": "extend", "ids": [id.0]})).unwrap();
assert_eq!(stroke(&s, id).arrow_align, ArrowAlign::Extend);
assert!(s.execute("stroke.set", &json!({"arrowAlign": "middle"})).is_err());
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(stroke(&s, id).arrow_align, ArrowAlign::Tip);
}
#[test]
fn profiles_come_from_the_preset_catalogue() {
let (mut s, id) = session_with_line();
s.execute("stroke.set", &json!({"profile": "lens"})).unwrap();
assert_eq!(stroke(&s, id).profile.map(|p| p.points), Some(vec![(0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 0.0, 0.0)]));
s.execute("stroke.set", &json!({"profile": "pinch"})).unwrap();
assert_eq!(stroke(&s, id).profile.map(|p| p.points), Some(vec![(0.0, 1.0, 1.0), (0.5, 0.25, 0.25), (1.0, 1.0, 1.0)]));
s.execute("stroke.set", &json!({"profile": "uniform"})).unwrap();
assert!(stroke(&s, id).profile.is_none(), "uniform is the plain stroke");
assert!(s.execute("stroke.set", &json!({"profile": "zigzag"})).is_err());
// The params doc (what agents read) lists every preset and the arrow alignment.
let spec = command_specs().iter().find(|c| c.id == "stroke.set").unwrap();
for p in WidthProfile::PRESETS {
assert!(spec.params.contains(&format!("\"{}\"", p.id)), "{}", p.id);
}
assert!(spec.params.contains("arrowAlign"));
// …and every arrowhead name `startArrow`/`endArrow` accept.
for a in vectorcraft_doc::Arrowhead::ALL {
let name = serde_json::to_value(&a).unwrap();
assert!(spec.params.contains(name.as_str().unwrap()), "{name}");
}
s.execute("stroke.set", &json!({"endArrow": "TriangleOpen", "startArrow": "Diamond"})).unwrap();
let st = stroke(&s, id);
assert_eq!((st.start_arrow, st.end_arrow), (Some(Arrowhead::Diamond), Some(Arrowhead::TriangleOpen)));
assert!(s.execute("stroke.set", &json!({"endArrow": "Arrow999"})).is_err());
assert_eq!(stroke(&s, id).end_arrow, Some(Arrowhead::TriangleOpen), "a refused name changes nothing");
}
#[test]
fn the_notched_arrow_survives_the_native_format() {
let (mut s, id) = session_with_line();
s.execute("stroke.set", &json!({"startArrow": "NotchedArrow", "endArrow": "NotchedArrow"})).unwrap();
let st = stroke(&s, id);
assert_eq!((st.start_arrow, st.end_arrow), (Some(Arrowhead::NotchedArrow), Some(Arrowhead::NotchedArrow)));
let doc = &s.doc().unwrap().doc;
let text = String::from_utf8(vectorcraft_format::save(doc, true)).unwrap();
assert_eq!(text.matches("\"NotchedArrow\"").count(), 2, "saved by name");
let back = vectorcraft_format::load(text.as_bytes()).unwrap();
assert_eq!(back.node(id).unwrap().appearance.stroke().unwrap(), &stroke(&s, id));
}
#[test]
fn arrow_align_and_profiles_survive_the_native_format() {
let (mut s, id) = session_with_line();
s.execute(
"stroke.set",
&json!({"startArrow": "CircleOpen", "endArrow": "Arrow", "arrowAlign": "tip", "profile": "lens", "dash": [0, 6], "cap": "round"}),
)
.unwrap();
let doc = &s.doc().unwrap().doc;
let back = vectorcraft_format::load(&vectorcraft_format::save(doc, false)).unwrap();
assert_eq!(back.node(id).unwrap().appearance.stroke().unwrap(), &stroke(&s, id));
// A stroke with the default alignment writes nothing new, so older builds read it unchanged.
s.execute("stroke.set", &json!({"arrowAlign": "extend"})).unwrap();
let text = String::from_utf8(vectorcraft_format::save(&s.doc().unwrap().doc, true)).unwrap();
assert!(!text.contains("arrow_align"));
}