-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
141 lines (118 loc) · 3.14 KB
/
example_test.go
File metadata and controls
141 lines (118 loc) · 3.14 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package statekit_test
import (
"fmt"
"go.klarlabs.de/statekit"
)
func Example() {
// Create a simple traffic light state machine
machine, _ := statekit.NewMachine[struct{}]("traffic").
WithInitial("green").
State("green").On("TIMER").Target("yellow").Done().
State("yellow").On("TIMER").Target("red").Done().
State("red").On("TIMER").Target("green").Done().
Build()
interp := statekit.NewInterpreter(machine)
interp.Start()
fmt.Println(interp.State().Value)
interp.Send(statekit.Event{Type: "TIMER"})
fmt.Println(interp.State().Value)
// Output:
// green
// yellow
}
func ExampleNewMachine_withGuard() {
type Context struct {
Approved bool
}
machine, _ := statekit.NewMachine[Context]("approval").
WithInitial("pending").
WithContext(Context{Approved: true}).
WithGuard("isApproved", func(ctx Context, e statekit.Event) bool {
return ctx.Approved
}).
State("pending").
On("SUBMIT").Target("approved").Guard("isApproved").
Done().
State("approved").Final().Done().
Build()
interp := statekit.NewInterpreter(machine)
interp.Start()
interp.Send(statekit.Event{Type: "SUBMIT"})
fmt.Println(interp.State().Value)
// Output: approved
}
func ExampleNewMachine_withAction() {
type Context struct {
Count int
}
machine, _ := statekit.NewMachine[Context]("counter").
WithInitial("idle").
WithAction("increment", func(ctx *Context, e statekit.Event) {
ctx.Count++
}).
State("idle").
OnEntry("increment").
On("COUNT").Target("idle").
Done().
Build()
interp := statekit.NewInterpreter(machine)
interp.Start()
fmt.Println(interp.State().Context.Count) // Entry action runs on Start
interp.Send(statekit.Event{Type: "COUNT"})
fmt.Println(interp.State().Context.Count) // Entry action runs again on self-transition
// Output:
// 1
// 2
}
func ExampleNewMachine_hierarchical() {
// Hierarchical state machine with nested states
machine, _ := statekit.NewMachine[struct{}]("editor").
WithInitial("editing").
State("editing").
WithInitial("idle").
On("SAVE").Target("saved").End(). // Parent handles SAVE for all children
State("idle").
On("TYPE").Target("dirty").
End().
End().
State("dirty").
On("CLEAR").Target("idle").
End().
End().
Done().
State("saved").Final().Done().
Build()
interp := statekit.NewInterpreter(machine)
interp.Start()
fmt.Println(interp.State().Value) // Starts in leaf state
fmt.Println(interp.Matches("editing")) // Matches parent
interp.Send(statekit.Event{Type: "TYPE"})
fmt.Println(interp.State().Value)
interp.Send(statekit.Event{Type: "SAVE"}) // Bubbles up to parent
fmt.Println(interp.State().Value)
// Output:
// idle
// true
// dirty
// saved
}
func ExampleInterpreter_Matches() {
machine, _ := statekit.NewMachine[struct{}]("nested").
WithInitial("parent").
State("parent").
WithInitial("child").
State("child").End().
Done().
Build()
interp := statekit.NewInterpreter(machine)
interp.Start()
// Current state is "child"
fmt.Println(interp.State().Value)
// Matches both the current state and its ancestors
fmt.Println(interp.Matches("child"))
fmt.Println(interp.Matches("parent"))
// Output:
// child
// true
// true
}