use rsfsm::business_object::{BusinessObject, BusinessObjectId}; use rsfsm::event::Event; use rsfsm::lifecycle_object::LifecycleObject; use rsfsm::state::State; use rsfsm::state_machine::StateMachine; use rsfsm::transition::Transition; fn test_state_machine() -> StateMachine { let state_off: State = "Off"; let state_on: State = "On"; let event_switch_on: Event = "SwitchOn"; let event_switch_off: Event = "SwitchOff"; let transitions = vec![ Transition { from_state: state_off, on_event: event_switch_on, to_state: state_on, }, Transition { from_state: state_on, on_event: event_switch_off, to_state: state_off, }, ]; StateMachine::new(state_off, transitions) } struct Light { pub id: Option, pub state_machine: StateMachine, } impl Light { pub fn new(id: Option) -> Light { Light { id, state_machine: test_state_machine() } } } impl LifecycleObject for Light { fn get_state_machine(&mut self) -> &mut StateMachine { &mut self.state_machine } fn get_data(&self) -> &Light { self } fn get_data_mut(&mut self) -> &mut Light { self } } impl BusinessObject for Light { fn get_id(&self) -> Option { self.id } fn set_id(&mut self, id: BusinessObjectId) { self.id = Some(id); } fn get_type_name(&self) -> &'static str { "Light" } } #[test] fn successfully_retrieve_empty_id() { let light = Light::new(None); assert_eq!(light.get_id(), None); } #[test] fn successfully_retrieve_id() { let light = Light::new(Some(1)); assert_eq!(light.get_id(), Some(1)); } #[test] fn successfully_modify_id() { let mut light = Light::new(None); assert_eq!(light.get_id(), None); light.set_id(1); assert_eq!(light.get_id(), Some(1)); }