-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
59 lines (52 loc) · 1.14 KB
/
example_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
package fsm_test
import (
"math"
"github.com/d5/go-fsm"
)
var script = []byte(`
fmt := import("fmt")
export {
truthy: func(src, dst, v) { return !!v },
falsy: func(src, dst, v) { return !v },
action: func(src, dst, v) { fmt.printf("%s -> %s: %v\n", src, dst, v) },
enter: func(src, dst, v) { fmt.printf("%s ->: %v\n", dst, v) },
leave: func(src, dst, v) { fmt.printf("-> %s: %v\n", src, v) }
}
`)
func Example() {
machine, err := fsm.New(script).
State("S", "enter", "leave").
State("T", "enter", "leave").
State("F", "enter", "leave").
Transition("S", "T", "truthy", "action").
Transition("S", "F", "falsy", "action").
Compile()
if err != nil {
panic(err)
}
if _, err := machine.Run("S", 1); err != nil {
panic(err)
}
if _, err := machine.Run("S", math.NaN()); err != nil {
panic(err)
}
if _, err := machine.Run("S", "foobar"); err != nil {
panic(err)
}
if _, err := machine.Run("S", []interface{}{}); err != nil {
panic(err)
}
// Output:
// -> S: 1
// S -> T: 1
// T ->: 1
// -> S: NaN
// S -> F: NaN
// F ->: NaN
// -> S: "foobar"
// S -> T: "foobar"
// T ->: "foobar"
// -> S: []
// S -> F: []
// F ->: []
}