-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtree_handler_programmably_output_test.go
201 lines (184 loc) · 4.36 KB
/
tree_handler_programmably_output_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
package gtree_test
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/ddddddO/gtree"
tu "github.com/ddddddO/gtree/testutil"
)
func Example() {
var root *gtree.Node = gtree.NewRoot("root")
root.Add("child 1").Add("child 2")
root.Add("child 1").Add("child 3")
child4 := root.Add("child 4")
var child7 *gtree.Node = child4.Add("child 5").Add("child 6").Add("child 7")
child7.Add("child 8")
buf := &bytes.Buffer{}
if err := gtree.OutputProgrammably(buf, root); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(buf.String())
// Output:
// root
// ├── child 1
// │ ├── child 2
// │ └── child 3
// └── child 4
// └── child 5
// └── child 6
// └── child 7
// └── child 8
}
func TestOutputProgrammably(t *testing.T) {
tests := []struct {
name string
root *gtree.Node
options []gtree.Option
want string
wantErr error
}{
{
name: "case(succeeded)",
root: tu.Prepare(),
want: strings.TrimPrefix(`
root
└── child 1
└── child 2
`, "\n"),
wantErr: nil,
},
{
name: "case(succeeded/massive)",
root: tu.Prepare(),
options: []gtree.Option{gtree.WithMassive(context.Background())},
want: strings.TrimPrefix(`
root
└── child 1
└── child 2
`, "\n"),
wantErr: nil,
},
{
name: "case(succeeded / added same name)",
root: tu.PrepareSameNameChild(),
want: strings.TrimPrefix(`
root
└── child 1
├── child 2
└── child 3
`, "\n"),
wantErr: nil,
},
{
name: "case(not root)",
root: tu.PrepareNotRoot(),
want: "",
wantErr: gtree.ErrNotRoot,
},
{
name: "case(nil node)",
root: tu.PrepareNilNode(),
want: "",
wantErr: gtree.ErrNilNode,
},
{
name: "case(succeeded / branch format)",
root: tu.PrepareMultiNode(),
options: []gtree.Option{
gtree.WithBranchFormatIntermedialNode("+--", ": "),
gtree.WithBranchFormatLastNode("+--", " "),
},
want: strings.TrimPrefix(`
root1
+-- child 1
: +-- child 2
: +-- child 3
: +-- child 4
: +-- child 5
: +-- child 6
: +-- child 7
+-- child 8
`, "\n"),
},
{
name: "case(succeeded / output json)",
root: tu.PrepareMultiNode(),
options: []gtree.Option{gtree.WithEncodeJSON()},
want: strings.TrimPrefix(`
{"value":"root1","children":[{"value":"child 1","children":[{"value":"child 2","children":[{"value":"child 3","children":null},{"value":"child 4","children":[{"value":"child 5","children":null},{"value":"child 6","children":[{"value":"child 7","children":null}]}]}]}]},{"value":"child 8","children":null}]}
`, "\n"),
},
{
name: "case(succeeded / output yaml)",
root: tu.PrepareMultiNode(),
options: []gtree.Option{gtree.WithEncodeYAML()},
want: strings.TrimPrefix(`
value: root1
children:
- value: child 1
children:
- value: child 2
children:
- value: child 3
children: []
- value: child 4
children:
- value: child 5
children: []
- value: child 6
children:
- value: child 7
children: []
- value: child 8
children: []
`, "\n"),
},
{
name: "case(succeeded / output toml)",
root: tu.PrepareMultiNode(),
options: []gtree.Option{gtree.WithEncodeTOML()},
want: strings.TrimPrefix(`
value = 'root1'
[[children]]
value = 'child 1'
[[children.children]]
value = 'child 2'
[[children.children.children]]
value = 'child 3'
children = []
[[children.children.children]]
value = 'child 4'
[[children.children.children.children]]
value = 'child 5'
children = []
[[children.children.children.children]]
value = 'child 6'
[[children.children.children.children.children]]
value = 'child 7'
children = []
[[children]]
value = 'child 8'
children = []
`, "\n"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
gotErr := gtree.OutputProgrammably(buf, tt.root, tt.options...)
got := buf.String()
if got != tt.want {
t.Errorf("\ngot: \n%s\nwant: \n%s", got, tt.want)
}
if gotErr != tt.wantErr {
t.Errorf("\ngotErr: \n%v\nwantErr: \n%v", gotErr, tt.wantErr)
}
})
}
}