-
Notifications
You must be signed in to change notification settings - Fork 9
/
behaviortree_test.go
263 lines (230 loc) · 5.16 KB
/
behaviortree_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
Copyright 2021 Joseph Cumines
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package behaviortree
import (
"fmt"
"github.com/go-test/deep"
"strings"
"testing"
)
func TestNode_Tick_nil(t *testing.T) {
var (
status Status
err error
)
var tree Node
//noinspection GoNilness
status, err = tree.Tick()
if status != Failure {
t.Error("expected status to be failure but it was", status)
}
if err == nil {
t.Error("expected non-nil error but it was", err)
}
}
func TestNode_Tick_nilTick(t *testing.T) {
var (
status Status
err error
)
var tree Node = func() (Tick, []Node) {
return nil, nil
}
status, err = tree.Tick()
if status != Failure {
t.Error("expected status to be failure but it was", status)
}
if err == nil {
t.Error("expected non-nil error but it was", err)
}
}
func TestNewNode(t *testing.T) {
out := make(chan int)
var (
status Status
err error
)
tree := NewNode(
Sequence,
[]Node{
NewNode(
func(children []Node) (Status, error) {
out <- 1
return Success, nil
},
nil,
),
NewNode(
func(children []Node) (Status, error) {
out <- 2
return Success, nil
},
nil,
),
NewNode(
func(children []Node) (Status, error) {
out <- 3
return Success, nil
},
nil,
),
},
)
go func() {
status, err = tree.Tick()
out <- 4
}()
expected := []int{1, 2, 3, 4}
actual := []int{
<-out,
<-out,
<-out,
<-out,
}
if diff := deep.Equal(expected, actual); diff != nil {
t.Fatalf("expected tick order != actual: %s", strings.Join(diff, "\n >"))
}
if status != Success {
t.Error("expected status to be success but it was", status)
}
if err != nil {
t.Error("expected nil error but it was", err)
}
}
func TestStatus_String(t *testing.T) {
testCases := []struct {
Status Status
String string
}{
{
Status: Success,
String: `success`,
},
{
Status: Failure,
String: `failure`,
},
{
Status: Running,
String: `running`,
},
{
Status: 0,
String: `unknown status (0)`,
},
{
Status: 234,
String: `unknown status (234)`,
},
}
for i, testCase := range testCases {
name := fmt.Sprintf("TestStatus_String_#%d", i)
if actual := testCase.Status.String(); actual != testCase.String {
t.Errorf("%s failed: expected stringer '%s' != actual '%s'", name, testCase.String, actual)
}
}
}
func TestStatus_Status(t *testing.T) {
testCases := []struct {
Status Status
Expected Status
}{
{
Status: Success,
Expected: Success,
},
{
Status: Failure,
Expected: Failure,
},
{
Status: Running,
Expected: Running,
},
{
Status: 0,
Expected: Failure,
},
{
Status: 234,
Expected: Failure,
},
}
for i, testCase := range testCases {
name := fmt.Sprintf("TestStatus_Status_#%d", i)
if actual := testCase.Status.Status(); actual != testCase.Expected {
t.Errorf("%s failed: expected behaviortree.Status.Status '%s' != actual '%s'", name, testCase.Status, actual)
}
}
}
func genTestNewFrame() *Frame { return New(Sequence).Frame() }
func TestNew_frame(t *testing.T) {
var (
expected = newFrame(genTestNewFrame)
actual = genTestNewFrame()
)
if expected == nil || actual == nil || expected.PC == 0 || actual.PC == 0 || expected.PC != expected.Entry || actual.PC == actual.Entry {
t.Fatal(expected, actual)
}
actual.PC = actual.Entry
if *expected != *actual {
t.Errorf("expected != actual\nEXPECTED: %#v\nACTUAL: %#v", expected, actual)
}
}
func genTestNewNodeFrame() *Frame { return NewNode(Sequence, nil).Frame() }
func TestNewNode_frame(t *testing.T) {
var (
expected = newFrame(genTestNewNodeFrame)
actual = genTestNewNodeFrame()
)
if expected == nil || actual == nil || expected.PC == 0 || actual.PC == 0 || expected.PC != expected.Entry || actual.PC == actual.Entry {
t.Fatal(expected, actual)
}
actual.PC = actual.Entry
if *expected != *actual {
t.Errorf("expected != actual\nEXPECTED: %#v\nACTUAL: %#v", expected, actual)
}
}
func Test_factory_nilFrame(t *testing.T) {
if v := New(nil).Value(vkFrame{}); v == nil {
t.Error(v)
}
if v := New(nil).Value(1); v != nil {
t.Error(v)
}
if v := NewNode(nil, nil).Value(vkFrame{}); v == nil {
t.Error(v)
}
if v := NewNode(nil, nil).Value(1); v != nil {
t.Error(v)
}
defer func() func() {
old := runtimeCallers
runtimeCallers = func(skip int, pc []uintptr) int { return 0 }
return func() {
runtimeCallers = old
}
}()()
if v := New(nil).Value(vkFrame{}); v != nil {
t.Error(v)
}
if v := New(nil).Value(1); v != nil {
t.Error(v)
}
if v := NewNode(nil, nil).Value(vkFrame{}); v != nil {
t.Error(v)
}
if v := NewNode(nil, nil).Value(1); v != nil {
t.Error(v)
}
}