This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
182 lines (164 loc) · 3.63 KB
/
util_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package fsm
import (
"errors"
"testing"
cachestore "github.com/fsm/cache-store"
"github.com/fsm/fsm"
"github.com/fsm/test"
)
var (
buzzIntent = &fsm.Intent{
Slug: "buzz",
Utterances: []string{
"buzz",
},
}
catchAllIntent = &fsm.Intent{
Slug: "catch-all",
Slots: map[string]*fsm.Type{
"input": {
Slug: "literal",
IsValid: func(string) bool {
return true
},
},
},
Utterances: []string{
"{input}",
},
}
simpleMachine = []fsm.BuildState{
getStartState,
getStateA,
getStateB,
getStateC,
getStateD,
}
)
func getStartState(emitter fsm.Emitter, traverser fsm.Traverser) *fsm.State {
return &fsm.State{
Slug: fsm.StartState,
Entry: func(isReentry bool) error {
return nil
},
ValidIntents: func() []*fsm.Intent {
return []*fsm.Intent{
catchAllIntent,
}
},
Transition: func(*fsm.Intent, map[string]string) *fsm.State {
return getStateA(emitter, traverser)
},
}
}
func getStateA(emitter fsm.Emitter, traverser fsm.Traverser) *fsm.State {
return &fsm.State{
Slug: "A",
Entry: func(isReentry bool) error {
return nil
},
ValidIntents: func() []*fsm.Intent {
return []*fsm.Intent{
catchAllIntent,
}
},
Transition: func(intent *fsm.Intent, params map[string]string) *fsm.State {
if params["input"] == "hello" {
return getStateB(emitter, traverser)
}
return nil
},
}
}
func getStateB(emitter fsm.Emitter, traverser fsm.Traverser) *fsm.State {
return &fsm.State{
Slug: "B",
Entry: func(isReentry bool) error {
return nil
},
ValidIntents: func() []*fsm.Intent {
return []*fsm.Intent{
buzzIntent,
}
},
Transition: func(intent *fsm.Intent, params map[string]string) *fsm.State {
if intent == buzzIntent {
return getStateC(emitter, traverser)
}
return nil
},
}
}
func getStateC(emitter fsm.Emitter, traverser fsm.Traverser) *fsm.State {
return &fsm.State{
Slug: "C",
Entry: func(isReentry bool) error {
traverser.SetCurrentState(getStateD(emitter, traverser).Slug)
return nil
},
ValidIntents: func() []*fsm.Intent {
return nil
},
Transition: func(intent *fsm.Intent, params map[string]string) *fsm.State {
return nil
},
}
}
func getStateD(emitter fsm.Emitter, traverser fsm.Traverser) *fsm.State {
return &fsm.State{
Slug: "D",
Entry: func(isReentry bool) error {
return errors.New("END")
},
ValidIntents: func() []*fsm.Intent {
return nil
},
Transition: func(intent *fsm.Intent, params map[string]string) *fsm.State {
return nil
},
}
}
func TestStateMap(t *testing.T) {
stateMap := fsm.GetStateMap(simpleMachine)
// Test state map was generated properly
if stateMap["A"](nil, nil).Slug != "A" {
t.Fail()
}
if stateMap["B"](nil, nil).Slug != "B" {
t.Fail()
}
if len(stateMap) != 5 {
t.Fail()
}
}
func TestStep(t *testing.T) {
traverser := test.New(simpleMachine, cachestore.New())
// Testing that saying anything shifts us to 'A' state
traverser.Send("Hello")
if traverser.CurrentState() != "A" {
t.Fail()
}
// Testing that saying 'world' does not shift us to any state and
// we remain in state A.
traverser.Send("world")
if traverser.CurrentState() != "A" {
t.Fail()
}
// Testing that "hello" moves us to 'B' state
traverser.Send("hello")
if traverser.CurrentState() != "B" {
t.Fail()
}
// Testing that saying 'hello' does not shift us out of B state
// (only buzz can move us out)
traverser.Send("hello")
if traverser.CurrentState() != "B" {
t.Fail()
}
// Testing that 'buzz' shifts us out of B state to C state,
// but then immediately redirects to the D state.
traverser.Send("buzz")
if traverser.CurrentState() != "D" {
t.Fail()
}
}