bones3_test_utils/
lib.rs

1//! This cargo crate contains a set of useful unit testing utilities, designed
2//! for the purpose of reducing boilerplate code when writing unit tests for
3//! Bevy. This module focuses primarily on test cleanliness rather than
4//! performance. As such, it is not recommended to use this library in a
5//! production setting. This crate is intended to be used as a development
6//! dependency only.
7
8#![warn(missing_docs)]
9#![warn(clippy::missing_docs_in_private_items)]
10#![warn(rustdoc::invalid_codeblock_attributes)]
11#![warn(rustdoc::invalid_html_tags)]
12
13use bevy::ecs::event::Event;
14use bevy::prelude::*;
15
16/// An extension for the standard Bevy app that adds more unit test helper
17/// functions.
18pub trait TestApp {
19    /// Collects all events of the indicated type currently within the system
20    /// and returns an iterator over all of them.
21    ///
22    /// Note that the events are still removed from the app, even the iterator
23    /// is not used.
24    fn collect_events<E: Event + Clone>(&mut self) -> Box<dyn Iterator<Item = E>>;
25}
26
27impl TestApp for App {
28    fn collect_events<E: Event + Clone>(&mut self) -> Box<dyn Iterator<Item = E>> {
29        let event_res = self.world.resource::<Events<E>>();
30        let mut event_reader = event_res.get_reader();
31        Box::new(
32            event_reader
33                .iter(event_res)
34                .map(|e| (*e).clone())
35                .collect::<Vec<_>>()
36                .into_iter(),
37        )
38    }
39}