-
Notifications
You must be signed in to change notification settings - Fork 134
/
family_parser_test.go
179 lines (175 loc) · 3.4 KB
/
family_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
172
173
174
175
176
177
178
179
package text
import (
"testing"
"golang.org/x/exp/slices"
)
func TestParser(t *testing.T) {
type scenario struct {
variantName string
input string
}
type testcase struct {
name string
inputs []scenario
expected []string
shouldErr bool
}
for _, tc := range []testcase{
{
name: "empty",
inputs: []scenario{
{
variantName: "",
},
},
shouldErr: true,
},
{
name: "comma failure",
inputs: []scenario{
{
variantName: "bare single",
input: ",",
},
{
variantName: "bare multiple",
input: ",, ,,",
},
},
shouldErr: true,
},
{
name: "comma success",
inputs: []scenario{
{
variantName: "squote",
input: "','",
},
{
variantName: "dquote",
input: `","`,
},
},
expected: []string{","},
},
{
name: "comma success multiple",
inputs: []scenario{
{
variantName: "squote",
input: "',,', ',,'",
},
{
variantName: "dquote",
input: `",,", ",,"`,
},
},
expected: []string{",,", ",,"},
},
{
name: "backslashes",
inputs: []scenario{
{
variantName: "bare",
input: `\font\\`,
},
{
variantName: "dquote",
input: `"\\font\\\\"`,
},
{
variantName: "squote",
input: `'\\font\\\\'`,
},
},
expected: []string{`\font\\`},
},
{
name: "invalid backslashes",
inputs: []scenario{
{
variantName: "dquote",
input: `"\\""`,
},
{
variantName: "squote",
input: `'\\''`,
},
},
shouldErr: true,
},
{
name: "too many quotes",
inputs: []scenario{
{
variantName: "dquote",
input: `"""`,
},
{
variantName: "squote",
input: `'''`,
},
},
shouldErr: true,
},
{
name: "serif serif's serif\"s",
inputs: []scenario{
{
variantName: "bare",
input: `serif, serif's, serif"s`,
},
{
variantName: "squote",
input: `'serif', 'serif\'s', 'serif"s'`,
},
{
variantName: "dquote",
input: `"serif", "serif's", "serif\"s"`,
},
},
expected: []string{"serif", `serif's`, `serif"s`},
},
{
name: "complex list",
inputs: []scenario{
{
variantName: "bare",
input: `Times New Roman, Georgia Common, Helvetica Neue, serif`,
},
{
variantName: "squote",
input: `'Times New Roman', 'Georgia Common', 'Helvetica Neue', 'serif'`,
},
{
variantName: "dquote",
input: `"Times New Roman", "Georgia Common", "Helvetica Neue", "serif"`,
},
{
variantName: "mixed",
input: `Times New Roman, "Georgia Common", 'Helvetica Neue', "serif"`,
},
{
variantName: "mixed with weird spacing",
input: `Times New Roman ,"Georgia Common" , 'Helvetica Neue' ,"serif"`,
},
},
expected: []string{"Times New Roman", "Georgia Common", "Helvetica Neue", "serif"},
},
} {
t.Run(tc.name, func(t *testing.T) {
var p parser
for _, scen := range tc.inputs {
t.Run(scen.variantName, func(t *testing.T) {
actual, err := p.parse(scen.input)
if (err != nil) != tc.shouldErr {
t.Errorf("unexpected error state: %v", err)
}
if !slices.Equal(tc.expected, actual) {
t.Errorf("expected\n%q\ngot\n%q", tc.expected, actual)
}
})
}
})
}
}