-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
184 lines (156 loc) · 4.82 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package fine_test
import (
"fmt"
"interrato.dev/fine"
)
func ExampleMachine() {
powerSwitch := fine.Machine("off", fine.States{
"off": {"toggle": "on"},
"on": {"toggle": "off"},
})
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Current FSM state: off
}
func ExampleFSM_Add() {
powerSwitch := fine.Machine("off", fine.States{
"off": {"toggle": "on"},
})
// Here I add the "on" state.
powerSwitch.Add("on", fine.Transitions{"toggle": "off"})
// Trying to add the "off" state, which is already in the FSM, will fail.
err := powerSwitch.Add("off", fine.Transitions{"smash": "broken"})
fmt.Println("Adding \"off\" failed?", err != nil)
powerSwitch.Do("toggle")
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Adding "off" failed? true
// Current FSM state: on
}
func ExampleFSM_AddOrReplace() {
powerSwitch := fine.Machine("off", fine.States{
"off": {"toggle": "on"},
})
// Here I add the "on" state. No difference with Add() here.
powerSwitch.AddOrReplace("on", fine.Transitions{"toggle": "off"})
// Here I try to add the "off" state, but, because it's already in the FSM,
// its transitions are now replaced.
powerSwitch.AddOrReplace("off", fine.Transitions{"smash": "broken"})
// The "toggle" event for the "off" state does not exist anymore, so
// nothing changes even if I try to invoke "toggle" many times.
powerSwitch.Do("toggle")
fmt.Println("Current FSM state:", powerSwitch.State())
powerSwitch.Do("toggle")
fmt.Println("Current FSM state still", powerSwitch.State())
powerSwitch.Do("toggle")
fmt.Println("Current FSM state is", powerSwitch.State(), "again")
// So, let's break this switch.
powerSwitch.Do("smash")
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Current FSM state: off
// Current FSM state still off
// Current FSM state is off again
// Current FSM state: broken
}
func ExampleFSM_AddOrMerge() {
powerSwitch := fine.Machine("off", fine.States{
"off": {"toggle": "on"},
})
// Here I add the "on" state. No difference with Add() here.
powerSwitch.AddOrMerge("on", fine.Transitions{"toggle": "off"})
// Here I try to add the "off" state, which is already in the FSM, and the
// new transitions are merged to the old ones.
powerSwitch.AddOrMerge("off", fine.Transitions{"smash": "broken"})
// So I can still toggle it.
powerSwitch.Do("toggle")
fmt.Println("Current FSM state:", powerSwitch.State())
// And toggle it again...
powerSwitch.Do("toggle")
fmt.Println("Current FSM state:", powerSwitch.State())
// ...and now I smash it.
powerSwitch.Do("smash")
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Current FSM state: on
// Current FSM state: off
// Current FSM state: broken
}
func ExampleFSM_Do() {
powerSwitch := fine.Machine("off", fine.States{
"off": {"toggle": "on"},
"on": {"toggle": "off"},
})
fmt.Println("Current FSM state:", powerSwitch.State())
fmt.Println("Toggling...")
powerSwitch.Do("toggle")
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Current FSM state: off
// Toggling...
// Current FSM state: on
}
func ExampleFSM_Do_lifecycle() {
powerSwitch := fine.Machine("off", fine.States{
"off": {
"@exit": func(metadata fine.Metadata) {
fmt.Println("[INFO] from:", metadata.From)
fmt.Println("[INFO] to:", metadata.To)
fmt.Println("[INFO] event:", metadata.Event)
fmt.Println("[INFO] args:", metadata.Args)
},
"toggle": func(args ...interface{}) string {
message := args[0].(string)
fmt.Printf("message: %q\n", message)
return "on"
},
},
"on": {
"@enter": func() {
fmt.Println("Finally, light!")
},
"toggle": "off",
},
})
fmt.Println("Current FSM state:", powerSwitch.State())
fmt.Println("Toggling...")
powerSwitch.Do("toggle", "Shine, step into the light")
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Current FSM state: off
// Toggling...
// message: "Shine, step into the light"
// [INFO] from: off
// [INFO] to: on
// [INFO] event: toggle
// [INFO] args: [Shine, step into the light]
// Finally, light!
// Current FSM state: on
}
func ExampleFSM_Subscribe() {
powerSwitch := fine.Machine("off", fine.States{
"off": {"toggle": "on"},
"on": {"toggle": "off"},
})
onSubscribe := true
unsubscribe := powerSwitch.Subscribe(func(state string) {
if onSubscribe {
fmt.Printf("Subscribed with state set to %q\n", state)
onSubscribe = false
} else {
fmt.Printf("State just changed to %q\n", state)
}
})
powerSwitch.Do("toggle")
powerSwitch.Do("toggle")
powerSwitch.Do("toggle")
unsubscribe()
powerSwitch.Do("toggle")
fmt.Println("Current FSM state:", powerSwitch.State())
// Output:
// Subscribed with state set to "off"
// State just changed to "on"
// State just changed to "off"
// State just changed to "on"
// Current FSM state: off
}