-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfsm_test.go
101 lines (92 loc) · 1.87 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
package easyfsm
import (
"reflect"
"testing"
)
var (
DefaultBusinessName = BusinessName("business_order")
)
func Init() {
args := DefaultArgList()
for i := range DefaultArgList() {
RegisterStateMachine(DefaultBusinessName, args[i].state, args[i].entity)
}
}
func TestNewFSM(t *testing.T) {
initState := State(0)
wantFsm := &FSM{
state: initState,
businessName: DefaultBusinessName,
}
fsm := NewFSM(DefaultBusinessName, initState)
if !reflect.DeepEqual(fsm, wantFsm) {
t.Errorf("fsm %v want %v", fsm, wantFsm)
}
}
func TestFSM_Call(t *testing.T) {
Init()
type (
wantRes struct {
error error
state State
}
arg struct {
state State
eventName EventName
wantRes wantRes
}
)
args := []arg{
{
state: State(0),
eventName: EventName("crateOrder"),
wantRes: wantRes{
error: nil,
state: 1,
},
},
{
state: State(1),
eventName: EventName("payOrder"),
wantRes: wantRes{
error: nil,
state: 2,
},
},
{
state: State(1),
eventName: EventName("cancelOrder"),
wantRes: wantRes{
error: nil,
state: 3,
},
},
{
state: State(3),
eventName: EventName("no_state"),
wantRes: wantRes{
error: UnKnownStateError{businessName: DefaultBusinessName, state: State(3)},
state: State(3),
},
},
{
state: State(1),
eventName: EventName("no_event"),
wantRes: wantRes{
error: UnKnownEventError{businessName: DefaultBusinessName,
event: EventName("no_event"), state: State(1)},
state: State(1),
},
},
}
for i := range args {
fsm := NewFSM(DefaultBusinessName, args[i].state)
resState, err := fsm.Call(args[i].eventName)
if err != args[i].wantRes.error {
t.Errorf("err %v want %v", err, args[i].wantRes.error)
}
if resState != args[i].wantRes.state {
t.Errorf("state %v want %v", resState, args[i].wantRes.state)
}
}
}