#[cfg(test)]
mod tests_any_err2 {
use std::error::Error;
use bt_any_error::any_err::{AnyErr, IntoAnyErr, StringError};
#[derive(Debug)]
struct SimpleError;
impl std::fmt::Display for SimpleError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "simple error")
}
}
impl Error for SimpleError {}
#[derive(Debug)]
struct NonSendError(std::rc::Rc<u32>);
impl std::fmt::Display for NonSendError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "non-send error")
}
}
impl Error for NonSendError {}
#[test]
fn converts_simple_error() {
let result: Result<(), SimpleError> = Err(SimpleError);
let converted = result.any();
assert!(converted.is_err());
}
#[test]
fn converts_boxed_error() {
let err: Box<dyn Error> = Box::new(SimpleError);
let result: Result<(), Box<dyn Error>> = Err(err);
let converted = result.any();
assert!(converted.is_err());
}
#[test]
fn test_string_error_debug_formatting() {
let original_result: Result<(), Box<dyn Error>> = Err("database connection timeout".into());
let converted: Result<(), AnyErr> = original_result.any();
let error = converted.unwrap_err();
let debug_output = format!("{:?}", error);
assert_eq!(debug_output, "database connection timeout",
"Debug output should include the inner error message"
);
}
#[test]
fn wraps_non_send_error() {
let err = NonSendError(std::rc::Rc::new(42));
let result: Result<(), NonSendError> = Err(err);
let converted = result.any();
assert!(converted.is_err());
}
#[test]
fn wrapper_is_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<StringError>();
}
#[test]
fn any_err_is_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<AnyErr>();
}
#[test]
fn any_method_allows_question_mark() {
fn test_fn() -> Result<(), AnyErr> {
let result: Result<(), SimpleError> = Err(SimpleError);
result.any()?; Ok(())
}
let out = test_fn();
assert!(out.is_err());
}
}