#![cfg(feature = "test-util")]
use ohno::{Error, OhnoCore, assert_error_message};
#[derive(Error)]
struct DefaultSimpleError {
#[error]
inner: OhnoCore,
}
#[derive(Error)]
struct DefaultComplexError {
message: String,
code: u32,
#[error]
inner: OhnoCore,
}
#[derive(Error)]
#[no_constructors]
struct NoConstructorsError {
#[error]
inner: OhnoCore,
}
#[test]
fn test_default_simple_constructors() {
let error = DefaultSimpleError::new();
assert_error_message!(error, "DefaultSimpleError");
let error = DefaultSimpleError::caused_by("test error");
assert_error_message!(error, "test error");
}
#[test]
fn test_default_complex_constructors() {
let error = DefaultComplexError::new("Processing failed", 404u32);
assert_eq!(error.message, "Processing failed");
assert_eq!(error.code, 404);
let error = DefaultComplexError::caused_by("Processing failed", 404u32, "Database connection lost");
assert_eq!(error.message, "Processing failed");
assert_eq!(error.code, 404);
assert!(format!("{error}").contains("Database connection lost"));
}
#[test]
fn test_no_constructors() {
let error = NoConstructorsError {
inner: OhnoCore::from("manually constructed"),
};
assert!(format!("{error}").contains("manually constructed"));
}
#[test]
fn test_constructor_compatibility() {
let _error1 = DefaultSimpleError::caused_by("string error");
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let _error3 = DefaultSimpleError::caused_by(io_err);
}