use rsfsm::event::Event; use rsfsm::state::State; use rsfsm::state_machine::StateMachine; use rsfsm::transition::Transition; pub 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) } #[test] fn successful_state_transition() { let mut state_machine = test_state_machine(); assert_eq!(state_machine.current_state, "Off"); state_machine.apply_event("SwitchOn").expect("Failed to apply event, but it should have succeeded"); assert_eq!(state_machine.current_state, "On"); } #[test] fn invalid_state_transition() { let mut state_machine = test_state_machine(); assert_eq!(state_machine.current_state, "Off"); assert_eq!(state_machine.apply_event("SwitchOff").is_err(), true); assert_eq!(state_machine.current_state, "Off"); }