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
use event::Event;
use behaviour::Behaviour;

/// A simple custom behaviour for quick-fixes and testing
pub struct Custom {
    handler: Box<Fn(Event) -> Event>,
}

impl Custom {
    /// Creates a new instance of `Custom`
    pub fn new<F: 'static>(handler: F) -> Custom
        where F: Fn(Event) -> Event
    {
        Custom { handler: Box::new(handler) }
    }
}

impl Behaviour for Custom {
    fn handle_event(&self, event: Event) -> Event {
        (self.handler)(event)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use event::Event;
    use entity::Entity;

    #[test]
    fn new_custom() {
        let custom = Custom::new(|event| Event::Nothing);

        let mut entity = Entity::new("TestEntity");

        entity.append_behaviour(custom);

        assert_eq!(entity.send_event(Event::Nothing), Event::Nothing);
    }
}