Testing the update function
We’ll use the same test function which we used in the previous example. Let’s recap how it works quickly by looking at its type signature:
assertWalk
:: forall msg state
. Eq state
=> Show state
=> (state -> msg -> state)
-> state
-> Array (msg /\ state)
-> Aff UnitWe want to start the state machine in the DoorOpen state
and then follow this sequence of transitions:
Closethe door, expect transition toDoorClosedLockthe door with PIN “1234”, expect transition toDoorLockedwith the stored PIN- Attempt to
Unlockwith the wrong PIN “abcd”, expect to stay inDoorLockedwith the original PIN Unlockwith the correct PIN “1234”, expect transition toDoorClosedOpenthe door, expect transition toDoorOpen
In code this looks like this:
specWalk :: Spec Unit
specWalk =
it "should follow the walk and visit the expected intermediate states" do
assertWalk update
(v @"DoorOpen")
[ v @"Close" ~> v @"DoorClosed"
, v @"Lock" { newPin: "1234" } ~> v @"DoorLocked" { storedPin: "1234" }
, v @"Unlock" { enteredPin: "abcd" } ~> v @"DoorLocked" { storedPin: "1234" }
, v @"Unlock" { enteredPin: "1234" } ~> v @"DoorClosed"
, v @"Open" ~> v @"DoorOpen"
]🗎 test/Examples/DoorPin.purs L83-L93
Since this test passes, we can be pretty confident that the update function is correct.