From 6ca0b7b6ebc1cf025dc9c62f50f1de6420df925b Mon Sep 17 00:00:00 2001 From: Marco Perone Date: Tue, 17 Jan 2023 10:39:25 +0100 Subject: [PATCH] check with examples that run is working properly --- spec/CRM/StateMachineSpec.hs | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 spec/CRM/StateMachineSpec.hs diff --git a/spec/CRM/StateMachineSpec.hs b/spec/CRM/StateMachineSpec.hs new file mode 100644 index 0000000..cafa10f --- /dev/null +++ b/spec/CRM/StateMachineSpec.hs @@ -0,0 +1,68 @@ +module CRM.StateMachineSpec where + +import CRM.Example.LockDoor +import CRM.Example.Switch (switchMachine) +import "crm" CRM.StateMachine +import "singletons-base" Data.Singletons.Base.TH +import "hspec" Test.Hspec (Expectation, Spec, describe, it, shouldBe) + +shouldOutput :: (Eq b, Show b) => (b, StateMachine a b) -> b -> Expectation +shouldOutput (output, _) expectedOutput = output `shouldBe` expectedOutput + +spec :: Spec +spec = + describe "StateMachine" $ do + describe "run" $ do + describe "switch machine" $ do + it "outputs () when the state is False" $ do + run (Basic $ switchMachine SFalse) () `shouldOutput` () + + it "outputs () when the state is True" $ do + run (Basic $ switchMachine STrue) () `shouldOutput` () + + describe "lock door machine" $ do + it "outputs Opened when it is closed and receive an Open command" $ do + run (Basic $ lockDoorMachine SIsLockClosed) LockOpen `shouldOutput` LockOpened + + it "outputs NoOp when it is closed and receive a Close command" $ do + run (Basic $ lockDoorMachine SIsLockClosed) LockClose `shouldOutput` LockNoOp + + it "outputs Locked when it is closed and receive a Lock command" $ do + run (Basic $ lockDoorMachine SIsLockClosed) LockLock `shouldOutput` LockLocked + + it "outputs NoOp when it is closed and receive an Unlock command" $ do + run (Basic $ lockDoorMachine SIsLockClosed) LockUnlock `shouldOutput` LockNoOp + + it "outputs NoOp when it is open and receive an Open command" $ do + run (Basic $ lockDoorMachine SIsLockOpen) LockOpen `shouldOutput` LockNoOp + + it "outputs Closed when it is open and receive a Close command" $ do + run (Basic $ lockDoorMachine SIsLockOpen) LockClose `shouldOutput` LockClosed + + it "outputs NoOp when it is open and receive a Lock command" $ do + run (Basic $ lockDoorMachine SIsLockOpen) LockLock `shouldOutput` LockNoOp + + it "outputs NoOp when it is open and receive an Unlock command" $ do + run (Basic $ lockDoorMachine SIsLockOpen) LockUnlock `shouldOutput` LockNoOp + + it "outputs NoOp when it is locked and receive an Open command" $ do + run (Basic $ lockDoorMachine SIsLockLocked) LockOpen `shouldOutput` LockNoOp + + it "outputs NoOp when it is locked and receive a Close command" $ do + run (Basic $ lockDoorMachine SIsLockLocked) LockClose `shouldOutput` LockNoOp + + it "outputs NoOp when it is locked and receive a Lock command" $ do + run (Basic $ lockDoorMachine SIsLockLocked) LockLock `shouldOutput` LockNoOp + + it "outputs Unlocked when it is locked and receive an Unlock command" $ do + run (Basic $ lockDoorMachine SIsLockLocked) LockUnlock `shouldOutput` LockUnlocked + + it "outputs Locked when it is open and receive a Close and a Lock command" $ do + let + runOnce = snd $ run (Basic $ lockDoorMachine SIsLockOpen) LockClose + run runOnce LockLock `shouldOutput` LockLocked + + it "outputs Opened when it is locked and receive a Unlock and an Open command" $ do + let + runOnce = snd $ run (Basic $ lockDoorMachine SIsLockLocked) LockUnlock + run runOnce LockOpen `shouldOutput` LockOpened