-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields_test.go
104 lines (98 loc) · 1.44 KB
/
fields_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
package qualified
import (
"testing"
)
var splitCases = []struct {
input, sep, qual string
expected int // num of resulting elements
}{
{
"one,two,three,four",
",",
"",
4,
},
{
"\"one,two\",three,four",
",",
"\"",
3,
},
{
"one",
",",
"",
1,
},
{
"one|two|three|four",
"|",
"",
4,
},
{
"one|\", two\"|three|four",
"|",
"",
4,
},
}
var fieldLenCases = []struct {
input, sep, qual string
expected map[int]int
}{
{
"one,two,three,four",
",",
"",
map[int]int{0: 3, 1: 3, 2: 5, 3: 4},
},
{
"\"one,two\",three,four",
",",
"\"",
map[int]int{0: 9, 1: 5, 2: 4},
},
{
"one",
",",
"",
map[int]int{0: 3},
},
{
"one|two|three|four",
"|",
"",
map[int]int{1: 3, 2: 5, 3: 4, 0: 3},
},
{
"zero,\"onetwothreefour\"",
",",
"\"",
map[int]int{0: 4, 1: 17},
},
{
"zero,\", onetwothreefour\"",
",",
"\"",
map[int]int{0: 4, 1: 19},
},
}
func TestSplitWithQual(t *testing.T) {
for _, tt := range splitCases {
got := SplitWithQual(tt.input, tt.sep, tt.qual)
if len(got) != tt.expected {
t.Fatalf("SplitWithQual(%v) = len(%v) ; want %v", tt.input, len(got), tt.expected)
}
}
}
func TestFieldLengths(t *testing.T) {
for _, tt := range fieldLenCases {
got := FieldLengths(tt.input, tt.sep, tt.qual)
for k := range tt.expected {
if got[k] != tt.expected[k] {
t.Fatalf("FieldLengths(%v) = %v; want %v", tt.input, got, tt.expected)
}
}
}
}