pub fn add(a: i32, b: i32) -> i32 { a + b }
pub fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
if b == 0 { Err("divide by zero") } else { Ok(a / b) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adds_positive_numbers() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn handles_negative_numbers() {
assert_eq!(add(-2, -3), -5);
}
#[test]
fn divide_by_zero_returns_error() {
let result = divide(10, 0);
assert!(matches!(result, Err("divide by zero")));
}
#[test]
#[should_panic(expected = "explicit panic")]
fn panics_on_bad_input() {
panic!("explicit panic");
}
#[test]
#[ignore]
fn slow_integration_test() {
// run with: cargo test -- --ignored
}
}
Create a free account and build your private vault. Share publicly whenever you want.