@@ -2,6 +2,7 @@ package parse_test
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"path/filepath"
6
7
"reflect"
7
8
"strings"
@@ -132,6 +133,8 @@ func cloneFile(s StructInfo) StructInfo {
132
133
}
133
134
134
135
func TestFileGood (t * testing.T ) {
136
+ t .Parallel ()
137
+
135
138
s , err := File (filepath .FromSlash ("../internal/test/models/good.go" ))
136
139
assert .NoError (t , err )
137
140
require .Len (t , s , 6 )
@@ -144,6 +147,8 @@ func TestFileGood(t *testing.T) {
144
147
}
145
148
146
149
func TestFileExtra (t * testing.T ) {
150
+ t .Parallel ()
151
+
147
152
s , err := File (filepath .FromSlash ("../internal/test/models/extra.go" ))
148
153
assert .NoError (t , err )
149
154
require .Len (t , s , 2 )
@@ -152,6 +157,8 @@ func TestFileExtra(t *testing.T) {
152
157
}
153
158
154
159
func TestFileBogus (t * testing.T ) {
160
+ t .Parallel ()
161
+
155
162
dir := filepath .FromSlash ("../internal/test/models/bogus/" )
156
163
for file , msg := range map [string ]error {
157
164
"bogus1.go" : errors .New (`reform: Bogus1 has anonymous field BogusType with "reform:" tag, it is not allowed` ),
@@ -169,13 +176,20 @@ func TestFileBogus(t *testing.T) {
169
176
170
177
"bogus_ignore.go" : nil ,
171
178
} {
172
- s , err := File (filepath .Join (dir , file ))
173
- assert .Nil (t , s )
174
- assert .Equal (t , msg , err )
179
+ file , msg := file , msg
180
+ t .Run (file , func (t * testing.T ) {
181
+ t .Parallel ()
182
+
183
+ s , err := File (filepath .Join (dir , file ))
184
+ assert .Nil (t , s )
185
+ assert .Equal (t , msg , err )
186
+ })
175
187
}
176
188
}
177
189
178
190
func TestObjectGood (t * testing.T ) {
191
+ t .Parallel ()
192
+
179
193
s , err := Object (new (models.Person ), "" , "people" )
180
194
assert .NoError (t , err )
181
195
assert .Equal (t , & person , s )
@@ -202,6 +216,8 @@ func TestObjectGood(t *testing.T) {
202
216
}
203
217
204
218
func TestObjectExtra (t * testing.T ) {
219
+ t .Parallel ()
220
+
205
221
s , err := Object (new (models.Extra ), "" , "extra" )
206
222
assert .NoError (t , err )
207
223
assert .Equal (t , & extra , s )
@@ -212,6 +228,8 @@ func TestObjectExtra(t *testing.T) {
212
228
}
213
229
214
230
func TestObjectBogus (t * testing.T ) {
231
+ t .Parallel ()
232
+
215
233
for obj , msg := range map [interface {}]error {
216
234
new (bogus.Bogus1 ): errors .New (`reform: Bogus1 has anonymous field BogusType with "reform:" tag, it is not allowed` ),
217
235
new (bogus.Bogus2 ): errors .New (`reform: Bogus2 has anonymous field bogusType with "reform:" tag, it is not allowed` ),
@@ -228,14 +246,23 @@ func TestObjectBogus(t *testing.T) {
228
246
229
247
// new(bogus.BogusIgnore): do not test,
230
248
} {
231
- s , err := Object (obj , "" , "bogus" )
232
- assert .Nil (t , s )
233
- assert .Equal (t , msg , err )
249
+ obj , msg := obj , msg
250
+ t .Run (fmt .Sprint (obj ), func (t * testing.T ) {
251
+ t .Parallel ()
252
+
253
+ s , err := Object (obj , "" , "bogus" )
254
+ assert .Nil (t , s )
255
+ assert .Equal (t , msg , err )
256
+ })
234
257
}
235
258
}
236
259
237
260
func TestHelpersGood (t * testing.T ) {
261
+ t .Parallel ()
262
+
238
263
t .Run ("person" , func (t * testing.T ) {
264
+ t .Parallel ()
265
+
239
266
assert .Equal (t , strings .TrimSpace (`
240
267
parse.StructInfo{
241
268
Type: "Person",
@@ -265,6 +292,8 @@ parse.StructInfo{
265
292
})
266
293
267
294
t .Run ("project" , func (t * testing.T ) {
295
+ t .Parallel ()
296
+
268
297
assert .Equal (t , strings .TrimSpace (`
269
298
parse.StructInfo{
270
299
Type: "Project",
@@ -290,6 +319,8 @@ parse.StructInfo{
290
319
})
291
320
292
321
t .Run ("personProject" , func (t * testing.T ) {
322
+ t .Parallel ()
323
+
293
324
assert .Equal (t , strings .TrimSpace (`
294
325
parse.StructInfo{
295
326
Type: "PersonProject",
@@ -310,6 +341,8 @@ parse.StructInfo{
310
341
})
311
342
312
343
t .Run ("constraints" , func (t * testing.T ) {
344
+ t .Parallel ()
345
+
313
346
assert .Equal (t , strings .TrimSpace (`
314
347
parse.StructInfo{
315
348
Type: "Constraints",
@@ -331,6 +364,8 @@ parse.StructInfo{
331
364
})
332
365
333
366
t .Run ("idOnly" , func (t * testing.T ) {
367
+ t .Parallel ()
368
+
334
369
assert .Equal (t , strings .TrimSpace (`
335
370
parse.StructInfo{
336
371
Type: "IDOnly",
@@ -350,6 +385,8 @@ parse.StructInfo{
350
385
})
351
386
352
387
t .Run ("legacyPerson" , func (t * testing.T ) {
388
+ t .Parallel ()
389
+
353
390
assert .Equal (t , strings .TrimSpace (`
354
391
parse.StructInfo{
355
392
Type: "LegacyPerson",
@@ -373,7 +410,11 @@ parse.StructInfo{
373
410
}
374
411
375
412
func TestHelpersExtra (t * testing.T ) {
413
+ t .Parallel ()
414
+
376
415
t .Run ("extra" , func (t * testing.T ) {
416
+ t .Parallel ()
417
+
377
418
assert .Equal (t , strings .TrimSpace (`
378
419
parse.StructInfo{
379
420
Type: "Extra",
@@ -420,6 +461,8 @@ parse.StructInfo{
420
461
})
421
462
422
463
t .Run ("notExported" , func (t * testing.T ) {
464
+ t .Parallel ()
465
+
423
466
assert .Equal (t , strings .TrimSpace (`
424
467
parse.StructInfo{
425
468
Type: "notExported",
@@ -439,21 +482,30 @@ parse.StructInfo{
439
482
})
440
483
}
441
484
442
- func TestAssertUpToDate (t * testing.T ) {
443
- AssertUpToDate (& person , new (models.Person ))
485
+ func TestInit (t * testing.T ) {
486
+ t .Parallel ()
487
+
488
+ t .Run ("NotPanics" , func (t * testing.T ) {
489
+ t .Parallel ()
444
490
445
- func () {
446
- defer func () {
447
- expected := `reform:
491
+ p := cloneFile (person )
492
+ assert .Empty (t , p .PKField ().Kind )
493
+ Init (& p , new (models.Person ))
494
+ assert .NotEmpty (t , p .PKField ().Kind )
495
+ })
496
+
497
+ t .Run ("Panics" , func (t * testing.T ) {
498
+ t .Parallel ()
499
+
500
+ expected := `reform:
448
501
Person struct information is not up-to-date.
449
502
Typically this means that Person type definition was changed, but 'reform' command / 'go generate' was not run.
450
503
451
504
`
452
- assert .Equal (t , expected , recover ())
453
- }()
454
-
455
- p := person
456
- p .PKFieldIndex = 1
457
- AssertUpToDate (& p , new (models.Person ))
458
- }()
505
+ assert .PanicsWithValue (t , expected , func () {
506
+ p := cloneFile (person )
507
+ p .PKFieldIndex = 1
508
+ Init (& p , new (models.Person ))
509
+ })
510
+ })
459
511
}
0 commit comments