-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm_test.go
76 lines (68 loc) · 1.92 KB
/
fsm_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
package easyFSM_test
import (
"errors"
"fmt"
"sort"
"testing"
"github.com/KScaesar/easyFSM"
)
func TestFSM_StateAll(t *testing.T) {
expected := []OrderState{
OrderStateAwaitingPayment,
OrderStateConfirmed,
OrderStateShipped,
OrderStateDelivered,
OrderStateCancelled,
OrderStateReturnInProgress,
OrderStateReturned,
OrderStateRefundInProgress,
OrderStateRefunded,
}
sort.Slice(expected, func(i, j int) bool {
return expected[i] < expected[j]
})
actual := OrderFSM.ShowStates()
sort.Slice(actual, func(i, j int) bool {
return actual[i] < actual[j]
})
expected_ := fmt.Sprintf("%v", expected)
actual_ := fmt.Sprintf("%v", actual)
if expected_ != actual_ {
t.Errorf("expected = %v, but actual = %v", expected_, actual_)
}
}
func TestFSM_OnAction(t *testing.T) {
fsm1 := OrderFSM.CopyFSM(OrderStateAwaitingPayment)
err := fsm1.OnAction(OrderEventPlaced, func(nextState OrderState) error {
expected := OrderStateConfirmed
if expected != nextState {
return fmt.Errorf("expected = %v, but actual = %v", expected, nextState)
}
return nil
})
if err != nil {
t.Errorf("Payed.OnAction: %v", err)
}
fsm2 := OrderFSM.CopyFSM(OrderStateShipped)
err = fsm2.OnAction(OrderEventReturnRequested, func(nextState OrderState) error {
expected := OrderStateReturnInProgress
if expected != nextState {
return fmt.Errorf("expected = %v, but actual = %v", expected, nextState)
}
return nil
})
if err != nil {
t.Errorf("ReturnInProgress.OnAction: %v", err)
}
fsm3 := OrderFSM.CopyFSM(OrderStateShipped)
err = fsm3.OnAction("CloudNetwork.Created", func(nextState OrderState) error {
expected := OrderStateReturnInProgress
if expected != nextState {
return fmt.Errorf("expected = %v, but actual = %v", expected, nextState)
}
return nil
})
if !errors.Is(err, easyFSM.ErrEventNotDefined) {
t.Errorf("NotExistEvent.OnAction: expected = %v, but actual = %v", easyFSM.ErrEventNotDefined, err)
}
}