-
Notifications
You must be signed in to change notification settings - Fork 4
/
fsm_test.go
225 lines (207 loc) · 4.42 KB
/
fsm_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package fsm_test
import (
"fmt"
"testing"
"github.com/cocoonspace/fsm"
)
const (
StateFoo fsm.State = iota
StateBar
)
const (
EventFoo fsm.Event = iota
EventBar
)
func TestFSM(t *testing.T) {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar),
)
res := f.Event(EventFoo)
if !res {
t.Error("Event returned false")
}
if f.Current() != StateBar {
t.Error("Bad destination state")
}
f.Reset()
if f.Current() != StateFoo {
t.Error("Bad state after Reset")
}
}
func TestCheck(t *testing.T) {
check := false
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Check(func() bool {
return check
}),
fsm.Dst(StateBar),
)
res := f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen because of Check")
}
check = true
res = f.Event(EventFoo)
if !res && f.Current() != StateBar {
t.Error("Transition should happen thanks to Check")
}
}
func ExampleCheck() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Check(func() bool {
return true
}),
fsm.Dst(StateBar),
)
}
func TestNotCheck(t *testing.T) {
check := true
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.NotCheck(func() bool {
return check
}),
fsm.Dst(StateBar),
)
res := f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen because of NotCheck")
}
check = false
res = f.Event(EventFoo)
if !res && f.Current() != StateBar {
t.Error("Transition should happen thanks to NotCheck")
}
}
func TestCall(t *testing.T) {
call := false
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Call(func() {
call = true
}),
)
_ = f.Event(EventFoo)
if !call {
t.Error("Call should have been called")
}
}
func ExampleCall() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar), fsm.Call(func() {
fmt.Println("Call called")
}),
)
}
func TestTimes(t *testing.T) {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
f.Transition(
fsm.On(EventBar), fsm.Src(StateBar),
fsm.Dst(StateFoo),
)
res := f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen the first time")
}
res = f.Event(EventFoo)
if !res || f.Current() != StateBar {
t.Error("Transition should happen the second time")
}
res = f.Event(EventBar)
if !res || f.Current() != StateFoo {
t.Error("FSM should have returned to StateFoo")
}
res = f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen the first time of the second run")
}
res = f.Event(EventFoo)
if !res || f.Current() != StateBar {
t.Error("Transition should happen the second time of the second run")
}
}
func ExampleTimes() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
_ = f.Event(EventFoo) // no transition
_ = f.Event(EventFoo) // transition to StateBar
}
func TestEnterExit(t *testing.T) {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar),
)
f.Transition(
fsm.On(EventBar), fsm.Src(StateBar),
fsm.Dst(StateFoo),
)
var entry, exit fsm.State
f.Enter(func(state fsm.State) {
entry = state
})
f.Exit(func(state fsm.State) {
exit = state
})
_ = f.Event(EventFoo)
if entry != StateBar {
t.Error("Enter func has not been called")
}
if exit != StateFoo {
t.Error("Exit func has not been called")
}
_ = f.Event(EventBar)
if entry != StateFoo {
t.Error("Enter func has not been called")
}
if exit != StateBar {
t.Error("Exit func has not been called")
}
}
func TestEnterExitState(t *testing.T) {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar),
)
f.Transition(
fsm.On(EventBar), fsm.Src(StateBar),
fsm.Dst(StateFoo),
)
entry, exit := false, false
f.EnterState(StateBar, func() {
entry = true
})
f.ExitState(StateBar, func() {
exit = true
})
_ = f.Event(EventFoo)
if !entry {
t.Error("EnterState func has not been called")
}
if exit {
t.Error("ExitState func has wrongly been called")
}
entry, exit = false, false
_ = f.Event(EventBar)
if entry {
t.Error("EnterState func has wrongly been called")
}
if !exit {
t.Error("ExitState func has not been called")
}
}