Skip to content
Merged
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
91 changes: 65 additions & 26 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8949,8 +8949,14 @@ fn field_floors_hold_for_every_command(shard: usize) {
}
}
};
let ids: Vec<&str> =
fixture.registry.commands.iter().enumerate().filter(|(i, _)| i % HOSTILE_SHARDS == shard).map(|(_, c)| c.id).collect();
let ids: Vec<(&str, Option<Vec<&str>>)> = fixture
.registry
.commands
.iter()
.enumerate()
.filter(|(i, _)| i % HOSTILE_SHARDS == shard)
.map(|(_, c)| (c.id, c.param_keys()))
.collect();
// With the junk, parameters that let commands which make features make them: a place in a
// point layer, features to paste, rows to copy from, so a path that adds is reached.
let cities = fixture.map().layer_by_name("Cities").map(|l| l.id).unwrap_or_default();
Expand All @@ -8962,11 +8968,11 @@ fn field_floors_hold_for_every_command(shard: usize) {
json!({"layer": cities, "text": point}),
json!({"layer": cities, "rows": [0, 1], "row": 0, "dx": 0.5, "dy": 0.5}),
]);
for id in ids {
for (id, keys) in ids {
if hostile_skips(id) || id == "map.route" {
continue;
}
for p in &params {
for p in distinct_params(keys.as_deref(), &params) {
let mut s = Session::new(fixture.project.clone());
let before = held(&s);
let _ = s.run(id, p);
Expand Down Expand Up @@ -9027,6 +9033,35 @@ fn field_floors_are_checked_on_a_command_that_adds() {
assert_eq!(added, floor + 1);
}

/// The parameters of `all` that a command taking `keys` can tell apart, for the sweeps that run
/// every command with each of them: of those that hold the same values for its keys, only the one
/// with the most keys besides, so what reads keys it doesn't name (a check of unexpected keys, a
/// tool's own parameters) still sees them. A command reads only the keys it names (checked by
/// [`params_name_every_key_a_command_reads`]), so the others would run it the same way again: in
/// the sweeps, the commands that take few keys and accept anything ran each of the ~80 junk
/// parameters, rebuilding their fixtures after each, for most of the sweeps' time. A command
/// whose description names no keys (`None`) gets them all.
fn distinct_params<'a>(keys: Option<&[&str]>, all: &'a [Value]) -> Vec<&'a Value> {
let Some(keys) = keys else { return all.iter().collect() };
let mut kept: Vec<(String, &Value)> = Vec::new();
for p in all {
let class = match p {
Value::Object(o) => {
Value::Object(o.iter().filter(|(k, _)| keys.contains(&k.as_str())).map(|(k, v)| (k.clone(), v.clone())).collect())
.to_string()
}
other => other.to_string(),
};
let width = |v: &Value| v.as_object().map_or(0, serde_json::Map::len);
match kept.iter_mut().find(|(c, _)| *c == class) {
Some(slot) if width(p) > width(slot.1) => slot.1 = p,
Some(_) => {}
None => kept.push((class, p)),
}
}
kept.into_iter().map(|(_, p)| p).collect()
}

/// The junk parameters every command is run with by [`hostile_params_never_panic`] and
/// [`field_floors_hold_for_every_command`].
fn hostile_junk() -> Vec<Value> {
Expand Down Expand Up @@ -9184,13 +9219,13 @@ fn hostile_params_never_panic(shard: usize) {
return;
}
let junk = hostile_junk();
let ids: Vec<&str> = Session::new(Project::default())
let ids: Vec<(&str, Option<Vec<&str>>)> = Session::new(Project::default())
.registry
.commands
.iter()
.enumerate()
.filter(|(i, _)| i % HOSTILE_SHARDS == shard)
.map(|(_, c)| c.id)
.map(|(_, c)| (c.id, c.param_keys()))
.collect();
// Shards share the process under `cargo test`: each streams its own file.
let streamed = grid_fgb(&format!("hostile-{shard}"), 20);
Expand Down Expand Up @@ -9227,7 +9262,7 @@ fn hostile_params_never_panic(shard: usize) {
.collect();
(s.states, s.unsaved_marks, s.opened, s.active_layer, s.tool, s.project.maps.len(), cam, layers)
};
for id in ids {
for (id, keys) in ids {
if hostile_skips(id) {
continue;
}
Expand All @@ -9237,7 +9272,7 @@ fn hostile_params_never_panic(shard: usize) {
let base_world = state_of(&s_world);
let base_raster = state_of(&s_raster);
let base_streamed = state_of(&s_streamed);
for p in &junk {
for p in distinct_params(keys.as_deref(), &junk) {
// Places given by name go to the public geocoder: with an answer given, the junk
// goes through reading it and the options, and nothing is fetched.
let routed;
Expand Down Expand Up @@ -56986,7 +57021,8 @@ fn a_pdf_is_added_back_as_a_raster() {
#[test]
fn a_pdf_with_layers_is_added_as_a_group_of_them() {
let mut s = world();
s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap();
// A small page: the reader draws each layer at 150 dpi, so an A4 page costs a minute of tests.
s.run("layout.new", &json!({"width": 50, "height": 50, "template": "map"})).unwrap();
let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos());
// Removed when the test ends, though an assertion fails.
struct Dir(std::path::PathBuf);
Expand Down Expand Up @@ -57156,7 +57192,8 @@ fn a_pdf_with_layers_is_added_as_a_group_of_them() {
fn an_uploaded_or_downloaded_pdf_with_layers_is_added_as_a_group_of_them() {
use base64::Engine;
let mut s = world();
s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap();
// A small page: the reader draws each layer at 150 dpi, so an A4 page costs a minute of tests.
s.run("layout.new", &json!({"width": 50, "height": 50, "template": "map"})).unwrap();
struct Dir(std::path::PathBuf);
impl Drop for Dir {
fn drop(&mut self) {
Expand Down Expand Up @@ -61284,34 +61321,36 @@ fn export_map_to_geotiff_places_the_view_on_the_ground() {
s.notices[n..].iter().find_map(|x| x.text.split("drawn at ").nth(1).and_then(|t| t.split(' ').next()).map(str::to_string));
(out, said)
};
s.view_size = (1200.0, 800.0);
// The fit follows the long side: the views below are a few points high, so the pictures that
// prove it stay small.
s.view_size = (1200.0, 80.0);
assert_eq!(fitted(&mut s, "small.tif").1, None);
assert_eq!(fitted(&mut s, "small.png").1, None);
s.view_size = (5000.0, 3000.0);
s.view_size = (5000.0, 30.0);
// 8192 × 96 / 5000 = 157.3: whole DPI, down.
let (out, said) = fitted(&mut s, "wide.tif");
assert_eq!((said.as_deref(), out["width"].as_u64()), (Some("157"), Some(8177)));
assert_eq!(fitted(&mut s, "wide.png").1.as_deref(), Some("157"));
// Past 8000 points the view is clamped first: 16000 wide fits 98 DPI.
s.view_size = (16000.0, 3000.0);
s.view_size = (16000.0, 30.0);
assert_eq!(fitted(&mut s, "huge.tif").1.as_deref(), Some("98"));
// A GeoTIFF of a turned view counts its box (3000 points square turned 45° is 4243); a PNG
// is drawn turned in the view's own size.
s.view_size = (3000.0, 3000.0);
// 3000 points fit 192 DPI (6000 pixels).
s.view_size = (3000.0, 30.0);
assert_eq!(fitted(&mut s, "square.tif").1, None);
s.run("map.setBearing", &json!({"bearing": 45})).unwrap();
assert_eq!(fitted(&mut s, "square-turned.tif").1.as_deref(), Some("185"));
assert_eq!(fitted(&mut s, "square-turned.png").1, None);
// A box just past 8192 pixels (8192.2) is rounded out, as the GeoTIFF's canvas is: lowered to
// 95 DPI, and written.
s.view_size = (5792.76, 5792.76);
let n = s.notices.len();
let out = s.run("share.exportMap", &json!({"path": dir.join("edge.tif").display().to_string(), "dpi": 96, "fit": true})).unwrap();
assert!(s.notices[n..].iter().any(|x| x.text.contains("drawn at 95 DPI")), "{out}");
// A GeoTIFF of a turned view counts its box, a PNG is drawn turned in the view's own size: 4095
// × 100 points turned 1° is a box 4096.12 wide, 8192.24 pixels at 192 DPI, rounded out as
// the GeoTIFF's canvas is (8193, one past the most): lowered to 191 DPI, and written. The PNG
// is 8190 pixels, and fits.
s.view_size = (4095.0, 100.0);
s.run("map.setBearing", &json!({"bearing": 1})).unwrap();
let (out, said) = fitted(&mut s, "edge.tif");
assert_eq!(said.as_deref(), Some("191"), "{out}");
assert!(out["bytes"].as_u64().is_some_and(|b| b > 0), "{out}");
assert_eq!(fitted(&mut s, "edge.png").1, None);
s.run("map.setBearing", &json!({"bearing": 0})).unwrap();
// The app's download and clipboard copy (no size given) fit too: 5000 points at 192 DPI is
// drawn at 157, and said.
s.view_size = (5000.0, 3000.0);
s.view_size = (5000.0, 30.0);
let n = s.notices.len();
let png = crate::cmd::share::map_png_fitted(&mut s, 192.0).unwrap();
assert_eq!(rustgis_render::decode_png(&png).map(|p| p.width()), Some(8177));
Expand Down
Loading