-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_test.go
More file actions
261 lines (253 loc) · 5.15 KB
/
Copy pathtransform_test.go
File metadata and controls
261 lines (253 loc) · 5.15 KB
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
package simplecsv
import (
"reflect"
"testing"
)
func TestSimpleCsv_ReplaceInField(t *testing.T) {
type args struct {
columnName string
old string
new string
}
tests := []struct {
name string
s SimpleCsv
args args
want SimpleCsv
want1 bool
}{
{
name: "ReplaceInField1",
want: SimpleCsv{
{"Name", "Status"},
{"Ana", ""},
{"Bob", "ok"},
{"Cid", ""},
},
want1: true,
s: SimpleCsv{
{"Name", "Status"},
{"Ana", "N/A"},
{"Bob", "ok"},
{"Cid", "N/A"},
},
args: args{columnName: "Status", old: "N/A", new: ""},
},
{
name: "ReplaceInFieldNoMatch",
want: SimpleCsv{
{"Name", "Status"},
{"Ana", "ok"},
{"Bob", "ok"},
},
want1: true,
s: SimpleCsv{
{"Name", "Status"},
{"Ana", "ok"},
{"Bob", "ok"},
},
args: args{columnName: "Status", old: "N/A", new: ""},
},
{
name: "ReplaceInFieldCaseSensitive",
want: SimpleCsv{
{"Name", "Status"},
{"Ana", "n/a"},
},
want1: true,
s: SimpleCsv{
{"Name", "Status"},
{"Ana", "n/a"},
},
args: args{columnName: "Status", old: "N/A", new: ""},
},
{
name: "ReplaceInFieldWholeCellOnly",
want: SimpleCsv{
{"Name", "Code"},
{"Ana", "AB"},
{"Bob", "B"},
},
want1: true,
s: SimpleCsv{
{"Name", "Code"},
{"Ana", "AB"},
{"Bob", "B"},
},
args: args{columnName: "Code", old: "A", new: "X"},
},
{
name: "ReplaceInFieldHeaderNotModified",
want: SimpleCsv{
{"N/A", "Status"},
{"Ana", ""},
},
want1: true,
s: SimpleCsv{
{"N/A", "Status"},
{"Ana", "N/A"},
},
args: args{columnName: "Status", old: "N/A", new: ""},
},
{
name: "ReplaceInFieldMissing",
want: SimpleCsv{
{"Name", "Status"},
{"Ana", "N/A"},
},
want1: false,
s: SimpleCsv{
{"Name", "Status"},
{"Ana", "N/A"},
},
args: args{columnName: "Book", old: "N/A", new: ""},
},
{
name: "ReplaceInFieldEmpty",
want: SimpleCsv{},
want1: false,
s: SimpleCsv{},
args: args{columnName: "Status", old: "N/A", new: ""},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := tt.s.ReplaceInField(tt.args.columnName, tt.args.old, tt.args.new)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SimpleCsv.ReplaceInField() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("SimpleCsv.ReplaceInField() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestSimpleCsv_TrimSpace(t *testing.T) {
tests := []struct {
name string
s SimpleCsv
want SimpleCsv
want1 bool
}{
{
name: "TrimSpace1",
want: SimpleCsv{
{"Name", "Age"},
{"Ana", "30"},
{"Bob", "7"},
},
want1: true,
s: SimpleCsv{
{" Name ", " Age\t"},
{"\tAna ", " 30"},
{"Bob\n", "7"},
},
},
{
name: "TrimSpaceHeaderOnly",
want: SimpleCsv{
{"Name", "Age"},
},
want1: true,
s: SimpleCsv{
{" Name ", " Age "},
},
},
{
name: "TrimSpaceAlreadyTrimmed",
want: SimpleCsv{
{"Name", "Age"},
{"Ana", "30"},
},
want1: true,
s: SimpleCsv{
{"Name", "Age"},
{"Ana", "30"},
},
},
{
name: "TrimSpaceDuplicateHeaderRejected",
want: SimpleCsv{
{" A ", "A"},
{"1", "2"},
},
want1: false,
s: SimpleCsv{
{" A ", "A"},
{"1", "2"},
},
},
{
name: "TrimSpaceEmpty",
want: SimpleCsv{},
want1: false,
s: SimpleCsv{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := tt.s.TrimSpace()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SimpleCsv.TrimSpace() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("SimpleCsv.TrimSpace() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
// The original csv is not modified, and a failure path returns an
// independent copy.
func TestTransformOpsDoNotMutateSource(t *testing.T) {
want := SimpleCsv{
{" Name ", "Status"},
{"Ana ", "N/A"},
{"Bob", " ok "},
}
s := SimpleCsv{
{" Name ", "Status"},
{"Ana ", "N/A"},
{"Bob", " ok "},
}
got, ok := s.ReplaceInField("Status", "N/A", "")
if !ok {
t.Fatalf("ReplaceInField() ok = false, want true")
}
if got[1][1] != "" {
t.Errorf("ReplaceInField() cell = %q, want empty", got[1][1])
}
if !reflect.DeepEqual(s, want) {
t.Errorf("ReplaceInField() modified the original csv: %v", s)
}
got[0][0] = "CHANGED"
if !reflect.DeepEqual(s, want) {
t.Errorf("mutating ReplaceInField result changed source: %v", s)
}
got, ok = s.TrimSpace()
if !ok {
t.Fatalf("TrimSpace() ok = false, want true")
}
if !reflect.DeepEqual(s, want) {
t.Errorf("TrimSpace() modified the original csv: %v", s)
}
got[0][0] = "CHANGED"
if !reflect.DeepEqual(s, want) {
t.Errorf("mutating TrimSpace result changed source: %v", s)
}
dup := SimpleCsv{
{" A ", "A"},
{"1", "2"},
}
dupWant := SimpleCsv{
{" A ", "A"},
{"1", "2"},
}
failed, ok := dup.TrimSpace()
if ok || failed[0][0] != " A " {
t.Fatalf("TrimSpace() = %v, %v, want unchanged copy, false", failed, ok)
}
failed[0][0] = "CHANGED"
if !reflect.DeepEqual(dup, dupWant) {
t.Errorf("mutating failed TrimSpace result changed source: %v", dup)
}
}