-
Notifications
You must be signed in to change notification settings - Fork 8
/
tree_handler_programmably_walk_test.go
80 lines (75 loc) · 1.64 KB
/
tree_handler_programmably_walk_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
package gtree_test
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/ddddddO/gtree"
tu "github.com/ddddddO/gtree/testutil"
)
// TODO: 何パターンかのcallbackを用意してWalkerNode用メソッドのテストもしたい
func TestWalkProgrammably(t *testing.T) {
tests := []struct {
name string
root *gtree.Node
options []gtree.Option
out out
}{
{
name: "case(succeeded)",
root: tu.PrepareMultiNode(),
options: []gtree.Option{
gtree.WithBranchFormatIntermedialNode("+--", ": "),
gtree.WithBranchFormatLastNode("+--", " "),
},
out: out{
output: strings.TrimLeft(`
root1
+-- child 1
: +-- child 2
: +-- child 3
: +-- child 4
: +-- child 5
: +-- child 6
: +-- child 7
+-- child 8
`, "\n"),
err: nil,
},
},
{
name: "case(succeeded/massive)",
root: tu.Prepare(),
options: []gtree.Option{gtree.WithMassive(nil)},
out: out{
output: strings.TrimLeft(`
root
└── child 1
└── child 2
`, "\n"),
err: nil,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
callback := func(wn *gtree.WalkerNode) error {
fmt.Fprintln(buf, wn.Row())
return nil
}
gotErr := gtree.WalkProgrammably(tt.root, callback, tt.options...)
if gotErr != nil || tt.out.err != nil {
if gotErr.Error() != tt.out.err.Error() {
t.Errorf("\ngotErr: \n%s\nwantErr: \n%s", gotErr, tt.out.err.Error())
}
}
got := buf.String()
if got != tt.out.output {
t.Errorf("\ngot: \n%s\nwant: \n%s", got, tt.out.output)
}
})
}
}