From 438063ac6bef143cdcc923e229600f5da696f18a Mon Sep 17 00:00:00 2001 From: laurens Date: Sun, 13 Sep 2020 20:04:26 +0200 Subject: [PATCH] refactor rectangles to have lib.rs to allow integration tests --- rectangles/src/lib.rs | 121 ++++++++++++++++++++++++ rectangles/src/main.rs | 135 +-------------------------- rectangles/tests/integration_test.rs | 6 ++ 3 files changed, 129 insertions(+), 133 deletions(-) create mode 100644 rectangles/src/lib.rs create mode 100644 rectangles/tests/integration_test.rs diff --git a/rectangles/src/lib.rs b/rectangles/src/lib.rs new file mode 100644 index 0000000..524c404 --- /dev/null +++ b/rectangles/src/lib.rs @@ -0,0 +1,121 @@ +#[derive(Debug)] +pub struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + pub fn build_rectangle(width: u32, height: u32) -> Rectangle { + Rectangle { width, height } + } + + pub fn square(size: u32) -> Rectangle { + Rectangle::build_rectangle(size, size) + } + + pub fn area(&self) -> u32 { + self.width * self.height + } + + pub fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } + + // convenience function to show use of Result in tests + fn return_result(&self, is_succes: bool) -> Result<(), ()> { + if is_succes { + Ok(()) + } else { + Err(()) + } + } + + // convenience function to show panic used in tests + fn return_panic(&self, is_panic: bool) { + if is_panic { + panic!("HEEELP"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn square() { + let sq = Rectangle::square(4); + + assert_eq!(sq.width, sq.height); + } + + #[test] + fn area() { + let width = 4; + let height = 5; + let rect = Rectangle::build_rectangle(width, height); + + assert_eq!(rect.area(), width * height); + } + + #[test] + fn larger_can_hold_smaller() { + let smaller = Rectangle::build_rectangle(3, 4); + let larger = Rectangle::build_rectangle(5, 6); + + assert!(larger.can_hold(&smaller)); + } + + #[test] + fn smaller_cannot_hold_larger() { + let smaller = Rectangle::build_rectangle(3, 4); + let larger = Rectangle::build_rectangle(5, 6); + + assert!(!smaller.can_hold(&larger)); + } + + #[test] + fn result_success() -> Result<(), ()> { + let rect = Rectangle::build_rectangle(3, 4); + + rect.return_result(true)?; + + Ok(()) + } + + #[test] + #[should_panic(expected = "HEEELP")] + fn result_panic() { + let rect = Rectangle::build_rectangle(3, 4); + + rect.return_panic(true); + } + + #[test] + #[ignore] + fn result_will_fail_with_result() -> Result<(), ()> { + let rect = Rectangle::build_rectangle(3, 4); + + rect.return_result(false)?; + + Ok(()) + } + + #[test] + #[ignore] + #[should_panic] + fn result_will_not_panic_and_fail() { + let rect = Rectangle::build_rectangle(3, 4); + + rect.return_panic(false); + } + + #[test] + #[ignore] + #[should_panic(expected = "HEEEEEEEEEEEEEEEEEEEELP")] + fn result_will_panic_but_with_wrong_message_and_fail() { + let rect = Rectangle::build_rectangle(3, 4); + + rect.return_panic(false); + } +} diff --git a/rectangles/src/main.rs b/rectangles/src/main.rs index ba131a0..424a137 100644 --- a/rectangles/src/main.rs +++ b/rectangles/src/main.rs @@ -1,43 +1,4 @@ -#[derive(Debug)] -struct Rectangle { - width: u32, - height: u32, -} - -impl Rectangle { - fn build_rectangle(width: u32, height: u32) -> Rectangle { - Rectangle { width, height } - } - - fn square(size: u32) -> Rectangle { - Rectangle::build_rectangle(size, size) - } - - fn area(&self) -> u32 { - self.width * self.height - } - - fn can_hold(&self, other: &Rectangle) -> bool { - self.width > other.width && self.height > other.height - } - - // convenience function to show use of Result in tests - fn return_result(&self, is_succes: bool) -> Result<(), ()> { - if is_succes { - Ok(()) - } else { - Err(()) - } - } - - // convenience function to show panic used in tests - fn return_panic(&self, is_panic: bool) { - if is_panic { - panic!("HEEELP"); - } - } - -} +use rectangles::Rectangle; fn main() { let width1 = 30; @@ -55,15 +16,7 @@ fn main() { area_tuple(rect) ); - let rect = Rectangle { - width: 30, - height: 50, - }; - - println!( - "The area of the rectangle is {} square pixels.", - area_struct(&rect) - ); + let rect = Rectangle::build_rectangle(30, 50); println!( "The area of the rectangle is {} square pixels.", @@ -92,87 +45,3 @@ fn area(width: u32, height: u32) -> u32 { fn area_tuple(dimensions: (u32, u32)) -> u32 { dimensions.0 * dimensions.1 } - -fn area_struct(rect: &Rectangle) -> u32 { - rect.width * rect.height -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn square() { - let sq = Rectangle::square(4); - - assert_eq!(sq.width, sq.height); - } - - #[test] - fn area() { - let width = 4; - let height = 5; - let rect = Rectangle::build_rectangle(width, height); - - assert_eq!(rect.area(), width * height); - } - - #[test] - fn larger_can_hold_smaller() { - let smaller = Rectangle::build_rectangle(3, 4); - let larger = Rectangle::build_rectangle(5, 6); - - assert!(larger.can_hold(&smaller)); - } - - #[test] - fn smaller_cannot_hold_larger() { - let smaller = Rectangle::build_rectangle(3, 4); - let larger = Rectangle::build_rectangle(5, 6); - - assert!(!smaller.can_hold(&larger)); - } - - #[test] - fn result_success() -> Result<(), ()> { - let rect = Rectangle::build_rectangle(3, 4); - - rect.return_result(true)?; - - Ok(()) - } - - #[test] - fn result_will_fail_with_result() -> Result<(), ()> { - let rect = Rectangle::build_rectangle(3, 4); - - rect.return_result(false)?; - - Ok(()) - } - - #[test] - #[should_panic(expected = "HEEELP")] - fn result_panic() { - let rect = Rectangle::build_rectangle(3, 4); - - rect.return_panic(true); - } - - #[test] - #[should_panic] - fn result_will_not_panic_and_fail() { - let rect = Rectangle::build_rectangle(3, 4); - - rect.return_panic(false); - } - - #[test] - #[should_panic(expected = "HEEEEEEEEEEEEEEEEEEEELP")] - fn result_will_panic_but_with_wrong_message_and_fail() { - let rect = Rectangle::build_rectangle(3, 4); - - rect.return_panic(false); - } - -} diff --git a/rectangles/tests/integration_test.rs b/rectangles/tests/integration_test.rs new file mode 100644 index 0000000..1c60fdc --- /dev/null +++ b/rectangles/tests/integration_test.rs @@ -0,0 +1,6 @@ +use rectangles::Rectangle; + +#[test] +fn integration_tests_works() { + let rect = Rectangle::build_rectangle(1, 1); +}