diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 4c911e6be0..da7734edcb 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -67,6 +67,8 @@ pub mod color_cmds; pub mod color_to_alpha_cmds; pub mod comps_cmds; pub mod contour_lib_cmds; +#[cfg(test)] +mod count_record_label_tests; pub mod cutout_cmds; pub mod distort_cmds; pub mod document_preset_cmds; diff --git a/crates/engine/src/cmd/analysis_cmds.rs b/crates/engine/src/cmd/analysis_cmds.rs index 0bf2a335ad..f9c62bf0f4 100644 --- a/crates/engine/src/cmd/analysis_cmds.rs +++ b/crates/engine/src/cmd/analysis_cmds.rs @@ -1012,7 +1012,9 @@ fn record(s: &mut Session, p: &Value) -> Result { other => return Err(bad(CMD, format!("unknown source `{other}` (auto|selection|ruler|count)"))), }; // Numbered only once the rows exist, so a failed measurement does not use up a label. - let label = format!("Measurement {}", s.analysis.next_measurement + 1); + // A count's row is "Count N" (#11775); the number is the shared one. + let word = if source == "count" { "Count" } else { "Measurement" }; + let label = format!("{word} {}", s.analysis.next_measurement + 1); let now = p.get("dateTime").and_then(Value::as_str).map(str::to_string).unwrap_or_else(now_iso); let factor = d.measurement.scale.factor(); let mut rows: Vec> = Vec::new(); diff --git a/crates/engine/src/cmd/count_record_label_tests.rs b/crates/engine/src/cmd/count_record_label_tests.rs new file mode 100644 index 0000000000..4ee40729de --- /dev/null +++ b/crates/engine/src/cmd/count_record_label_tests.rs @@ -0,0 +1,41 @@ +//! #11775: Record Measurements labels a Count tool row "Count N"; selection and ruler rows keep +//! their labels, and N is the number the row gets today (the shared counter). + +use serde_json::{Value, json}; + +use crate::Session; + +fn five_counts() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); + for i in 0..5 { + s.execute("count.add", json!({"x": 10 + 10 * i, "y": 10})).unwrap(); + } + s +} + +fn record(s: &mut Session, source: &str) -> Value { + let r = s.execute("image.analysis.recordMeasurements", json!({"source": source})).unwrap(); + r["rows"][0]["values"].clone() +} + +#[test] +fn a_count_row_is_labelled_count_n() { + for source in ["count", "auto"] { + let mut s = five_counts(); + let row = record(&mut s, source); + assert_eq!(row["label"], json!("Count 1"), "{source}: {row}"); + assert_eq!(row["count"], json!(5), "{source}: {row}"); + assert_eq!(row["source"], json!("Count Tool"), "{source}: {row}"); + } +} + +#[test] +fn other_rows_keep_their_labels_and_the_counter_is_shared() { + let mut s = five_counts(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); + let selection = record(&mut s, "selection"); + assert!(selection["label"].as_str().is_some_and(|l| !l.starts_with("Count")), "{selection}"); + let count = record(&mut s, "count"); + assert_eq!(count["label"], json!("Count 2"), "{count}"); +}