// Remove when is fixed #![allow(clippy::tests_outside_test_module)] use std::{error::Error, fmt}; use wee_woo::ErrorExt; macro_rules! mk_simple_error { ($name:ident, $msg:literal $(,)?) => { #[derive(Debug, Eq, PartialEq, Hash)] struct $name; impl fmt::Display for $name { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { write!(f, $msg) } } impl Error for $name {} }; } mk_simple_error!(EncabulationFailed, "failed to encabulate"); mk_simple_error!(EncabulatorBroken, "encabulator is broken"); mk_simple_error!( WaneshaftDiscombobulated, "ambifacient lunar waneshaft discombobulated", ); #[test] fn error_with_source_1() { let ews = EncabulationFailed.with_source(EncabulatorBroken); assert_eq!( ews.source().and_then(|x| x.downcast_ref::()), Some(&EncabulatorBroken), ); } #[test] fn error_with_source_2() { let ews = EncabulationFailed .with_source(EncabulatorBroken.with_source(WaneshaftDiscombobulated)); assert_eq!( ews.source() .and_then(|x| x.source()) .and_then(|x| x.downcast_ref::()), Some(&WaneshaftDiscombobulated), ); } #[test] fn display_with_sources_1() { let actual = EncabulationFailed.display_with_sources(": ").to_string(); let expected = "failed to encabulate"; assert_eq!(actual, expected); } #[test] fn display_with_sources_2() { let actual = EncabulationFailed .with_source(EncabulatorBroken) .display_with_sources(": ") .to_string(); let expected = "failed to encabulate: encabulator is broken"; assert_eq!(actual, expected); } #[test] fn display_with_sources_3() { let actual = EncabulationFailed .with_source(EncabulatorBroken.with_source(WaneshaftDiscombobulated)) .display_with_sources(": ") .to_string(); let expected = "failed to encabulate: encabulator is broken: ambifacient \ lunar waneshaft discombobulated"; assert_eq!(actual, expected); } #[test] fn display_with_sources_3_alt() { let actual = WaneshaftDiscombobulated .caused(EncabulatorBroken) .caused(EncabulationFailed) .display_with_sources(": ") .to_string(); let expected = "failed to encabulate: encabulator is broken: ambifacient \ lunar waneshaft discombobulated"; assert_eq!(actual, expected); }