You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

3 years ago
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)