-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
285 lines (271 loc) · 6.67 KB
/
parse_test.go
File metadata and controls
285 lines (271 loc) · 6.67 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package fmt_test
import (
"testing"
. "github.com/tinywasm/fmt"
)
func TestParseKeyValue(t *testing.T) {
testCases := []struct {
name string
input string
delimiter string
wantValue string
wantErrText string
}{
{
name: "Basic key-value with default delimiter",
input: "name:John",
delimiter: "",
wantValue: "John",
wantErrText: "",
},
{
name: "No delimiter in string",
input: "invalid-string",
delimiter: "",
wantValue: "",
wantErrText: Translate("format", "invalid", "delimiter", "not", "found").String(),
},
{
name: "Custom delimiter",
input: "age=30",
delimiter: "=",
wantValue: "30",
wantErrText: "",
},
{
name: "Value contains delimiter",
input: "address:123 Main St:Apt 4",
delimiter: "",
wantValue: "123 Main St:Apt 4",
wantErrText: "",
},
{
name: "Empty input",
input: "",
delimiter: "",
wantValue: "",
wantErrText: Translate("format", "invalid", "delimiter", "not", "found").String(),
},
{
name: "Only delimiter",
input: ":",
delimiter: "",
wantValue: "",
wantErrText: "",
},
{
name: "Multi-character delimiter",
input: "key=>value",
delimiter: "=>",
wantValue: "value",
wantErrText: "",
},
{
name: "Missing custom delimiter",
input: "key:value",
delimiter: "=",
wantValue: "",
wantErrText: Translate("format", "invalid", "delimiter", "not", "found").String(),
},
{
name: "Empty delimiter uses default",
input: "name:John",
delimiter: "",
wantValue: "John",
wantErrText: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var delimiters []string
if tc.delimiter != "" {
delimiters = append(delimiters, tc.delimiter)
}
gotValue, gotErr := Convert(tc.input).ExtractValue(delimiters...)
if gotValue != tc.wantValue {
t.Errorf("ParseKeyValue() value = %q, want %q", gotValue, tc.wantValue)
}
// Check error
if tc.wantErrText == "" {
if gotErr != nil {
t.Errorf("ParseKeyValue() error = %v, want nil", gotErr)
}
} else {
if gotErr == nil {
t.Errorf("ParseKeyValue() error = nil, want error containing %q", tc.wantErrText)
} else if !Contains(gotErr.Error(), tc.wantErrText) {
t.Errorf("ParseKeyValue() error = %v, want error containing %q", gotErr, tc.wantErrText)
}
}
})
}
}
func TestTagValue(t *testing.T) {
testCases := []struct {
name string
input string
key string
wantValue string
wantFound bool
}{
{
name: "Basic tag value extraction",
input: `json:"name"`,
key: "json",
wantValue: "name",
wantFound: true,
},
{
name: "Multiple tags with target in middle",
input: `json:"name" Label:"Nombre" xml:"nm"`,
key: "Label",
wantValue: "Nombre",
wantFound: true,
},
{
name: "Multiple tags with target at end",
input: `json:"name" Label:"Nombre" xml:"nm"`,
key: "xml",
wantValue: "nm",
wantFound: true,
},
{
name: "Multiple tags with target at start",
input: `json:"name" Label:"Nombre" xml:"nm"`,
key: "json",
wantValue: "name",
wantFound: true,
},
{
name: "Key not found",
input: `json:"name" Label:"Nombre"`,
key: "xml",
wantValue: "",
wantFound: false,
},
{
name: "Empty input",
input: "",
key: "json",
wantValue: "",
wantFound: false,
},
{
name: "No quotes in value",
input: `json:name`,
key: "json",
wantValue: "name",
wantFound: true,
},
{
name: "Extra spaces between tags",
input: `json:"name" Label:"Nombre" xml:"nm"`,
key: "Label",
wantValue: "Nombre",
wantFound: true,
},
{
name: "Tag without colon",
input: `json:"name" invalid Label:"Nombre"`,
key: "Label",
wantValue: "Nombre",
wantFound: true,
},
{
name: "Complex struct tag",
input: `json:"user_name,omitempty" validate:"required,min=3" db:"username"`,
key: "validate",
wantValue: "required,min=3",
wantFound: true,
},
{
name: "Single tag",
input: `json:"name"`,
key: "json",
wantValue: "name",
wantFound: true,
},
{
name: "Empty quotes",
input: `json:""`,
key: "json",
wantValue: "",
wantFound: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotValue, gotFound := Convert(tc.input).TagValue(tc.key)
if gotValue != tc.wantValue {
t.Errorf("TagValue() value = %q, want %q", gotValue, tc.wantValue)
}
if gotFound != tc.wantFound {
t.Errorf("TagValue() found = %v, want %v", gotFound, tc.wantFound)
}
})
}
}
func TestTagPairs(t *testing.T) {
testCases := []struct {
name string
input string
key string
wantPairs []KeyValue
}{
{
name: "Basic options extraction",
input: `options:"key1:text1,key2:text2"`,
key: "options",
wantPairs: []KeyValue{{Key: "key1", Value: "text1"}, {Key: "key2", Value: "text2"}},
},
{
name: "Options with other tags",
input: `json:"name" options:"key1:text1,key2:text2" validate:"required"`,
key: "options",
wantPairs: []KeyValue{{Key: "key1", Value: "text1"}, {Key: "key2", Value: "text2"}},
},
{
name: "Single option",
input: `options:"key1:text1"`,
key: "options",
wantPairs: []KeyValue{{Key: "key1", Value: "text1"}},
},
{
name: "Empty options tag",
input: `options:""`,
key: "options",
wantPairs: nil,
},
{
name: "Key not found",
input: `json:"name"`,
key: "options",
wantPairs: nil,
},
{
name: "Malfomed pairs (missing colon)",
input: `options:"key1,key2:text2"`,
key: "options",
wantPairs: []KeyValue{{Key: "key2", Value: "text2"}},
},
{
name: "Multiple pairs without spaces (standard format)",
input: `options:"key1:text1,key2:text2"`,
key: "options",
wantPairs: []KeyValue{{Key: "key1", Value: "text1"}, {Key: "key2", Value: "text2"}},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotPairs := Convert(tc.input).TagPairs(tc.key)
if len(gotPairs) != len(tc.wantPairs) {
t.Fatalf("TagPairs() length = %d, want %d", len(gotPairs), len(tc.wantPairs))
}
for i := range gotPairs {
if gotPairs[i].Key != tc.wantPairs[i].Key || gotPairs[i].Value != tc.wantPairs[i].Value {
t.Errorf("TagPairs()[%d] = %v, want %v", i, gotPairs[i], tc.wantPairs[i])
}
}
})
}
}