rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

excercises for tests should teach should_panic

Open fleetingbytes opened this issue 1 year ago • 0 comments

I think rustlings should learn that they can test if a function panics when it is supposed to, e.g.:

pub fn superstitious(n: u8, cat_color: &str) {
    if n == 13 {
        panic!("I refuse to work with {}, that's an unlucky number", n)
    }
    if cat_color == "black" {
        panic!("I refuse to work with a {} cat, that brings bad luck", cat_color)
    }
}

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

    #[test]
    #[should_panic(expected = "unlucky number")]
    fn panic_with_13() {
        superstitious(13, "white");
    }

    #[test]
    #[should_panic(expected = "bad luck")]
    fn panic_with_black_cat() {
        superstitious(7, "black");
    }
}

fleetingbytes avatar Mar 06 '23 04:03 fleetingbytes