-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachine_test.go
91 lines (81 loc) · 2.5 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
/* Package statemachine
* @Author 砚池/Ivan
* @Date 2024/04/08
* @Description:
*/
package statemachine
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var (
f Factory[string, string, any]
statemachine *StateMachine[string, string, any]
)
func init() {
f = Factory[string, string, any]{}
b := Builder[string, string, any]{}
b.ExternalTransition().From("bar").To("baz").On("hi").Perform(func(from string, to string, event string, context *any) {
fmt.Printf("from %v ,to %v,on %v\n", from, to, event)
})
b.ExternalTransition().From("foo").To("bar").On("ping").When(func(ctx *any) bool {
fmt.Println("check true")
return true
}).Perform(func(from string, to string, event string, context *any) {
fmt.Printf("from %v ,to %v,on %v\n", from, to, event)
})
b.ExternalTransitions().FromAmong("foo", "bar").To("zzz").On("sleep").When(func(ctx *any) bool {
return true
}).Perform(func(from string, to string, event string, context *any) {
fmt.Printf("from %v ,to %v,on %v\n", from, to, event)
})
b.InternalTransition().Within("foo").On("in").Perform(func(from string, to string, event string, context *any) {
fmt.Printf("from %v ,to %v,on %v\n", from, to, event)
})
var fetcher CurrentStateFetcher[string, any] = func(ctx *any) string {
return "foo"
}
b.SetCurrentStateFetcher(fetcher)
b.SetFailCallback(&NumbFailCallback[string, string, any]{})
statemachine = b.Build("test")
_ = f.Register(statemachine)
}
func TestShowStatemachine(t *testing.T) {
statemachine.ShowStateMachine()
}
func TestFireEvent(t *testing.T) {
is := assert.New(t)
target := statemachine.FireEvent("foo", "ping", nil)
is.Equal("bar", target)
target = statemachine.FireEvent("bar", "zzz", nil)
is.Equal("bar", target)
target = statemachine.FireEvent("bar", "hi", nil)
is.Equal("baz", target)
target = statemachine.FireEvent("foo", "in", nil)
is.Equal("foo", target)
target = statemachine.FireEvent("foo", "sleep", nil)
is.Equal("zzz", target)
target = statemachine.FireEvent("bar", "sleep", nil)
is.Equal("zzz", target)
target = statemachine.FireEventByFetcher("ping", nil)
is.Equal("bar", target)
}
func TestVerify(t *testing.T) {
is := assert.New(t)
v := statemachine.Verify("foo", "ping")
is.True(v)
v = statemachine.Verify("foo", "xxx")
is.False(v)
v = statemachine.VerifyWithFetcher("ping", nil)
is.True(v)
v = statemachine.VerifyWithFetcher("xxx", nil)
is.False(v)
}
func TestFactory(t *testing.T) {
sm, err := f.Get("test")
if err != nil {
panic(err)
}
sm.ShowStateMachine()
}