mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 08:11:41 +00:00
Tests: look up brush libraries by id and check Stipple's tint and opacity
The library list test finds each library's count by its id, so a library added to the list does not shift the checks. The Stipple test draws each brush with a red stroke and checks that every dot takes the stroke color and that only Soft Stipple's dots are translucent.
This commit is contained in:
@@ -123,36 +123,39 @@ fn the_builtin_libraries_are_original_brushes_generated_in_code() {
|
||||
]
|
||||
);
|
||||
assert!(libs.iter().all(|l| l["category"] == "builtIn"));
|
||||
// Looked up by id, so a library added to the list does not shift these checks.
|
||||
let entry = |id: &str| libs.iter().find(|l| l["id"] == id).unwrap();
|
||||
let lib = run(&mut s, "brush.library", json!({"library": "art ribbons"}));
|
||||
assert_eq!(
|
||||
lib["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect::<Vec<_>>(),
|
||||
["Notched Ribbon", "Double Line", "Wave", "Teardrop", "Striped Ribbon"]
|
||||
);
|
||||
assert_eq!(libs[0]["count"].as_u64(), Some(5));
|
||||
assert_eq!(entry("art-ribbons")["count"].as_u64(), Some(5));
|
||||
let trails = run(&mut s, "brush.library", json!({"library": "game-trails"}));
|
||||
assert_eq!(
|
||||
trails["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect::<Vec<_>>(),
|
||||
["Speed Line", "Dash Trail", "Burst Ray", "Zigzag"]
|
||||
);
|
||||
assert_eq!(libs[8]["count"].as_u64(), Some(4));
|
||||
assert_eq!(entry("game-trails")["count"].as_u64(), Some(4));
|
||||
let beams = run(&mut s, "brush.library", json!({"library": "energy-beams"}));
|
||||
assert_eq!(
|
||||
beams["brushes"].as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect::<Vec<_>>(),
|
||||
["Laser Bolt", "Plasma Arc", "Charge Wave", "Spark Trail"]
|
||||
);
|
||||
assert_eq!(libs[9]["count"].as_u64(), Some(4));
|
||||
assert_eq!(entry("energy-beams")["count"].as_u64(), Some(4));
|
||||
let dry = run(&mut s, "brush.library", json!({"library": "Dry Media"}));
|
||||
assert_eq!(
|
||||
dry["brushes"].as_array().unwrap().iter().map(|b| (b["name"].as_str().unwrap(), b["type"].as_str().unwrap())).collect::<Vec<_>>(),
|
||||
[("Chalk Grain", "art"), ("Graphite Pencil", "art"), ("Wax Crayon", "art"), ("Charcoal Dust", "art")]
|
||||
);
|
||||
assert_eq!((libs[10]["name"].as_str(), libs[10]["count"].as_u64()), (Some("Dry Media"), Some(4)));
|
||||
assert_eq!((entry("dry-media")["name"].as_str(), entry("dry-media")["count"].as_u64()), (Some("Dry Media"), Some(4)));
|
||||
let arrows = run(&mut s, "brush.library", json!({"library": "Arrows"}));
|
||||
assert_eq!(
|
||||
arrows["brushes"].as_array().unwrap().iter().map(|b| (b["name"].as_str().unwrap(), b["type"].as_str().unwrap())).collect::<Vec<_>>(),
|
||||
[("Solid Arrow", "art"), ("Open Arrow", "art"), ("Double Arrow", "art"), ("Chevron Arrow", "art"), ("Dashed Arrow", "art")]
|
||||
);
|
||||
assert_eq!((libs[12]["name"].as_str(), libs[12]["count"].as_u64()), (Some("Arrows"), Some(5)));
|
||||
assert_eq!((entry("arrows")["name"].as_str(), entry("arrows")["count"].as_u64()), (Some("Arrows"), Some(5)));
|
||||
assert_eq!((entry("stipple")["name"].as_str(), entry("stipple")["count"].as_u64()), (Some("Stipple"), Some(4)));
|
||||
run(&mut s, "file.new", json!({"width": 200, "height": 200}));
|
||||
let id = run(&mut s, "shape.rectangle", json!({"x": 10, "y": 20, "width": 40, "height": 8}))["id"].as_u64().unwrap();
|
||||
let added = run(&mut s, "brush.addFromLibrary", json!({"library": "energy-beams", "name": "Laser Bolt", "apply": true, "ids": [id]}));
|
||||
@@ -241,9 +244,31 @@ fn stipple_library_draws_dot_textures_and_survives_save() {
|
||||
let BrushKind::Scatter(scatter) = &b.kind else { panic!("{} is not a scatter brush", b.name) };
|
||||
assert_eq!(scatter.colorization, vectorcraft_brush::Colorization::Tints);
|
||||
assert!(scatter.spacing.0 > 0.0 && scatter.spacing.1 >= scatter.spacing.0);
|
||||
let pieces = vectorcraft_brush::stroke_pieces(&lib.doc, b, &line_to(200.0), &black_stroke());
|
||||
// A red stroke: the black dots take it as their color, and Soft Stipple's dots stay translucent.
|
||||
let red = vectorcraft_color::Color::rgb(0.8, 0.1, 0.2);
|
||||
let stroke = vectorcraft_doc::StrokeLayer::new(vectorcraft_color::Paint::solid(red), 1.0);
|
||||
let pieces = vectorcraft_brush::stroke_pieces(&lib.doc, b, &line_to(200.0), &stroke);
|
||||
let points = anchors(&pieces.iter().collect::<Vec<_>>());
|
||||
assert!(!points.is_empty() && points.iter().all(|p| p.x.is_finite() && p.y.is_finite()), "{}", b.name);
|
||||
let mut dots = vec![];
|
||||
for piece in &pieces {
|
||||
piece.walk(&mut |c| {
|
||||
if c.path_data().is_some() {
|
||||
let fill = c.appearance.fill().unwrap_or_else(|| panic!("a {} dot has no fill", b.name));
|
||||
dots.push((fill.paint.color(), fill.opacity));
|
||||
}
|
||||
});
|
||||
}
|
||||
assert!(!dots.is_empty(), "{}", b.name);
|
||||
for (color, opacity) in dots {
|
||||
let rgb = color.unwrap_or_else(|| panic!("a {} dot is not a solid color", b.name)).to_rgb();
|
||||
assert!(rgb.iter().zip(red.to_rgb()).all(|(a, e)| (a - e).abs() < 1e-4), "{} dot is {rgb:?}, not the stroke color", b.name);
|
||||
if b.name == "Soft Stipple" {
|
||||
assert!(opacity > 0.0 && opacity < 1.0, "Soft Stipple dot opacity {opacity}");
|
||||
} else {
|
||||
assert_eq!(opacity, 1.0, "{}", b.name);
|
||||
}
|
||||
}
|
||||
let before = undo_len(&s);
|
||||
let result = run(&mut s, "brush.addFromLibrary", json!({"library": "stipple", "name": b.name, "apply": true, "ids": [path]}));
|
||||
assert_eq!(strs(&result["added"]), [b.name.as_str()]);
|
||||
|
||||
Reference in New Issue
Block a user