-
Notifications
You must be signed in to change notification settings - Fork 0
/
micromachine_test.go
123 lines (110 loc) · 3.13 KB
/
micromachine_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
package micromachine_test
import (
"errors"
"testing"
mm "github.com/MrDaar/micromachine"
mock "github.com/MrDaar/micromachine/test/mock"
)
const (
// Default.
StateUnknown mm.State = 0
// Operational statuses.
StateReady mm.State = 10
StateRiding mm.State = 11
StateBatteryLow mm.State = 12
StateBounty mm.State = 20
StateCollected mm.State = 21
StateDropped mm.State = 22
// Not commissioned for service.
StateServiceMode mm.State = 30
StateTerminated mm.State = 32
GroupPublic mm.Group = mm.GroupPublic
GroupUser mm.Group = 1
GroupStaff mm.Group = 2
GroupAdmin mm.Group = mm.GroupSuperAdmin
GroupSystem mm.Group = 4
)
var errTransitionDisabled = errors.New(`transition disabled`)
func TestTransition(t *testing.T) {
sm := &mm.StateMachine{
Paths: mm.Paths{
GroupPublic: {
StateReady: {StateRiding: nil},
StateRiding: {StateReady: nil},
},
GroupStaff: {
StateBounty: {StateCollected: nil},
StateCollected: {StateDropped: nil},
StateDropped: {StateReady: nil},
},
GroupAdmin: {
StateCollected: {StateDropped: {
func() error {
t.Log(errTransitionDisabled.Error())
return errTransitionDisabled
},
}},
},
GroupSystem: {
StateReady: {StateUnknown: nil, StateBounty: nil},
StateRiding: {StateBatteryLow: nil},
StateBatteryLow: {StateBounty: {
func() error {
t.Log(`Sending bounty notification...`)
return nil
},
}},
},
},
}
sm.Paths[GroupSystem][StateRiding][StateBatteryLow] = []mm.Condition{
func() error {
t.Log(`Transitioning to bounty state...`)
sm.Subject = &mock.Stateful{
GetStateFunc: func() mm.State { return StateBatteryLow },
}
return sm.Transition(GroupSystem, StateBounty)
},
}
for i, tt := range []struct {
in mm.Group
from mm.State
to mm.State
want error
}{
// Pass
{GroupPublic, StateReady, StateRiding, nil},
{GroupPublic, StateRiding, StateReady, nil},
{GroupUser, StateRiding, StateReady, nil},
{GroupStaff, StateBounty, StateCollected, nil},
{GroupStaff, StateCollected, StateDropped, nil},
{GroupStaff, StateDropped, StateReady, nil},
{GroupAdmin, StateCollected, StateReady, nil},
{GroupAdmin, StateBatteryLow, StateDropped, nil},
{GroupAdmin, StateBounty, StateUnknown, nil},
{GroupAdmin, StateServiceMode, StateTerminated, nil},
{GroupSystem, StateReady, StateUnknown, nil},
{GroupSystem, StateReady, StateBounty, nil},
{GroupSystem, StateBatteryLow, StateBounty, nil},
{GroupSystem, StateRiding, StateBatteryLow, nil},
// Fail
{123, StateCollected, StateReady, mm.ErrInvalidGroup},
{GroupPublic, StateCollected, StateReady, mm.ErrInvalidFromState},
{GroupPublic, StateReady, StateCollected, mm.ErrInvalidToState},
{GroupAdmin, StateCollected, StateDropped, errTransitionDisabled},
} {
in := tt.in
from := tt.from
to := tt.to
want := tt.want
sm.Subject = &mock.Stateful{
GetStateFunc: func() mm.State { return from },
}
if err := sm.Transition(in, to); err != want {
if err != nil {
t.Log(err.Error())
}
t.Fatalf(`Test %d - Could not transition - in %d, from %d, to %d`, i, in, from, to)
}
}
}