mirror of
https://github.com/gone-io/gone.git
synced 2025-01-23 01:00:23 +08:00
test: add test for error.go
This commit is contained in:
parent
2d0c5a2048
commit
5977e64e90
29
cemetery.go
29
cemetery.go
@ -32,19 +32,23 @@ func GetGoneDefaultId(goner Goner) GonerId {
|
||||
return GonerId(fmt.Sprintf("%s#%d", pkgName, reflect.ValueOf(goner).Elem().UnsafeAddr()))
|
||||
}
|
||||
|
||||
func (c *cemetery) bury(goner Goner, options ...GonerOption) Tomb {
|
||||
theTomb := NewTomb(goner)
|
||||
|
||||
func buildTomb(goner Goner, options ...GonerOption) Tomb {
|
||||
newTomb := NewTomb(goner)
|
||||
for _, option := range options {
|
||||
switch option.(type) {
|
||||
case GonerId:
|
||||
theTomb.SetId(option.(GonerId))
|
||||
newTomb.SetId(option.(GonerId))
|
||||
case defaultType:
|
||||
theTomb.SetDefault(option.(defaultType).t)
|
||||
newTomb.SetDefault(option.(defaultType).t)
|
||||
case Order:
|
||||
theTomb.SetOrder(option.(Order))
|
||||
newTomb.SetOrder(option.(Order))
|
||||
}
|
||||
}
|
||||
return newTomb
|
||||
}
|
||||
|
||||
func (c *cemetery) bury(goner Goner, options ...GonerOption) Tomb {
|
||||
theTomb := buildTomb(goner, options...)
|
||||
|
||||
if theTomb.GetId() == "" {
|
||||
theTomb.SetId(GetGoneDefaultId(goner))
|
||||
@ -90,18 +94,7 @@ func (c *cemetery) BuryOnce(goner Goner, options ...GonerOption) Cemetery {
|
||||
}
|
||||
|
||||
func (c *cemetery) ReplaceBury(goner Goner, options ...GonerOption) (err error) {
|
||||
newTomb := NewTomb(goner)
|
||||
|
||||
for _, option := range options {
|
||||
switch option.(type) {
|
||||
case GonerId:
|
||||
newTomb.SetId(option.(GonerId))
|
||||
case defaultType:
|
||||
newTomb.SetDefault(option.(defaultType).t)
|
||||
case Order:
|
||||
newTomb.SetOrder(option.(Order))
|
||||
}
|
||||
}
|
||||
newTomb := buildTomb(goner, options...)
|
||||
if newTomb.GetId() == "" {
|
||||
err = ReplaceBuryIdParamEmptyError()
|
||||
return
|
||||
|
@ -166,3 +166,66 @@ func TestToError(t *testing.T) {
|
||||
assert.Equal(t, "100", toError.Msg())
|
||||
})
|
||||
}
|
||||
|
||||
func TestBError_GetStatusCode(t *testing.T) {
|
||||
type fields struct {
|
||||
err Error
|
||||
data any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want int
|
||||
}{
|
||||
{
|
||||
name: "default",
|
||||
fields: fields{
|
||||
err: NewBusinessError("error", 100),
|
||||
data: nil,
|
||||
},
|
||||
want: http.StatusOK,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := &BError{
|
||||
err: tt.fields.err,
|
||||
data: tt.fields.data,
|
||||
}
|
||||
assert.Equalf(t, tt.want, e.GetStatusCode(), "GetStatusCode()")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_defaultErr_GetStatusCode(t *testing.T) {
|
||||
type fields struct {
|
||||
code int
|
||||
msg string
|
||||
statusCode int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want int
|
||||
}{
|
||||
{
|
||||
name: "default",
|
||||
fields: fields{
|
||||
code: 500,
|
||||
msg: "error",
|
||||
statusCode: http.StatusOK,
|
||||
},
|
||||
want: http.StatusOK,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := &defaultErr{
|
||||
code: tt.fields.code,
|
||||
msg: tt.fields.msg,
|
||||
statusCode: tt.fields.statusCode,
|
||||
}
|
||||
assert.Equalf(t, tt.want, e.GetStatusCode(), "GetStatusCode()")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user