#[cfg(test)] mod tests { use super::*; use tilecodec::mamaps::dict; fn station_mode(pairs: &[(&str, &str)]) -> &'static str { super::station_detail(pairs) } /// One `LineString` per line, written by `transit_shapes`. fn feature(color: &str, mode: &str, coords: &str) -> String { format!( "{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"LineString\",\ \"coordinates\":[{coords}]}},\"properties\":{{\"color\":\"{color}\",\ \"mode\":\"{mode}\"}}}}" ) } /// The same, carrying the corridor lane inputs. fn slotted(color: &str, mode: &str, ordinal: u8, lanes: u8, taper: u8, coords: &str) -> String { format!( "{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"LineString\",\ \"coordinates\":[{coords}]}},\"properties\":{{\"color\":\"{color}\",\ \"mode\":\"{mode}\",\"ordinal\":{ordinal},\"lanes\":{lanes},\ \"taper\":{taper}}}}}" ) } fn scratch(tag: &str) -> std::path::PathBuf { std::env::temp_dir().join(format!("mamaps_transit_{tag}_{}", std::process::id())) } #[test] fn every_mode_has_a_class_and_they_all_share_one_floor() { for mode in MODES { let class = transit_class(mode, false).expect("a class"); assert_eq!(class.layer, dict::LAYER_TRANSIT); assert_eq!(dict::KINDS[class.kind as usize - 1], "rail"); assert_eq!(dict::DETAILS[class.kind_detail as usize - 1], *mode); assert!(!class.area, "a line, even looped"); // The whole network appears at once; see [`MIN_ZOOM`]. The style's `transit-rail` // carries the same 8, and a floor deeper than that here would leave it asking for // features the archive does not hold. assert_eq!(class.min_zoom, 8, "{mode}"); } assert!(transit_class("bus", false).is_none()); } #[test] fn an_intercity_line_surfaces_at_country_zoom() { let class = transit_class("train", true).expect("a class"); assert_eq!(dict::DETAILS[class.kind_detail as usize - 1], "intercity"); assert_eq!(class.min_zoom, 3); assert_eq!(dict::KINDS[class.kind as usize - 1], "rail", "still drawn by transit-rail"); assert!(transit_class("bus", true).is_none(), "still only the rail modes"); let path = scratch("intercity.geojsonseq"); let coords = "[-122.4,37.7],[-118.2,34.0]"; std::fs::write( &path, format!( "{}\n{}\n", feature("0057A8", "train", coords) .replace("\"mode\":\"train\"", "\"mode\":\"train\",\"intercity\":1"), feature("0057A8", "train", coords), ), ) .expect("write"); let spill = scratch("intercity.features"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert_eq!(stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).expect("read"), 2); let store = sink.finish(&spill).expect("finish"); let mut reader = store.reader().expect("reader"); let floors: Vec = std::iter::from_fn(|| reader.next().expect("read")).map(|f| f.class.min_zoom).collect(); assert_eq!(floors, vec![3, 8], "flagged, then an ordinary line"); std::fs::write( &path, feature("0057A8", "train", coords) .replace("\"mode\":\"train\"", "\"mode\":\"train\",\"intercity\":2") + "\n", ) .expect("write"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert!( stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).is_err(), "2 is not a flag" ); let _ = std::fs::remove_file(&path); let _ = std::fs::remove_file(&spill); } #[test] fn colours_are_nonzero_bare_hex() { assert_eq!(hex_rrggbb("E4002B"), Some(0xE4002B)); assert_eq!(hex_rrggbb(" e4002b "), Some(0xE4002B)); assert_eq!(hex_rrggbb("#E4002B"), None, "the exporter writes bare hex"); assert_eq!(hex_rrggbb("F00"), None, "no short form"); assert_eq!(hex_rrggbb("GGGGGG"), None); // Zero means "no colour" on the wire, so it must never reach `push_transit`. assert_eq!(hex_rrggbb("000000"), None); } #[test] fn a_prepared_route_streams_in_as_a_coloured_transit_line() { let path = scratch("routes.geojsonseq"); std::fs::write( &path, format!( "{}\n{}\n", feature("0054A5", "subway", "[-122.4,37.7],[-122.39,37.71]"), feature("E31E24", "light_rail", "[-122.4,37.7],[-122.41,37.72]"), ), ) .expect("write"); let spill = scratch("routes.features"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert_eq!(stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).expect("read"), 2,); let store = sink.finish(&spill).expect("finish"); let mut reader = store.reader().expect("reader"); let first = reader.next().expect("read").expect("a feature"); assert_eq!(first.class.layer, dict::LAYER_TRANSIT); assert_eq!(dict::KINDS[first.class.kind as usize - 1], "rail"); assert_eq!(dict::DETAILS[first.class.kind_detail as usize - 1], "subway"); assert_eq!(first.transit_color, 0x00_54_A5, "the agency's own colour, verbatim"); assert_eq!( (first.transit_ordinal, first.transit_lanes, first.transit_taper), (0, 1, 255), "absent lane inputs are a route on its own alignment", ); let second = reader.next().expect("read").expect("a feature"); assert_eq!(second.transit_color, 0xE3_1E_24); assert_eq!(second.class.min_zoom, 8, "the network's single floor"); let _ = std::fs::remove_file(&path); let _ = std::fs::remove_file(&spill); } /// The lane inputs ride through to the feature untouched — the renderer, not the exporter, /// decides how many lanes to use, so nothing here clamps the count. #[test] fn the_corridor_lane_inputs_stream_through_to_the_feature() { let path = scratch("slots.geojsonseq"); std::fs::write( &path, format!( "{}\n{}\n{}\n", slotted("0054A5", "subway", 0, 10, 255, "[-122.4,37.7],[-122.39,37.71]"), slotted("0054A5", "subway", 9, 10, 128, "[-122.4,37.7],[-122.39,37.71]"), slotted("0054A5", "subway", 0, 1, 255, "[-122.4,37.7],[-122.39,37.71]"), ), ) .expect("write"); let spill = scratch("slots.features"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert_eq!(stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).expect("read"), 3); let store = sink.finish(&spill).expect("finish"); let mut reader = store.reader().expect("reader"); let mut read = Vec::new(); while let Some(feature) = reader.next().expect("read") { read.push((feature.transit_ordinal, feature.transit_lanes, feature.transit_taper)); } assert_eq!(read, vec![(0, 10, 255), (9, 10, 128), (0, 1, 255)]); let _ = std::fs::remove_file(&path); let _ = std::fs::remove_file(&spill); } #[test] fn a_lane_input_that_is_not_a_byte_is_an_error() { let spill = scratch("badslot.features"); let path = scratch("badslot.geojsonseq"); for body in ["\"ordinal\":\"left\"", "\"ordinal\":1.5", "\"lanes\":99999", "\"taper\":-1"] { std::fs::write( &path, format!( "{{\"type\":\"Feature\",\"geometry\":{{\"type\":\"LineString\",\ \"coordinates\":[[-122.4,37.7],[-122.39,37.71]]}},\"properties\":\ {{\"color\":\"0054A5\",\"mode\":\"subway\",{body}}}}}\n" ), ) .expect("write"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert!( stream_routes(&path, (-180.0, -90.0, 180.0, 90.0), &mut sink).is_err(), "{body} was accepted", ); } let _ = std::fs::remove_file(&path); let _ = std::fs::remove_file(&spill); } /// The same clip `earth` does, for the same reason: the file covers whatever regions were /// exported, and a corridor build must not carry lines from outside it. #[test] fn a_route_outside_the_build_area_is_not_carried() { let path = scratch("clip.geojsonseq"); std::fs::write( &path, format!( "{}\n{}\n", feature("0054A5", "subway", "[-122.4,37.7],[-122.39,37.71]"), feature("0054A5", "train", "[-9.0,38.0],[-8.9,38.1]"), ), ) .expect("write"); let spill = scratch("clip.features"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); let california = (-125.0, 32.0, -114.0, 42.0); assert_eq!(stream_routes(&path, california, &mut sink).expect("read"), 1); // And an area with no route in it is an error, not an empty layer. let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert!(stream_routes(&path, (-160.0, 0.0, -150.0, 10.0), &mut sink).is_err()); let _ = std::fs::remove_file(&path); let _ = std::fs::remove_file(&spill); } /// Every way this file can be the wrong file. Each is an error naming the line, because a /// transit layer that is quietly half a city is what this change exists to stop shipping. #[test] fn a_line_this_cannot_read_is_an_error_rather_than_a_skip() { let spill = scratch("bad.features"); let world = (-180.0, -90.0, 180.0, 90.0); for (tag, body) in [ ("nonsense", "not json at all".to_string()), ( "polygon", "{\"type\":\"Feature\",\"properties\":{\"color\":\"0054A5\",\"mode\":\"subway\"},\ \"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],\ [1.0,1.0],[0.0,0.0]]]}}" .to_string(), ), ("no_mode", feature("0054A5", "", "[-122.4,37.7],[-122.39,37.71]")), ("bus", feature("0054A5", "bus", "[-122.4,37.7],[-122.39,37.71]")), ("black", feature("000000", "subway", "[-122.4,37.7],[-122.39,37.71]")), ("no_colour", feature("nope", "subway", "[-122.4,37.7],[-122.39,37.71]")), ] { let path = scratch(&format!("bad_{tag}.geojsonseq")); std::fs::write(&path, format!("{body}\n")).expect("write"); let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert!(stream_routes(&path, world, &mut sink).is_err(), "{tag} was accepted"); let _ = std::fs::remove_file(&path); } // A missing file too, rather than an empty layer. let mut sink = crate::store::Sink::create(&spill).expect("sink"); assert!(stream_routes(Path::new("no_such_routes.geojsonseq"), world, &mut sink).is_err()); let _ = std::fs::remove_file(&spill); } #[test] fn stations_name_their_mode() { assert_eq!(station_mode(&[("railway", "station")]), "station"); assert_eq!(station_mode(&[("railway", "halt")]), "halt"); assert_eq!(station_mode(&[("railway", "station"), ("station", "subway")]), "subway"); assert_eq!( station_mode(&[("station", "light_rail")]), "light_rail", "a station tag alone counts" ); } }