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 Unit

We want to start the state machine in the DoorOpen state and then follow this sequence of transitions:

  1. Close the door, expect transition to DoorClosed
  2. Lock the door with PIN “1234”, expect transition to DoorLocked with the stored PIN
  3. Attempt to Unlock with the wrong PIN “abcd”, expect to stay in DoorLocked with the original PIN
  4. Unlock with the correct PIN “1234”, expect transition to DoorClosed
  5. Open the door, expect transition to DoorOpen

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.

↑ Back to top