mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 09:35:43 +00:00
Merge pull request #7044 from dotCooCoo/fix/effect-apply-strict-params
Effects: effect.apply refuses a wrongly typed parameter that has no default
This commit is contained in:
@@ -600,6 +600,105 @@ fn effect_params_are_the_effects_own() {
|
||||
assert!(s.check_params("effect.setParams", &json!({"index": 7, "params": {"blurr": 3}})).is_ok());
|
||||
}
|
||||
|
||||
/// An effect parameter that only the effect's params description names, with no dialog default,
|
||||
/// is held to the type the effect reads: the earlier 3D Perspective and light, Scribble's
|
||||
/// `variation` and an Outline Stroke's `width` are numbers, and a Gaussian Blur's `linear` is true
|
||||
/// or false. The `style` of Crop Marks is `roman` or `japanese`. Every such key of every effect is
|
||||
/// listed, or has a check of its own. A key left out keeps its default.
|
||||
#[test]
|
||||
fn an_effect_parameter_without_a_default_is_held_to_its_type() {
|
||||
use crate::cmd::params::undefaulted_type;
|
||||
use vectorcraft_render::effects::effect_catalog;
|
||||
// Read by checks of their own: a stored texture or a texture to load, an SVG filter, a Warp's
|
||||
// map and the style of Crop Marks.
|
||||
let own_check = |effect: &str, key: &str| match key {
|
||||
"textureImage" | "name" | "xml" => true,
|
||||
"version" => effect.starts_with("warp."),
|
||||
"style" => effect == "cropMarks",
|
||||
_ => false,
|
||||
};
|
||||
let mut unlisted = vec![];
|
||||
for e in effect_catalog() {
|
||||
for key in doc_keys(e.params).unwrap().keys {
|
||||
if e.defaults.get(key).is_none() && undefaulted_type(e.id, key).is_none() && !own_check(e.id, key) {
|
||||
unlisted.push(format!("{}.{key}", e.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(unlisted.is_empty(), "parameters without a default or a listed type: {unlisted:?}");
|
||||
let apply = |effect: &str, params| json!({"effect": effect, "params": params});
|
||||
for (effect, params, why) in [
|
||||
("threeD.revolve", json!({"perspective": true}), "`perspective` must be a number"),
|
||||
("threeD.extrude", json!({"lightAzimuth": "30"}), "`lightAzimuth` must be a number"),
|
||||
("threeD.rotate", json!({"lightElevation": null}), "`lightElevation` must be a number"),
|
||||
("threeD.inflate", json!({"perspective": "50%"}), "`perspective` must be a number"),
|
||||
("path.outlineStroke", json!({"width": "10 pt"}), "`width` must be a number"),
|
||||
("stylize.scribble", json!({"variation": false}), "`variation` must be a number"),
|
||||
("blur.gaussian", json!({"radius": 2, "linear": 1}), "`linear` must be true or false"),
|
||||
("cropMarks", json!({"style": "bogus"}), "`style` must be roman or japanese"),
|
||||
("cropMarks", json!({"style": 1}), "`style` must be roman or japanese"),
|
||||
("cropMarks", json!({"style": null}), "`style` must be roman or japanese"),
|
||||
] {
|
||||
assert_eq!(refusal("effect.apply", apply(effect, params.clone())), why, "{effect} {params}");
|
||||
assert_eq!(refusal("appearance.addEffect", apply(effect, params.clone())), why, "{effect} {params}");
|
||||
}
|
||||
for (effect, params) in [
|
||||
("threeD.revolve", json!({"perspective": 40, "lightAzimuth": 30.5, "lightElevation": 0})),
|
||||
("path.outlineStroke", json!({"width": 10})),
|
||||
("stylize.scribble", json!({"variation": 2})),
|
||||
("blur.gaussian", json!({"linear": true})),
|
||||
("blur.gaussian", json!({})),
|
||||
("cropMarks", json!({"style": "japanese"})),
|
||||
("cropMarks", json!({})),
|
||||
] {
|
||||
assert!(check_params("effect.apply", &apply(effect, params.clone())).is_ok(), "{effect} {params}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `effect.apply`, `appearance.addEffect` and `effect.setParams` refuse such a parameter of the
|
||||
/// wrong type when they run, and so does a step of a `command.batch` whose effect a step before it
|
||||
/// added, with the document and its undo history as they were. A saved step still plays with it,
|
||||
/// as with a key the effect no longer takes.
|
||||
#[test]
|
||||
fn effect_commands_refuse_an_undefaulted_parameter_of_the_wrong_type() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap();
|
||||
let state = |s: &Session| {
|
||||
let st = s.doc().unwrap();
|
||||
(st.history.undo.len(), st.doc.clone())
|
||||
};
|
||||
let before = state(&s);
|
||||
let apply = |effect: &str, params| json!({"effect": effect, "params": params});
|
||||
for (id, params, why) in [
|
||||
("effect.apply", apply("path.outlineStroke", json!({"width": "10 pt"})), "`width` must be a number"),
|
||||
("appearance.addEffect", apply("blur.gaussian", json!({"linear": 1})), "`linear` must be true or false"),
|
||||
("effect.apply", apply("threeD.revolve", json!({"perspective": "50"})), "`perspective` must be a number"),
|
||||
] {
|
||||
let e = s.execute(id, ¶ms).unwrap_err().to_string();
|
||||
assert!(e.contains(why), "{id} {params}: {e}");
|
||||
assert_eq!(state(&s), before, "{id} {params}");
|
||||
}
|
||||
let batch = json!({"commands": [
|
||||
{"command": "effect.apply", "params": apply("path.outlineStroke", json!({"width": 3}))},
|
||||
{"command": "effect.setParams", "params": {"index": 0, "params": {"width": "10 pt"}}},
|
||||
]});
|
||||
let e = s.execute("command.batch", &batch).unwrap_err().to_string();
|
||||
assert!(e.contains("`width` must be a number"), "{e}");
|
||||
assert_eq!(state(&s), before);
|
||||
s.execute("effect.apply", &apply("path.outlineStroke", json!({"width": 3}))).unwrap();
|
||||
let applied = state(&s);
|
||||
for params in [json!({"width": "10 pt"}), json!({"width": null}), json!({"width": true})] {
|
||||
let e = s.execute("effect.setParams", &json!({"index": 0, "params": params.clone()})).unwrap_err().to_string();
|
||||
assert!(e.contains("`width` must be a number"), "{params}: {e}");
|
||||
assert_eq!(state(&s), applied, "{params}");
|
||||
}
|
||||
s.execute("effect.setParams", &json!({"index": 0, "params": {"width": 4}})).unwrap();
|
||||
s.replay("effect.setParams", &json!({"index": 0, "params": {"retired": "x", "width": "10 pt"}})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(st.doc.layers[0].children().unwrap()[0].appearance.effects[0].params["width"], json!("10 pt"));
|
||||
}
|
||||
|
||||
/// The Transform effect (`distort.transform`) is held to its params description: `effect.apply`
|
||||
/// and `effect.setParams` refuse each of its on/off options given as text, such as `reflectX:
|
||||
/// "false"`, and each of its numbers given as text, such as `copies: "0"`, and leave the effect
|
||||
|
||||
@@ -235,8 +235,16 @@ fn live_effect_plugins_reshape_editable_art_and_expand() {
|
||||
run(&mut s, "select.all", json!({}));
|
||||
run(&mut s, "object.move", json!({"dx": 50, "dy": 0}));
|
||||
assert_eq!(effected(&s), *node(&s, a).path_data().unwrap());
|
||||
// A parameter of another type than the manifest gives it is refused.
|
||||
let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len());
|
||||
let wrong = json!({"ids": [a.0], "effect": "plugin.test.engine.tri", "params": {"size": "4 pt"}});
|
||||
let e = s.execute("effect.apply", &wrong).unwrap_err().to_string();
|
||||
assert!(e.contains("`size` must be a number"), "{e}");
|
||||
assert_eq!((s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()), before);
|
||||
run(&mut s, "effect.apply", json!({"ids": [a.0], "effect": "plugin.test.engine.tri", "params": {"size": 4}}));
|
||||
assert_eq!(effected(&s), triangle());
|
||||
let e = s.execute("effect.setParams", &json!({"ids": [a.0], "index": 1, "params": {"size": true}})).unwrap_err().to_string();
|
||||
assert!(e.contains("`size` must be a number"), "{e}");
|
||||
assert_eq!(node(&s, a).appearance.effects[1].params, json!({"size": 4}));
|
||||
// Uninstalled: the geometry is drawn as it is.
|
||||
run(&mut s, "plugin.remove", json!({"id": "test.engine.tri"}));
|
||||
|
||||
Reference in New Issue
Block a user