-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_parser_test.go
171 lines (160 loc) · 3.31 KB
/
csv_parser_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
package st2
import (
"bytes"
"encoding/json"
"io"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCsvParser_Parse(t *testing.T) {
type args struct {
reader io.Reader
}
tests := []struct {
name string
init func(t *testing.T) CsvParser
inspect func(r CsvParser, t *testing.T) //inspects receiver after test run
args func(t *testing.T) args
want1 []*Struct
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation after test
}{
{
name: "empty",
init: func(t *testing.T) CsvParser {
return *NewCsvParser(Context{})
},
args: func(t *testing.T) args {
return args{
reader: bytes.NewReader([]byte("")),
}
},
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.EqualError(t, err, "EOF")
},
},
{
name: "one item",
init: func(t *testing.T) CsvParser {
return *NewCsvParser(Context{})
},
args: func(t *testing.T) args {
return args{
reader: bytes.NewReader([]byte("a\n")),
}
},
want1: []*Struct{
{
Type: &StructLikeType{
Name: "Root",
Source: SLSStruct,
},
Members: []*Member{
{
Field: "a",
Type: StringVal,
Index: 1,
GoTag: []string{`csv:"a"`},
},
},
},
},
},
{
name: "succ",
init: func(t *testing.T) CsvParser {
return *NewCsvParser(Context{
Root: "hello",
})
},
args: func(t *testing.T) args {
return args{
reader: bytes.NewReader([]byte(`a,b,c,d,c d , a b ,hello world, t t
1,2,3,4
`)),
}
},
want1: []*Struct{
{
Type: &StructLikeType{
Name: "Hello",
Source: SLSStruct,
},
Members: []*Member{
{
Field: "a",
Type: StringVal,
Index: 1,
GoTag: []string{`csv:"a"`},
},
{
Field: "b",
Type: StringVal,
Index: 2,
GoTag: []string{`csv:"b"`},
},
{
Field: "c",
Type: StringVal,
Index: 3,
GoTag: []string{`csv:"c"`},
},
{
Field: "d",
Type: StringVal,
Index: 4,
GoTag: []string{`csv:"d"`},
},
{
Field: "c_d",
Type: StringVal,
Index: 5,
GoTag: []string{`csv:"c_d"`},
},
{
Field: "a_b",
Type: StringVal,
Index: 6,
GoTag: []string{`csv:"a_b"`},
},
{
Field: "hello_world",
Type: StringVal,
Index: 7,
GoTag: []string{`csv:"hello_world"`},
},
{
Field: "t_t",
Type: StringVal,
Index: 8,
GoTag: []string{`csv:"t_t"`},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)
receiver := tt.init(t)
got1, err := receiver.Parse(tArgs.reader)
if tt.inspect != nil {
tt.inspect(receiver, t)
}
if !reflect.DeepEqual(got1, tt.want1) {
got1json, _ := json.MarshalIndent(got1, "", " ")
want1json, _ := json.MarshalIndent(tt.want1, "", " ")
t.Errorf("CsvParser.Parse got1 = %v, want1: %v", string(got1json), string(want1json))
}
if (err != nil) != tt.wantErr {
t.Fatalf("CsvParser.Parse error = %v, wantErr: %t", err, tt.wantErr)
}
if tt.inspectErr != nil {
tt.inspectErr(err, t)
}
})
}
}