rust test result type in rust

main.rs
fn my_function() -> Result<i32, &'static str> {
    if some_condition {
        Ok(42)
    } else {
        Err("Something went wrong")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_my_function_success() {
        assert_eq!(my_function(), Ok(42));
    }

    #[test]
    fn test_my_function_failure() {
        assert_eq!(my_function(), Err("Something went wrong"));
    }
}
407 chars
23 lines

related categories

gistlibby LogSnag