use super::{AsDisplay, AsDynError, Error, Var};
use core::fmt;
use std::path::{Path, PathBuf};
#[derive(Debug)]
struct Leaf;
impl fmt::Display for Leaf {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("leaf")
}
}
impl Error for Leaf {}
#[test]
fn test_error_borrow_preserves_concrete_and_dynamic_object_identity() {
let leaf = Leaf;
let expected = &leaf as *const Leaf as *const ();
let plain: &dyn Error = &leaf;
let send: &(dyn Error + Send) = &leaf;
let sync: &(dyn Error + Send + Sync) = &leaf;
let unwind: &(dyn Error + Send + Sync + core::panic::UnwindSafe) = &leaf;
for observed in [
AsDynError::as_dyn_error(&leaf),
AsDynError::as_dyn_error(plain),
AsDynError::as_dyn_error(send),
AsDynError::as_dyn_error(sync),
AsDynError::as_dyn_error(unwind),
] {
assert_eq!(observed as *const dyn Error as *const (), expected);
assert_eq!(observed.to_string(), "leaf");
}
}
#[test]
fn test_error_borrow_accepts_non_static_non_send_non_sync_error() {
#[derive(Debug)]
struct Borrowed<'a> {
text: &'a str,
_local: std::rc::Rc<()>,
}
impl fmt::Display for Borrowed<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.text)
}
}
impl Error for Borrowed<'_> {}
let text = String::from("borrowed local error");
let error = Borrowed {
text: &text,
_local: std::rc::Rc::new(()),
};
let observed = AsDynError::as_dyn_error(&error);
assert_eq!(observed.to_string(), text);
assert_eq!(
observed as *const dyn Error as *const (),
&error as *const Borrowed<'_> as *const ()
);
}
#[test]
fn test_display_view_preserves_reference_and_standard_path_semantics() {
let text = String::from("display reference");
let reference = &text;
let observed = AsDisplay::as_display(&reference);
assert!(core::ptr::eq(observed, reference));
assert_eq!(observed.to_string(), text);
for path in [PathBuf::new(), PathBuf::from("config/settings.ini")] {
assert_eq!(
AsDisplay::as_display(path.as_path()).to_string(),
path.display().to_string()
);
assert_eq!(
AsDisplay::as_display(&path).to_string(),
path.display().to_string()
);
}
assert_eq!(AsDisplay::as_display(Path::new("")).to_string(), "");
}
#[cfg(unix)]
#[test]
fn test_path_display_view_preserves_non_utf8_path_semantics() {
use std::os::unix::ffi::OsStringExt;
let path = PathBuf::from(std::ffi::OsString::from_vec(b"config/\xff.ini".to_vec()));
assert!(path.to_str().is_none());
assert_eq!(
AsDisplay::as_display(path.as_path()).to_string(),
path.display().to_string()
);
assert_eq!(
AsDisplay::as_display(&path).to_string(),
path.display().to_string()
);
}
#[test]
fn test_pointer_wrapper_forwards_original_value_instead_of_wrapper_address() {
struct NamedPointer;
impl fmt::Pointer for NamedPointer {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("original-pointer-value")
}
}
let value = NamedPointer;
assert_eq!(format!("{:p}", Var(&value)), "original-pointer-value");
let byte = 7_u8;
let pointer = &byte as *const u8;
assert_eq!(format!("{:p}", Var(&pointer)), format!("{pointer:p}"));
}
#[test]
fn test_pointer_wrapper_preserves_formatter_error_without_panicking() {
struct FailingPointer;
impl fmt::Pointer for FailingPointer {
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Err(fmt::Error)
}
}
let mut output = String::new();
let result = fmt::write(&mut output, format_args!("{:p}", Var(&FailingPointer)));
assert_eq!(result, Err(fmt::Error));
assert!(output.is_empty());
}
#[cfg(nightly)]
#[test]
fn test_provider_forwarding_preserves_borrowed_and_non_copy_owned_values() {
use super::ThiserrorProvide;
#[derive(Debug)]
struct Provider(String);
impl fmt::Display for Provider {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("provider")
}
}
impl Error for Provider {
fn provide<'a>(&'a self, request: &mut core::error::Request<'a>) {
request.provide_ref::<str>(&self.0);
request.provide_value::<String>(format!("owned:{}", self.0));
}
}
#[derive(Debug)]
struct Forward(Provider);
impl fmt::Display for Forward {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("forward")
}
}
impl Error for Forward {
fn provide<'a>(&'a self, request: &mut core::error::Request<'a>) {
self.0.thiserror_provide(request);
}
}
let forwarded = Forward(Provider(String::from("payload")));
let observed = core::error::request_ref::<str>(&forwarded).unwrap();
assert_eq!(observed, "payload");
assert!(core::ptr::eq(observed, forwarded.0.0.as_str()));
assert_eq!(
core::error::request_value::<String>(&forwarded),
Some(String::from("owned:payload"))
);
assert!(core::error::request_ref::<usize>(&forwarded).is_none());
}