errors_test.go
This file
Path
errors_test.go
106 lines · 3.1 KiB · Go
Last changed by
add internal package which includes json package (#1504)田欧before adoption72db8ac
History of this fileTake it
The walk stopped after 200 commits: lines older than 7b50818 point at it.
style: remove optional return (#1036)田欧before adoption7b50818
lines 1–10why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "errors"9 "testing"10add internal package which includes json package (#1504)田欧before adoption72db8ac
line 11why, one step back11 "github.com/gin-gonic/gin/internal/json"style: remove optional return (#1036)田欧before adoption7b50818
lines 12–21why, one step back12 "github.com/stretchr/testify/assert"13)1415func TestError(t *testing.T) {16 baseError := errors.New("test error")17 err := &Error{18 Err: baseError,19 Type: ErrorTypePrivate,20 }21 assert.Equal(t, err.Error(), baseError.Error())update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 22why, one step back22 assert.Equal(t, H{"error": baseError.Error()}, err.JSON())style: remove optional return (#1036)田欧before adoption7b50818
lines 23–24why, one step back2324 assert.Equal(t, err.SetType(ErrorTypePublic), err)update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 25why, one step back25 assert.Equal(t, ErrorTypePublic, err.Type)style: remove optional return (#1036)田欧before adoption7b50818
lines 26–27why, one step back2627 assert.Equal(t, err.SetMeta("some data"), err)update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
lines 28–29why, one step back28 assert.Equal(t, "some data", err.Meta)29 assert.Equal(t, H{style: remove optional return (#1036)田欧before adoption7b50818
lines 30–31why, one step back30 "error": baseError.Error(),31 "meta": "some data",update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 32why, one step back32 }, err.JSON())style: remove optional return (#1036)田欧before adoption7b50818
lines 33–40why, one step back3334 jsonBytes, _ := json.Marshal(err)35 assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))3637 err.SetMeta(H{38 "status": "200",39 "data": "some data",40 })update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 41why, one step back41 assert.Equal(t, H{style: remove optional return (#1036)田欧before adoption7b50818
lines 42–44why, one step back42 "error": baseError.Error(),43 "status": "200",44 "data": "some data",update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 45why, one step back45 }, err.JSON())style: remove optional return (#1036)田欧before adoption7b50818
lines 46–51why, one step back4647 err.SetMeta(H{48 "error": "custom error",49 "status": "200",50 "data": "some data",51 })update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 52why, one step back52 assert.Equal(t, H{style: remove optional return (#1036)田欧before adoption7b50818
lines 53–55why, one step back53 "error": "custom error",54 "status": "200",55 "data": "some data",update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125
line 56why, one step back56 }, err.JSON())style: remove optional return (#1036)田欧before adoption7b50818
lines 57–62why, one step back5758 type customError struct {59 status string60 data string61 }62 err.SetMeta(customError{status: "200", data: "other data"})refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
line 63why, one step back63 assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())style: remove optional return (#1036)田欧before adoption7b50818
lines 64–73why, one step back64}6566func TestErrorSlice(t *testing.T) {67 errs := errorMsgs{68 {Err: errors.New("first"), Type: ErrorTypePrivate},69 {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},70 {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},71 }7273 assert.Equal(t, errs, errs.ByType(ErrorTypeAny))refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
lines 74–78why, one step back74 assert.Equal(t, "third", errs.Last().Error())75 assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())76 assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())77 assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())78 assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())style: remove optional return (#1036)田欧before adoption7b50818
lines 79–81why, one step back79 assert.Empty(t, errs.ByType(ErrorTypeBind))80 assert.Empty(t, errs.ByType(ErrorTypeBind).String())81refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
line 82why, one step back82 assert.Equal(t, `Error #01: firststyle: remove optional return (#1036)田欧before adoption7b50818
lines 83–86why, one step back83Error #02: second84 Meta: some data85Error #03: third86 Meta: map[status:400]refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
lines 87–88why, one step back87`, errs.String())88 assert.Equal(t, []interface{}{style: remove optional return (#1036)田欧before adoption7b50818
lines 89–91why, one step back89 H{"error": "first"},90 H{"error": "second", "meta": "some data"},91 H{"error": "third", "status": "400"},refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
line 92why, one step back92 }, errs.JSON())style: remove optional return (#1036)田欧before adoption7b50818
lines 93–97why, one step back93 jsonBytes, _ := json.Marshal(errs)94 assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))95 errs = errorMsgs{96 {Err: errors.New("first"), Type: ErrorTypePrivate},97 }refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
line 98why, one step back98 assert.Equal(t, H{"error": "first"}, errs.JSON())style: remove optional return (#1036)田欧before adoption7b50818
line 99why, one step back99 jsonBytes, _ = json.Marshal(errs)refactor(test): unify assert.Equal usage (#1054)Eason Linbefore adoption81007d2
line 100why, one step back100 assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))style: remove optional return (#1036)田欧before adoption7b50818
lines 101–106why, one step back101102 errs = errorMsgs{}103 assert.Nil(t, errs.Last())104 assert.Nil(t, errs.JSON())105 assert.Empty(t, errs.String())106}