@@ -190,44 +190,40 @@ func TestToMultipleFields(t *testing.T) {
190
190
}
191
191
192
192
func TestBindJSON (t * testing.T ) {
193
- assert := assert .New (t )
194
- testBindOkay (assert , strings .NewReader (userJSON ), nil , MIMEApplicationJSON )
195
- testBindOkay (assert , strings .NewReader (userJSON ), dummyQuery , MIMEApplicationJSON )
196
- testBindArrayOkay (assert , strings .NewReader (usersJSON ), nil , MIMEApplicationJSON )
197
- testBindArrayOkay (assert , strings .NewReader (usersJSON ), dummyQuery , MIMEApplicationJSON )
198
- testBindError (assert , strings .NewReader (invalidContent ), MIMEApplicationJSON , & json.SyntaxError {})
199
- testBindError (assert , strings .NewReader (userJSONInvalidType ), MIMEApplicationJSON , & json.UnmarshalTypeError {})
193
+ testBindOkay (t , strings .NewReader (userJSON ), nil , MIMEApplicationJSON )
194
+ testBindOkay (t , strings .NewReader (userJSON ), dummyQuery , MIMEApplicationJSON )
195
+ testBindArrayOkay (t , strings .NewReader (usersJSON ), nil , MIMEApplicationJSON )
196
+ testBindArrayOkay (t , strings .NewReader (usersJSON ), dummyQuery , MIMEApplicationJSON )
197
+ testBindError (t , strings .NewReader (invalidContent ), MIMEApplicationJSON , & json.SyntaxError {})
198
+ testBindError (t , strings .NewReader (userJSONInvalidType ), MIMEApplicationJSON , & json.UnmarshalTypeError {})
200
199
}
201
200
202
201
func TestBindXML (t * testing.T ) {
203
- assert := assert .New (t )
204
-
205
- testBindOkay (assert , strings .NewReader (userXML ), nil , MIMEApplicationXML )
206
- testBindOkay (assert , strings .NewReader (userXML ), dummyQuery , MIMEApplicationXML )
207
- testBindArrayOkay (assert , strings .NewReader (userXML ), nil , MIMEApplicationXML )
208
- testBindArrayOkay (assert , strings .NewReader (userXML ), dummyQuery , MIMEApplicationXML )
209
- testBindError (assert , strings .NewReader (invalidContent ), MIMEApplicationXML , errors .New ("" ))
210
- testBindError (assert , strings .NewReader (userXMLConvertNumberError ), MIMEApplicationXML , & strconv.NumError {})
211
- testBindError (assert , strings .NewReader (userXMLUnsupportedTypeError ), MIMEApplicationXML , & xml.SyntaxError {})
212
- testBindOkay (assert , strings .NewReader (userXML ), nil , MIMETextXML )
213
- testBindOkay (assert , strings .NewReader (userXML ), dummyQuery , MIMETextXML )
214
- testBindError (assert , strings .NewReader (invalidContent ), MIMETextXML , errors .New ("" ))
215
- testBindError (assert , strings .NewReader (userXMLConvertNumberError ), MIMETextXML , & strconv.NumError {})
216
- testBindError (assert , strings .NewReader (userXMLUnsupportedTypeError ), MIMETextXML , & xml.SyntaxError {})
202
+ testBindOkay (t , strings .NewReader (userXML ), nil , MIMEApplicationXML )
203
+ testBindOkay (t , strings .NewReader (userXML ), dummyQuery , MIMEApplicationXML )
204
+ testBindArrayOkay (t , strings .NewReader (userXML ), nil , MIMEApplicationXML )
205
+ testBindArrayOkay (t , strings .NewReader (userXML ), dummyQuery , MIMEApplicationXML )
206
+ testBindError (t , strings .NewReader (invalidContent ), MIMEApplicationXML , errors .New ("" ))
207
+ testBindError (t , strings .NewReader (userXMLConvertNumberError ), MIMEApplicationXML , & strconv.NumError {})
208
+ testBindError (t , strings .NewReader (userXMLUnsupportedTypeError ), MIMEApplicationXML , & xml.SyntaxError {})
209
+ testBindOkay (t , strings .NewReader (userXML ), nil , MIMETextXML )
210
+ testBindOkay (t , strings .NewReader (userXML ), dummyQuery , MIMETextXML )
211
+ testBindError (t , strings .NewReader (invalidContent ), MIMETextXML , errors .New ("" ))
212
+ testBindError (t , strings .NewReader (userXMLConvertNumberError ), MIMETextXML , & strconv.NumError {})
213
+ testBindError (t , strings .NewReader (userXMLUnsupportedTypeError ), MIMETextXML , & xml.SyntaxError {})
217
214
}
218
215
219
216
func TestBindForm (t * testing.T ) {
220
- assert := assert .New (t )
221
217
222
- testBindOkay (assert , strings .NewReader (userForm ), nil , MIMEApplicationForm )
223
- testBindOkay (assert , strings .NewReader (userForm ), dummyQuery , MIMEApplicationForm )
218
+ testBindOkay (t , strings .NewReader (userForm ), nil , MIMEApplicationForm )
219
+ testBindOkay (t , strings .NewReader (userForm ), dummyQuery , MIMEApplicationForm )
224
220
e := New ()
225
221
req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (userForm ))
226
222
rec := httptest .NewRecorder ()
227
223
c := e .NewContext (req , rec )
228
224
req .Header .Set (HeaderContentType , MIMEApplicationForm )
229
225
err := c .Bind (& []struct { Field string }{})
230
- assert .Error (err )
226
+ assert .Error (t , err )
231
227
}
232
228
233
229
func TestBindQueryParams (t * testing.T ) {
@@ -317,14 +313,13 @@ func TestBindUnmarshalParam(t *testing.T) {
317
313
err := c .Bind (& result )
318
314
ts := Timestamp (time .Date (2016 , 12 , 6 , 19 , 9 , 5 , 0 , time .UTC ))
319
315
320
- assert := assert .New (t )
321
- if assert .NoError (err ) {
316
+ if assert .NoError (t , err ) {
322
317
// assert.Equal( Timestamp(reflect.TypeOf(&Timestamp{}), time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), result.T)
323
- assert .Equal (ts , result .T )
324
- assert .Equal (StringArray ([]string {"one" , "two" , "three" }), result .SA )
325
- assert .Equal ([]Timestamp {ts , ts }, result .TA )
326
- assert .Equal (Struct {"" }, result .ST ) // child struct does not have a field with matching tag
327
- assert .Equal ("baz" , result .StWithTag .Foo ) // child struct has field with matching tag
318
+ assert .Equal (t , ts , result .T )
319
+ assert .Equal (t , StringArray ([]string {"one" , "two" , "three" }), result .SA )
320
+ assert .Equal (t , []Timestamp {ts , ts }, result .TA )
321
+ assert .Equal (t , Struct {"" }, result .ST ) // child struct does not have a field with matching tag
322
+ assert .Equal (t , "baz" , result .StWithTag .Foo ) // child struct has field with matching tag
328
323
}
329
324
}
330
325
@@ -426,38 +421,35 @@ func TestBindMultipartForm(t *testing.T) {
426
421
mw .Close ()
427
422
body := bodyBuffer .Bytes ()
428
423
429
- assert := assert .New (t )
430
- testBindOkay (assert , bytes .NewReader (body ), nil , mw .FormDataContentType ())
431
- testBindOkay (assert , bytes .NewReader (body ), dummyQuery , mw .FormDataContentType ())
424
+ testBindOkay (t , bytes .NewReader (body ), nil , mw .FormDataContentType ())
425
+ testBindOkay (t , bytes .NewReader (body ), dummyQuery , mw .FormDataContentType ())
432
426
}
433
427
434
428
func TestBindUnsupportedMediaType (t * testing.T ) {
435
- assert := assert .New (t )
436
- testBindError (assert , strings .NewReader (invalidContent ), MIMEApplicationJSON , & json.SyntaxError {})
429
+ testBindError (t , strings .NewReader (invalidContent ), MIMEApplicationJSON , & json.SyntaxError {})
437
430
}
438
431
439
432
func TestBindbindData (t * testing.T ) {
440
- a := assert .New (t )
441
433
ts := new (bindTestStruct )
442
434
b := new (DefaultBinder )
443
435
err := b .bindData (ts , values , "form" )
444
- a .NoError (err )
445
-
446
- a .Equal (0 , ts .I )
447
- a .Equal (int8 (0 ), ts .I8 )
448
- a .Equal (int16 (0 ), ts .I16 )
449
- a .Equal (int32 (0 ), ts .I32 )
450
- a .Equal (int64 (0 ), ts .I64 )
451
- a .Equal (uint (0 ), ts .UI )
452
- a .Equal (uint8 (0 ), ts .UI8 )
453
- a .Equal (uint16 (0 ), ts .UI16 )
454
- a .Equal (uint32 (0 ), ts .UI32 )
455
- a .Equal (uint64 (0 ), ts .UI64 )
456
- a .Equal (false , ts .B )
457
- a .Equal (float32 (0 ), ts .F32 )
458
- a .Equal (float64 (0 ), ts .F64 )
459
- a .Equal ("" , ts .S )
460
- a .Equal ("" , ts .cantSet )
436
+ assert .NoError (t , err )
437
+
438
+ assert .Equal (t , 0 , ts .I )
439
+ assert .Equal (t , int8 (0 ), ts .I8 )
440
+ assert .Equal (t , int16 (0 ), ts .I16 )
441
+ assert .Equal (t , int32 (0 ), ts .I32 )
442
+ assert .Equal (t , int64 (0 ), ts .I64 )
443
+ assert .Equal (t , uint (0 ), ts .UI )
444
+ assert .Equal (t , uint8 (0 ), ts .UI8 )
445
+ assert .Equal (t , uint16 (0 ), ts .UI16 )
446
+ assert .Equal (t , uint32 (0 ), ts .UI32 )
447
+ assert .Equal (t , uint64 (0 ), ts .UI64 )
448
+ assert .Equal (t , false , ts .B )
449
+ assert .Equal (t , float32 (0 ), ts .F32 )
450
+ assert .Equal (t , float64 (0 ), ts .F64 )
451
+ assert .Equal (t , "" , ts .S )
452
+ assert .Equal (t , "" , ts .cantSet )
461
453
}
462
454
463
455
func TestBindParam (t * testing.T ) {
@@ -528,7 +520,6 @@ func TestBindUnmarshalTypeError(t *testing.T) {
528
520
}
529
521
530
522
func TestBindSetWithProperType (t * testing.T ) {
531
- assert := assert .New (t )
532
523
ts := new (bindTestStruct )
533
524
typ := reflect .TypeOf (ts ).Elem ()
534
525
val := reflect .ValueOf (ts ).Elem ()
@@ -543,96 +534,94 @@ func TestBindSetWithProperType(t *testing.T) {
543
534
}
544
535
val := values [typeField .Name ][0 ]
545
536
err := setWithProperType (typeField .Type .Kind (), val , structField )
546
- assert .NoError (err )
537
+ assert .NoError (t , err )
547
538
}
548
- assertBindTestStruct (assert , ts )
539
+ assertBindTestStruct (t , ts )
549
540
550
541
type foo struct {
551
542
Bar bytes.Buffer
552
543
}
553
544
v := & foo {}
554
545
typ = reflect .TypeOf (v ).Elem ()
555
546
val = reflect .ValueOf (v ).Elem ()
556
- assert .Error (setWithProperType (typ .Field (0 ).Type .Kind (), "5" , val .Field (0 )))
547
+ assert .Error (t , setWithProperType (typ .Field (0 ).Type .Kind (), "5" , val .Field (0 )))
557
548
}
558
549
559
550
func TestBindSetFields (t * testing.T ) {
560
- assert := assert .New (t )
561
551
562
552
ts := new (bindTestStruct )
563
553
val := reflect .ValueOf (ts ).Elem ()
564
554
// Int
565
- if assert .NoError (setIntField ("5" , 0 , val .FieldByName ("I" ))) {
566
- assert .Equal (5 , ts .I )
555
+ if assert .NoError (t , setIntField ("5" , 0 , val .FieldByName ("I" ))) {
556
+ assert .Equal (t , 5 , ts .I )
567
557
}
568
- if assert .NoError (setIntField ("" , 0 , val .FieldByName ("I" ))) {
569
- assert .Equal (0 , ts .I )
558
+ if assert .NoError (t , setIntField ("" , 0 , val .FieldByName ("I" ))) {
559
+ assert .Equal (t , 0 , ts .I )
570
560
}
571
561
572
562
// Uint
573
- if assert .NoError (setUintField ("10" , 0 , val .FieldByName ("UI" ))) {
574
- assert .Equal (uint (10 ), ts .UI )
563
+ if assert .NoError (t , setUintField ("10" , 0 , val .FieldByName ("UI" ))) {
564
+ assert .Equal (t , uint (10 ), ts .UI )
575
565
}
576
- if assert .NoError (setUintField ("" , 0 , val .FieldByName ("UI" ))) {
577
- assert .Equal (uint (0 ), ts .UI )
566
+ if assert .NoError (t , setUintField ("" , 0 , val .FieldByName ("UI" ))) {
567
+ assert .Equal (t , uint (0 ), ts .UI )
578
568
}
579
569
580
570
// Float
581
- if assert .NoError (setFloatField ("15.5" , 0 , val .FieldByName ("F32" ))) {
582
- assert .Equal (float32 (15.5 ), ts .F32 )
571
+ if assert .NoError (t , setFloatField ("15.5" , 0 , val .FieldByName ("F32" ))) {
572
+ assert .Equal (t , float32 (15.5 ), ts .F32 )
583
573
}
584
- if assert .NoError (setFloatField ("" , 0 , val .FieldByName ("F32" ))) {
585
- assert .Equal (float32 (0.0 ), ts .F32 )
574
+ if assert .NoError (t , setFloatField ("" , 0 , val .FieldByName ("F32" ))) {
575
+ assert .Equal (t , float32 (0.0 ), ts .F32 )
586
576
}
587
577
588
578
// Bool
589
- if assert .NoError (setBoolField ("true" , val .FieldByName ("B" ))) {
590
- assert .Equal (true , ts .B )
579
+ if assert .NoError (t , setBoolField ("true" , val .FieldByName ("B" ))) {
580
+ assert .Equal (t , true , ts .B )
591
581
}
592
- if assert .NoError (setBoolField ("" , val .FieldByName ("B" ))) {
593
- assert .Equal (false , ts .B )
582
+ if assert .NoError (t , setBoolField ("" , val .FieldByName ("B" ))) {
583
+ assert .Equal (t , false , ts .B )
594
584
}
595
585
596
586
ok , err := unmarshalFieldNonPtr ("2016-12-06T19:09:05Z" , val .FieldByName ("T" ))
597
- if assert .NoError (err ) {
598
- assert .Equal (ok , true )
599
- assert .Equal (Timestamp (time .Date (2016 , 12 , 6 , 19 , 9 , 5 , 0 , time .UTC )), ts .T )
587
+ if assert .NoError (t , err ) {
588
+ assert .Equal (t , ok , true )
589
+ assert .Equal (t , Timestamp (time .Date (2016 , 12 , 6 , 19 , 9 , 5 , 0 , time .UTC )), ts .T )
600
590
}
601
591
}
602
592
603
593
func BenchmarkBindbindDataWithTags (b * testing.B ) {
604
594
b .ReportAllocs ()
605
- assert := assert .New (b )
606
595
ts := new (bindTestStructWithTags )
607
596
binder := new (DefaultBinder )
608
597
var err error
609
598
b .ResetTimer ()
610
599
for i := 0 ; i < b .N ; i ++ {
611
600
err = binder .bindData (ts , values , "form" )
612
601
}
613
- assert .NoError (err )
614
- assertBindTestStruct (assert , (* bindTestStruct )(ts ))
602
+ assert .NoError (b , err )
603
+ assertBindTestStruct (b , (* bindTestStruct )(ts ))
615
604
}
616
605
617
- func assertBindTestStruct (a * assert. Assertions , ts * bindTestStruct ) {
618
- a .Equal (0 , ts .I )
619
- a .Equal (int8 (8 ), ts .I8 )
620
- a .Equal (int16 (16 ), ts .I16 )
621
- a .Equal (int32 (32 ), ts .I32 )
622
- a .Equal (int64 (64 ), ts .I64 )
623
- a .Equal (uint (0 ), ts .UI )
624
- a .Equal (uint8 (8 ), ts .UI8 )
625
- a .Equal (uint16 (16 ), ts .UI16 )
626
- a .Equal (uint32 (32 ), ts .UI32 )
627
- a .Equal (uint64 (64 ), ts .UI64 )
628
- a .Equal (true , ts .B )
629
- a .Equal (float32 (32.5 ), ts .F32 )
630
- a .Equal (float64 (64.5 ), ts .F64 )
631
- a .Equal ("test" , ts .S )
632
- a .Equal ("" , ts .GetCantSet ())
606
+ func assertBindTestStruct (tb testing. TB , ts * bindTestStruct ) {
607
+ assert .Equal (tb , 0 , ts .I )
608
+ assert .Equal (tb , int8 (8 ), ts .I8 )
609
+ assert .Equal (tb , int16 (16 ), ts .I16 )
610
+ assert .Equal (tb , int32 (32 ), ts .I32 )
611
+ assert .Equal (tb , int64 (64 ), ts .I64 )
612
+ assert .Equal (tb , uint (0 ), ts .UI )
613
+ assert .Equal (tb , uint8 (8 ), ts .UI8 )
614
+ assert .Equal (tb , uint16 (16 ), ts .UI16 )
615
+ assert .Equal (tb , uint32 (32 ), ts .UI32 )
616
+ assert .Equal (tb , uint64 (64 ), ts .UI64 )
617
+ assert .Equal (tb , true , ts .B )
618
+ assert .Equal (tb , float32 (32.5 ), ts .F32 )
619
+ assert .Equal (tb , float64 (64.5 ), ts .F64 )
620
+ assert .Equal (tb , "test" , ts .S )
621
+ assert .Equal (tb , "" , ts .GetCantSet ())
633
622
}
634
623
635
- func testBindOkay (assert * assert. Assertions , r io.Reader , query url.Values , ctype string ) {
624
+ func testBindOkay (t * testing. T , r io.Reader , query url.Values , ctype string ) {
636
625
e := New ()
637
626
path := "/"
638
627
if len (query ) > 0 {
@@ -644,13 +633,13 @@ func testBindOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctyp
644
633
req .Header .Set (HeaderContentType , ctype )
645
634
u := new (user )
646
635
err := c .Bind (u )
647
- if assert .NoError ( err ) {
648
- assert .Equal (1 , u .ID )
649
- assert .Equal ("Jon Snow" , u .Name )
636
+ if assert .Equal ( t , nil , err ) {
637
+ assert .Equal (t , 1 , u .ID )
638
+ assert .Equal (t , "Jon Snow" , u .Name )
650
639
}
651
640
}
652
641
653
- func testBindArrayOkay (assert * assert. Assertions , r io.Reader , query url.Values , ctype string ) {
642
+ func testBindArrayOkay (t * testing. T , r io.Reader , query url.Values , ctype string ) {
654
643
e := New ()
655
644
path := "/"
656
645
if len (query ) > 0 {
@@ -662,14 +651,14 @@ func testBindArrayOkay(assert *assert.Assertions, r io.Reader, query url.Values,
662
651
req .Header .Set (HeaderContentType , ctype )
663
652
u := []user {}
664
653
err := c .Bind (& u )
665
- if assert .NoError (err ) {
666
- assert .Equal (1 , len (u ))
667
- assert .Equal (1 , u [0 ].ID )
668
- assert .Equal ("Jon Snow" , u [0 ].Name )
654
+ if assert .NoError (t , err ) {
655
+ assert .Equal (t , 1 , len (u ))
656
+ assert .Equal (t , 1 , u [0 ].ID )
657
+ assert .Equal (t , "Jon Snow" , u [0 ].Name )
669
658
}
670
659
}
671
660
672
- func testBindError (assert * assert. Assertions , r io.Reader , ctype string , expectedInternal error ) {
661
+ func testBindError (t * testing. T , r io.Reader , ctype string , expectedInternal error ) {
673
662
e := New ()
674
663
req := httptest .NewRequest (http .MethodPost , "/" , r )
675
664
rec := httptest .NewRecorder ()
@@ -681,14 +670,14 @@ func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expecte
681
670
switch {
682
671
case strings .HasPrefix (ctype , MIMEApplicationJSON ), strings .HasPrefix (ctype , MIMEApplicationXML ), strings .HasPrefix (ctype , MIMETextXML ),
683
672
strings .HasPrefix (ctype , MIMEApplicationForm ), strings .HasPrefix (ctype , MIMEMultipartForm ):
684
- if assert .IsType (new (HTTPError ), err ) {
685
- assert .Equal (http .StatusBadRequest , err .(* HTTPError ).Code )
686
- assert .IsType (expectedInternal , err .(* HTTPError ).Internal )
673
+ if assert .IsType (t , new (HTTPError ), err ) {
674
+ assert .Equal (t , http .StatusBadRequest , err .(* HTTPError ).Code )
675
+ assert .IsType (t , expectedInternal , err .(* HTTPError ).Internal )
687
676
}
688
677
default :
689
- if assert .IsType (new (HTTPError ), err ) {
690
- assert .Equal (ErrUnsupportedMediaType , err )
691
- assert .IsType (expectedInternal , err .(* HTTPError ).Internal )
678
+ if assert .IsType (t , new (HTTPError ), err ) {
679
+ assert .Equal (t , ErrUnsupportedMediaType , err )
680
+ assert .IsType (t , expectedInternal , err .(* HTTPError ).Internal )
692
681
}
693
682
}
694
683
}
0 commit comments