-
Notifications
You must be signed in to change notification settings - Fork 9
/
printer_test.go
247 lines (230 loc) · 7.49 KB
/
printer_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
/*
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 (
"bytes"
"errors"
"fmt"
"github.com/go-test/deep"
"io"
"regexp"
"runtime"
"strings"
"testing"
)
func replacePointers(b string) string {
var (
m = make(map[string]struct{})
r []string
n int
)
for _, v := range regexp.MustCompile(`(?:[[:^alnum:]]|^)(0x[[:alnum:]]{1,16})(?:[[:^alnum:]]|$)`).FindAllStringSubmatch(b, -1) {
if v := v[1]; v != `0x0` {
if _, ok := m[v]; !ok {
n++
m[v] = struct{}{}
r = append(r, v, fmt.Sprintf(`%#x`, n))
}
}
}
return strings.NewReplacer(r...).Replace(b)
}
func TestNode_String(t *testing.T) {
for _, testCase := range []struct {
Name string
Node Node
Value string
}{
{
Name: `nil node`,
Node: nil,
Value: `<nil>`,
},
{
Name: `single sequence`,
Node: New(Sequence),
Value: "[0x1 printer_test.go:62 0x2 sequence.go:21] github.com/joeycumines/go-behaviortree.TestNode_String | github.com/joeycumines/go-behaviortree.Sequence",
},
{
Name: `single closure`,
Node: New(func(children []Node) (Status, error) { panic(`TestNode_String`) }),
Value: "[0x1 printer_test.go:67 0x2 printer_test.go:67] github.com/joeycumines/go-behaviortree.TestNode_String | github.com/joeycumines/go-behaviortree.TestNode_String.func1",
},
{
Name: `nil tick`,
Node: New(nil),
Value: "[0x1 printer_test.go:72 0x0 -] github.com/joeycumines/go-behaviortree.TestNode_String | <nil>",
},
{
Name: `example counter`,
Node: newExampleCounter(),
Value: "[0x1 example_test.go:47 0x2 selector.go:21] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.Selector\n├── [0x3 example_test.go:49 0x4 sequence.go:21] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.Sequence\n│ ├── [0x5 example_test.go:51 0x6 example_test.go:52] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.newExampleCounter.func3\n│ ├── [0x7 example_test.go:40 0x8 example_test.go:41] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.newExampleCounter.func2\n│ └── [0x9 example_test.go:32 0xa example_test.go:33] github.com/joeycumines/go-behaviortree.newExampleCounter.func1 | github.com/joeycumines/go-behaviortree.newExampleCounter.func1.1\n└── [0xb example_test.go:62 0x4 sequence.go:21] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.Sequence\n ├── [0xc example_test.go:64 0xd example_test.go:65] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.newExampleCounter.func4\n ├── [0x7 example_test.go:40 0x8 example_test.go:41] github.com/joeycumines/go-behaviortree.newExampleCounter | github.com/joeycumines/go-behaviortree.newExampleCounter.func2\n └── [0x9 example_test.go:32 0xa example_test.go:33] github.com/joeycumines/go-behaviortree.newExampleCounter.func1 | github.com/joeycumines/go-behaviortree.newExampleCounter.func1.1",
},
} {
t.Run(testCase.Name, func(t *testing.T) {
value := testCase.Node.String()
//t.Logf("\n---\n%s\n---", value)
value = replacePointers(value)
if value != testCase.Value {
t.Errorf("unexpected value: %q\n> %s", value, strings.ReplaceAll(value, "\n", "\n> "))
}
})
}
}
type mockPrinter struct {
fprint func(output io.Writer, node Node) error
}
func (m *mockPrinter) Fprint(output io.Writer, node Node) error { return m.fprint(output, node) }
func TestNode_String_error(t *testing.T) {
defer func() func() {
old := DefaultPrinter
DefaultPrinter = &mockPrinter{fprint: func(output io.Writer, node Node) error {
return errors.New(`some_error`)
}}
return func() {
DefaultPrinter = old
}
}()()
if v := Node(nil).String(); v != `behaviortree.DefaultPrinter error: some_error` {
t.Error(v)
}
}
func TestTreePrinter_Fprint_copyError(t *testing.T) {
r, w := io.Pipe()
_ = r.Close()
if err := (TreePrinter{Formatter: DefaultPrinterFormatter, Inspector: DefaultPrinterInspector}).Fprint(w, Node(nil)); err != io.ErrClosedPipe {
t.Error(err)
}
}
func Test_treePrinterNodeXlabMeta_String_panicLen(t *testing.T) {
defer func() {
if r := fmt.Sprint(recover()); r != `m.sizes [4] mismatched m.strings [one two]` {
t.Error(r)
}
}()
m := &treePrinterNodeXlabMeta{
treePrinterNodeXlab: &treePrinterNodeXlab{
sizes: []int{4},
},
strings: []string{`one`, `two`},
}
_ = m.String()
t.Error(`expected panic`)
}
func Test_treePrinterNodeXlabMeta_String_panicInterfaces(t *testing.T) {
defer func() {
if r := fmt.Sprint(recover()); r != `m.interfaces [] should be nil` {
t.Error(r)
}
}()
m := &treePrinterNodeXlabMeta{
treePrinterNodeXlab: &treePrinterNodeXlab{
sizes: []int{4},
},
strings: []string{`one`, `two`},
interfaces: make([]interface{}, 0),
}
_ = m.String()
t.Error(`expected panic`)
}
func dummyNode() (Tick, []Node) {
return nil, nil
}
func TestDefaultPrinterInspector_nil(t *testing.T) {
var actual [2]interface{}
actual[0], actual[1] = DefaultPrinterInspector(nil, nil)
if diff := deep.Equal(
actual,
[2]interface{}{
[]interface{}{
`0x0`,
`-`,
`0x0`,
`-`,
},
`<nil> | <nil>`,
},
); diff != nil {
t.Errorf("unexpected diff:\n%s", strings.Join(diff, "\n"))
}
var node Node = dummyNode
actual[0], actual[1] = DefaultPrinterInspector(node, nil)
if diff := deep.Equal(
actual,
[2]interface{}{
[]interface{}{
fmt.Sprintf(`%p`, node),
`printer_test.go:155`,
`0x0`,
`-`,
},
`github.com/joeycumines/go-behaviortree.dummyNode | <nil>`,
},
); diff != nil {
t.Errorf("unexpected diff:\n%s", strings.Join(diff, "\n"))
}
tick := Selector
actual[0], actual[1] = DefaultPrinterInspector(nil, tick)
if diff := deep.Equal(
actual,
[2]interface{}{
[]interface{}{
`0x0`,
`-`,
fmt.Sprintf(`%p`, tick),
`selector.go:21`,
},
`<nil> | github.com/joeycumines/go-behaviortree.Selector`,
},
); diff != nil {
t.Errorf("unexpected diff:\n%s", strings.Join(diff, "\n"))
}
}
func TestDefaultPrinterInspector_noName(t *testing.T) {
defer func() func() {
old := runtimeFuncForPC
runtimeFuncForPC = func(pc uintptr) *runtime.Func { return nil }
return func() {
runtimeFuncForPC = old
}
}()()
var actual [2]interface{}
actual[0], actual[1] = DefaultPrinterInspector(dummyNode, Sequence)
if diff := deep.Equal(
actual,
[2]interface{}{
[]interface{}{
`0x0`,
`-`,
`0x0`,
`-`,
},
`- | -`,
},
); diff != nil {
t.Errorf("unexpected diff:\n%s", strings.Join(diff, "\n"))
}
}
func TestTreePrinter_Fprint_emptyMeta(t *testing.T) {
p := TreePrinter{
Inspector: func(node Node, tick Tick) (meta []interface{}, value interface{}) {
return []interface{}{``, ``, ``}, ``
},
Formatter: DefaultPrinterFormatter,
}
b := new(bytes.Buffer)
_ = p.Fprint(b, nn(nil))
if v := b.String(); v != `[ ] ` {
t.Error(v)
}
}