github.com/jxskiss/gopkg@v0.17.3/errcode/registry_test.go (about) 1 package errcode 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestRegistry(t *testing.T) { 9 reg := New() 10 got1 := reg.Register(-1024, "oops") 11 got2 := reg.Register(0, "success") 12 got3 := reg.Register(404, "not found") 13 14 assert.Equal(t, "[-1024] oops", got1.Error()) 15 assert.Equal(t, "[0] success", got2.Error()) 16 assert.Equal(t, "[404] not found", got3.Error()) 17 18 list := reg.Dump() 19 assert.Equal(t, int32(-1024), list[0].Code()) 20 assert.Equal(t, int32(0), list[1].Code()) 21 assert.Equal(t, int32(404), list[2].Code()) 22 23 reg.UpdateMessages(map[int32]string{ 24 400: "bad request", 25 401: "unauthorized", 26 403: "forbidden", 27 }) 28 assert.Equal(t, "bad request", reg.getMessage(400)) 29 assert.Equal(t, "unauthorized", reg.getMessage(401)) 30 assert.Equal(t, "forbidden", reg.getMessage(403)) 31 32 reg.UpdateMessages(map[int32]string{ 33 -1: "error", 34 0: "success", 35 }) 36 assert.Equal(t, "oops", got1.Message()) 37 assert.Equal(t, "success", got2.Message()) 38 assert.Equal(t, "not found", got3.Message()) 39 } 40 41 func TestRegistryWithReserved(t *testing.T) { 42 reg := NewWithReserved(func(code int32) bool { return code <= 99 }) 43 44 assert.Panics(t, func() { reg.Register(-1, "") }) 45 assert.Panics(t, func() { reg.Register(99, "") }) 46 assert.NotPanics(t, func() { reg.Register(100, "") }) 47 assert.NotPanics(t, func() { reg.RegisterReserved(-1, "") }) 48 assert.NotPanics(t, func() { reg.RegisterReserved(99, "") }) 49 }