-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatemachine_test.go
126 lines (120 loc) · 2.87 KB
/
statemachine_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
package statemachine_test
import (
"testing"
"github.com/dipeshdulal/statemachine"
"github.com/stretchr/testify/assert"
)
func TestMachineStructure(t *testing.T) {
machine := statemachine.Machine{
ID: "machine-1",
Initial: "on",
States: statemachine.StateMap{
"on": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "off",
},
},
},
"off": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "on",
},
},
},
},
}
output := machine.Transition("TOGGLE")
assert.Equal(t, output, "off", "Transition should occur on toggle.")
output = machine.Transition("TOGGLE")
assert.Equal(t, output, "on", "Transition should occurr on toggle.")
}
func TestMachineCondition(t *testing.T) {
machine := statemachine.Machine{
ID: "machine-1",
Initial: "on",
States: statemachine.StateMap{
"on": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "off",
Cond: func(curr, next string) bool {
return curr == ""
},
},
},
},
"off": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "on",
},
},
},
},
}
output := machine.Transition("TOGGLE")
assert.Equal(t, output, "on", "Transition should not occur on toggle. due to condition")
}
func TestMultipleActions(t *testing.T) {
times := 0
actions := []func(string, string){
func(c, n string) { times++ },
func(c, n string) { times++ },
func(c, n string) { times++ },
}
machine := statemachine.Machine{
ID: "machine-1",
Initial: "on",
States: statemachine.StateMap{
"on": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "off",
Actions: actions,
},
},
},
"off": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "on",
},
},
},
},
}
machine.Transition("TOGGLE")
assert.Equal(t, times, len(actions), "actions are not called")
}
func TestStateSubscribers(t *testing.T) {
times := 0
machine := statemachine.Machine{
ID: "machine-1",
Initial: "on",
Subscribers: []func(string, string){
func(c, n string) { times++ },
},
States: statemachine.StateMap{
"on": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "off",
},
},
},
"off": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "on",
},
},
},
},
}
for i := 0; i < 10; i++ {
machine.Transition("TOGGLE")
}
assert.Equal(t, times, 10, "subscribers are not called")
}