#[allow(unused_imports)] use super::p::*;
use std::fmt::Debug;
use bevy::ecs::query::ReadOnlyQueryData;
use crate::{mismatch, unexpected_match};
pub struct AssertQuery<'w, D: ReadOnlyQueryData>
where
D::Item<'w>: Debug + PartialEq,
{
pub(crate) query: Vec<D::Item<'w>>,
pub(crate) invert: bool,
}
impl<'w, D: ReadOnlyQueryData> AssertQuery<'w, D>
where
D::Item<'w>: Debug + PartialEq,
{
#[allow(clippy::should_implement_trait)] pub fn not(mut self) -> Self {
self.invert = !self.invert;
self
}
pub fn matches(self, given: Vec<D::Item<'w>>) -> Self {
if self.invert {
return self.not_matches(given);
}
for bundle in self.query.iter() {
let is_match = given.iter().any(|v| v == bundle);
if !is_match {
mismatch(
"One of the given bundles wasn't found in the query.",
&given,
None::<()>,
);
}
}
for bundle in given.iter() {
let is_match = self.query.iter().any(|v| v == bundle);
if !is_match {
mismatch(
"The query contains an unexpected bundle.",
None::<()>,
bundle,
);
}
}
if given.len() != self.query.len() {
mismatch(
"The length of the query result and the given result mismatches.",
given.len(),
self.query.len(),
);
}
self
}
fn not_matches(self, given: Vec<D::Item<'w>>) -> Self {
for bundle in self.query.iter() {
let is_match = given.iter().any(|v| v == bundle);
if !is_match {
return self.reset_invert();
}
}
for bundle in given.iter() {
let is_match = self.query.iter().any(|v| v == bundle);
if !is_match {
return self.reset_invert();
}
}
if given.len() != self.query.len() {
return self.reset_invert();
}
unexpected_match("The query matches with the given bundles", given);
}
pub fn has(self, given: D::Item<'w>) -> Self {
if self.invert {
return self.not_has(given);
}
let is_match = self.query.iter().any(|bundle| bundle == &given);
if !is_match {
mismatch(
"The given bundle wasn't found in the query.",
&given,
None::<()>,
);
}
self
}
fn not_has(self, given: D::Item<'w>) -> Self {
let is_match = self.query.iter().any(|bundle| bundle == &given);
if !is_match {
return self.reset_invert();
}
unexpected_match("The query contains all given bundles", given);
}
pub fn has_all(self, given: Vec<D::Item<'w>>) -> Self {
if self.invert {
return self.not_has_all(given);
}
for given in given.iter() {
let is_match = self.query.iter().any(|bundle| bundle == given);
if !is_match {
mismatch(
"The given bundle wasn't found in the query.",
given,
None::<()>,
);
}
}
self
}
fn not_has_all(self, given: Vec<D::Item<'w>>) -> Self {
for given in given.iter() {
let is_match = self.query.iter().any(|bundle| bundle == given);
if !is_match {
return self.reset_invert();
}
}
unexpected_match("The query has all given bundles.", given);
}
pub fn has_any(self, given: Vec<D::Item<'w>>) -> Self {
if self.invert {
return self.not_has_any(given);
}
let is_match = self
.query
.iter()
.any(|bundle| given.iter().any(|given| given == bundle));
if !is_match {
mismatch(
"None of the given bundles were found in the query.",
given,
None::<()>,
);
}
self
}
fn not_has_any(self, given: Vec<D::Item<'w>>) -> Self {
let is_match = self
.query
.iter()
.any(|bundle| given.iter().any(|given| given == bundle));
if is_match {
unexpected_match(
"Some of the given bundles were found in the query.",
self.query
.iter()
.find(|bundle| given.iter().any(|given| given == *bundle)),
);
}
self.reset_invert()
}
pub fn all(self, predicate: impl Fn(&D::Item<'w>) -> bool) -> Self {
if self.invert {
return self.not_all(predicate);
}
let predicate = &predicate;
for bundle in self.query.iter() {
if !predicate(bundle) {
mismatch(
"The predicate fails on one of the bundles",
"impl Fn(&D::Item<'w>) -> bool",
bundle,
);
}
}
self
}
fn not_all(self, predicate: impl Fn(&D::Item<'w>) -> bool) -> Self {
let predicate = &predicate;
for bundle in self.query.iter() {
if !predicate(bundle) {
return self.reset_invert();
}
}
unexpected_match("The predicate matches on all of the bundles.", self.query);
}
pub fn any(self, predicate: impl Fn(&D::Item<'w>) -> bool) -> Self {
if self.invert {
return self.not_any(predicate);
}
let predicate = &predicate;
let is_match = self.query.iter().any(predicate);
if !is_match {
mismatch(
"The predicate didn't match on any of the bundles",
"impl Fn(&D::Item<'w>) -> bool",
None::<()>,
);
}
self
}
fn not_any(self, predicate: impl Fn(&D::Item<'w>) -> bool) -> Self {
let predicate = &predicate;
let is_match = self.query.iter().any(predicate);
if is_match {
unexpected_match(
"The predicate matched on one of the bundles",
self.query.iter().find(|bundle| predicate(bundle)),
);
}
self.reset_invert()
}
pub fn length(self, given: usize) -> Self {
if self.invert {
return self.not_length(given);
}
if self.query.len() != given {
mismatch(
"The length of the query result mismatches.",
given,
self.query.len(),
);
}
self
}
fn not_length(self, given: usize) -> Self {
if self.query.len() == given {
unexpected_match("The length of the query result matches.", given);
}
self.reset_invert()
}
fn reset_invert(mut self) -> Self {
self.invert = false;
self
}
}