[#13] added some more unit tests, hated life for a bit

master
Jef Roosens 2021-04-08 22:53:29 +02:00
parent 2e73d88ae9
commit e4b8be3ba3
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 32 additions and 2 deletions

View File

@ -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";

View File

@ -28,3 +28,5 @@ impl Serialize for PickupTime {
s.end()
}
}
// I'd put tests here, but there's barely anything to do

View File

@ -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));
}
}

View File

@ -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");