-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars_test.go
629 lines (546 loc) · 15.6 KB
/
vars_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package kama
import (
"context"
"errors"
"fmt"
"io"
"math"
"net/http"
"strings"
"testing"
"time"
)
type Person struct {
Name string
Age int
Height float32
//lint:ignore U1001,U1000 used for tests
somePrivateField string
}
//lint:ignore U1001,U1000 used for tests
func (p Person) somePrivateMethodOne() {}
//lint:ignore U1001,U1000 used for tests
func (p *Person) somePrivateMethodTwo() {}
//lint:ignore U1001 used for tests
func (p Person) ValueMethodOne() {}
//lint:ignore U1001 used for tests
func (p *Person) PtrMethodOne() {}
//lint:ignore U1001 used for tests
func (p Person) ValueMethodTwo() {}
//lint:ignore U1001 used for tests
func (p *Person) PtrMethodTwo() float32 { return p.Height }
func ThisFunction(arg1 string, arg2 int) (string, error) {
return "", nil
}
var thisFunctionVar = ThisFunction //nolint:gochecknoglobals
type customerID uint16
//lint:ignore U1001 used for tests
func (c customerID) ID() uint16 { return uint16(c) }
func bigSlice() []int {
x := []int{}
for i := 0; i < 10_000; i++ {
x = append(x, i)
}
return x
}
var MyBigSlice = bigSlice() //nolint:gochecknoglobals
func bigMap() map[int]string {
y := map[int]string{}
for i := 0; i < 10_000; i++ {
y[i] = fmt.Sprintf("%d", i)
}
return y
}
func bigChan() chan int {
z := make(chan int, 10_000)
for i := 0; i < 10_000; i++ {
z <- i
}
return z
}
func bigArray() [10_000]int {
a := [10_000]int{}
for i := 0; i < 10_000; i++ {
a[i] = i
}
return a
}
const BigString = `AT last the sleepy atmosphere was stirred—and vigorously: the murder trial came on in the court. It became the absorbing topic of village talk immediately. Tom could not get away from it. Every reference to the murder sent a shudder to his heart, for his troubled conscience and fears almost persuaded him that these remarks were put forth in his hearing as “feelers”; he did not see how he could be suspected of knowing anything about the murder, but still he could not be comfortable in the midst of this gossip. It kept him in a cold shiver all the time. He took Huck to a lonely place to have a talk with him. It would be some relief to unseal his tongue for a little while; to divide his burden of distress with another sufferer. Moreover, he wanted to assure himself that Huck had remained discreet.
“Huck, have you ever told anybody about—that?”
“'Bout what?”
“You know what.”
“Oh—'course I haven't.”
“Never a word?”
“Never a solitary word, so help me. What makes you ask?”
“Well, I was afeard.”
“Why, Tom Sawyer, we wouldn't be alive two days if that got found out. You know that.”
Tom felt more comfortable. After a pause:
“Huck, they couldn't anybody get you to tell, could they?”
“Get me to tell? Why, if I wanted that halfbreed devil to drownd me they could get me to tell. They ain't no different way.”
“Well, that's all right, then. I reckon we're safe as long as we keep mum. But let's swear again, anyway. It's more surer.”
“I'm agreed.”
So they swore again with dread solemnities.
“What is the talk around, Huck? I've heard a power of it.”
“Talk? Well, it's just Muff Potter, Muff Potter, Muff Potter all the time. It keeps me in a sweat, constant, so's I want to hide som'ers.”
“That's just the same way they go on round me. I reckon he's a goner. Don't you feel sorry for him, sometimes?”
“Most always—most always. He ain't no account; but then he hain't ever done anything to hurt anybody. Just fishes a little, to get money to get drunk on—and loafs around considerable; but lord, we all do that—leastways most of us—preachers and such like. But he's kind of good—he give me half a fish, once, when there warn't enough for two; and lots of times he's kind of stood by me when I was out of luck.”
“Well, he's mended kites for me, Huck, and knitted hooks on to my line. I wish we could get him out of there.”
“My! we couldn't get him out, Tom. And besides, 'twouldn't do any good; they'd ketch him again.”
“Yes—so they would. But I hate to hear 'em abuse him so like the dickens when he never done—that.”
“I do too, Tom. Lord, I hear 'em say he's the bloodiest looking villain in this country, and they wonder he wasn't ever hung before.”
“Yes, they talk like that, all the time. I've heard 'em say that if he was to get free they'd lynch him.”
“And they'd do it, too.”
The boys had a long talk, but it brought them little comfort. As the twilight drew on, they found themselves hanging about the neighborhood of the little isolated jail, perhaps with an undefined hope that something would happen that might clear away their difficulties. But nothing happened; there seemed to be no angels or fairies interested in this luckless captive.
The boys did as they had often done before—went to the cell grating and gave Potter some tobacco and matches. He was on the ground floor and there were no guards.`
type SomeStructWIthSlice struct {
Name string
MyAwesome []int
}
var zeroValuePointer *http.Request //nolint:gochecknoglobals
type StructWithTags struct {
Allowed bool `json:"enabled"`
Name string `json:"their_name"`
}
type Hey struct {
Another struct {
Allowed bool `json:"enabled"`
Name string `json:"their_name"`
}
}
func TestBasicVariables(t *testing.T) {
t.Parallel()
tt := []struct {
tName string
variable interface{}
}{
{
tName: "value struct",
variable: Person{Name: "John"},
},
{
tName: "pointer struct",
variable: &Person{Name: "Jane"},
},
{
tName: "zero value pointer",
variable: zeroValuePointer,
},
{
tName: "function",
variable: ThisFunction,
},
{
tName: "function variable",
variable: thisFunctionVar,
},
{
tName: "distinct type",
variable: customerID(9),
},
{
tName: "big slice",
variable: MyBigSlice,
},
{
tName: "big map",
variable: bigMap(),
},
{
tName: "big chan",
variable: bigChan(),
},
{
tName: "big array",
variable: bigArray(),
},
{
tName: "big string",
variable: BigString,
},
{
tName: "value struct with slice field",
variable: SomeStructWIthSlice{Name: "Hello", MyAwesome: bigSlice()},
},
{
tName: "pointer struct with slice field",
variable: &SomeStructWIthSlice{Name: "HelloPointery", MyAwesome: bigSlice()},
},
{
tName: "struct with tags",
variable: StructWithTags{},
},
{
tName: "embedded struct with tags",
variable: Hey{Another: struct {
Allowed bool `json:"enabled"`
Name string `json:"their_name"`
}{
Allowed: true,
Name: "Jane",
}},
},
}
for _, v := range tt {
v := v
t.Run(v.tName, func(t *testing.T) {
t.Parallel()
res := newVari(
v.variable,
Config{
MaxLength: 14,
ShowPrivateFields: false,
MaxIndentLevel: 10,
},
)
path := getDataPath(t, "vars_test.go", v.tName)
dealWithTestData(t, path, res.String())
})
}
}
func TestStdlibVariables(t *testing.T) {
t.Parallel()
tt := []struct {
tName string
variable interface{}
}{
{
tName: "value struct of http.Request",
variable: http.Request{},
},
{
tName: "pointer struct of http.Request",
variable: &http.Request{},
},
}
for _, v := range tt {
v := v
t.Run(v.tName, func(t *testing.T) {
t.Parallel()
res := newVari(
v.variable,
Config{
MaxLength: 14,
ShowPrivateFields: false,
MaxIndentLevel: 10,
},
)
path := getDataPath(t, "vars_test.go", v.tName)
dealWithTestData(t, path, res.String())
})
}
}
func TestSliceMap(t *testing.T) {
t.Parallel()
var nilSlice []string = nil
var nilMap map[string]int = nil
tt := []struct {
tName string
variable interface{}
}{
{
tName: "nil slice",
variable: nilSlice,
},
{
tName: "no element slice",
variable: []string{},
},
{
tName: "one element slice",
variable: []string{"hello"},
},
{
tName: "two element slice",
variable: []string{"one", "two"},
},
{
tName: "nil map",
variable: nilMap,
},
{
tName: "no element map",
variable: map[string]int{},
},
{
tName: "one element map",
variable: map[string]int{"o": 1},
},
{
tName: "two element map",
variable: map[string]int{"o": 1, "two": 2},
},
}
for _, v := range tt {
v := v
t.Run(v.tName, func(t *testing.T) {
t.Parallel()
res := newVari(
v.variable,
Config{
MaxLength: 14,
ShowPrivateFields: false,
MaxIndentLevel: 10,
},
)
path := getDataPath(t, "vars_test.go", v.tName)
dealWithTestData(t, path, res.String())
})
}
}
type customContext struct{ parent context.Context }
func (d customContext) Deadline() (time.Time, bool) { return time.Time{}, false }
func (d customContext) Done() <-chan struct{} { return nil }
func (d customContext) Err() error { return nil }
func (d customContext) Value(key interface{}) interface{} { return d.parent.Value(key) }
func TestContexts(t *testing.T) {
t.Parallel()
type myContextKeyType string
{
// const shortForm = "2006-Jan-02"
// when, err := time.ParseInLocation(shortForm, "2013-Feb-03", time.UTC)
// attest.Ok(t, err)
// // This WithDeadline does not work because the printed value of WithDeadline is not stable
// // https://github.com/golang/go/blob/39effbc105f5c54117a6011af3c48e3c8f14eca9/src/context/context.go#L654-L657
// ctxWithDeadline, cancel := context.WithDeadline(context.Background(), when)
// t.Cleanup(func() {
// cancel()
// })
}
type StructWithContext struct {
Name string
Age int64
OurCtx context.Context
}
ctxWithCancel, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
ctxWithValue := context.WithValue(context.TODO(), myContextKeyType("ctxWithValueType"), "OKAYY") //nolint:gocritic
encapsulatedStdlibCtx := context.WithValue(ctxWithCancel, myContextKeyType("myContextKeyType"), "ThisIsSomeContextValue")
tt := []struct {
tName string
variable interface{}
}{
{
tName: "stdlib context TODO",
variable: context.TODO(), //nolint:gocritic
},
{
tName: "stdlib context Background",
variable: context.Background(),
},
{
tName: "stdlib context WithCancel",
variable: ctxWithCancel,
},
// This WithDeadline does not work because the printed value of WithDeadline is not stable
// https://github.com/golang/go/blob/39effbc105f5c54117a6011af3c48e3c8f14eca9/src/context/context.go#L654-L657
// {
// tName: "stdlib context WithDeadline",
// variable: ctxWithDeadline,
// },
{
tName: "stdlib context WithValue",
variable: ctxWithValue,
},
{
tName: "stdlib context encapsulated",
variable: encapsulatedStdlibCtx,
},
{
tName: "custom context",
variable: customContext{context.Background()},
},
{
tName: "context inside struct",
variable: StructWithContext{Name: "John", Age: 763, OurCtx: encapsulatedStdlibCtx},
},
}
for _, v := range tt {
v := v
t.Run(v.tName, func(t *testing.T) {
t.Parallel()
res := newVari(
v.variable,
Config{
MaxLength: 14,
ShowPrivateFields: false,
MaxIndentLevel: 10,
},
)
path := getDataPath(t, "vars_test.go", v.tName)
dealWithTestData(t, path, res.String())
})
}
}
func TestLong(t *testing.T) {
t.Parallel()
type Hey struct {
BigSlice []int
BigArray [10_000]int
BigMap map[int]string
BigString string
}
h := Hey{
BigSlice: bigSlice(),
BigArray: bigArray(),
BigMap: bigMap(),
BigString: BigString,
}
tt := []struct {
tName string
variable interface{}
c Config
}{
{
tName: "no_config",
variable: h,
},
{
tName: "default_config",
variable: h,
c: Config{
MaxLength: 14,
ShowPrivateFields: false,
MaxIndentLevel: 10,
},
},
{
tName: "maxLength_config",
variable: h,
c: Config{MaxLength: math.MaxInt},
},
{
tName: "maxLength_big-string_config",
variable: BigString,
c: Config{MaxLength: math.MaxInt},
},
{
tName: "maxLength_empty-string_config",
variable: "",
c: Config{MaxLength: math.MaxInt},
},
}
for _, v := range tt {
v := v
t.Run(v.tName, func(t *testing.T) {
t.Parallel()
res := newVari(v.variable, v.c)
path := getDataPath(t, "vars_test.go", v.tName)
dealWithTestData(t, path, res.String())
})
}
}
func TestPublicPrivate(t *testing.T) {
t.Parallel()
type Hey struct {
Name string
agePrivate int
}
type SomeStruct struct {
AAA io.Reader
SomeNilError error
SomeConcreteError error
SomePublic Hey
privateThing string
anotherPrivate Hey
}
for _, name := range []string{"default", "ShowPrivateFields", "DoNotShowPrivateFields"} {
name := name
tName := fmt.Sprintf("TestPublicPrivate-custom-%s", name)
x := SomeStruct{
AAA: strings.NewReader("hello from strings NewReader"),
SomeConcreteError: errors.New("houston something bad happened"),
SomePublic: Hey{Name: "SomePublicName", agePrivate: 67},
privateThing: "privateThing",
anotherPrivate: Hey{Name: "anotherPrivateName", agePrivate: 12},
}
t.Run(tName, func(t *testing.T) {
t.Parallel()
var res string
switch name {
default:
t.Fatalf("option `%s` is not expected", name)
case "default":
res = Dir(x)
case "ShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: true})
case "DoNotShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: false})
}
path := getDataPath(t, "vars_test.go", tName)
dealWithTestData(t, path, res)
})
}
for _, name := range []string{"default", "ShowPrivateFields", "DoNotShowPrivateFields"} {
name := name
tName := fmt.Sprintf("TestPublicPrivate-stdlib-%s", name)
x := http.Request{Method: "Hello"}
t.Run(tName, func(t *testing.T) {
t.Parallel()
var res string
switch name {
default:
t.Fatalf("option `%s` is not expected", name)
case "default":
res = Dir(x)
case "ShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: true})
case "DoNotShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: false})
}
path := getDataPath(t, "vars_test.go", tName)
dealWithTestData(t, path, res)
})
}
}
type Client struct {
Public string
srv srvRef
}
type srvRef struct {
cli *Client
}
func TestCircularRef(t *testing.T) {
t.Parallel()
for _, name := range []string{"ShowPrivateFields", "DoNotShowPrivateFields"} {
name := name
tName := fmt.Sprintf("TestCircularRef-with-cirlce-%s", name)
x := &Client{Public: "PublicName"}
x.srv.cli = x // circular
t.Run(tName, func(t *testing.T) {
t.Parallel()
var res string
switch name {
default:
t.Fatalf("option `%s` is not expected", name)
case "ShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: true})
case "DoNotShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: false})
}
path := getDataPath(t, "vars_test.go", tName)
dealWithTestData(t, path, res)
})
}
for _, name := range []string{"ShowPrivateFields", "DoNotShowPrivateFields"} {
name := name
tName := fmt.Sprintf("TestCircularRef-with-NO-cirlce-%s", name)
x := &Client{Public: "PublicName"}
x.srv.cli = &Client{Public: "NewPubName"} // no circle
t.Run(tName, func(t *testing.T) {
t.Parallel()
var res string
switch name {
default:
t.Fatalf("option `%s` is not expected", name)
case "ShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: true})
case "DoNotShowPrivateFields":
res = Dir(x, Config{ShowPrivateFields: false})
}
path := getDataPath(t, "vars_test.go", tName)
dealWithTestData(t, path, res)
})
}
}