-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_test.go
82 lines (70 loc) · 1.6 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
package fsm_test
import (
"testing"
fsm "github.com/hyeonjae/fsm"
)
func TestFSM(t *testing.T) {
s1 := fsm.State{State: "s1"}
s2 := fsm.State{State: "s2"}
s3 := fsm.State{State: "s3"}
a1 := fsm.Action{Action: "a"}
a2 := fsm.Action{Action: "b"}
t1 := fsm.Transition{
Tuple: fsm.StateActionTuple{State: s1, Action: a1},
State: s2,
}
t2 := fsm.Transition{
Tuple: fsm.StateActionTuple{State: s1, Action: a2},
State: s1,
}
t3 := fsm.Transition{
Tuple: fsm.StateActionTuple{State: s2, Action: a1},
State: s1,
}
t4 := fsm.Transition{
Tuple: fsm.StateActionTuple{State: s2, Action: a2},
State: s3,
}
builder := fsm.Builder().
States([]fsm.State{s1, s2, s3}).
Actions([]fsm.Action{a1, a2}).
Start(s1).
Accepts([]fsm.State{s3}).
Transitions([]fsm.Transition{t1, t2, t3, t4})
statemachine := fsm.New(builder)
statemachine.Start()
if statemachine.Current() != s1 {
t.Error("Wrong result")
}
if statemachine.Accepted() != false {
t.Error("Wrong result")
}
statemachine.Transit(a1)
if statemachine.Current() != s2 {
t.Error("Wrong result")
}
if statemachine.Accepted() != false {
t.Error("Wrong result")
}
statemachine.Transit(a1)
if statemachine.Current() != s1 {
t.Error("Wrong result")
}
if statemachine.Accepted() != false {
t.Error("Wrong result")
}
statemachine.Transit(a1)
if statemachine.Current() != s2 {
t.Error("Wrong result")
}
if statemachine.Accepted() != false {
t.Error("Wrong result")
}
statemachine.Transit(a2)
if statemachine.Current() != s3 {
t.Error("Wrong result")
}
if statemachine.Accepted() != true {
t.Error("Wrong result")
}
}