From 3d6fb16db13a7f800890b3c96f15d46eac8b8fae Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 25 Aug 2020 15:23:59 -0700 Subject: [PATCH] Finally hit the point where unit testing lane_specs_ltr is necessary... --- map_model/src/make/initial/lane_specs.rs | 86 +++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/map_model/src/make/initial/lane_specs.rs b/map_model/src/make/initial/lane_specs.rs index b6dbe85e6c..b349534ee3 100644 --- a/map_model/src/make/initial/lane_specs.rs +++ b/map_model/src/make/initial/lane_specs.rs @@ -5,6 +5,7 @@ use abstutil::Tags; use geom::Distance; use std::iter; +#[derive(PartialEq)] pub struct LaneSpec { pub lt: LaneType, pub dir: Direction, @@ -35,7 +36,6 @@ fn back(lt: LaneType) -> LaneSpec { } } -// TODO This is ripe for unit testing. pub fn get_lane_specs_ltr(tags: &Tags) -> Vec { // Easy special cases first. if tags.is_any("railway", vec!["light_rail", "rail"]) { @@ -266,3 +266,87 @@ pub fn get_lane_specs_ltr(tags: &Tags) -> Vec { back_side.extend(fwd_side); back_side } + +#[cfg(test)] +mod tests { + use super::*; + + fn lt_to_char(lt: LaneType) -> &'static str { + match lt { + LaneType::Driving => "d", + LaneType::Biking => "b", + LaneType::Bus => "B", + LaneType::Parking => "p", + LaneType::Sidewalk => "s", + LaneType::Shoulder => "S", + LaneType::SharedLeftTurn => "C", + LaneType::Construction => "x", + LaneType::LightRail => "l", + } + } + + fn tags(kv: Vec<&str>) -> Tags { + let mut tags = Tags::new(std::collections::BTreeMap::new()); + for pair in kv { + let parts = pair.split('=').collect::>(); + tags.insert(parts[0], parts[1]); + } + tags + } + + #[test] + fn test_osm_to_specs() { + let mut ok = true; + for (input, expected_lt, expected_dir) in vec![ + ( + // https://www.openstreetmap.org/way/428294122 + vec![ + "lanes=2", + "oneway=yes", + "sidewalk=both", + "cycleway:left=lane", + ], + "sbdds", + "v^^^^", + ), + ( + // https://www.openstreetmap.org/way/8591383 + vec![ + "lanes=1", + "oneway=yes", + "sidewalk=both", + "cycleway:left=track", + "oneway:bicycle=no", + ], + "sbbds", + "vv^^^", + ), + ] { + let actual = get_lane_specs_ltr(&tags(input.clone())); + let actual_lt = actual + .iter() + .map(|s| lt_to_char(s.lt)) + .collect::>() + .join(""); + let actual_dir = actual + .iter() + .map(|s| if s.dir == Direction::Fwd { "^" } else { "v" }) + .collect::>() + .join(""); + if actual_lt != expected_lt || actual_dir != expected_dir { + ok = false; + println!("For input:"); + for kv in input { + println!(" {}", kv); + } + println!("Got:"); + println!(" {}", actual_lt); + println!(" {}", actual_dir); + println!("Expected:"); + println!(" {}", expected_lt); + println!(" {}", expected_dir); + } + } + assert!(ok); + } +}