mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-10-02 15:26:08 +00:00
64 lines
1.3 KiB
Rust
64 lines
1.3 KiB
Rust
use anyhow::{bail, ensure, Result};
|
|
use std::io;
|
|
|
|
fn bail_literal() -> Result<()> {
|
|
bail!("oh no!");
|
|
}
|
|
|
|
fn bail_fmt() -> Result<()> {
|
|
bail!("{} {}!", "oh", "no");
|
|
}
|
|
|
|
fn bail_error() -> Result<()> {
|
|
bail!(io::Error::new(io::ErrorKind::Other, "oh no!"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_messages() {
|
|
assert_eq!("oh no!", bail_literal().unwrap_err().to_string());
|
|
assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());
|
|
assert_eq!("oh no!", bail_error().unwrap_err().to_string());
|
|
}
|
|
|
|
#[test]
|
|
fn test_downcast() {
|
|
assert_eq!(
|
|
"oh no!",
|
|
bail_literal().unwrap_err().downcast::<&str>().unwrap(),
|
|
);
|
|
assert_eq!(
|
|
"oh no!",
|
|
bail_fmt().unwrap_err().downcast::<String>().unwrap(),
|
|
);
|
|
assert_eq!(
|
|
"oh no!",
|
|
bail_error()
|
|
.unwrap_err()
|
|
.downcast::<io::Error>()
|
|
.unwrap()
|
|
.to_string(),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_ensure() {
|
|
let f = || {
|
|
ensure!(1 + 1 == 2, "This is correct");
|
|
Ok(())
|
|
};
|
|
assert!(f().is_ok());
|
|
|
|
let v = 1;
|
|
let f = || {
|
|
ensure!(v + v == 2, "This is correct, v: {}", v);
|
|
Ok(())
|
|
};
|
|
assert!(f().is_ok());
|
|
|
|
let f = || {
|
|
ensure!(v + v == 1, "This is not correct, v: {}", v);
|
|
Ok(())
|
|
};
|
|
assert!(f().is_err());
|
|
}
|