1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]

//! Test harness to support testing of Amethyst types, including:
//!
//! * `Bundle`
//! * `State`
//! * `System`
//! * Resource loading.
//! * Arbitrary types that `System`s use during processing.
//!
//! The test harness minimizes boilerplate code to set up an Amethyst `Application` with common
//! bundles, and can take in logic that is normally masked behind a number of layers through a thin
//! interface.
//!
//! # Usage
//!
//! The following shows a simple example of testing a `State`. More examples are in the
//! [Examples](#Examples) section.
//!
//! ```rust
//! # extern crate amethyst;
//! # extern crate amethyst_test;
//! #
//! # use std::marker::PhantomData;
//! #
//! # use amethyst_test::prelude::*;
//! # use amethyst::{
//! #     ecs::prelude::*,
//! #     prelude::*,
//! # };
//! #
//! # #[derive(Debug)]
//! # struct LoadResource;
//! #
//! # #[derive(Debug)]
//! # struct LoadingState;
//! #
//! # impl LoadingState {
//! #     fn new() -> Self {
//! #         LoadingState
//! #     }
//! # }
//! #
//! # impl<'a, 'b, E> State<GameData<'a, 'b>, E> for LoadingState
//! # where
//! #     E: Send + Sync + 'static,
//! # {
//! #     fn update(&mut self, data: StateData<'_, GameData<'_, '_>>) -> Trans<GameData<'a, 'b>, E> {
//! #         data.data.update(&data.world);
//! #
//! #         data.world.add_resource(LoadResource);
//! #
//! #         Trans::Pop
//! #     }
//! # }
//! #
//! // #[test]
//! fn loading_state_adds_load_resource() {
//!     assert!(
//!         AmethystApplication::blank()
//!             .with_state(|| LoadingState::new())
//!             .with_assertion(|world| {
//!                 world.read_resource::<LoadResource>();
//!             })
//!             .run()
//!             .is_ok()
//!     );
//! }
//! #
//! # fn main() {
//! #     loading_state_adds_load_resource();
//! # }
//! ```
//!
//! The Amethyst application is initialized with one of the following functions, each providing a
//! different set of bundles:
//!
//! ```rust,no_run
//! extern crate amethyst_test;
//!
//! use amethyst_test::prelude::*;
//!
//! #[test]
//! fn test_name() {
//!     // Start with no bundles
//!     AmethystApplication::blank();
//!
//!     // Start with the Transform, Input, and UI bundles
//!     // The type parameters here are the Axis and Action types for the `InputBundle` and
//!     // `UiBundle`.
//!     AmethystApplication::ui_base::<String, String>();
//!
//!     // Start with the Animation, Transform, and Render bundles.
//!     // If you want the Input and UI bundles, you can use the `.with_ui_bundles::<AX, AC>()`
//!     // method.
//!     let visibility = false; // Whether the window should be shown
//!     AmethystApplication::render_base("test_name", visibility);
//! }
//! ```
//!
//! Next, attach the logic you wish to test using the various `.with_*(..)` methods:
//!
//! ```rust,no_run
//! #[test]
//! fn test_name() {
//!     let visibility = false; // Whether the window should be shown
//!     AmethystApplication::render_base::<String, String, _>("test_name", visibility)
//!         .with_bundle(MyBundle::new())                // Registers a bundle.
//!         .with_bundle_fn(|| MyNonSendBundle::new())   // Registers a `!Send` bundle.
//!         .with_resource(MyResource::new())            // Adds a resource to the world.
//!         .with_system(MySystem::new(), "my_sys", &[]) // Registers a system with the main
//!                                                      // dispatcher.
//!
//!         // These are run in the order they are invoked.
//!         // You may invoke them multiple times.
//!         .with_setup(|world| { /* do something */ })
//!         .with_state(|| MyState::new())
//!         .with_effect(|world| { /* do something */ })
//!         .with_assertion(|world| { /* do something */ })
//!          // ...
//! }
//! ```
//!
//! Finally, call `.run()` to run the application. This returns `amethyst::Result<()>`, so you can
//! wrap it in an `assert!(..);`:
//!
//! ```rust,no_run
//! #[test]
//! fn test_name() {
//!     let visibility = false; // Whether the window should be shown
//!     assert!(
//!         AmethystApplication::render_base("test_name", visibility)
//!             // ...
//!             .run()
//!             .is_ok()
//!     );
//! }
//! ```
//!
//! # Examples
//!
//! Testing a bundle:
//!
//! ```rust
//! # extern crate amethyst;
//! # extern crate amethyst_test;
//! #
//! # use amethyst_test::prelude::*;
//! # use amethyst::{
//! #     core::bundle::{self, SystemBundle},
//! #     ecs::prelude::*,
//! #     prelude::*,
//! # };
//! #
//! # #[derive(Debug)]
//! # struct ApplicationResource;
//! #
//! # #[derive(Debug)]
//! # struct MySystem;
//! #
//! # impl<'s> System<'s> for MySystem {
//! #     type SystemData = ReadExpect<'s, ApplicationResource>;
//! #
//! #     fn run(&mut self, _: Self::SystemData) {}
//! #
//! #     fn setup(&mut self, res: &mut Resources) {
//! #         Self::SystemData::setup(res);
//! #         res.insert(ApplicationResource);
//! #     }
//! # }
//! #
//! # #[derive(Debug)]
//! # struct MyBundle;
//! # impl<'a, 'b> SystemBundle<'a, 'b> for MyBundle {
//! #     fn build(self, builder: &mut DispatcherBuilder<'a, 'b>) -> bundle::Result<()> {
//! #         builder.add(MySystem, "my_system", &[]);
//! #         Ok(())
//! #     }
//! # }
//! #
//! // #[test]
//! fn bundle_registers_system_with_resource() {
//!     assert!(
//!         AmethystApplication::blank()
//!             .with_bundle(MyBundle)
//!             .with_assertion(|world| { world.read_resource::<ApplicationResource>(); })
//!             .run()
//!             .is_ok()
//!     );
//! }
//! #
//! # fn main() {
//! #     bundle_registers_system_with_resource();
//! # }
//! ```
//!
//! Testing a system:
//!
//! ```rust
//! # extern crate amethyst;
//! # extern crate amethyst_test;
//! #
//! # use amethyst_test::prelude::*;
//! # use amethyst::{
//! #     ecs::prelude::*,
//! #     prelude::*,
//! # };
//! #
//! # struct MyComponent(pub i32);
//! #
//! # impl Component for MyComponent {
//! #     type Storage = DenseVecStorage<Self>;
//! # }
//! #
//! # #[derive(Debug)]
//! # struct MySystem;
//! #
//! # impl<'s> System<'s> for MySystem {
//! #     type SystemData = WriteStorage<'s, MyComponent>;
//! #
//! #     fn run(&mut self, mut my_component_storage: Self::SystemData) {
//! #         for mut my_component in (&mut my_component_storage).join() {
//! #             my_component.0 += 1
//! #         }
//! #     }
//! # }
//! #
//! // #[test]
//! fn system_increases_component_value_by_one() {
//!     assert!(
//!         AmethystApplication::blank()
//!             .with_system(MySystem, "my_system", &[])
//!             .with_effect(|world| {
//!                 let entity = world.create_entity().with(MyComponent(0)).build();
//!                 world.add_resource(EffectReturn(entity));
//!             })
//!             .with_assertion(|world| {
//!                 let entity = world.read_resource::<EffectReturn<Entity>>().0.clone();
//!
//!                 let my_component_storage = world.read_storage::<MyComponent>();
//!                 let my_component = my_component_storage
//!                     .get(entity)
//!                     .expect("Entity should have a `MyComponent` component.");
//!
//!                 // If the system ran, the value in the `MyComponent` should be 1.
//!                 assert_eq!(1, my_component.0);
//!             })
//!             .run()
//!             .is_ok()
//!     );
//! }
//! #
//! # fn main() {
//! #     system_increases_component_value_by_one();
//! # }
//! ```
//!
//! Testing a System in a custom dispatcher. This is useful when your system must run *after* some
//! setup has been done:
//!
//! ```rust
//! # extern crate amethyst;
//! # extern crate amethyst_test;
//! #
//! # use amethyst_test::prelude::*;
//! # use amethyst::{
//! #     ecs::prelude::*,
//! #     prelude::*,
//! # };
//! #
//! # // !Default
//! # struct MyResource(pub i32);
//! #
//! # #[derive(Debug)]
//! # struct MySystem;
//! #
//! # impl<'s> System<'s> for MySystem {
//! #     type SystemData = WriteExpect<'s, MyResource>;
//! #
//! #     fn run(&mut self, mut my_resource: Self::SystemData) {
//! #         my_resource.0 += 1
//! #     }
//! # }
//! #
//! // #[test]
//! fn system_increases_resource_value_by_one() {
//!     assert!(
//!         AmethystApplication::blank()
//!             .with_setup(|world| {
//!                 world.add_resource(MyResource(0));
//!             })
//!             .with_system_single(MySystem, "my_system", &[])
//!             .with_assertion(|world| {
//!                 let my_resource = world.read_resource::<MyResource>();
//!
//!                 // If the system ran, the value in the `MyResource` should be 1.
//!                 assert_eq!(1, my_resource.0);
//!             })
//!             .run()
//!             .is_ok()
//!     );
//! }
//! #
//! # fn main() {
//! #     system_increases_resource_value_by_one();
//! # }
//! ```

use amethyst;

#[macro_use]
extern crate derivative;
#[macro_use]
extern crate derive_new;

#[macro_use]
extern crate lazy_static;

pub(crate) use crate::system_injection_bundle::SystemInjectionBundle;
pub use crate::{
    amethyst_application::{AmethystApplication, HIDPI, SCREEN_HEIGHT, SCREEN_WIDTH},
    effect_return::EffectReturn,
    fixture::{MaterialAnimationFixture, SpriteRenderAnimationFixture},
    game_update::GameUpdate,
    state::{
        CustomDispatcherState, CustomDispatcherStateBuilder, FunctionState, PopState,
        SequencerState,
    },
};

mod amethyst_application;
mod effect_return;
mod fixture;
mod game_update;
pub mod prelude;
mod state;
mod system_injection_bundle;