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
use std::any::Any;
use std::fmt::Debug;

use crate::prelude::*;

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

/// Behavior of an object
pub trait ObjectBehavior: Debug + ObjectBehaviorClone {
    /// Returns all actions performed by the object
    /// in the current simulation tick
    fn step(
        &mut self,
        own_description: &ObjectDescription,
        world_interactor: &dyn WorldInteractor,
    ) -> Option<Action>;

    /// Cast implementation to `Any`.
    /// This is needed in order to downcast trait objects of type `&dyn ObjectBehavior` to
    /// concrete types.
    ///
    /// Implement this as follows.
    /// ```rust,ignore
    /// fn as_any(&self) -> &Any {
    ///    self
    /// }
    /// ```
    /// [Additional information](https://stackoverflow.com/a/47642317/5903309)
    fn as_any(&self) -> &'_ dyn Any;
}

/// Supertrait used to make sure that all implementors
/// of [`ObjectBehavior`] are [`Clone`]. You don't need
/// to care about this type.
///
/// [`ObjectBehavior`]: ./trait.ObjectBehavior.html
/// [`Clone`]: https://doc.rust-lang.org/nightly/std/clone/trait.Clone.html
#[doc(hidden)]
pub trait ObjectBehaviorClone {
    fn clone_box(&self) -> Box<dyn ObjectBehavior>;
}

impl<T> ObjectBehaviorClone for T
where
    T: ObjectBehavior + Clone + 'static,
{
    default fn clone_box(&self) -> Box<dyn ObjectBehavior> {
        box self.clone()
    }
}

impl Clone for Box<dyn ObjectBehavior> {
    fn clone(&self) -> Self {
        self.clone_box()
    }
}

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

    /// Mock for [`ObjectBehavior`]
    ///
    /// [`ObjectBehavior`]: ../trait.ObjectBehavior.html
    #[derive(Debug, Default, Clone)]
    pub struct ObjectBehaviorMock {
        expect_step_and_return: Option<(ObjectDescription, Option<Action>)>,

        step_was_called: RefCell<bool>,
    }

    impl ObjectBehaviorMock {
        /// Construt a new `ObjectBehaviorMock`
        pub fn new() -> Self {
            Default::default()
        }

        /// Expect a call to `step`
        pub fn expect_step_and_return(
            &mut self,
            own_description: &ObjectDescription,
            return_value: Option<Action>,
        ) {
            self.expect_step_and_return = Some((own_description.clone(), return_value));
        }
    }

    impl ObjectBehavior for ObjectBehaviorMock {
        fn step(
            &mut self,
            own_description: &ObjectDescription,
            _world_interactor: &dyn WorldInteractor,
        ) -> Option<Action> {
            *self.step_was_called.borrow_mut() = true;

            let (expected_own_description, return_value) = self
                .expect_step_and_return
                .clone()
                .expect("step() was called unexpectedly");

            assert_eq!(
                expected_own_description, *own_description,
                "step() was called with {:?}, expected {:?}",
                own_description, expected_own_description
            );

            return_value.clone()
        }

        fn as_any(&self) -> &'_ dyn Any {
            self
        }
    }

    impl Drop for ObjectBehaviorMock {
        fn drop(&mut self) {
            if panicking() {
                return;
            }

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn object_behavior_can_be_downcast() {
        let object_behavior: Box<dyn ObjectBehavior> = box ObjectBehaviorMock::new();

        let object_behavior_as_any = object_behavior.as_any();
        let downcast_behavior = object_behavior_as_any.downcast_ref::<ObjectBehaviorMock>();

        let _unwrapped_downcast_behavior: &ObjectBehaviorMock = downcast_behavior.unwrap();
    }
}