module Test.FSM ( unitTests ) where import Control.Concurrent.STM.TVar (readTVarIO) import FSM import System.Timeout (timeout) import Test.Tasty import Test.Tasty.HUnit (testCase, (@?=)) unitTests :: TestTree unitTests = testGroup "FSM" [ testSimpleStateChange , testStateWithoutCallback ] data SimpleState = StateInitial | StateIntermediate | StateFinal deriving (Show, Eq, Ord) instance FSMState SimpleState where isTerminalState StateFinal = True isTerminalState _ = False testSimpleStateChange = testCase "Simple state change" $ do fsm <- makeFsm StateInitial [(StateInitial, callbackChangeState StateIntermediate), (StateIntermediate, callbackChangeState StateFinal), (StateFinal, doNothing)] timeout 100000 $ runFsm fsm endState <- readTVarIO (fsmCurrentState fsm) endState @?= StateFinal where callbackChangeState x = FSMCallback (pure $ Just x) doNothing = FSMCallback (pure Nothing) testStateWithoutCallback = testCase "Test without callback" $ do fsm <- makeFsm StateInitial [(StateInitial, callbackChangeState StateIntermediate), (StateFinal, doNothing)] timeout 100000 $ runFsm fsm endState <- readTVarIO (fsmCurrentState fsm) endState @?= StateIntermediate where callbackChangeState x = FSMCallback (pure $ Just x) doNothing = FSMCallback (pure Nothing)