From b8c7b5e68b8bca87d818a9dea89884adc7764c90 Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Mon, 19 Jan 2026 08:01:27 +0000 Subject: [PATCH] finish testing - convert project to a library by moving program logic into lib.rs - create ./tests to store integration tests - create ./common to store code we want shared across integration tests --- testing/src/lib.rs | 175 ++++++++++++++++++++++++++++ testing/src/main.rs | 179 +---------------------------- testing/tests/common/mod.rs | 3 + testing/tests/integration_tests.rs | 24 ++++ 4 files changed, 204 insertions(+), 177 deletions(-) create mode 100644 testing/src/lib.rs create mode 100644 testing/tests/common/mod.rs create mode 100644 testing/tests/integration_tests.rs diff --git a/testing/src/lib.rs b/testing/src/lib.rs new file mode 100644 index 0000000..a7ca49a --- /dev/null +++ b/testing/src/lib.rs @@ -0,0 +1,175 @@ +pub fn print_operations(a: &i32, b: &i32) { + let sum = ops::add(&a, &b); + let difference = ops::sub(&a, &b); + let product = ops::mul(&a, &b); + let quotient = ops::div(&a, &b); + + println!( + " + a is {a} and b is {b} and + sum = {sum} + difference = {difference} + product = {product} + quotient = {quotient} + " + ); +} + +pub mod ops { + pub fn add(a: &i32, b: &i32) -> i32 { + *a + *b + } + pub fn sub(a: &i32, b: &i32) -> i32 { + *a - *b + } + pub fn mul(a: &i32, b: &i32) -> i32 { + *a * *b + } + pub fn div(a: &i32, b: &i32) -> i32 { + *a / *b + } +} + +pub mod users { + + pub fn admin_checker(user: &str) -> bool { + if *user == "admin".to_string() { + true + } else { + panic!("bruh moment"); + } + } + + pub fn john_checker(user: &str) -> Result { + if *user == "john".to_string() { + Ok(true) + } else { + Err("panicking because you're not john!".to_string()) + } + } +} + +#[cfg(test)] +mod panic_tests { + + use crate::users::{admin_checker, john_checker}; + + #[test] + #[should_panic(expected = "bruh")] + fn is_admin() { + let user = "notadmin"; + admin_checker(user); + } + + #[test] + fn is_john() -> Result<(), String> { + let user = "john"; + let result = john_checker(user); + + match result { + std::result::Result::Ok(true) => Ok(()), + std::result::Result::Ok(false) => { + Err("is_john test failed because it's someone else!".to_string()) + } + Err(_) => Err("is_john test failed because it's someone else!".to_string()), + } + } +} + +#[cfg(test)] +mod tests_returning_results { + use crate::ops::*; + + #[test] + fn test_add() -> Result<(), String> { + let a = 12; + let b = 14; + let result = add(&a, &b); + if result == 26 { + Ok(()) + } else { + Err("12 + 14 should be 26 but it's not".to_string()) + } + } + + #[test] + fn test_sub() -> Result<(), String> { + let a = 12; + let b = 14; + let result = sub(&a, &b); + + if result == -2 { + Ok(()) + } else { + Err("error calculating difference".to_string()) + } + } + + #[test] + #[ignore] + fn test_mul() -> Result<(), String> { + let a = 14; + let b = 12; + let result = mul(&a, &b); + + if result == 168 { + Ok(()) + } else { + Err("error calculating product".to_string()) + } + } + + #[test] + #[ignore] + fn test_div() -> Result<(), String> { + let a = 14; + let b = 12; + let result = div(&a, &b); + + if result == 1 { + Ok(()) + } else { + Err("error calculating quotient".to_string()) + } + } +} + +#[cfg(test)] +mod tests_returning_bool { + + use crate::ops::*; + + #[test] + fn test_add() { + let a = 12; + let b = 14; + let result = add(&a, &b); + assert_eq!(result, a + b, "12 + 14 should be 26 but it's not"); + } + + #[test] + fn test_sub() { + let a = 12; + let b = 14; + let result = sub(&a, &b); + assert_eq!(result, a - b); + } + + #[test] + #[ignore] + fn test_mul() { + let a = 12; + let b = 14; + let result = mul(&a, &b); + assert_eq!(result, a * b); + } + + #[test] + #[ignore] + fn test_div() { + let a = 12; + let b = 14; + let result = div(&a, &b); + assert_eq!(result, a / b); + } +} diff --git a/testing/src/main.rs b/testing/src/main.rs index c3e541e..df34f46 100644 --- a/testing/src/main.rs +++ b/testing/src/main.rs @@ -1,4 +1,5 @@ -use crate::users::{admin_checker, john_checker}; +use testing::print_operations; +use testing::users::*; fn main() { let a = 14; @@ -16,179 +17,3 @@ fn main() { println!("hi john!"); }; } - -fn print_operations(a: &i32, b: &i32) { - let sum = ops::add(&a, &b); - let difference = ops::sub(&a, &b); - let product = ops::mul(&a, &b); - let quotient = ops::div(&a, &b); - - println!( - " - a is {a} and b is {b} and - sum = {sum} - difference = {difference} - product = {product} - quotient = {quotient} - " - ); -} - -mod ops { - pub fn add(a: &i32, b: &i32) -> i32 { - *a + *b - } - pub fn sub(a: &i32, b: &i32) -> i32 { - *a - *b - } - pub fn mul(a: &i32, b: &i32) -> i32 { - *a * *b - } - pub fn div(a: &i32, b: &i32) -> i32 { - *a / *b - } -} - -mod users { - - pub fn admin_checker(user: &str) -> bool { - if *user == "admin".to_string() { - true - } else { - panic!("bruh moment"); - } - } - - pub fn john_checker(user: &str) -> Result { - if *user == "john".to_string() { - Ok(true) - } else { - Err("panicking because you're not john!".to_string()) - } - } -} - -#[cfg(test)] -mod panic_tests { - - use crate::users::{admin_checker, john_checker}; - - #[test] - #[should_panic(expected = "bruh")] - fn is_admin() { - let user = "notadmin"; - admin_checker(user); - } - - #[test] - fn is_john() -> Result<(), String> { - let user = "john"; - let result = john_checker(user); - - match result { - std::result::Result::Ok(true) => Ok(()), - std::result::Result::Ok(false) => { - Err("is_john test failed because it's someone else!".to_string()) - } - Err(_) => Err("is_john test failed because it's someone else!".to_string()), - } - } -} - -#[cfg(test)] -mod tests_returning_results { - use crate::ops::*; - - #[test] - fn test_add() -> Result<(), String> { - let a = 12; - let b = 14; - let result = add(&a, &b); - if result == 26 { - Ok(()) - } else { - Err("12 + 14 should be 26 but it's not".to_string()) - } - } - - #[test] - fn test_sub() -> Result<(), String> { - let a = 12; - let b = 14; - let result = sub(&a, &b); - - if result == -2 { - Ok(()) - } else { - Err("error calculating difference".to_string()) - } - } - - #[test] - #[ignore] - fn test_mul() -> Result<(), String> { - let a = 14; - let b = 12; - let result = mul(&a, &b); - - if result == 168 { - Ok(()) - } else { - Err("error calculating product".to_string()) - } - } - - #[test] - #[ignore] - fn test_div() -> Result<(), String> { - let a = 14; - let b = 12; - let result = div(&a, &b); - - if result == 1 { - Ok(()) - } else { - Err("error calculating quotient".to_string()) - } - } -} - -#[cfg(test)] -mod tests_returning_bool { - - use crate::ops::*; - - #[test] - fn test_add() { - let a = 12; - let b = 14; - let result = add(&a, &b); - assert_eq!(result, a + b, "12 + 14 should be 26 but it's not"); - } - - #[test] - fn test_sub() { - let a = 12; - let b = 14; - let result = sub(&a, &b); - assert_eq!(result, a - b); - } - - #[test] - #[ignore] - fn test_mul() { - let a = 12; - let b = 14; - let result = mul(&a, &b); - assert_eq!(result, a * b); - } - - #[test] - #[ignore] - fn test_div() { - let a = 12; - let b = 14; - let result = div(&a, &b); - assert_eq!(result, a / b); - } -} diff --git a/testing/tests/common/mod.rs b/testing/tests/common/mod.rs new file mode 100644 index 0000000..297cbdc --- /dev/null +++ b/testing/tests/common/mod.rs @@ -0,0 +1,3 @@ +pub fn give_me_5() -> usize { + 5 +} diff --git a/testing/tests/integration_tests.rs b/testing/tests/integration_tests.rs new file mode 100644 index 0000000..05aaeed --- /dev/null +++ b/testing/tests/integration_tests.rs @@ -0,0 +1,24 @@ +use testing::ops::*; + +mod common; + +#[test] +fn subtract_and_divide() { + let a = 20; + let b = 2; + let c = 3; + + let difference = sub(&a, &b); + let quotient = div(&difference, &c); + assert_eq!(quotient, 6); +} +#[test] +fn add_and_multiply() { + let a = 10; + let b = common::give_me_5() as i32; + let c = 3; + + let sum = add(&a, &b); + let product = mul(&sum, &c); + assert_eq!(product, 45); +}