Skip to content
Myr

errors_test.go

This file

Path

errors_test.go

106 lines · 3.1 KiB · Go

Last changed by

update assert param(expect, actual) position (#1421)vzbefore adoptiond17a125

History of this file

Take it

1// 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"1011	"github.com/gin-gonic/gin/json"12	"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())22	assert.Equal(t, H{"error": baseError.Error()}, err.JSON())2324	assert.Equal(t, err.SetType(ErrorTypePublic), err)25	assert.Equal(t, ErrorTypePublic, err.Type)2627	assert.Equal(t, err.SetMeta("some data"), err)28	assert.Equal(t, "some data", err.Meta)29	assert.Equal(t, H{30		"error": baseError.Error(),31		"meta":  "some data",32	}, err.JSON())3334	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	})41	assert.Equal(t, H{42		"error":  baseError.Error(),43		"status": "200",44		"data":   "some data",45	}, err.JSON())4647	err.SetMeta(H{48		"error":  "custom error",49		"status": "200",50		"data":   "some data",51	})52	assert.Equal(t, H{53		"error":  "custom error",54		"status": "200",55		"data":   "some data",56	}, err.JSON())5758	type customError struct {59		status string60		data   string61	}62	err.SetMeta(customError{status: "200", data: "other data"})63	assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())64}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))74	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())79	assert.Empty(t, errs.ByType(ErrorTypeBind))80	assert.Empty(t, errs.ByType(ErrorTypeBind).String())8182	assert.Equal(t, `Error #01: first83Error #02: second84     Meta: some data85Error #03: third86     Meta: map[status:400]87`, errs.String())88	assert.Equal(t, []interface{}{89		H{"error": "first"},90		H{"error": "second", "meta": "some data"},91		H{"error": "third", "status": "400"},92	}, errs.JSON())93	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	}98	assert.Equal(t, H{"error": "first"}, errs.JSON())99	jsonBytes, _ = json.Marshal(errs)100	assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))101102	errs = errorMsgs{}103	assert.Nil(t, errs.Last())104	assert.Nil(t, errs.JSON())105	assert.Empty(t, errs.String())106}