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"
]Since this test passes, we can be pretty confident that the update function is correct.