trpl-zh-cn/listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs

35 lines
680 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
pub struct Guess {
value: i32,
}
impl Guess {
pub fn new(value: i32) -> Guess {
// ANCHOR: here
if value < 1 {
panic!(
"Guess value must be less than or equal to 100, got {}.",
value
);
} else if value > 100 {
panic!(
"Guess value must be greater than or equal to 1, got {}.",
value
);
}
// ANCHOR_END: here
Guess { value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
2023-01-16 17:34:52 +08:00
#[should_panic(expected = "less than or equal to 100")]
2022-02-06 16:43:51 +08:00
fn greater_than_100() {
Guess::new(200);
}
}