-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.go
87 lines (73 loc) · 1.71 KB
/
run_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
package fsm_test
import (
"context"
"testing"
"time"
"github.com/andygeiss/fsm"
)
func setup() (ctx context.Context, cancel context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Millisecond*1)
}
func TestRun_Given_0_Should_Return_0_After_StateFn(t *testing.T) {
// Arrange
type stateData struct{ state int }
ctx, cancel := setup()
defer cancel()
data := stateData{state: 0}
stateFn := func(ctx context.Context, data stateData) fsm.StateFn[stateData] {
return nil
}
// Act
go fsm.Run(stateFn, ctx, data)
// Assert
select {
case <-ctx.Done():
}
if data.state != 0 {
t.Errorf("State should be 0, but got %d", data.state)
}
}
func TestRun_Given_0_Should_Return_1_After_StateFn(t *testing.T) {
// Arrange
type stateData struct{ state int }
ctx, cancel := setup()
defer cancel()
data := &stateData{state: 0}
stateFn := func(ctx context.Context, data *stateData) fsm.StateFn[*stateData] {
data.state = 1
return nil
}
// Act
go fsm.Run(stateFn, ctx, data)
// Assert
select {
case <-ctx.Done():
}
if data.state != 1 {
t.Errorf("State should be 1, but got %d", data.state)
}
}
func TestRun_Given_0_Should_Return_2_After_Two_StateFn(t *testing.T) {
// Arrange
type stateData struct{ state int }
ctx, cancel := setup()
defer cancel()
data := &stateData{state: 0}
stateFnB := func(ctx context.Context, data *stateData) fsm.StateFn[*stateData] {
data.state++
return nil
}
stateFnA := func(ctx context.Context, data *stateData) fsm.StateFn[*stateData] {
data.state++
return stateFnB(ctx, data)
}
// Act
go fsm.Run(stateFnA, ctx, data)
// Assert
select {
case <-ctx.Done():
}
if data.state != 2 {
t.Errorf("State should be 2, but got %d", data.state)
}
}