-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder_test.go
74 lines (61 loc) · 2.17 KB
/
builder_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
package fsm_test
import (
"testing"
"github.com/d5/go-fsm"
"github.com/d5/tengo/v2/require"
)
var testScript = []byte(`
export {
fn1: func(src, dst, v) {},
fn2: func(src, dst, v) { return "foobar" },
fn3: func(src, dst) {},
err1: func(src, dst, v) { return error("an error occurred") },
foo: [1, 2, 3]
}`)
func TestBuilder_Validate(t *testing.T) {
// empty state name
err := fsm.New(testScript).State("", "", "").Validate()
require.Error(t, err)
require.Equal(t, "state name must not be empty", err.Error())
// entry function not found
err = fsm.New(testScript).State("s1", "fn4", "").Validate()
require.Error(t, err)
require.Equal(t, "function 'fn4' not found", err.Error())
// exit function not found
err = fsm.New(testScript).State("s1", "", "fn4").Validate()
require.Error(t, err)
require.Equal(t, "function 'fn4' not found", err.Error())
// transition src not found
err = fsm.New(testScript).State("s1", "", "").
Transition("s0", "s1", "", "").Validate()
require.Error(t, err)
require.Equal(t, "state 's0' not found", err.Error())
// transition dst not found
err = fsm.New(testScript).State("s1", "", "").
Transition("s1", "s2", "", "").Validate()
require.Error(t, err)
require.Equal(t, "state 's1' not found", err.Error())
// transition condition function not found
err = fsm.New(testScript).State("s1", "", "").
Transition("s1", "s1", "fn4", "").Validate()
require.Error(t, err)
require.Equal(t, "function 'fn4' not found", err.Error())
// transition action function not found
err = fsm.New(testScript).State("s1", "", "").
Transition("s1", "s1", "", "fn4").Validate()
require.Error(t, err)
require.Equal(t, "function 'fn4' not found", err.Error())
// not a function
err = fsm.New(testScript).State("s1", "foo", "").Validate()
require.Error(t, err)
require.Equal(t, "'foo' is not callable", err.Error())
// wrong number of arguments
err = fsm.New(testScript).State("s1", "fn3", "").Validate()
require.Error(t, err)
require.Equal(t, "function 'fn3' wrong number of arguments: want 3 got 2",
err.Error())
// no error
err = fsm.New(testScript).State("s1", "fn1", "fn2").
Transition("s1", "s1", "fn1", "fn2").Validate()
require.NoError(t, err)
}