#![cfg(test)]
use oct::io::{Error, ErrorKind};
#[cfg(feature = "alloc")]
use core::fmt::{self, Display, Formatter};
#[cfg(feature = "alloc")]
use alloc::ffi::CString;
#[cfg(feature = "alloc")]
use alloc::string::ToString;
#[cfg(feature = "alloc")]
#[test]
fn test_custom_error() {
#[derive(Debug)]
struct SourceError;
impl Display for SourceError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "!")
}
}
impl core::error::Error for SourceError {}
let e = Error::other(SourceError);
assert_eq!(e.kind(), ErrorKind::Other);
let inner = e.into_inner().unwrap();
inner.downcast::<SourceError>().unwrap();
}
#[cfg(feature = "alloc")]
#[test]
fn test_error_message() {
let e = CString::new([0]).unwrap_err();
let e = Error::from(e);
assert_eq!(
e.to_string(),
"invalid data: nul octet found in c-like string",
);
}
#[test]
fn test_simple_error() {
let e = Error::from(ErrorKind::Unsupported);
assert_eq!(e.kind(), ErrorKind::Unsupported);
}