From e4b8be3ba3bfd0a978bdb2bc2746943948a9f4b8 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 8 Apr 2021 22:53:29 +0200 Subject: [PATCH] [#13] added some more unit tests, hated life for a bit --- src/ivago/controller/structs/basic_date.rs | 1 + src/ivago/controller/structs/pickup_time.rs | 2 ++ src/ivago/controller/structs/street.rs | 28 +++++++++++++++++++-- tests/ivago.rs | 3 +++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/ivago/controller/structs/basic_date.rs b/src/ivago/controller/structs/basic_date.rs index 97ef3e4..7468da5 100644 --- a/src/ivago/controller/structs/basic_date.rs +++ b/src/ivago/controller/structs/basic_date.rs @@ -60,6 +60,7 @@ impl Serialize for BasicDate { mod tests { use super::*; + /// Tests catching a string with an invalid date #[test] fn test_invalid_date() { let val = "2012-13-12"; diff --git a/src/ivago/controller/structs/pickup_time.rs b/src/ivago/controller/structs/pickup_time.rs index 6f2249d..33718db 100644 --- a/src/ivago/controller/structs/pickup_time.rs +++ b/src/ivago/controller/structs/pickup_time.rs @@ -28,3 +28,5 @@ impl Serialize for PickupTime { s.end() } } + +// I'd put tests here, but there's barely anything to do diff --git a/src/ivago/controller/structs/street.rs b/src/ivago/controller/structs/street.rs index 1ca64ec..00f4de4 100644 --- a/src/ivago/controller/structs/street.rs +++ b/src/ivago/controller/structs/street.rs @@ -6,8 +6,19 @@ use std::convert::TryFrom; /// Represents a street pub struct Street { - pub name: String, - pub city: String, + name: String, + city: String, +} + +impl Street { + // This constructor just makes my life a bit easier during testing + #[cfg(test)] + fn new(name: String, city: String) -> Street { + Street { + name: name, + city: city, + } + } } impl From<&Street> for String { @@ -50,6 +61,7 @@ impl<'v> FromFormValue<'v> for Street { // This regex is pretty loose tbh, but not sure how I can make it more // strict right now let re = Regex::new(r"^(.+) \((.+)\)$").unwrap(); + match re.captures(&form_value.url_decode_lossy()) { None => Err(form_value), Some(caps) => Ok(Street { @@ -59,3 +71,15 @@ impl<'v> FromFormValue<'v> for Street { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_to_string() { + let street = Street::new(String::from("testname"), String::from("city")); + + assert_eq!(String::from("testname (city)"), String::from(&street)); + } +} diff --git a/tests/ivago.rs b/tests/ivago.rs index 619b9c7..d78501a 100644 --- a/tests/ivago.rs +++ b/tests/ivago.rs @@ -1,3 +1,4 @@ +/// In here, any non-unit tests are placed. use rocket::http::Status; use rocket::local::Client; @@ -12,9 +13,11 @@ fn test_404_response() { let response = client.get("/").dispatch(); assert_eq!(response.status(), Status::NotFound); + // TODO add text check as well } /// Test 404 on invalid parameters +// TODO make this check a 400 instead #[test] fn test_invalid_parameters() { let client = Client::new(rocket()).expect("valid rocket instance");