MCPcopy Create free account
hub / github.com/cogentcore/core / TestNodeWalk

Function TestNodeWalk

tree/node_test.go:209–276  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

207}
208
209func TestNodeWalk(t *testing.T) {
210 parent := NewNodeBase()
211 testdata.NewNodeEmbed(parent).SetName("child0")
212 child1 := NewNodeBase(parent).SetName("child1")
213 testdata.NewNodeEmbed(parent).SetName("child2")
214 schild1 := testdata.NewNodeEmbed(child1).SetName("subchild1")
215 NewNodeBase(parent).SetName("child3")
216
217 res := []string{}
218
219 schild1.WalkUp(func(n Node) bool {
220 res = append(res, n.AsTree().Name)
221 return Continue
222 })
223
224 trg := []string{"subchild1", "child1", "node-base"}
225 assert.Equal(t, trg, res)
226 res = res[:0]
227
228 parent.WalkDownPost(func(n Node) bool {
229 return Continue
230 },
231 func(n Node) bool {
232 res = append(res, fmt.Sprintf("[%s]", n.AsTree().Name))
233 return Continue
234 })
235 trg = []string{"[child0]", "[subchild1]", "[child1]", "[child2]", "[child3]", "[node-base]"}
236 assert.Equal(t, trg, res)
237 res = res[:0]
238
239 // test for Break working
240 parent.WalkDownPost(func(n Node) bool {
241 if n.AsTree().Name == "child1" {
242 return Break
243 }
244 return Continue
245 },
246 func(n Node) bool {
247 if n.AsTree().Name == "child1" {
248 return Break
249 }
250 res = append(res, fmt.Sprintf("[%s]", n.AsTree().Name))
251 return Continue
252 })
253 trg = []string{"[child0]", "[child2]", "[child3]", "[node-base]"}
254 assert.Equal(t, trg, res)
255 res = res[:0]
256
257 parent.WalkDownBreadth(func(n Node) bool {
258 res = append(res, fmt.Sprintf("[%v]", n.AsTree().Name))
259 return Continue
260 })
261 trg = []string{"[node-base]", "[child0]", "[child1]", "[child2]", "[child3]", "[subchild1]"}
262 assert.Equal(t, trg, res)
263 res = res[:0]
264
265 // test for return false
266 parent.WalkDownBreadth(func(n Node) bool {

Callers

nothing calls this directly

Calls 8

WalkDownPostMethod · 0.95
WalkDownBreadthMethod · 0.95
NewNodeEmbedFunction · 0.92
WalkUpMethod · 0.80
EqualMethod · 0.80
NewNodeBaseFunction · 0.70
SetNameMethod · 0.65
AsTreeMethod · 0.65

Tested by

no test coverage detected