-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_test.go
359 lines (355 loc) · 9.54 KB
/
set_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
package seltabl
import (
"reflect"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
)
// TestStruct is a test struct
// fields in this struct are used in the tests
type TestStruct struct {
Name string
Age int
Score float64
IntField int
FloatField float64
FloatField2 float32
StringField string
UIntField uint
Uint8Field uint8
Uint16Field uint16
Uint32Field uint32
Uint64Field uint64
Int8Field int8
Int16Field int16
Int32Field int32
Int64Field int64
}
// TestSetStructField tests the SetStructField function
func TestSetStructField(t *testing.T) {
baseModelType := reflect.TypeOf(TestStruct{})
tests := []struct {
name string
structPtr *TestStruct
cellHTML string
structField reflect.StructField
selector SelectorI
wantErr bool
expected interface{}
}{
{
name: "Set string field",
structPtr: &TestStruct{},
structField: baseModelType.Field(0),
cellHTML: `<div id="name">John Doe</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: "John Doe",
},
{
name: "Set uint field",
structPtr: &TestStruct{},
structField: baseModelType.Field(7),
cellHTML: `<div id="uint">123</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: uint(123),
},
{
name: "Set uint8 field",
structPtr: &TestStruct{},
structField: baseModelType.Field(8),
cellHTML: `<div id="uint8">123</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: uint8(123),
},
{
name: "Set uint16 field",
structPtr: &TestStruct{},
structField: baseModelType.Field(9),
cellHTML: `<div id="uint16">123</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: uint16(123),
},
{
name: "Set uint32 field",
structPtr: &TestStruct{},
structField: baseModelType.Field(10),
cellHTML: `<div id="uint32">123</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: uint32(123),
},
{
name: "Set uint64 field",
structPtr: &TestStruct{},
structField: baseModelType.Field(11),
cellHTML: `<div id="uint64">123</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: uint64(123),
},
{
name: "Set int field",
structPtr: &TestStruct{},
structField: baseModelType.Field(1),
cellHTML: `<div id="age">30</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: 30,
},
{
name: "Set float field (float64)",
structPtr: &TestStruct{},
structField: baseModelType.Field(2),
cellHTML: `<div id="score">99.5</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: 99.5,
},
{
name: "Set float field (float32)",
structPtr: &TestStruct{},
structField: baseModelType.Field(5),
cellHTML: `<div id="score">99.5</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: float32(99.5),
},
{
name: "Invalid int value",
structPtr: &TestStruct{},
structField: baseModelType.Field(1),
cellHTML: `<div id="age">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid uint value",
structPtr: &TestStruct{},
structField: baseModelType.Field(7),
cellHTML: `<div id="uint">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid float value",
structPtr: &TestStruct{},
structField: baseModelType.Field(2),
cellHTML: `<div id="score">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid Uint8",
structPtr: &TestStruct{},
structField: baseModelType.Field(8),
cellHTML: `<div id="uint8">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid Float32",
structPtr: &TestStruct{},
structField: baseModelType.Field(5),
cellHTML: `<div id="float">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid Uint16",
structPtr: &TestStruct{},
structField: baseModelType.Field(9),
cellHTML: `<div id="uint16">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid Uint32",
structPtr: &TestStruct{},
structField: baseModelType.Field(10),
cellHTML: `<div id="uint32">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid Uint64",
structPtr: &TestStruct{},
structField: baseModelType.Field(11),
cellHTML: `<div id="uint64">invalid</div>`,
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Set string field",
structPtr: &TestStruct{},
structField: baseModelType.Field(0),
cellHTML: "<div>Test String</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: "Test String",
},
{
name: "Set int field",
structPtr: &TestStruct{},
structField: baseModelType.Field(1),
cellHTML: "<div>123</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: 123,
},
{
name: "Set float field",
structPtr: &TestStruct{},
structField: baseModelType.Field(4),
cellHTML: "<div>123.45</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: 123.45,
},
{
name: "set float32 field",
structPtr: &TestStruct{},
structField: baseModelType.Field(6),
cellHTML: "<div>1.45</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: float32(1.45),
},
{
name: "Selector not found (attr)",
structPtr: &TestStruct{},
structField: baseModelType.Field(1),
cellHTML: "<div></div>",
selector: selector{control: ctlAttrSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid int value",
structPtr: &TestStruct{},
structField: baseModelType.Field(1),
cellHTML: "<div>invalid</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid float value",
structPtr: &TestStruct{},
structField: baseModelType.Field(2),
cellHTML: "<div>invalid</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid int8 value",
structPtr: &TestStruct{},
cellHTML: "<div>invalid</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid int16 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(13),
cellHTML: "<div>invalid</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid int32 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(14),
cellHTML: "<div>invalid</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Invalid int64 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(15),
cellHTML: "<div>invalid</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: true,
expected: nil,
},
{
name: "Valid int8 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(12),
cellHTML: "<div>123</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: int8(123),
},
{
name: "Valid int16 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(13),
cellHTML: "<div>123</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: int16(123),
},
{
name: "Valid int32 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(14),
cellHTML: "<div>456</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: int32(456),
},
{
name: "Valid int64 value",
structPtr: &TestStruct{},
structField: baseModelType.Field(15),
cellHTML: "<div>25565</div>",
selector: selector{control: ctlInnerTextSelector},
wantErr: false,
expected: int64(25565),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt := tt
t.Parallel()
doc, err := goquery.NewDocumentFromReader(
strings.NewReader(tt.cellHTML),
)
if err != nil {
t.Fatalf("failed to create document: %v", err)
}
cellValue := doc.Find("div")
err = SetStructField(
tt.structPtr,
tt.structField,
cellValue,
tt.selector,
)
if (err != nil) != tt.wantErr {
t.Errorf(
"SetStructField() error = %v, wantErr %v",
err,
tt.wantErr,
)
return
}
})
}
}