mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 11:34:27 +00:00
Dimension: test each Arrow Style's arrowhead against the expected head
The engine test now names the head each style draws (Filled a filled triangle, Open an open triangle, Tick a slash, None no head) instead of comparing the dimension line with ArrowStyle::arrowhead, which the engine itself calls. It moves to crates/engine/tests/it/dimensions.rs. The Arrow Style previews take each style from a constant list in the order of the dropdown entries instead of parsing the entry's value every frame, and a test checks that each entry's value matches its style. Part of #8049.
This commit is contained in:
@@ -95,6 +95,29 @@ fn apply_to_all_restyles_every_dimension_in_one_undo_step() {
|
||||
assert!(s.execute("dimension.applyToAll", &json!({"id":line})).is_err(), "not a dimension");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_arrow_style_draws_its_stroke_arrowhead_at_both_ends() {
|
||||
use vectorcraft_doc::Arrowhead;
|
||||
let mut s = session();
|
||||
for (style, head) in
|
||||
[("filled", Some(Arrowhead::Triangle)), ("open", Some(Arrowhead::TriangleOpen)), ("tick", Some(Arrowhead::Slash)), ("none", None)]
|
||||
{
|
||||
let id = run(&mut s, "dimension.create", json!({"a":[0,0],"b":[72,0],"options":{"arrowStyle":style}}))["id"].as_u64().unwrap();
|
||||
let n = s.doc().unwrap().doc.node(NodeId(id)).unwrap();
|
||||
let heads: Vec<_> = n
|
||||
.children()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|n| n.appearance.stroke().map(|s| (s.start_arrow.clone(), s.end_arrow.clone())))
|
||||
.filter(|(a, b)| a.is_some() || b.is_some())
|
||||
.collect();
|
||||
match head {
|
||||
Some(head) => assert!(!heads.is_empty() && heads.iter().all(|h| *h == (Some(head.clone()), Some(head.clone()))), "{style}: {heads:?}"),
|
||||
None => assert!(heads.is_empty(), "{style}: {heads:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dimension_angles_circles_arcs_and_curves() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -32,6 +32,8 @@ const UNITS: Choices = &[
|
||||
];
|
||||
const LINE_TYPES: Choices = &[("solid", "Solid"), ("dashed", "Dashed"), ("dotted", "Dotted")];
|
||||
const ARROWS: Choices = &[("filled", "Filled"), ("open", "Open"), ("tick", "Tick"), ("none", "None")];
|
||||
/// The style of each `ARROWS` entry, in the same order.
|
||||
const ARROW_STYLES: [ArrowStyle; 4] = [ArrowStyle::Filled, ArrowStyle::Open, ArrowStyle::Tick, ArrowStyle::None];
|
||||
/// The size of an Arrow Style preview: a line with the head at its right end.
|
||||
const ARROW_PREVIEW: egui::Vec2 = egui::vec2(40.0, 16.0);
|
||||
const POSITIONS: Choices = &[("away", "Away from the Line"), ("between", "Between Line and Object")];
|
||||
@@ -81,9 +83,7 @@ fn arrow_style(ui: &mut Ui, fields: &mut serde_json::Map<String, Value>, id: &st
|
||||
let shown = ARROWS.iter().position(|(v, _)| *v == current);
|
||||
let names: Vec<&str> = ARROWS.iter().map(|(_, s)| tl!(*s)).collect();
|
||||
let icon = |ui: &Ui, rect: egui::Rect, i: usize, ink: egui::Color32, _: bool| {
|
||||
if let Some((value, _)) = ARROWS.get(i)
|
||||
&& let Ok(style) = serde_json::from_value::<ArrowStyle>(json!(value))
|
||||
{
|
||||
if let Some(style) = ARROW_STYLES.get(i) {
|
||||
crate::panels::stroke::paint_arrow(ui, rect, style.arrowhead().as_ref(), false, ink);
|
||||
}
|
||||
};
|
||||
@@ -448,31 +448,12 @@ mod tests {
|
||||
assert!(picture_before(&out, "Tick"), "Properties");
|
||||
}
|
||||
|
||||
/// Each preview shows the stroke head that the dimension line draws at its ends.
|
||||
/// Each Arrow Style entry previews the style its value names.
|
||||
#[test]
|
||||
fn arrow_previews_match_the_dimension_line() {
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
for style in [ArrowStyle::Filled, ArrowStyle::Open, ArrowStyle::Tick, ArrowStyle::None] {
|
||||
let options = json!({"arrowStyle": style});
|
||||
let id = app.run("dimension.create", json!({"a":[10,10],"b":[110,10],"options":options})).unwrap()["id"].as_u64().unwrap();
|
||||
let st = app.session.active().unwrap();
|
||||
let heads: Vec<_> = st
|
||||
.doc
|
||||
.node(vectorcraft_doc::NodeId(id))
|
||||
.unwrap()
|
||||
.children()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|n| n.appearance.stroke().map(|s| (s.start_arrow.clone(), s.end_arrow.clone())))
|
||||
.filter(|(a, b)| a.is_some() || b.is_some())
|
||||
.collect();
|
||||
match style.arrowhead() {
|
||||
Some(head) => {
|
||||
assert!(!heads.is_empty() && heads.iter().all(|h| *h == (Some(head.clone()), Some(head.clone()))), "{style:?}: {heads:?}")
|
||||
}
|
||||
None => assert!(heads.is_empty(), "{style:?}: {heads:?}"),
|
||||
}
|
||||
fn arrow_previews_follow_the_listed_styles() {
|
||||
assert_eq!(ARROWS.len(), ARROW_STYLES.len());
|
||||
for ((value, _), style) in ARROWS.iter().zip(ARROW_STYLES) {
|
||||
assert_eq!(serde_json::to_value(style).unwrap(), json!(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user