MCPcopy
hub / github.com/looplab/fsm / TestMultipleEvents

Function TestMultipleEvents

fsm_test.go:156–206  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

154}
155
156func TestMultipleEvents(t *testing.T) {
157 fsm := NewFSM(
158 "start",
159 Events{
160 {Name: "first", Src: []string{"start"}, Dst: "one"},
161 {Name: "second", Src: []string{"start"}, Dst: "two"},
162 {Name: "reset", Src: []string{"one"}, Dst: "reset_one"},
163 {Name: "reset", Src: []string{"two"}, Dst: "reset_two"},
164 {Name: "reset", Src: []string{"reset_one", "reset_two"}, Dst: "start"},
165 },
166 Callbacks{},
167 )
168
169 err := fsm.Event(context.Background(), "first")
170 if err != nil {
171 t.Errorf("transition failed %v", err)
172 }
173 err = fsm.Event(context.Background(), "reset")
174 if err != nil {
175 t.Errorf("transition failed %v", err)
176 }
177 if fsm.Current() != "reset_one" {
178 t.Error("expected state to be 'reset_one'")
179 }
180 err = fsm.Event(context.Background(), "reset")
181 if err != nil {
182 t.Errorf("transition failed %v", err)
183 }
184 if fsm.Current() != "start" {
185 t.Error("expected state to be 'start'")
186 }
187
188 err = fsm.Event(context.Background(), "second")
189 if err != nil {
190 t.Errorf("transition failed %v", err)
191 }
192 err = fsm.Event(context.Background(), "reset")
193 if err != nil {
194 t.Errorf("transition failed %v", err)
195 }
196 if fsm.Current() != "reset_two" {
197 t.Error("expected state to be 'reset_two'")
198 }
199 err = fsm.Event(context.Background(), "reset")
200 if err != nil {
201 t.Errorf("transition failed %v", err)
202 }
203 if fsm.Current() != "start" {
204 t.Error("expected state to be 'start'")
205 }
206}
207
208func TestGenericCallbacks(t *testing.T) {
209 beforeEvent := false

Callers

nothing calls this directly

Calls 4

EventMethod · 0.95
CurrentMethod · 0.95
NewFSMFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…