pub fn empty() -> __internal::EmptyMatcher {
__internal::EmptyMatcher
}
pub mod __internal {
use crate::{
description::Description,
matcher::{Describable, Matcher, MatcherResult},
};
use core::fmt::Debug;
#[doc(hidden)]
pub struct EmptyMatcher;
impl<T: Debug + ?Sized> Matcher<T> for EmptyMatcher
where
for<'a> &'a T: IntoIterator,
{
fn matches(&self, actual: &T) -> MatcherResult {
actual.into_iter().next().is_none().into()
}
}
impl Describable for EmptyMatcher {
fn describe(&self, matcher_result: MatcherResult) -> Description {
if matcher_result.into() { "is empty" } else { "isn't empty" }.into()
}
}
}
#[cfg(test)]
mod tests {
use super::empty;
use crate::prelude::*;
use alloc::vec::Vec;
#[test]
fn empty_matcher_matches_empty_vec() -> TestResult<()> {
let value: Vec<i32> = vec![];
verify_that!(value, empty())
}
#[test]
fn empty_matcher_matches_empty_array() -> TestResult<()> {
verify_that!([] as [u32; 0], empty())
}
#[test]
fn empty_matcher_matches_empty_ref_to_array_with_points_to() -> TestResult<()> {
verify_that!(&([] as [u32; 0]), points_to(empty()))
}
#[test]
fn empty_matcher_matches_empty_ref_to_array_with_deref_notation() -> TestResult<()> {
let value: [u32; 0] = [];
let reference = &value;
verify_that!(*reference, empty())
}
#[test]
fn empty_matcher_matches_empty_slice_with_points_to() -> TestResult<()> {
let value: Vec<u32> = vec![];
let slice = value.as_slice();
verify_that!(slice, points_to(empty()))
}
#[test]
fn empty_matcher_matches_empty_slice_with_deref_notation() -> TestResult<()> {
let value: Vec<u32> = vec![];
let slice = value.as_slice();
verify_that!(*slice, empty())
}
#[test]
fn empty_matcher_does_not_match_non_empty_vec() -> TestResult<()> {
verify_that!(vec![1, 2, 3], not(empty()))
}
#[test]
fn empty_matcher_does_not_match_non_empty_array() -> TestResult<()> {
verify_that!([1, 2, 3], not(empty()))
}
#[test]
fn empty_matcher_does_not_match_non_empty_slice() -> TestResult<()> {
let value: Vec<u32> = vec![1, 2, 3];
let slice = value.as_slice();
verify_that!(*slice, not(empty()))
}
#[cfg(feature = "std")]
#[test]
fn empty_matcher_matches_empty_hash_set() -> TestResult<()> {
use std::collections::HashSet;
let value: HashSet<i32> = HashSet::new();
verify_that!(value, empty())
}
#[derive(Debug)]
struct OwnedItemContainer(Vec<i32>);
impl<'a> IntoIterator for &'a OwnedItemContainer {
type Item = i32;
type IntoIter = core::iter::Copied<core::slice::Iter<'a, i32>>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter().copied()
}
}
#[test]
fn empty_matches_on_container_when_ref_to_container_has_into_iterator_producing_owned_values()
-> TestResult<()> {
verify_that!(OwnedItemContainer(vec![]), empty())
}
}