-
Notifications
You must be signed in to change notification settings - Fork 7
/
align_test.go
725 lines (665 loc) · 13.2 KB
/
align_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
package align
import (
"bytes"
"io"
"os"
"strings"
"testing"
)
const comma = ","
var columnCountCases = []struct {
input string
sep string
isQual bool
qual string
counts map[int]int
}{
{
"John,Doe,Henry\nMichael,Douglas,F",
comma,
false,
"",
map[int]int{
0: 7,
1: 7,
2: 5,
},
},
{
"John,Doe,\"Henry, Mellencamp\",[email protected]\nMichael,Douglas,F",
comma,
true,
"\"",
map[int]int{
0: 7,
1: 7,
2: 19,
},
}, {
"John,Doe,\"Henry, Mellencamp\",[email protected]\nMichael,Douglas,F",
comma,
true,
"\"",
map[int]int{
0: 7,
1: 7,
2: 19,
},
},
{
"one||two||three||four\nuno||dos||tres||quatro",
"||",
true,
"",
map[int]int{
0: 3,
1: 3,
2: 5,
3: 6,
},
},
{
"one||\"two|| number\"||three||four\nuno||dos||\"tres|\"||quatro",
"||",
true,
"\"",
map[int]int{
0: 3,
1: 14,
2: 7,
3: 6,
},
},
{
"one,tisß\nseven,two", // with byte count > 1
comma,
false,
"",
map[int]int{
0: 5,
1: 5,
},
},
}
var fieldLenCases = []struct {
input string
sep string
expected int
}{
{
"first,last",
",",
5,
},
{
"phone-number||email",
"||",
12,
},
}
var fieldLenEscapedCases = []struct {
input string
sep string
qual string
expected int
}{
{
"\"address1, address2\",last",
",",
"\"",
20,
},
{
"'expenseline2|expenseline2'||email",
"||",
"'",
27,
},
}
var countPaddingCases = []struct {
input string
fieldLen int
expected int
}{
{
"Roy",
10,
7,
},
{
"S",
20,
19,
},
{
"Luü",
5,
2,
},
}
var paddingCases = []struct {
input string
columnCount int
po PaddingOpts
pad Padder
expected int
}{
{
"Go",
8,
PaddingOpts{Justification: JustifyLeft, Pad: 1},
&fieldPad{},
10,
},
{
"Go",
8,
PaddingOpts{Justification: JustifyCenter, Pad: 0},
&fieldPad{},
10,
},
{
"Go",
4,
PaddingOpts{Justification: JustifyCenter, Pad: -1},
&fieldPad{},
6,
},
{
"Go",
8,
PaddingOpts{Justification: JustifyRight, Pad: 2},
&fieldPad{},
10,
},
}
var qualifiedSplitCases = []struct {
input string
sep string
qual string
expected int
}{
{
"First,\"Middle, Nickname\",Last",
",",
"\"",
3,
},
{
"First||\"Middle|| Nickname\"||Last",
"||",
"\"",
3,
},
{
"First,'Middle Nickname',Last",
",",
"'",
3,
},
{
"First,Middle Nickname,Last",
",",
"",
3,
},
{
"First",
",",
"\"",
1,
},
}
var exportCases = []struct {
input io.Reader
output io.Writer
outColumns []int
overrides map[int]Justification
pad int
numOfDelimiter int
}{
{
strings.NewReader("one,two,three\nfour,five,six"),
bytes.NewBufferString(""),
[]int{1},
map[int]Justification{1: JustifyRight},
1,
1,
},
{
strings.NewReader("first,middle,last"),
bytes.NewBufferString(""),
[]int{1, 3},
map[int]Justification{1: JustifyRight},
-1,
1,
},
{
strings.NewReader("first,middle,last"),
bytes.NewBufferString(""),
[]int{1, 4},
map[int]Justification{1: JustifyRight},
0,
0,
},
{
strings.NewReader("first,middle,last"),
bytes.NewBufferString(""),
nil,
map[int]Justification{1: JustifyRight},
1,
2,
},
}
var runCases = []struct {
hValue bool
helpValue bool
fValue string
oValue string
qValue string
sValue string
dValue string
aValue string
cValue string
iValue string
shouldErr bool
}{
{
hValue: true,
shouldErr: true,
},
{
helpValue: true,
shouldErr: true,
},
{
iValue: "1:right,2:center",
shouldErr: true,
},
{
iValue: "1:left,2:,NaN:left",
shouldErr: true,
},
{
iValue: "3:noexist",
shouldErr: true,
},
{
cValue: "a2-right,NaN",
shouldErr: true,
},
{
cValue: "1,2",
shouldErr: true,
},
{
cValue: "1-left,2-right,3-center",
shouldErr: true,
},
{
cValue: "1,2,a",
shouldErr: true,
},
{
qValue: "\"",
shouldErr: true,
},
{
oValue: "out.csv",
shouldErr: true,
},
}
var ouputSepCases = []struct {
input string
expected string
}{
{
",",
",",
},
{
"||",
"||",
},
}
var columnSizeCases = []struct {
counts map[int]int
cNum int
expected int
}{
{
map[int]int{
0: 2,
1: 2,
2: 5,
},
0,
2,
},
{
map[int]int{
0: 2,
1: 2,
2: 5,
},
3,
-1,
},
}
var updatePaddingCases = []struct {
input Justification
expected Justification
}{
{
JustifyCenter,
JustifyCenter,
},
{
JustifyRight,
JustifyRight,
},
}
var genFieldLenCases = []struct {
input string
sep string
qual string
expected int // len of first field for the input
}{
{
"as,df,q,wer,1234,zxc,v",
",",
"",
2,
},
{
"hello|hithere",
"|",
"",
5,
},
{
"hello|\"hith|ere\"",
"|",
"\"",
5,
},
{
"'hello'",
",",
"'",
7,
},
}
// TestUpdatePadding
func TestUpdatePadding(t *testing.T) {
for _, tt := range updatePaddingCases {
a := &Align{}
a.UpdatePadding(PaddingOpts{Justification: tt.input})
if a.padOpts.Justification != tt.expected {
t.Fatalf("updatePadding(%v) = %v; want %v", tt.input, a.padOpts.Justification, tt.expected)
}
}
}
// TestColumnSize
func TestColumnSize(t *testing.T) {
for _, tt := range columnSizeCases {
a := &Align{columnCounts: tt.counts}
got := a.columnSize(tt.cNum)
if got != tt.expected {
t.Fatalf("columnSize(%v) = %v; want %v", tt.cNum, got, tt.expected)
}
}
}
// TestOutputSep
func TestOutputSep(t *testing.T) {
for _, tt := range ouputSepCases {
a := &Align{}
a.OutputSep(tt.input)
if a.sepOut != tt.expected {
t.Fatalf("outputSep(%v) = %v; want %v", tt.input, a.sepOut, tt.expected)
}
}
}
// TestColumnFilter
func TestColumnFilter(t *testing.T) {
for _, tt := range exportCases {
a := NewAlign(tt.input, tt.output, comma, TextQualifier{})
a.UpdatePadding(PaddingOpts{ColumnOverride: tt.overrides, Pad: tt.pad})
a.FilterColumns(tt.outColumns)
a.Align()
}
}
// TestSplit
func TestSplit(t *testing.T) {
for _, tt := range qualifiedSplitCases {
a := NewAlign(strings.NewReader(tt.input), os.Stdout, comma, TextQualifier{On: true, Qualifier: "\""})
got := a.splitWithQual(tt.input, tt.sep, tt.qual)
if len(got) != tt.expected {
t.Fatalf("splitWithQual(%v, %v, %v) = %v; want %v", tt.input, tt.sep, tt.qual, len(got), tt.expected)
}
}
}
// TestPad
func TestPad(t *testing.T) {
for _, tt := range paddingCases {
padLen := countPadding(tt.input, tt.columnCount)
got := applyPadding(tt.pad, tt.input, " ", 1, padLen, tt.po.Justification)
if len(got) != tt.expected {
t.Fatalf("pad(%v) =%v; want %v", tt.input, got, tt.expected)
}
}
}
// TestColumnCounts
func TestColumnCounts(t *testing.T) {
for _, tt := range columnCountCases {
a := NewAlign(strings.NewReader(tt.input), os.Stdout, tt.sep, TextQualifier{On: tt.isQual, Qualifier: tt.qual})
a.columnLength()
for i := range tt.counts {
if a.columnSize(i) != tt.counts[i] {
t.Fatalf("Count for column %v = %v, want %v", i, a.columnSize(i), tt.counts[i])
}
}
}
}
// TestFieldLenEscaped
func TestFieldLenEscaped(t *testing.T) {
for _, tt := range fieldLenEscapedCases {
got := fieldLenEscaped(tt.input, tt.sep, tt.qual)
if got != tt.expected {
t.Fatalf("FieldLenEscaped(%v) = %v; want %v", tt.input, got, tt.expected)
}
}
}
// TestFieldLen
func TestFieldLen(t *testing.T) {
for _, tt := range fieldLenCases {
got := fieldLen(tt.input, tt.sep)
if got != tt.expected {
t.Fatalf("FieldLen(%v) = %v; want %v", tt.input, got, tt.expected)
}
}
}
// TestCountPadding
func TestCountPadding(t *testing.T) {
for _, tt := range countPaddingCases {
got := countPadding(tt.input, tt.fieldLen)
if got != tt.expected {
t.Fatalf("countPadding(%v) = %v; want %v", tt.input, got, tt.expected)
}
}
}
// TestGenFieldLen
func TestGenFieldLen(t *testing.T) {
for _, tt := range genFieldLenCases {
got := genFieldLen(tt.input, tt.sep, tt.qual)
if got != tt.expected {
t.Fatalf("genFieldLen(%v, %v, %v) = %v; want %v", tt.input, tt.sep, tt.qual, got, tt.expected)
}
}
}
func TestGenFieldLen_Failure(t *testing.T) {
got := genFieldLen("", "", "")
expected := 0
if got != expected {
t.Fatalf(`genFieldLen("", "", "") = %v; want %v`, got, expected)
}
}
// BenchmarkColumnCounts
func BenchmarkColumnCounts(b *testing.B) {
input := `First,Middle,Last,Email,Region,City,Zip,Full_Name,First,Middle,Last,Email,Region,City,Zip,Full_Name,First,Middle,Last,Email,Region,City,Zip,Full_Name
Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly,Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly,Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly
Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez ,Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez ,Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez
Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly,Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly,Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly
Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez ,Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez ,Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez
Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly,Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly,Karleigh,Destiny,Dean,[email protected],Stockholms län,Märsta,9038,Shaine Reilly
Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez ,Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez ,Alisa,Walker,Armand,[email protected],Himachal Pradesh,Shimla,MZ0 4QS,Olivia Velez
`
a := NewAlign(strings.NewReader(input), &bytes.Buffer{}, comma, TextQualifier{On: false})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.columnLength()
}
}
// BenchmarkSplitWithQual
func BenchmarkSplitWithQual(b *testing.B) {
input := "First,\"Middle, name\",Last,Email,Region,City,Zip,Full_Name"
a := NewAlign(strings.NewReader(input), &bytes.Buffer{}, comma, TextQualifier{On: true, Qualifier: "\""})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.splitWithQual(input, comma, "\"")
}
}
// BenchmarkSplitWithQualNoQual
func BenchmarkSplitWithQualNoQual(b *testing.B) {
input := "First,Middle,Last,Email,Region,City,Zip,Full_Name"
a := NewAlign(strings.NewReader(input), &bytes.Buffer{}, comma, TextQualifier{On: false})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.splitWithQual(input, comma, "\"")
}
}
func BenchmarkExport(b *testing.B) {
input := "First,Middle,Last,Email,Region,City,Zip,Full_Name"
a := NewAlign(strings.NewReader(input), &bytes.Buffer{}, comma, TextQualifier{On: false})
a.columnLength()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
a.export()
}
}
func TestExportLastColumnEmpty(t *testing.T) {
input := `First,Middle,Last,Email,Phone
k,o,doe,no-email,
dewey,ben,finn,[email protected],""
daffy,,"duck, jr.",[email protected],
bilbo,,baggins,[email protected],444
`
out := &bytes.Buffer{}
a := NewAlign(strings.NewReader(input), out, comma, TextQualifier{On: true, Qualifier: `"`})
a.Align()
got := out.String()
expected := `First , Middle , Last , Email , Phone
k , o , doe , no-email ,
dewey , ben , finn , [email protected] , ""
daffy , , "duck, jr." , [email protected] ,
bilbo , , baggins , [email protected] , 444
`
if got != expected {
t.Fatalf("export() = \n%v; want\n%v", got, expected)
}
}
func Test_genFieldLen(t *testing.T) {
type args struct {
s string
sep string
qual string
}
tests := []struct {
name string
args args
want int
}{
{
name: "Basic length",
args: args{
s: "John",
sep: ",",
qual: "\"",
},
want: 4,
},
{
name: "Qualified Len",
args: args{
s: "\"John\"",
sep: ",",
qual: "\"",
},
want: 6,
},
{
name: "Qualified Len with sep",
args: args{
s: "\"John, M.\"",
sep: ",",
qual: "\"",
},
want: 10,
},
{
name: "Qualified Len with space and sep",
args: args{
s: "\" ,\"",
sep: ",",
qual: "\"",
},
want: 4,
},
{
name: "Qualified Len with sep at start",
args: args{
s: "\", abc\"",
sep: ",",
qual: "\"",
},
want: 7,
},
{
name: "Qualified Len with sep at start not last field",
args: args{
s: "\", abc\", ,",
sep: ",",
qual: "\"",
},
want: 7,
},
{
name: "No qualifier not last field",
args: args{
s: "abc d, ,",
sep: ",",
qual: "",
},
want: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := genFieldLen(tt.args.s, tt.args.sep, tt.args.qual); got != tt.want {
t.Errorf("genFieldLen() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkGenFieldLen(b *testing.B) {
s := "\" , abc\", ,"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
genFieldLen(s, ",", "\"")
}
}