From 28aab01e7784450cdfafdb3e87e448364e35bfaa Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 5 Mar 2021 23:34:38 +0100 Subject: [PATCH] Added first unit tests --- Makefile | 6 ++++++ src/hello/mod.rs | 17 ++++++++++++++++- src/hello/routes.rs | 14 -------------- src/hello/tests.rs | 24 ++++++++++++++++++++++++ src/ivago/controller.rs | 1 - src/ivago/{routes.rs => tests.rs} | 0 src/main.rs | 6 +++--- 7 files changed, 49 insertions(+), 19 deletions(-) delete mode 100644 src/hello/routes.rs create mode 100644 src/hello/tests.rs delete mode 100644 src/ivago/controller.rs rename src/ivago/{routes.rs => tests.rs} (100%) diff --git a/Makefile b/Makefile index 70d000e..79daa85 100644 --- a/Makefile +++ b/Makefile @@ -23,4 +23,10 @@ image: Dockerfile # Run run: @ cargo run +.PHONY: run + +# Testing +test: + @ cargo test +.PHONY: test diff --git a/src/hello/mod.rs b/src/hello/mod.rs index 6a664ab..547276a 100644 --- a/src/hello/mod.rs +++ b/src/hello/mod.rs @@ -1 +1,16 @@ -pub mod routes; +#[cfg(test)] mod tests; + +#[get("/world")] +pub fn world() -> &'static str { + "Hello, world!" +} + +#[get("/")] +pub fn hello(name: String) -> String { + format!("Hello, {}", name) +} + +#[get("/world?&")] +pub fn name_age(name: String, age: u16) -> String { + format!("Hello, {} who is {} years old!", name, age) +} diff --git a/src/hello/routes.rs b/src/hello/routes.rs deleted file mode 100644 index c7482e9..0000000 --- a/src/hello/routes.rs +++ /dev/null @@ -1,14 +0,0 @@ -#[get("/world")] -pub fn world() -> &'static str { - "Hello, world!" -} - -#[get("/")] -pub fn hello(name: String) -> String { - format!("Hello, {}", name) -} - -#[get("/world?&")] -pub fn name_age(name: String, age: u16) -> String { - format!("Hello, {} who is {} years old!", name, age) -} diff --git a/src/hello/tests.rs b/src/hello/tests.rs new file mode 100644 index 0000000..83b122d --- /dev/null +++ b/src/hello/tests.rs @@ -0,0 +1,24 @@ +use rocket::local::Client; +use rocket::http::Status; + +fn rocket() -> rocket::Rocket { + rocket::ignite().mount("/", routes![super::world, super::hello, super::name_age]) +} + +#[test] +fn test_world() { + let client = Client::new(rocket()).expect("valid rocket instance"); + let mut response = client.get("/world").dispatch(); + + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some("Hello, world!".into())); +} + +#[test] +fn test_hello() { + let client = Client::new(rocket()).expect("valid rocket instance"); + let mut response = client.get("/thisisaname").dispatch(); + + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some("Hello, thisisaname".into())); +} diff --git a/src/ivago/controller.rs b/src/ivago/controller.rs deleted file mode 100644 index 0b3b0a3..0000000 --- a/src/ivago/controller.rs +++ /dev/null @@ -1 +0,0 @@ -mod ivago.controller; diff --git a/src/ivago/routes.rs b/src/ivago/tests.rs similarity index 100% rename from src/ivago/routes.rs rename to src/ivago/tests.rs diff --git a/src/main.rs b/src/main.rs index 7f811c2..a4ce2c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,8 @@ mod hello; fn main() { rocket::ignite().mount("/hello", routes![ - hello::routes::world, - hello::routes::hello, - hello::routes::name_age + hello::world, + hello::hello, + hello::name_age ]).launch(); }