Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/engine/src/cmd/layout/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ pub(in crate::cmd::layout) fn update_inputs(input: &crate::cmd::InputContext<'_>
}

/// The layers a legend's `exclude`, `layerOrder`, `columnBreaks`, `labels` and `classLabels` name,
/// as inputs. Tile layers' names are stored selector text, not invented layer/table identities.
/// as inputs: every layer of a name, in any case, as the legend applies its lists, each once. Tile
/// layers' names are stored selector text, not invented layer/table identities.
pub(in crate::cmd::layout) fn legend_inputs(
inputs: &mut crate::cmd::Inputs,
map: &Map,
Expand All @@ -279,7 +280,9 @@ pub(in crate::cmd::layout) fn legend_inputs(
.chain(breaks.iter().flatten())
.chain(labels.iter().flatten().map(|(n, _)| n))
.chain(classes.iter().flatten().map(|(n, _)| n));
inputs.layers.extend(named.filter_map(|n| map.layer_by_name(n)).map(|l| (map.instance_id(), l.id)));
// Walking the map's layers, not the names, binds a layer two lists name once.
let reached = map.layers.iter().filter(|l| named.clone().any(|n| l.name.eq_ignore_ascii_case(n)));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
inputs.layers.extend(reached.map(|l| (map.instance_id(), l.id)));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
inputs.state = json!({"exclude": names, "layerOrder": order, "columnBreaks": breaks, "labels": labels, "classLabels": classes});
}

Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ mod layout_legend_breaks;
#[path = "tests/layout_legend_row.rs"]
mod layout_legend_row;

#[path = "tests/layout_legend_shared_names.rs"]
mod layout_legend_shared_names;

#[path = "tests/layout_legend_subtitle.rs"]
mod layout_legend_subtitle;

Expand Down
76 changes: 76 additions & 0 deletions crates/engine/src/tests/layout_legend_shared_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! A legend's lists name layers, and reach every layer of the name (#3288).

use super::*;

/// A world layout and its legend.
fn laid_out() -> (Session, u64) {
let mut s = world();
s.run("layout.new", &json!({"template": "map"})).unwrap();
let legend = s.project.layouts[0].elements.iter().find(|e| e.kind.code() == "legend").unwrap().id.into();
(s, legend)
}

/// The active map's layer `name`, as inputs bind it.
fn binding(s: &Session, name: &str) -> (u64, u32) {
(s.map().instance_id(), s.map().layer_by_name(name).unwrap().id)
}

/// The layers `id` binds for `p`, in order.
fn bound(s: &Session, id: &str, p: &Value) -> Vec<(u64, u32)> {
let spec = crate::cmd::all().into_iter().find(|c| c.id == id).unwrap();
let mut layers = spec.inputs.unwrap()(&crate::cmd::InputSource::input_context(s), p).unwrap().layers;
layers.sort_unstable();
layers
}

/// Both commands that give a legend its lists, each with `given` among its parameters.
fn calls(legend: u64, given: &Value) -> [(&'static str, Value); 2] {
let mut add = given.clone();
add["kind"] = json!("legend");
let mut update = given.clone();
update["element"] = json!(legend);
[("layout.addElement", add), ("layout.update", update)]
}

#[test]
fn every_legend_list_binds_all_the_layers_of_a_name() {
let (mut s, legend) = laid_out();
// Taken while each still has a name of its own, then Cities is renamed to share one.
let mut both = vec![binding(&s, "Rivers"), binding(&s, "Cities")];
both.sort_unstable();
s.run("layer.rename", &json!({"layer": "Cities", "name": "Rivers"})).unwrap();
for given in [
json!({"exclude": ["Rivers"]}),
json!({"layerOrder": ["Rivers"]}),
json!({"columnBreaks": ["Rivers"]}),
json!({"labels": {"Rivers": "Water"}}),
json!({"classLabels": {"Rivers": null}}),
json!({"exclude": ["RIVERS"]}),
// Two lists that name them bind each once.
json!({"exclude": ["Rivers"], "labels": {"rivers": "Water"}}),
] {
for (id, p) in calls(legend, &given) {
assert_eq!(bound(&s, id, &p), both, "{id} {p}");
}
}
}

#[test]
fn a_legend_list_binds_only_the_layers_of_the_names_it_gives() {
let (mut s, legend) = laid_out();
// A layer of the same name in another map isn't the frame's.
let mut second = s.map().clone();
second.name = "Second".into();
second.renew_instance_id();
for l in &mut second.layers {
l.renew_instance_id();
}
s.project.maps.push(second);
let one = vec![binding(&s, "Rivers")];
for (id, p) in calls(legend, &json!({"exclude": ["Rivers"]})) {
assert_eq!(bound(&s, id, &p), one, "{id} {p}");
}
for (id, p) in calls(legend, &json!({})) {
assert_eq!(bound(&s, id, &p), Vec::new(), "{id} {p}");
}
}
Loading