-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwalk_test.go
More file actions
164 lines (152 loc) · 3.42 KB
/
walk_test.go
File metadata and controls
164 lines (152 loc) · 3.42 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package iter
import (
"bytes"
"container/ring"
"fmt"
"strings"
"testing"
)
type TestTree struct {
At int
Children []*TestTree
}
func (t *TestTree) Walk(parent Pair, f func(el Pair) error) error {
return f(NewPair(parent, "foo", "bar", nil))
}
func newTestTree() *TestTree {
return new(TestTree).fill(0, 4, 4)
}
func (t *TestTree) Dump() string {
var buf bytes.Buffer
space := strings.Repeat(" ", t.At)
buf.WriteString(fmt.Sprintf("%sTree(%d)\n", space, t.At))
for _, c := range t.Children {
buf.WriteString(fmt.Sprintf("%s%v", space, c.Dump()))
}
return buf.String()
}
func (t *TestTree) String() string {
return fmt.Sprintf("Tree(%d)\n", t.At)
}
func (t *TestTree) fill(d, c, max int) *TestTree {
t.At = d
d++
if d >= max {
return t
}
t.Children = make([]*TestTree, c)
for i := 0; i < c; i++ {
t.Children[i] = new(TestTree).fill(d, c, max)
}
return t
}
func TestDfsWalker(t *testing.T) {
type testDfsWalker struct {
Head string
Child *testDfsWalker
MapEnter string
Map map[string]int
SliceEnter string
Slice []string
ChanEnter string
Chan chan string
Tail string
}
tid := func(ident string, depth int) string {
return fmt.Sprintf("%s|%d", ident, depth)
}
trnew := func(pnt *testDfsWalker, i int) *testDfsWalker {
tr := &testDfsWalker{
Head: tid("Head", i),
Child: pnt,
MapEnter: tid("MapEnter|1", i),
Map: map[string]int{
tid("Map", i): i * 2,
tid("Map", i+1): i * 2,
tid("Map", i+2): i * 2},
SliceEnter: tid("SliceEnter", i),
Slice: []string{tid("Slice", i*10), tid("Slice", i*20), tid("Slice", i*30)},
ChanEnter: tid("ChanEnter", i),
Chan: make(chan string, 3),
Tail: tid("Tail", i),
}
tr.Chan <- tid("Chan", i*10)
tr.Chan <- tid("Chan", i*20)
tr.Chan <- tid("Chan", i*30)
return tr
}
tr := trnew(nil, 4)
for i := 3; i > 0; i-- {
tr = trnew(tr, i)
}
w := NewWalker(&Iter{ChanRecv: true})
t.Run("Walk", func(t *testing.T) {
fns := []func(value interface{}, f func(el Pair) error) error{
Walk, w.Walk,
}
for _, fn := range fns {
err := fn(tr, func(el Pair) error {
return nil
})
if err != nil {
t.Fatalf("expected nil err, got: %v", err)
}
}
})
t.Run("NoCircular", func(t *testing.T) {
fns := []func(value interface{}, f func(el Pair) error) error{
Walk, w.Walk,
}
for _, fn := range fns {
i := 0
err := fn(ring.New(10), func(el Pair) error {
i++
return nil
})
if err != nil {
t.Fatalf("expected nil err, got: %v", err)
}
if i != 1 {
t.Fatalf("expected exactly 1 visit, got: %v", i)
}
}
})
t.Run("NilChan", func(t *testing.T) {
type privateString string
w := NewWalker(invalidIter{})
i := 0
ch := make(chan string, 1)
ch <- "foo"
err := w.Walk(ch, func(el Pair) error {
i++
return nil
})
if err != nil {
t.Fatalf("expected nil err, got: %v", err)
}
err = w.Walk(map[int]string{1: "123"}, func(el Pair) error {
i++
return nil
})
if err != nil {
t.Fatalf("expected nil err, got: %v", err)
}
err = w.Walk([]string{"123"}, func(el Pair) error {
i++
return nil
})
if err != nil {
t.Fatalf("expected nil err, got: %v", err)
}
err = w.Walk(struct{ name string }{"123"}, func(el Pair) error {
i++
return nil
})
if err != nil {
t.Fatalf("expected nil err, got: %v", err)
}
if i != 0 {
t.Fatalf("expected exactly 0 visits, got: %v", i)
}
})
}