Skip to content

Commit

Permalink
Start unit tests for LTS. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Sep 8, 2023
1 parent e91e502 commit 936e663
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ mod bike_ottawa;
mod parse;
mod speed_limit_only;
mod tags;
#[cfg(test)]
mod tests;
#[cfg(target_arch = "wasm32")]
mod wasm;

pub use bike_ottawa::bike_ottawa;
pub use speed_limit_only::speed_limit_only;
pub use tags::Tags;

#[derive(PartialEq, PartialOrd)]
#[derive(Debug, PartialEq, PartialOrd)]
pub enum LTS {
NotAllowed,
LTS1,
Expand Down
47 changes: 47 additions & 0 deletions lts/src/osm_unit_test_tool.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<title>OpenStreetMap Unit Test tool</title>
<script>
async function generate() {
document.getElementById("output").value = "";

let wayID = document.getElementById("wayID").value;
let url = `https://api.openstreetmap.org/api/0.6/way/${wayID}.json`;
try {
let resp = await fetch(url);
let json = await resp.json();
let tags = json.elements[0].tags;

// Remove tags likely to not influence the output at all
delete tags["name"];
let tagsArray = Object.entries(tags)
.map(([k, v]) => `"${k}=${v}"`)
.join(", ");

document.getElementById(
"output"
).value = `(${wayID}, vec![${tagsArray}], LTS::),`;
} catch (err) {
document.getElementById("output").value = `Error: ${err}`;
}
}

function copy() {
navigator.clipboard.writeText(document.getElementById("output").value);
}
</script>
</head>
<body>
<h1>OpenStreetMap Unit Test tool</h1>
<label for="wayID">Enter OpenStreetMap Way ID:</label>
<input type="text" id="wayID" />
<button onclick="generate()">Generate</button>
<br />

<h2>Output:</h2>
<button onclick="copy()">Copy</button>
<br />
<textarea id="output" rows="10" cols="100" readonly></textarea>
</body>
</html>
24 changes: 24 additions & 0 deletions lts/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{bike_ottawa, Tags, LTS};

#[test]
fn test_bike_ottawa() {
// Use osm_unit_test_tool.html (open the file in your browser) to help generate
for (way_id, input, expected_lts) in vec![(
170171587,
vec!["bicycle=yes", "foot=yes", "highway=footway"],
LTS::LTS1,
)] {
let mut tags = Tags::new();
for kv in input {
let parts = kv.split("=").collect::<Vec<_>>();
tags.insert(parts[0], parts[1]);
}
let (actual_lts, _) = bike_ottawa(tags);
if actual_lts != expected_lts {
panic!(
"For http://openstreetmap.org/way/{way_id}, got {:?} but expected {:?}",
actual_lts, expected_lts
);
}
}
}

0 comments on commit 936e663

Please sign in to comment.