* claim: #5174 * component.create refuses parameters it does not take (#5174) A call with `bodies` (or a misspelled key) used to succeed and make an empty component. Unknown keys are now refused before anything changes, the error lists the accepted parameters, and `bodies` points to component.move_bodies and component.from_bodies.
This commit is contained in:
@@ -109,6 +109,12 @@ pub(super) fn valid_position(m: &Mat) -> bool {
|
||||
|
||||
pub(super) fn new_component(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "component.create";
|
||||
if let Some(params) = p.as_object()
|
||||
&& let Some(key) = params.keys().find(|key| !matches!(key.as_str(), "name" | "parent" | "parent_path" | "activate"))
|
||||
{
|
||||
let hint = if params.contains_key("bodies") { "; use component.move_bodies or component.from_bodies to move bodies" } else { "" };
|
||||
return Err(bad(cmd, format!("unknown parameter `{key}`; accepted parameters: name, parent, parent_path, activate{hint}")));
|
||||
}
|
||||
let parent = if p.get("parent").is_some() { component(s, p, "parent", cmd)? } else { s.active_component };
|
||||
let parent_path = if let Some(value) = p.get("parent_path") {
|
||||
let path = value
|
||||
|
||||
@@ -13,6 +13,45 @@ fn near(value: &Value, expected: f64) {
|
||||
assert!((value.as_f64().unwrap() - expected).abs() < 1e-6, "{value} != {expected}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn component_create_rejects_bodies_without_changing_design() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Body1"}));
|
||||
let doc = s.doc.clone();
|
||||
let active_path = s.active_occurrence_path.clone();
|
||||
let error = s.execute("component.create", &json!({"name":"Part","bodies":["Body1"]})).unwrap_err();
|
||||
assert!(matches!(&error, crate::EngineError::BadParams { cmd, .. } if cmd == "component.create"));
|
||||
let msg = error.to_string();
|
||||
for expected in ["bodies", "name, parent, parent_path, activate", "component.move_bodies", "component.from_bodies"] {
|
||||
assert!(msg.contains(expected), "{msg}");
|
||||
}
|
||||
assert_eq!(s.doc, doc);
|
||||
assert_eq!(s.active_component, 0);
|
||||
assert_eq!(s.active_occurrence_path, active_path);
|
||||
assert_eq!(s.doc.model_body_component(s.model.state().body("Body1").unwrap()), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn component_create_rejects_unknown_keys_and_accepts_documented_keys() {
|
||||
let mut s = Session::default();
|
||||
let (parent, occurrence) = created(&mut s, "Parent");
|
||||
let params = json!({"name":"Child","parent":parent,"parent_path":[occurrence],"activate":false});
|
||||
let mut invalid = params.clone();
|
||||
invalid["activte"] = json!(true);
|
||||
let doc = s.doc.clone();
|
||||
let error = s.execute("component.create", &invalid).unwrap_err().to_string();
|
||||
assert!(error.contains("activte"), "{error}");
|
||||
assert!(error.contains("name, parent, parent_path, activate"), "{error}");
|
||||
assert_eq!(s.doc, doc);
|
||||
assert_eq!(s.active_component, parent);
|
||||
assert_eq!(s.active_occurrence_path, Some(vec![occurrence]));
|
||||
let child = run(&mut s, "component.create", params);
|
||||
assert_eq!(child["name"], "Child");
|
||||
assert_eq!(child["parent"], parent);
|
||||
assert_eq!(s.active_component, parent);
|
||||
assert_eq!(s.active_occurrence_path, Some(vec![occurrence]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_copy_uses_selected_source_transform() {
|
||||
let mut s = Session::default();
|
||||
|
||||
Reference in New Issue
Block a user