Skip to content

Commit 4c44305

Browse files
authored
update tests (#2275)
update tests
1 parent 79221d9 commit 4c44305

File tree

2 files changed

+104
-115
lines changed

2 files changed

+104
-115
lines changed

bind_test.go

+103-114
Original file line numberDiff line numberDiff line change
@@ -190,44 +190,40 @@ func TestToMultipleFields(t *testing.T) {
190190
}
191191

192192
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{})
200199
}
201200

202201
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{})
217214
}
218215

219216
func TestBindForm(t *testing.T) {
220-
assert := assert.New(t)
221217

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)
224220
e := New()
225221
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm))
226222
rec := httptest.NewRecorder()
227223
c := e.NewContext(req, rec)
228224
req.Header.Set(HeaderContentType, MIMEApplicationForm)
229225
err := c.Bind(&[]struct{ Field string }{})
230-
assert.Error(err)
226+
assert.Error(t, err)
231227
}
232228

233229
func TestBindQueryParams(t *testing.T) {
@@ -317,14 +313,13 @@ func TestBindUnmarshalParam(t *testing.T) {
317313
err := c.Bind(&result)
318314
ts := Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC))
319315

320-
assert := assert.New(t)
321-
if assert.NoError(err) {
316+
if assert.NoError(t, err) {
322317
// 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
328323
}
329324
}
330325

@@ -426,38 +421,35 @@ func TestBindMultipartForm(t *testing.T) {
426421
mw.Close()
427422
body := bodyBuffer.Bytes()
428423

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())
432426
}
433427

434428
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{})
437430
}
438431

439432
func TestBindbindData(t *testing.T) {
440-
a := assert.New(t)
441433
ts := new(bindTestStruct)
442434
b := new(DefaultBinder)
443435
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)
461453
}
462454

463455
func TestBindParam(t *testing.T) {
@@ -528,7 +520,6 @@ func TestBindUnmarshalTypeError(t *testing.T) {
528520
}
529521

530522
func TestBindSetWithProperType(t *testing.T) {
531-
assert := assert.New(t)
532523
ts := new(bindTestStruct)
533524
typ := reflect.TypeOf(ts).Elem()
534525
val := reflect.ValueOf(ts).Elem()
@@ -543,96 +534,94 @@ func TestBindSetWithProperType(t *testing.T) {
543534
}
544535
val := values[typeField.Name][0]
545536
err := setWithProperType(typeField.Type.Kind(), val, structField)
546-
assert.NoError(err)
537+
assert.NoError(t, err)
547538
}
548-
assertBindTestStruct(assert, ts)
539+
assertBindTestStruct(t, ts)
549540

550541
type foo struct {
551542
Bar bytes.Buffer
552543
}
553544
v := &foo{}
554545
typ = reflect.TypeOf(v).Elem()
555546
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)))
557548
}
558549

559550
func TestBindSetFields(t *testing.T) {
560-
assert := assert.New(t)
561551

562552
ts := new(bindTestStruct)
563553
val := reflect.ValueOf(ts).Elem()
564554
// 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)
567557
}
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)
570560
}
571561

572562
// 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)
575565
}
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)
578568
}
579569

580570
// 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)
583573
}
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)
586576
}
587577

588578
// 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)
591581
}
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)
594584
}
595585

596586
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)
600590
}
601591
}
602592

603593
func BenchmarkBindbindDataWithTags(b *testing.B) {
604594
b.ReportAllocs()
605-
assert := assert.New(b)
606595
ts := new(bindTestStructWithTags)
607596
binder := new(DefaultBinder)
608597
var err error
609598
b.ResetTimer()
610599
for i := 0; i < b.N; i++ {
611600
err = binder.bindData(ts, values, "form")
612601
}
613-
assert.NoError(err)
614-
assertBindTestStruct(assert, (*bindTestStruct)(ts))
602+
assert.NoError(b, err)
603+
assertBindTestStruct(b, (*bindTestStruct)(ts))
615604
}
616605

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())
633622
}
634623

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) {
636625
e := New()
637626
path := "/"
638627
if len(query) > 0 {
@@ -644,13 +633,13 @@ func testBindOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctyp
644633
req.Header.Set(HeaderContentType, ctype)
645634
u := new(user)
646635
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)
650639
}
651640
}
652641

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) {
654643
e := New()
655644
path := "/"
656645
if len(query) > 0 {
@@ -662,14 +651,14 @@ func testBindArrayOkay(assert *assert.Assertions, r io.Reader, query url.Values,
662651
req.Header.Set(HeaderContentType, ctype)
663652
u := []user{}
664653
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)
669658
}
670659
}
671660

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) {
673662
e := New()
674663
req := httptest.NewRequest(http.MethodPost, "/", r)
675664
rec := httptest.NewRecorder()
@@ -681,14 +670,14 @@ func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expecte
681670
switch {
682671
case strings.HasPrefix(ctype, MIMEApplicationJSON), strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML),
683672
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)
687676
}
688677
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)
692681
}
693682
}
694683
}

context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ type (
181181
// Logger returns the `Logger` instance.
182182
Logger() Logger
183183

184-
// Set the logger
184+
// SetLogger Set the logger
185185
SetLogger(l Logger)
186186

187187
// Echo returns the `Echo` instance.

0 commit comments

Comments
 (0)