-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathtransition_test.go
More file actions
135 lines (112 loc) · 3.83 KB
/
transition_test.go
File metadata and controls
135 lines (112 loc) · 3.83 KB
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
130
131
132
133
134
135
package stateless
import (
"context"
"testing"
)
func TestStateMachine_Fire_TriggerHandledOnSuperStateAndSubState_UsesSubstateTransition(t *testing.T) {
sm := NewStateMachine(stateA)
sm.Configure(stateA).
Permit(triggerX, stateB)
sm.Configure(stateB).
SubstateOf(stateA).
Permit(triggerX, stateC)
sm.Fire(triggerX)
if got := sm.MustState(); got != stateB {
t.Errorf("sm.MustState() = %v, want %v", got, stateB)
}
sm.Fire(triggerX)
if got := sm.MustState(); got != stateC {
t.Errorf("sm.MustState() = %v, want %v", got, stateC)
}
}
func TestStateMachine_Fire_TriggerHandledOnSuperStateAndSubState_SubstateGuardBlocked_UsesSuperstateTransition(t *testing.T) {
guardConditionValue := false
sm := NewStateMachine(stateB)
sm.Configure(stateA).
Permit(triggerX, stateD)
sm.Configure(stateB).
SubstateOf(stateA).
Permit(triggerX, stateC, func(_ context.Context, _ ...any) bool {
return guardConditionValue
})
sm.Fire(triggerX)
if got := sm.MustState(); got != stateD {
t.Errorf("sm.MustState() = %v, want %v", got, stateD)
}
}
func TestStateMachine_Fire_TriggerHandledOnSuperStateAndSubState_SubstateGuardOpen_UsesSubstateTransition(t *testing.T) {
guardConditionValue := true
sm := NewStateMachine(stateB)
sm.Configure(stateA).
Permit(triggerX, stateD)
sm.Configure(stateB).
SubstateOf(stateA).
Permit(triggerX, stateC, func(_ context.Context, _ ...any) bool {
return guardConditionValue
})
sm.Fire(triggerX)
if got := sm.MustState(); got != stateC {
t.Errorf("sm.MustState() = %v, want %v", got, stateC)
}
}
func TestStateMachine_InternalTransitionIf_ExecutesOnlyFirstMatchingAction(t *testing.T) {
sm := NewStateMachine(1)
executed := []int{}
sm.Configure(1).
InternalTransition(1, func(_ context.Context, _ ...any) error {
executed = append(executed, 1)
return nil
}, func(_ context.Context, _ ...any) bool {
return true
}).
InternalTransition(1, func(_ context.Context, _ ...any) error {
executed = append(executed, 2)
return nil
}, func(_ context.Context, _ ...any) bool {
return false
})
sm.Fire(1)
if len(executed) != 1 || executed[0] != 1 {
t.Errorf("expected only first action to execute, got executions: %v", executed)
}
}
func TestStateMachine_Fire_MultiLayerSubstates_ClosestAncestorTransitionUsed(t *testing.T) {
tests := []struct {
name string
parentGuardConditionValue bool
childGuardConditionValue bool
grandchildGuardConditionValue bool
expectedState string
}{
{"GrandchildOpen", false, false, true, "GrandchildStateTarget"},
{"ChildOpen_GrandchildClosed", false, true, false, "ChildStateTarget"},
{"ChildOpen_GrandchildOpen", false, true, true, "GrandchildStateTarget"},
{"ParentOpen_ChildClosed_GrandchildClosed", true, false, false, "ParentStateTarget"},
{"ParentOpen_ChildClosed_GrandchildOpen", true, false, true, "GrandchildStateTarget"},
{"ParentOpen_ChildOpen_GrandchildClosed", true, true, false, "ChildStateTarget"},
{"AllOpen", true, true, true, "GrandchildStateTarget"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := NewStateMachine("GrandchildState")
sm.Configure("ParentState").
Permit(triggerX, "ParentStateTarget", func(_ context.Context, _ ...any) bool {
return tt.parentGuardConditionValue
})
sm.Configure("ChildState").
SubstateOf("ParentState").
Permit(triggerX, "ChildStateTarget", func(_ context.Context, _ ...any) bool {
return tt.childGuardConditionValue
})
sm.Configure("GrandchildState").
SubstateOf("ChildState").
Permit(triggerX, "GrandchildStateTarget", func(_ context.Context, _ ...any) bool {
return tt.grandchildGuardConditionValue
})
sm.Fire(triggerX)
if got := sm.MustState(); got != tt.expectedState {
t.Errorf("sm.MustState() = %v, want %v", got, tt.expectedState)
}
})
}
}