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
//! Entrypoint for the Simulaton API handling the world itself

mod simulation_impl;

pub use self::simulation_impl::*;
use crate::prelude::*;
use std::fmt::Debug;

#[cfg(any(test, feature = "use-mocks"))]
pub use self::mocks::*;

/// A Simulation that can be filled with [`Object`] on
/// which it will apply physical rules when calling [`step`].
/// This trait represents our API.
///
/// [`Object`]: ./object/struct.Object.html
/// [`step`]: ./trait.Simulation.html#tymethod.step
pub trait Simulation: Debug {
    /// Advance the simulation by one tick. This will apply
    /// forces to the objects, handle collisions and allow them to
    /// take action.
    fn step(&mut self);

    /// Add a new object to the world.
    fn add_object(
        &mut self,
        object_description: ObjectDescription,
        object_behavior: Box<dyn ObjectBehavior>,
    ) -> Object<'_>;

    /// Returns a read-only description of all objects currently inhabiting the simulation.
    fn objects(&self) -> Snapshot<'_>;

    /// Sets how much time in seconds is simulated for each step.
    /// # Examples
    /// If you want to run a simulation with 60 steps per second, you
    /// can run `set_simulated_timestep(1.0/60.0)`. Note that this method
    /// does not block the thread if called faster than expected.
    fn set_simulated_timestep(&mut self, timestep: f64);

    /// Returns read-only descriptions for all objects either completely
    /// contained or intersecting with the given area.
    fn objects_in_area(&self, area: Aabb) -> Snapshot<'_>;

    /// Returns read-only descriptions for all objects either completely
    /// contained or intersecting with the given area.
    fn objects_in_polygon(&self, area: Polygon) -> Snapshot<'_>;
}

/// Unique identifier of an Object
pub type Id = usize;

/// A representation of the current state of the simulation
pub type Snapshot<'a> = Vec<Object<'a>>;

#[cfg(any(test, feature = "use-mocks"))]
mod mocks {
    use super::*;
    use std::cell::RefCell;
    use std::collections::VecDeque;
    use std::thread::panicking;

    #[derive(Debug, Clone)]
    enum AddObjectExpectation<Input, ReturnValue> {
        None,
        Any(ReturnValue),
        AtLeastOnce(Input, ReturnValue),
    }

    impl<Input, ReturnValue> Default for AddObjectExpectation<Input, ReturnValue> {
        fn default() -> Self {
            AddObjectExpectation::None
        }
    }

    #[derive(Debug, Clone)]
    enum ObjectsInAreaExpectation<Input, ReturnValue> {
        None,
        AtLeastOnce(Input, ReturnValue),
        Sequence(RefCell<VecDeque<(Input, ReturnValue)>>),
    }

    impl<Input, ReturnValue> Default for ObjectsInAreaExpectation<Input, ReturnValue> {
        fn default() -> Self {
            ObjectsInAreaExpectation::None
        }
    }

    /// Mock for [`Simulation`]
    ///
    /// [`Simulation`]: ../trait.Simulation.html
    #[derive(Debug, Default)]
    pub struct SimulationMock<'a> {
        expect_step: Option<()>,
        expect_objects_and_return: Option<(Snapshot<'a>,)>,
        expect_add_object_and_return: AddObjectExpectation<ObjectDescription, FullyOwnedObject>,
        expect_set_simulated_timestep: Option<(f64,)>,
        expect_objects_in_area_and_return: ObjectsInAreaExpectation<Aabb, Snapshot<'a>>,
        expect_objects_in_polygon_and_return: ObjectsInAreaExpectation<Polygon, Snapshot<'a>>,

        step_was_called: RefCell<bool>,
        objects_was_called: RefCell<bool>,
        add_object_was_called: RefCell<bool>,
        set_simulated_timestep_was_called: RefCell<bool>,
        objects_in_area_was_called: RefCell<bool>,
        objects_in_polygon_was_called: RefCell<bool>,
    }

    /// A helper tuple with an owned [`ObjectBehavior`], used to assemble an [`Object`] in mocks.
    ///
    /// [`ObjectBehavior`]: ./object/trait.ObjectBehavior.html
    /// [`Object`]: ./object/struct.Object.html
    pub type FullyOwnedObject = (Id, ObjectDescription, Box<dyn ObjectBehavior>);

    impl<'a> SimulationMock<'a> {
        /// Construct a new `SimulationMock`
        pub fn new() -> Self {
            Default::default()
        }

        /// Expect a call to `step`
        pub fn expect_step(&mut self) {
            self.expect_step = Some(());
        }

        /// Expects an arbitrary amount of calls to `add_object` and return the specified object every time
        pub fn expect_add_object_any_times_and_return(&mut self, object: FullyOwnedObject) {
            self.expect_add_object_and_return = AddObjectExpectation::Any(object);
        }

        /// Expect a call to `add_object`
        pub fn expect_add_object_and_return(
            &mut self,
            object_description: ObjectDescription,
            return_value: FullyOwnedObject,
        ) {
            self.expect_add_object_and_return =
                AddObjectExpectation::AtLeastOnce(object_description, return_value);
        }

        /// Expect a call to `objects`
        pub fn expect_objects_and_return(&mut self, return_value: Snapshot<'a>) {
            self.expect_objects_and_return = Some((return_value,));
        }

        /// Expect a call to `set_simulated_timestep`
        pub fn expect_set_simulated_timestep(&mut self, timestep: f64) {
            self.expect_set_simulated_timestep = Some((timestep,));
        }

        /// Expect a call to `objects_in_area`
        pub fn expect_objects_in_area_and_return(
            &mut self,
            area: Aabb,
            return_value: Snapshot<'a>,
        ) {
            self.expect_objects_in_area_and_return =
                ObjectsInAreaExpectation::AtLeastOnce(area, return_value);
        }

        /// Expects a sequence of calls to `objects_in_area`
        pub fn expect_objects_in_area_and_return_in_sequence(
            &mut self,
            expected_calls_and_return_values: Vec<(Aabb, Snapshot<'a>)>,
        ) {
            self.expect_objects_in_area_and_return = ObjectsInAreaExpectation::Sequence(
                RefCell::new(expected_calls_and_return_values.into()),
            );
        }
    }

    impl<'a> Simulation for SimulationMock<'a> {
        fn step(&mut self) {
            *self.step_was_called.borrow_mut() = true;
        }

        fn add_object(
            &mut self,
            object_description: ObjectDescription,
            _object_behavior: Box<dyn ObjectBehavior>,
        ) -> Object<'_> {
            *self.add_object_was_called.borrow_mut() = true;

            match &self.expect_add_object_and_return {
                AddObjectExpectation::None => panic!("add_object() was called unexpectedly"),
                AddObjectExpectation::Any((id, description, behavior)) => Object {
                    id: *id,
                    description: description.clone(),
                    behavior: behavior.as_ref(),
                },
                AddObjectExpectation::AtLeastOnce(expected_object_description, return_value) => {
                    assert_eq!(
                        *expected_object_description, object_description,
                        "add_object() was called with {:?}, expected {:?}",
                        object_description, expected_object_description
                    );

                    let (id, description, behavior) = return_value;
                    Object {
                        id: *id,
                        description: description.clone(),
                        behavior: behavior.as_ref(),
                    }
                }
            }
        }

        fn objects(&self) -> Snapshot<'_> {
            *self.objects_was_called.borrow_mut() = true;

            let (return_value,) = self
                .expect_objects_and_return
                .clone()
                .expect("objects() was called unexpectedly");

            return_value.clone()
        }

        fn set_simulated_timestep(&mut self, timestep: f64) {
            *self.set_simulated_timestep_was_called.borrow_mut() = true;

            let (expected_timestep,) = self
                .expect_set_simulated_timestep
                .expect("set_simulated_timestep() was called unexpectedly");

            assert_eq!(
                expected_timestep, timestep,
                "set_simulated_timestep() was called with {:?}, expected {:?}",
                timestep, expected_timestep
            );
        }

        fn objects_in_area(&self, area: Aabb) -> Snapshot<'_> {
            *self.objects_in_area_was_called.borrow_mut() = true;

            const UNEXPECTED_CALL_ERROR_MESSAGE: &str = "objects_in_area() was called unexpectedly";

            let (expected_area, return_value) = match self.expect_objects_in_area_and_return {
                ObjectsInAreaExpectation::None => panic!(UNEXPECTED_CALL_ERROR_MESSAGE),
                ObjectsInAreaExpectation::AtLeastOnce(ref expected_area, ref return_value) => {
                    (*expected_area, return_value.clone())
                }
                ObjectsInAreaExpectation::Sequence(ref expected_calls_and_return_values) => {
                    expected_calls_and_return_values
                        .borrow_mut()
                        .pop_front()
                        .expect(UNEXPECTED_CALL_ERROR_MESSAGE)
                }
            };

            assert_eq!(
                expected_area, area,
                "objects_in_area() was called with {:?}, expected {:?}",
                area, expected_area
            );

            return_value.clone()
        }

        fn objects_in_polygon(&self, area: Polygon) -> Snapshot<'_> {
            *self.objects_in_polygon_was_called.borrow_mut() = true;

            const UNEXPECTED_CALL_ERROR_MESSAGE: &str =
                "objects_in_polygon() was called unexpectedly";

            let (expected_area, return_value) = match self.expect_objects_in_polygon_and_return {
                ObjectsInAreaExpectation::None => panic!(UNEXPECTED_CALL_ERROR_MESSAGE),
                ObjectsInAreaExpectation::AtLeastOnce(ref expected_area, ref return_value) => {
                    (expected_area.clone(), return_value.clone())
                }
                ObjectsInAreaExpectation::Sequence(ref expected_calls_and_return_values) => {
                    expected_calls_and_return_values
                        .borrow_mut()
                        .pop_front()
                        .expect(UNEXPECTED_CALL_ERROR_MESSAGE)
                }
            };

            assert_eq!(
                expected_area, area,
                "objects_in_polygon() was called with {:?}, expected {:?}",
                area, expected_area
            );

            return_value.clone()
        }
    }

    impl<'a> Drop for SimulationMock<'a> {
        fn drop(&mut self) {
            if panicking() {
                return;
            }

            assert!(
                self.expect_step.is_some() == *self.step_was_called.borrow(),
                "step() was not called, but expected"
            );

            if let AddObjectExpectation::AtLeastOnce(..) = self.expect_add_object_and_return {
                assert!(
                    *self.add_object_was_called.borrow(),
                    "add_object() was not called, but expected"
                );
            }

            assert!(
                self.expect_objects_and_return.is_some() == *self.objects_was_called.borrow(),
                "objects() was not called, but expected"
            );

            assert!(
                self.expect_set_simulated_timestep.is_some()
                    == *self.set_simulated_timestep_was_called.borrow(),
                "set_simulated_timestep() was not called, but expected"
            );

            if let ObjectsInAreaExpectation::AtLeastOnce(..)
            | ObjectsInAreaExpectation::Sequence(..) = self.expect_objects_in_area_and_return
            {
                assert!(
                    *self.objects_in_area_was_called.borrow(),
                    "objects_in_area() was not called, but expected"
                );
            }

            if let ObjectsInAreaExpectation::AtLeastOnce(..)
            | ObjectsInAreaExpectation::Sequence(..) = self.expect_objects_in_polygon_and_return
            {
                assert!(
                    *self.objects_in_polygon_was_called.borrow(),
                    "objects_in_polygon() was not called, but expected"
                );
            }
        }
    }
}