-
Notifications
You must be signed in to change notification settings - Fork 5
/
dumper_test.go
979 lines (829 loc) · 20.1 KB
/
dumper_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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
package godump_test
import (
"bytes"
"fmt"
"os"
"testing"
"unsafe"
"github.com/yassinebenaid/godump"
)
func TestCanDumpPrimitives(t *testing.T) {
type IntType int
type Int8Type int8
type Int16Type int16
type Int32Type int32
type Int64Type int64
type UintType uint
type Uint8Type uint8
type Uint16Type uint16
type Uint32Type uint32
type Uint64Type uint64
type Float32Type float32
type Float64Type float64
type Complex64Type complex64
type Complex128Type complex128
type Bool1Type bool
type Bool2Type bool
type StringType string
type UintptrType uintptr
type IntPtrType *int
type Int8PtrType *int8
type Int16PtrType *int16
type Int32PtrType *int32
type Int64PtrType *int64
type UintPtrType *uint
type Uint8PtrType *uint8
type Uint16PtrType *uint16
type Uint32PtrType *uint32
type Uint64PtrType *uint64
type Float32PtrType *float32
type Float64PtrType *float64
type Complex64PtrType *complex64
type Complex128PtrType *complex128
type Bool1PtrType *bool
type Bool2PtrType *bool
type StringPtrType *string
type UintptrPtrType *uintptr
type FuncType func()
type Func2Type func(int) float64
type Func3Type func(...*any) any
type Func4Type func(byte, ...[]*complex128) bool
type ChanType chan struct{}
type Chan1Type <-chan struct{}
type Chan2Type chan<- struct{}
type UnsafePointer unsafe.Pointer
type Node struct {
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
Complex64 complex64
Complex128 complex128
Bool1 bool
Bool2 bool
String string
Uintptr uintptr
IntPtr *int
Int8Ptr *int8
Int16Ptr *int16
Int32Ptr *int32
Int64Ptr *int64
UintPtr *uint
Uint8Ptr *uint8
Uint16Ptr *uint16
Uint32Ptr *uint32
Uint64Ptr *uint64
Float32Ptr *float32
Float64Ptr *float64
Complex64Ptr *complex64
Complex128Ptr *complex128
Bool1Ptr *bool
Bool2Ptr *bool
StringPtr *string
UintptrPtr *uintptr
TypedInt IntType
TypedInt8 Int8Type
TypedInt16 Int16Type
TypedInt32 Int32Type
TypedInt64 Int64Type
TypedUint UintType
TypedUint8 Uint8Type
TypedUint16 Uint16Type
TypedUint32 Uint32Type
TypedUint64 Uint64Type
TypedFloat32 Float32Type
TypedFloat64 Float64Type
TypedComplex64 Complex64Type
TypedComplex128 Complex128Type
TypedBool1 Bool1Type
TypedBool2 Bool2Type
TypedString StringType
TypedUintptr UintptrType
TypedIntPtr IntPtrType
TypedInt8Ptr Int8PtrType
TypedInt16Ptr Int16PtrType
TypedInt32Ptr Int32PtrType
TypedInt64Ptr Int64PtrType
TypedUintPtr UintPtrType
TypedUint8Ptr Uint8PtrType
TypedUint16Ptr Uint16PtrType
TypedUint32Ptr Uint32PtrType
TypedUint64Ptr Uint64PtrType
TypedFloat32Ptr Float32PtrType
TypedFloat64Ptr Float64PtrType
TypedComplex64Ptr Complex64PtrType
TypedComplex128Ptr Complex128PtrType
TypedBool1Ptr Bool1PtrType
TypedBool2Ptr Bool2PtrType
TypedStringPtr StringPtrType
TypedUintptrPtr UintptrPtrType
PtrTypedInt *IntType
PtrTypedInt8 *Int8Type
PtrTypedInt16 *Int16Type
PtrTypedInt32 *Int32Type
PtrTypedInt64 *Int64Type
PtrTypedUint *UintType
PtrTypedUint8 *Uint8Type
PtrTypedUint16 *Uint16Type
PtrTypedUint32 *Uint32Type
PtrTypedUint64 *Uint64Type
PtrTypedFloat32 *Float32Type
PtrTypedFloat64 *Float64Type
PtrTypedComplex64 *Complex64Type
PtrTypedComplex128 *Complex128Type
PtrTypedBool1 *Bool1Type
PtrTypedBool2 *Bool2Type
PtrTypedString *StringType
PtrTypedUintptr *UintptrType
NilPointer *int
Func func()
Func2 func(int) float64
Func3 func(...*any) any
Func4 func(byte, ...[]*complex128) bool
FuncPtr *func()
Func2Ptr *func(int) float64
Func3Ptr *func(...*any) any
Func4Ptr *func(byte, ...[]*complex128) bool
TypedFunc FuncType
TypedFunc2 Func2Type
TypedFunc3 Func3Type
TypedFunc4 Func4Type
PtrTypedFunc *FuncType
PtrTypedFunc2 *Func2Type
PtrTypedFunc3 *Func3Type
PtrTypedFunc4 *Func4Type
NilFunc func()
Chan chan struct{}
Chan1 <-chan struct{}
Chan2 chan<- struct{}
ChanPtr *chan struct{}
Chan1Ptr *<-chan struct{}
Chan2Ptr *chan<- struct{}
TypedChan ChanType
TypedChan1 Chan1Type
TypedChan2 Chan2Type
PtrTypedChan *ChanType
PtrTypedChan1 *Chan1Type
PtrTypedChan2 *Chan2Type
BufferedChan chan struct{}
NilChan chan struct{}
UnsafePointer1 unsafe.Pointer
UnsafePointer2 *unsafe.Pointer
NamedUnsafePointer UnsafePointer
}
node := Node{
Int: 123,
Int8: -45,
Int16: 6789,
Int32: -987,
Int64: 3849876543247876432,
Uint: 837,
Uint8: 38,
Uint16: 3847,
Uint32: 9843,
Uint64: 2834,
Float32: 123.475,
Float64: -12345.09876,
Complex64: 12.987i,
Complex128: -473i,
Bool1: true,
Bool2: false,
String: "foo bar",
Uintptr: 1234567890,
TypedInt: IntType(123),
TypedInt8: Int8Type(-45),
TypedInt16: Int16Type(6789),
TypedInt32: Int32Type(-987),
TypedInt64: Int64Type(3849876543247876432),
TypedUint: UintType(837),
TypedUint8: Uint8Type(38),
TypedUint16: Uint16Type(3847),
TypedUint32: Uint32Type(9843),
TypedUint64: Uint64Type(2834),
TypedFloat32: Float32Type(123.475),
TypedFloat64: Float64Type(-12345.09876),
TypedComplex64: Complex64Type(12.987i),
TypedComplex128: Complex128Type(-473i),
TypedBool1: Bool1Type(true),
TypedBool2: Bool2Type(false),
TypedString: StringType("foo bar"),
TypedUintptr: UintptrType(1234567890),
UnsafePointer1: nil,
NamedUnsafePointer: nil,
Chan: make(chan struct{}),
Chan1: make(chan struct{}),
Chan2: make(chan struct{}),
BufferedChan: make(chan struct{}, 255),
}
node.IntPtr = &node.Int
node.Int8Ptr = &node.Int8
node.Int16Ptr = &node.Int16
node.Int32Ptr = &node.Int32
node.Int64Ptr = &node.Int64
node.UintPtr = &node.Uint
node.Uint8Ptr = &node.Uint8
node.Uint16Ptr = &node.Uint16
node.Uint32Ptr = &node.Uint32
node.Uint64Ptr = &node.Uint64
node.Float32Ptr = &node.Float32
node.Float64Ptr = &node.Float64
node.Complex64Ptr = &node.Complex64
node.Complex128Ptr = &node.Complex128
node.Bool1Ptr = &node.Bool1
node.Bool2Ptr = &node.Bool2
node.StringPtr = &node.String
node.UintptrPtr = &node.Uintptr
node.TypedIntPtr = node.IntPtr
node.TypedInt8Ptr = node.Int8Ptr
node.TypedInt16Ptr = node.Int16Ptr
node.TypedInt32Ptr = node.Int32Ptr
node.TypedInt64Ptr = node.Int64Ptr
node.TypedUintPtr = node.UintPtr
node.TypedUint8Ptr = node.Uint8Ptr
node.TypedUint16Ptr = node.Uint16Ptr
node.TypedUint32Ptr = node.Uint32Ptr
node.TypedUint64Ptr = node.Uint64Ptr
node.TypedFloat32Ptr = node.Float32Ptr
node.TypedFloat64Ptr = node.Float64Ptr
node.TypedComplex64Ptr = node.Complex64Ptr
node.TypedComplex128Ptr = node.Complex128Ptr
node.TypedBool1Ptr = node.Bool1Ptr
node.TypedBool2Ptr = node.Bool2Ptr
node.TypedStringPtr = node.StringPtr
node.TypedUintptrPtr = node.UintptrPtr
node.PtrTypedInt = &node.TypedInt
node.PtrTypedInt8 = &node.TypedInt8
node.PtrTypedInt16 = &node.TypedInt16
node.PtrTypedInt32 = &node.TypedInt32
node.PtrTypedInt64 = &node.TypedInt64
node.PtrTypedUint = &node.TypedUint
node.PtrTypedUint8 = &node.TypedUint8
node.PtrTypedUint16 = &node.TypedUint16
node.PtrTypedUint32 = &node.TypedUint32
node.PtrTypedUint64 = &node.TypedUint64
node.PtrTypedFloat32 = &node.TypedFloat32
node.PtrTypedFloat64 = &node.TypedFloat64
node.PtrTypedComplex64 = &node.TypedComplex64
node.PtrTypedComplex128 = &node.TypedComplex128
node.PtrTypedBool1 = &node.TypedBool1
node.PtrTypedBool2 = &node.TypedBool2
node.PtrTypedString = &node.TypedString
node.PtrTypedUintptr = &node.TypedUintptr
node.Func = func() {}
node.Func2 = func(int) float64 { return 0 }
node.Func3 = func(...*any) any { return nil }
node.Func4 = func(byte, ...[]*complex128) bool { return false }
node.TypedFunc = func() {}
node.TypedFunc2 = func(int) float64 { return 0 }
node.TypedFunc3 = func(...*any) any { return nil }
node.TypedFunc4 = func(byte, ...[]*complex128) bool { return false }
node.FuncPtr = &node.Func
node.Func2Ptr = &node.Func2
node.Func3Ptr = &node.Func3
node.Func4Ptr = &node.Func4
node.PtrTypedFunc = &node.TypedFunc
node.PtrTypedFunc2 = &node.TypedFunc2
node.PtrTypedFunc3 = &node.TypedFunc3
node.PtrTypedFunc4 = &node.TypedFunc4
ch := make(chan struct{})
var ch2 <-chan struct{} = ch
var ch3 chan<- struct{} = ch
tch := ChanType(ch)
tch1 := Chan1Type(ch2)
tch2 := Chan2Type(ch3)
node.ChanPtr = &ch
node.Chan1Ptr = &ch2
node.Chan2Ptr = &ch3
node.TypedChan = ch
node.TypedChan1 = ch2
node.TypedChan2 = ch3
node.PtrTypedChan = &tch
node.PtrTypedChan1 = &tch1
node.PtrTypedChan2 = &tch2
node.UnsafePointer2 = (*unsafe.Pointer)(unsafe.Pointer(&node))
var d godump.Dumper
result := d.Sprint(node)
checkFromFeed(t, []byte(result), "./testdata/primitives.txt")
}
func TestCanDumpNamedPrimitives(t *testing.T) {
type IntType int
type Int8Type int8
type Int16Type int16
type Int32Type int32
type Int64Type int64
type UintType uint
type Uint8Type uint8
type Uint16Type uint16
type Uint32Type uint32
type Uint64Type uint64
type Float32Type float32
type Float64Type float64
type Complex64Type complex64
type Complex128Type complex128
type Bool1Type bool
type Bool2Type bool
type StringType string
type UintptrType uintptr
type Node struct {
Int IntType
Int8 Int8Type
Int16 Int16Type
Int32 Int32Type
Int64 Int64Type
Uint UintType
Uint8 Uint8Type
Uint16 Uint16Type
Uint32 Uint32Type
Uint64 Uint64Type
Float32 Float32Type
Float64 Float64Type
Complex64 Complex64Type
Complex128 Complex128Type
Bool1 Bool1Type
Bool2 Bool2Type
String StringType
Uintptr UintptrType
PtrInt *IntType
PtrInt8 *Int8Type
PtrInt16 *Int16Type
PtrInt32 *Int32Type
PtrInt64 *Int64Type
PtrUint *UintType
PtrUint8 *Uint8Type
PtrUint16 *Uint16Type
PtrUint32 *Uint32Type
PtrUint64 *Uint64Type
PtrFloat32 *Float32Type
PtrFloat64 *Float64Type
PtrComplex64 *Complex64Type
PtrComplex128 *Complex128Type
PtrBool1 *Bool1Type
PtrBool2 *Bool2Type
PtrString *StringType
PtrUintptr *UintptrType
}
node := Node{
Int: IntType(123),
Int8: Int8Type(-45),
Int16: Int16Type(6789),
Int32: Int32Type(-987),
Int64: Int64Type(3849876543247876432),
Uint: UintType(837),
Uint8: Uint8Type(38),
Uint16: Uint16Type(3847),
Uint32: Uint32Type(9843),
Uint64: Uint64Type(2834),
Float32: Float32Type(123.475),
Float64: Float64Type(-12345.09876),
Complex64: Complex64Type(12.987i),
Complex128: Complex128Type(-473i),
Bool1: Bool1Type(true),
Bool2: Bool2Type(false),
String: StringType("foo bar"),
Uintptr: UintptrType(1234567890),
}
node.PtrInt = &node.Int
node.PtrInt8 = &node.Int8
node.PtrInt16 = &node.Int16
node.PtrInt32 = &node.Int32
node.PtrInt64 = &node.Int64
node.PtrUint = &node.Uint
node.PtrUint8 = &node.Uint8
node.PtrUint16 = &node.Uint16
node.PtrUint32 = &node.Uint32
node.PtrUint64 = &node.Uint64
node.PtrFloat32 = &node.Float32
node.PtrFloat64 = &node.Float64
node.PtrComplex64 = &node.Complex64
node.PtrComplex128 = &node.Complex128
node.PtrBool1 = &node.Bool1
node.PtrBool2 = &node.Bool2
node.PtrString = &node.String
node.PtrUintptr = &node.Uintptr
var d godump.Dumper
d.ShowPrimitiveNamedTypes = true
result := d.Sprint(node)
checkFromFeed(t, []byte(result), "./testdata/named-primitives.txt")
}
func TestCanDumpStructs(t *testing.T) {
type Number int
type Child1 struct {
X int
Y float64
Z Number
}
type Child struct {
Field1 Child1
Field2 *Child
}
type Node struct {
Inline struct {
Field1 struct {
X int
Y float64
Z Number
}
Field2 Child
}
Typed Child
Ptr **int
Empty struct{}
Ref *Node
}
num := 123
numaddr := &num
node := Node{
Inline: struct {
Field1 struct {
X int
Y float64
Z Number
}
Field2 Child
}{
Field1: struct {
X int
Y float64
Z Number
}{
X: 123,
Y: 123.456,
Z: Number(987),
},
Field2: Child{
Field1: Child1{
X: 12344,
Y: 578,
Z: Number(9876543),
},
Field2: &Child{
Field1: Child1{
X: 12344,
Y: 578,
Z: Number(9876543),
},
},
},
},
Ptr: &numaddr,
}
node.Inline.Field2.Field2.Field2 = node.Inline.Field2.Field2
node.Typed.Field2 = &node.Inline.Field2
node.Ref = &node
var d godump.Dumper
result := d.Sprint(node)
checkFromFeed(t, []byte(result), "./testdata/structs.txt")
}
func TestCannotDumpPrivateStructsWhenHidingOptionIsEnabled(t *testing.T) {
type number int
type child1 struct {
x int
y float64
z number
}
type child struct {
field1 child1
field2 *child
}
type node struct {
inline struct {
field1 struct {
x int
y float64
z number
}
field2 child
}
typed child
empty struct{}
ref *node
}
n := node{
inline: struct {
field1 struct {
x int
y float64
z number
}
field2 child
}{
field1: struct {
x int
y float64
z number
}{
x: 123,
y: 123.456,
z: number(987),
},
field2: child{
field1: child1{
x: 12344,
y: 578,
z: number(9876543),
},
field2: &child{
field1: child1{
x: 12344,
y: 578,
z: number(9876543),
},
},
},
},
empty: struct{}{},
}
n.inline.field2.field2.field2 = n.inline.field2.field2
n.typed.field2 = &n.inline.field2
n.ref = &n
var d godump.Dumper
d.HidePrivateFields = true
result := d.Sprint(n)
if result != "godump_test.node {}" {
t.Fatalf("unexpected result when trying to dump a private struct with hide private fields option enabled, expected `godump.node {}`, got `%v`", result)
}
}
func TestCanDumpPrivateStructs(t *testing.T) {
type number int
type child1 struct {
x int
y float64
z number
}
type child struct {
field1 child1
field2 *child
}
type node struct {
inline struct {
field1 struct {
x int
y float64
z number
}
field2 child
}
typed child
empty struct{}
ref *node
}
n := node{
inline: struct {
field1 struct {
x int
y float64
z number
}
field2 child
}{
field1: struct {
x int
y float64
z number
}{
x: 123,
y: 123.456,
z: number(987),
},
field2: child{
field1: child1{
x: 12344,
y: 578,
z: number(9876543),
},
field2: &child{
field1: child1{
x: 12344,
y: 578,
z: number(9876543),
},
},
},
},
empty: struct{}{},
}
n.inline.field2.field2.field2 = n.inline.field2.field2
n.typed.field2 = &n.inline.field2
n.ref = &n
var d godump.Dumper
result := d.Sprint(n)
checkFromFeed(t, []byte(result), "./testdata/private-structs.txt")
}
func TestCanDumpSlices(t *testing.T) {
type Slice []any
var nilSlice []Slice
var zeroArray [2]any
foo := "foo"
bar := "bar"
baz := "baz"
s := Slice{
1,
2.3,
true,
false,
nil,
[]*string{
&foo,
&bar,
&baz,
},
[]any{},
&[]bool{
true,
false,
},
make([]any, 3, 8),
nilSlice,
[2]int{1, 2},
zeroArray,
}
s = append(s, &s)
var d godump.Dumper
result := d.Sprint(s)
checkFromFeed(t, []byte(result), "./testdata/slices.txt")
}
func TestCanDumpMaps(t *testing.T) {
type SomeMap map[*SomeMap]*SomeMap
sm := &SomeMap{}
var nilMap SomeMap
m := map[any]any{12: 34}
maps := []any{
make(map[string]string),
map[any]int{
&m: 123,
},
map[string]any{
"cyclic": &m,
},
SomeMap{
&SomeMap{}: &SomeMap{sm: sm},
},
nilMap,
}
var d godump.Dumper
result := d.Sprint(maps)
checkFromFeed(t, []byte(result), "./testdata/maps.txt")
}
func TestCanCustomizeIndentation(t *testing.T) {
type User struct {
Name string
Age int
hobbies []string
bestFriend *User
}
me := User{
Name: "yassinebenaid",
Age: 22,
hobbies: []string{
"Dev",
"Go",
"Web",
"DevOps",
},
}
me.bestFriend = &me
d := godump.Dumper{
Indentation: " ",
}
result := d.Sprint(me)
checkFromFeed(t, []byte(result), "./testdata/indentation.txt")
}
type CSSColor struct {
R, G, B int
}
func (c CSSColor) Apply(s string) string {
return fmt.Sprintf(`<div style="color: rgb(%d, %d, %d); display: inline-block">%s</div>`, c.R, c.G, c.B, s)
}
func TestCanCustomizeTheme(t *testing.T) {
type User struct {
Name string
Age int
hobbies []string
bestFriend *User
}
me := User{
Name: "yassinebenaid",
Age: 22,
hobbies: []string{
"Dev",
"Go",
"Web",
"DevOps",
},
}
me.bestFriend = &me
var d godump.Dumper
d.Theme = godump.Theme{
String: CSSColor{138, 201, 38},
Quotes: CSSColor{112, 214, 255},
Bool: CSSColor{249, 87, 56},
Number: CSSColor{10, 178, 242},
Types: CSSColor{0, 150, 199},
Address: CSSColor{205, 93, 0},
PointerTag: CSSColor{110, 110, 110},
Nil: CSSColor{219, 57, 26},
Func: CSSColor{160, 90, 220},
Fields: CSSColor{189, 176, 194},
Chan: CSSColor{195, 154, 76},
UnsafePointer: CSSColor{89, 193, 180},
Braces: CSSColor{185, 86, 86},
}
result := d.Sprint(me)
checkFromFeed(t, []byte(result), "./testdata/theme.txt")
}
func TestDumperPrint_Sprint_And_Fprint(t *testing.T) {
type User struct {
Name string
Age int
hobbies []string
}
me := User{
Name: "yassinebenaid",
Age: 22,
hobbies: []string{
"Dev",
"Go",
"Web",
"DevOps",
},
}
expected := `godump_test.User {
Name: "yassinebenaid",
Age: 22,
hobbies: []string:4:4 {
"Dev",
"Go",
"Web",
"DevOps",
},
}`
var d godump.Dumper
if r := d.Sprint(me); expected != r {
t.Fatalf("unexpected result by Dumper.Sprint : `%s`", r)
}
if r := d.Sprintln(me); expected+"\n" != r {
t.Fatalf("unexpected result by Dumper.Sprintln : `%s`", r)
}
var buf bytes.Buffer
if err := d.Fprint(&buf, me); err != nil {
t.Fatalf("unexpected error by Dumper.Fprint : `%s`", err)
} else if expected != buf.String() {
t.Fatalf("unexpected result by Dumper.Fprint : `%s`", buf.String())
}
buf.Reset()
if err := d.Fprintln(&buf, me); err != nil {
t.Fatalf("unexpected error by Dumper.Fprintln : `%s`", err)
} else if expected+"\n" != buf.String() {
t.Fatalf("unexpected result by Dumper.Fprintln : `%s`", buf.String())
}
}
type X int
func (X) Write(_ []byte) (n int, err error) {
return 0, fmt.Errorf("foobar")
}
func TestDumperFprintReturnsAWriteErrorIfEncountered(t *testing.T) {
var d godump.Dumper
var x X
if err := d.Fprint(x, nil); err == nil {
t.Fatalf("unexpected nil error returned by Dumper.Fprint")
} else if err.Error() != "dumper error: encountered unexpected write error, foobar" {
t.Fatalf("unexpected error by Dumper.Fprint : `%s`", err.Error())
}
if err := d.Fprintln(x, nil); err == nil {
t.Fatalf("unexpected nil error returned by Dumper.Fprintln")
} else if err.Error() != "dumper error: encountered unexpected write error, foobar" {
t.Fatalf("unexpected error by Dumper.Fprintln : `%s`", err.Error())
}
}
func TestDump(t *testing.T) {
if err := godump.Dump(nil); err != nil {
t.Fatalf("unexpected error returned by Dump")
}
}
func checkFromFeed(t *testing.T, result []byte, feedPath string) {
t.Helper()
expectedOutput, err := os.ReadFile(feedPath)
if err != nil {
t.Fatal(err)
}
resultLines := bytes.Split(result, []byte("\n"))
expectedLines := bytes.Split(expectedOutput, []byte("\n"))
if len(resultLines) != len(expectedLines) {
t.Fatalf("expected %d lines, got %d", len(expectedLines), len(resultLines))
}
for i, line := range expectedLines {
if string(line) != string(resultLines[i]) {
t.Fatalf(`mismatch at line %d:
--- "%s" (%d)
+++ "%s" (%d)`, i+1, line, len(line), resultLines[i], len(resultLines[i]))
}
}
}