/
struct2csv_test.go
116 lines (109 loc) · 1.75 KB
/
struct2csv_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
package struct2csv
import (
"reflect"
"testing"
)
type testStruct struct {
A1 int
A2 bool
B1 []int
B2 []struct {
B21 int
B22 string
}
B3 []*struct {
B31 []*int
}
}
func TestStructConverter_Convert(t *testing.T) {
conv, _ := NewStructConverter(NewHeaderAutoIncrementConv())
type args struct {
data interface{}
}
type wants struct {
paths []string // un encode key
}
tests := []struct {
name string
args args
want wants
wantErr bool
}{
{
name: "success",
args: args{
data: []testStruct{{
A1: 1,
A2: true,
B1: []int{2, 3},
B2: []struct {
B21 int
B22 string
}{
{
B21: 4,
B22: "a",
},
},
B3: []*struct {
B31 []*int
}{
{
B31: []*int{newInt(5)},
},
},
},
{
A1: 1,
A2: true,
B1: []int{2, 3},
B2: []struct {
B21 int
B22 string
}{
{
B21: 4,
B22: "a",
},
},
B3: []*struct {
B31 []*int
}{
{
B31: []*int{newInt(5), newInt(6)},
},
},
},
},
},
want: wants{
paths: []string{
"/A1",
"/A2",
"/B1/0",
"/B1/1",
"/B2/0/B21",
"/B2/0/B22",
"/B3/0/B31/0",
"/B3/0/B31/1",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := conv.Convert(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Convert() error = %v, wantErr %v", err, tt.wantErr)
return
}
paths := got.GetUnEncodedSortHeader()
if !reflect.DeepEqual(paths, tt.want.paths) {
t.Errorf("Convert() got UnEncodedSortHeader = %v, want %v", paths, tt.want.paths)
}
})
}
}
func newInt(i int) *int {
return &i
}