aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/program/setup.rs
blob: f248fb6580c2a385d68e80c81ffef8faa10deb94 (plain) (blame)
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
use crate::{ProgramCollect, program::Program};

pub trait ProgramSetup<C>
where
    C: ProgramCollect<Enum = C>,
{
    fn setup(self, program: &mut Program<C>);
}

impl<C> Program<C>
where
    C: ProgramCollect<Enum = C>,
{
    /// Load and execute init logic
    pub fn with_setup<S: ProgramSetup<C> + 'static>(&mut self, setup: S) {
        S::setup(setup, self);
    }
}

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

    struct TestSetup {
        called: std::rc::Rc<std::cell::Cell<bool>>,
    }

    impl ProgramSetup<MockProgramCollect> for TestSetup {
        fn setup(self, _program: &mut Program<MockProgramCollect>) {
            self.called.set(true);
        }
    }

    #[test]
    fn test_with_setup_calls_setup() {
        let called = std::rc::Rc::new(std::cell::Cell::new(false));
        let setup = TestSetup {
            called: std::rc::Rc::clone(&called),
        };
        let mut program: Program<MockProgramCollect> = Program::new_with_args(["test"]);
        program.with_setup(setup);
        assert!(called.get());
    }
}