Compare commits

...

2 Commits

Author SHA1 Message Date
b8c7b5e68b 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
2026-01-19 08:01:27 +00:00
b287268919 testing
- create regular functions with various return types
- create test functions with various return types
- experiment with `cargo test`, `#[should_panic]`, `#[ignore]` and test modules
2026-01-19 07:06:25 +00:00
5 changed files with 227 additions and 0 deletions

6
testing/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "testing"
version = "0.1.0"
edition = "2024"
[dependencies]

175
testing/src/lib.rs Normal file
View File

@@ -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<bool, String> {
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);
}
}

19
testing/src/main.rs Normal file
View File

@@ -0,0 +1,19 @@
use testing::print_operations;
use testing::users::*;
fn main() {
let a = 14;
let b = 12;
let user = "admin";
let john = "john";
print_operations(&a, &b);
if admin_checker(user) {
println!("you have the relevant permissions");
};
if john_checker(john).unwrap() {
println!("hi john!");
};
}

View File

@@ -0,0 +1,3 @@
pub fn give_me_5() -> usize {
5
}

View File

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