-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm_test.go
129 lines (103 loc) · 2.64 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
// Based on https://en.wikipedia.org/wiki/Finite-state_machine
//
//=== RUN TestTurnstile
//[state] +Locked
// fsm_test.go:74: Push into (Locked:1)
// fsm_test.go:79: Coin into (Locked:1)
//[state] +InputCoin
//[state] -InputCoin
//[state] +Unlocked -Locked
// fsm_test.go:85: Coin into (Unlocked:1)
//[state] +InputCoin
//[state] -InputCoin
// fsm_test.go:91: Push into (Unlocked:1)
//[state] +InputPush
//[state] -InputPush
//[state] +Locked -Unlocked
// fsm_test.go:96: End with (Locked:3)
//--- PASS: TestTurnstile (0.00s)
//PASS
package fsm
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/joho/godotenv"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
)
func init() {
// load .env
_ = godotenv.Load()
// am-dbg is required for debugging, go run it
// go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest
// amhelp.EnableDebugging(false)
// amhelp.SetLogLevel(am.LogChanges)
}
// state enum pkg
var (
states = am.Struct{
// input states
InputPush: {},
InputCoin: {},
// "state" states
Locked: {
Auto: true,
Remove: groupUnlocked,
},
Unlocked: {Remove: groupUnlocked},
}
groupUnlocked = am.S{Locked, Unlocked}
InputPush = "InputPush"
InputCoin = "InputCoin"
Locked = "Locked"
Unlocked = "Unlocked"
Names = am.S{InputPush, InputCoin, Locked, Unlocked}
)
// handlers
type Turnstile struct{}
func (t *Turnstile) InputPushEnter(e *am.Event) bool {
return e.Machine.Is1(Unlocked)
}
func (t *Turnstile) InputPushState(e *am.Event) {
e.Machine.Remove1(InputPush, nil)
e.Machine.Add1(Locked, nil)
}
func (t *Turnstile) InputCoinState(e *am.Event) {
e.Machine.Remove1(InputCoin, nil)
e.Machine.Add1(Unlocked, nil)
}
// example
func TestTurnstile(t *testing.T) {
mach := am.New(context.Background(), states, &am.Opts{
ID: "turnstile",
DontPanicToException: true,
DontLogID: true,
LogLevel: am.LogChanges,
HandlerTimeout: time.Minute,
})
_ = mach.BindHandlers(&Turnstile{})
_ = mach.VerifyStates(Names)
// start
mach.Add1(Locked, nil)
assert.True(t, mach.Is1(Locked))
t.Logf("Push into %s", mach)
mach.Add1(InputPush, nil)
assert.True(t, mach.Is1(Locked))
// coin
t.Logf("Coin into %s", mach)
mach.Add1(InputCoin, nil)
assert.True(t, mach.Not1(Locked))
assert.True(t, mach.Is1(Unlocked))
// coin
t.Logf("Coin into %s", mach)
mach.Add1(InputCoin, nil)
assert.True(t, mach.Not1(Locked))
assert.True(t, mach.Is1(Unlocked))
// push
t.Logf("Push into %s", mach)
mach.Add1(InputPush, nil)
assert.True(t, mach.Not1(Unlocked))
assert.True(t, mach.Is1(Locked))
t.Logf("End with %s", mach)
}