AST Transformations
[ 200 ]
def toggle() {
context.state = new OffState(context)
}
String toString() {
"ON"
}
}
Finally, we need the client class, which implements the finite state machine:
class LEDToggle {
def context = new StateContext()
def toggle() {
context.state.toggle()
}
String getState() {
context.state
}
}
With all this in place, we can start to write some Spock unit tests to prove the state
machine is working as expected:
given:
def ledToggle = new LEDToggle()
expect:
ledToggle.state == 'OFF'
when:
ledToggle.toggle()
then:
ledToggle.state == 'ON'
when:
ledToggle.toggle()
then:
ledToggle.state == 'OFF'
We have a basic state machine and some unit tests that prove it works as expected.
Now, it's time to try and implement an AST transform that converts our previous
state DSL into a working version of this pattern. The final proof that it is working is
when we can run these tests against a state machine generated from the DSL.
http://www.ebook3000.com