tests: add tests for annotated error display, debug, and chaining (#19)

Introduced tests to verify the behavior of `AnnotatedError` in display,
debug, and error chaining scenarios. Ensured proper formatting for
standalone errors and preservation of the error chain for wrapped
errors.
This commit is contained in:
Harald Hoyer 2025-03-31 14:16:35 +02:00 committed by GitHub
commit 82a4164780
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -874,10 +874,43 @@ mod tests {
let err = TestError::from(TestErrorKind::Basic("test".into()));
assert!(matches!(err.kind(), TestErrorKind::Basic(_)));
// The annotated error should display "(passed error)" even in a chain
assert_eq!(format!("{}", err), "Basic error: test");
assert_eq!(format!("{:?}", err), "Basic(\"test\")");
let complex_err = TestError::from(TestErrorKind::Complex {
message: "test".into(),
});
assert!(matches!(complex_err.kind(), TestErrorKind::Complex { .. }));
// The annotated error should display "(passed error)" even in a chain
assert_eq!(format!("{}", complex_err), "Complex error: test");
assert_eq!(
format!("{:?}", complex_err),
"Complex { message: \"test\" }"
);
}
#[test]
fn test_annotated_error_display_and_debug() {
let annotated = AnnotatedError(());
// Test Display formatting
assert_eq!(format!("{}", annotated), "(passed error)");
// Test Debug formatting
assert_eq!(format!("{:?}", annotated), "(passed error)");
// Test with error chain
let io_error = io::Error::new(io::ErrorKind::NotFound, "file not found");
let err = Result::<(), _>::Err(io_error.into())
.annotate()
.unwrap_err();
// The annotated error should display "(passed error)" even in a chain
assert_eq!(format!("{}", err), "(passed error)");
assert!(format!("{:?}", err).contains("(passed error)"));
// Verify the error chain is preserved
assert!(err.source().is_some());
assert!(err.source().unwrap().is_chain::<io::Error>());
}
}