use crate::{AssertThat, Mode, Type, tracking::AssertionTracking};
use core::fmt::Write;
use indoc::writedoc;
#[allow(clippy::return_self_not_must_use)]
pub trait MemAssertions {
fn needs_drop(self) -> Self;
fn need_drop(self) -> Self
where
Self: Sized,
{
self.needs_drop()
}
}
impl<T, M: Mode> MemAssertions for AssertThat<'_, Type<T>, M> {
#[track_caller]
fn needs_drop(self) -> Self {
self.track_assertion();
let actual = self.actual();
if !actual.needs_drop() {
self.fail(|w: &mut String| {
writedoc! {w, r"
Type {actual:#?} was expected to need drop,
but dropping it is guaranteed to have no side effect.
You may forgot to `impl Drop` for this type.
", actual = actual.get_type_name()}
});
}
self
}
}
#[cfg(test)]
mod tests {
mod needs_drop {
use crate::assert_that_type;
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_type_needs_drop() {
struct NeedsDrop;
impl Drop for NeedsDrop {
fn drop(&mut self) {
}
}
assert_that_type::<NeedsDrop>().needs_drop();
}
#[test]
fn panics_when_type_does_not_need_drop() {
struct DoeNotNeedDrop;
assert_that_panic_by(|| {
assert_that_type::<DoeNotNeedDrop>()
.with_location(false)
.needs_drop();
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Type "assertr::assertions::std::mem::tests::needs_drop::panics_when_type_does_not_need_drop::DoeNotNeedDrop" was expected to need drop,
but dropping it is guaranteed to have no side effect.
You may forgot to `impl Drop` for this type.
-------- assertr --------
"#});
}
}
}