-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnest_test.go
236 lines (221 loc) · 5.17 KB
/
nest_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
package nest
import (
"bytes"
"io/ioutil"
"sync"
"testing"
)
func TestNew(t *testing.T) {
type want struct {
depth uint8
children int
}
n := New()
n2_0 := WithParent(n)
n2_1 := WithParent(n)
n3 := WithParent(n2_0)
tests := map[string]struct {
nest *Writer
want want
}{
"Writer with no children and two level of depth": {
nest: n3,
want: want{
depth: 2,
children: 0,
},
},
"Writer with one children and one level of depth": {
nest: n2_0,
want: want{
depth: 1,
children: 1,
},
},
"Writer with no children and one level of depth": {
nest: n2_1,
want: want{
depth: 1,
children: 0,
},
},
"Writer with two children and no level of depth": {
nest: n,
want: want{
depth: 0,
children: 2,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.nest.Buf == nil {
t.Error("could not find buffer in the Writer")
}
if test.nest.Depth != test.want.depth {
t.Error("could not match depth in the Writer")
t.Errorf("got: %d", test.nest.Depth)
t.Errorf("want: %d", test.want.depth)
}
if len(test.nest.Children) != test.want.children {
t.Error("could not match children in the Writer")
t.Errorf("got: %d", len(test.nest.Children))
t.Errorf("want: %d", test.want.children)
}
})
}
}
func TestWriter_Write(t *testing.T) {
tests := map[string]struct {
nest *Writer
p []byte
want []byte
}{
"one line string with a zero depth Writer": {
nest: New(),
p: []byte("a long string!"),
want: []byte("a long string!\n"),
},
"one line string with a one depth Writer": {
nest: WithParent(New()),
p: []byte("a long string!"),
want: []byte(" a long string!\n"),
},
"string with new lines with a zero depth Writer": {
nest: New(),
p: []byte("a long string!\nwith a new line!"),
want: []byte("a long string!\nwith a new line!\n"),
},
"string with new lines with a two depth Writer": {
nest: WithParent(WithParent(New())),
p: []byte("a long string!\nwith a new line!"),
want: []byte(" a long string!\n with a new line!\n"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
i, err := test.nest.Write(test.p)
if i != len(test.want) {
t.Error("could not match bytes written")
t.Errorf("got: %d", i)
t.Errorf("want: %d", len(test.p))
}
if err != nil {
t.Errorf("could not write string to Writer: %s", err)
}
if !bytes.Equal(test.want, test.nest.Buf.Bytes()) {
t.Error("could not match string written")
t.Errorf("got: %s", test.nest.Buf.String())
t.Errorf("want: %s", test.want)
}
})
}
}
func TestWriter_WriteString(t *testing.T) {
tests := map[string]struct {
nest *Writer
s string
want string
}{
"one line string with a zero depth Writer": {
nest: New(),
s: "a long string!",
want: "a long string!\n",
},
"one line string with a one depth Writer": {
nest: WithParent(New()),
s: "a long string!",
want: " a long string!\n",
},
"string with new lines with a zero depth Writer": {
nest: New(),
s: "a long string!\nwith a new line!",
want: "a long string!\nwith a new line!\n",
},
"string with new lines with a two depth Writer": {
nest: WithParent(WithParent(New())),
s: "a long string!\nwith a new line!",
want: " a long string!\n with a new line!\n",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
i, err := test.nest.WriteString(test.s)
if i != len(test.want) {
t.Error("could not match bytes written")
t.Errorf("got: %d", i)
t.Errorf("want: %d", len(test.s))
}
if err != nil {
t.Errorf("could not write string to Writer: %s", err)
}
if test.want != test.nest.Buf.String() {
t.Error("could not match string written")
t.Errorf("got: %s", test.nest.Buf.String())
t.Errorf("want: %s", test.want)
}
})
}
}
func TestWriter_WriteTo(t *testing.T) {
tests := map[string]struct {
i int64
nest *Writer
}{
"single simple": {
41,
&Writer{
Buf: bytes.NewBuffer([]byte("this is the content\n that I want to print")),
},
},
"simple with two depth": {
26,
&Writer{
Buf: bytes.NewBuffer([]byte("one\n")),
Children: []*Writer{
{
Depth: 1,
Buf: bytes.NewBuffer([]byte(" two\n")),
Children: []*Writer{
{
Depth: 2,
Buf: bytes.NewBuffer([]byte(" three\n")),
},
},
},
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
w := &bytes.Buffer{}
i, err := test.nest.WriteTo(w)
if err != nil {
t.Errorf("could not write simple content to the writer: %s", err)
}
if i != test.i {
t.Error("could not match written bytes")
t.Errorf("got: %d", i)
t.Errorf("want: %d", test.i)
}
})
}
}
func TestWriterRaceConditions(t *testing.T) {
if !raceEnabled {
t.Skip("race detector is not enabled")
}
const pool = 10_000
base := New()
wg := sync.WaitGroup{}
wg.Add(pool)
for i := 1; i <= pool; i++ {
go func(i int) {
_,_ = base.WriteString("hello")
_,_ = base.WriteTo(ioutil.Discard)
wg.Done()
}(i)
}
wg.Wait()
}