-
Notifications
You must be signed in to change notification settings - Fork 3
/
state_machine_test.go
39 lines (34 loc) · 944 Bytes
/
state_machine_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
package fsm_test
import (
"testing"
"github.com/d5/go-fsm"
"github.com/d5/tengo/v2/require"
)
func TestStateMachine_Run(t *testing.T) {
machine, err := fsm.New(testScript).
State("s1", "", "").
State("s2", "", "").
Transition("s1", "s2", "", "fn1"). // value not changed
Compile()
require.NoError(t, err)
out, err := machine.Run("s1", 123)
require.NoError(t, err)
require.Equal(t, int64(123), out.Value())
machine, err = fsm.New(testScript).
State("s1", "", "").
State("s2", "", "").
Transition("s1", "s2", "", "fn2"). // change it to "foobar"
Compile()
require.NoError(t, err)
out, err = machine.Run("s1", 123)
require.NoError(t, err)
require.Equal(t, "foobar", out.Value())
machine, err = fsm.New(testScript).
State("s1", "", "").
State("s2", "", "").
Transition("s1", "s2", "", "err1"). // error returned
Compile()
require.NoError(t, err)
_, err = machine.Run("s1", 123)
require.Error(t, err)
}