Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 8

[ 219 ]

ledSwitch.switchOn()
then:
ledSwitch.state == 'ON'

when:
ledSwitch.switchOn()
then:
ledSwitch.state == 'ON'

when:
ledSwitch.switchOff()
then:
ledSwitch.state == 'OFF'

when:
ledSwitch.switchOff()
then:
ledSwitch.state == 'OFF'

Both these state machines use just two states. What if we have a more complex
state machine with multiple states, events, and transitions? The following is a state
machine that mimics the operation of an old cassette type tape machine:


state: "EMPTY"
state: "LOADED"
state: "RUNNING"
state: "PAUSED"

event: "load"
when: "EMPTY"
next = "LOADED"

event: "start"
when: "LOADED"
next = "RUNNING"
when: "PAUSED"
next = "RUNNING"

event: "pause"
when: "RUNNING"
next = "PAUSED"

event: "stop"
when: "RUNNING"
next = "LOADED"
Free download pdf