Skip to content

Commit

Permalink
feat: add test case for error.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Dec 22, 2024
1 parent 0b00fae commit 891264f
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gone

import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -263,3 +264,78 @@ func TestPanicTrace(t *testing.T) {
t.Error("PanicTrace() does not contain test file")
}
}

func Test_iError_Error(t *testing.T) {
type fields struct {
defaultErr *defaultErr
trace []byte
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "test error",
fields: fields{
defaultErr: &defaultErr{
code: 100,
},
trace: []byte("test trace"),
},
want: "GoneError(code=100); \n\ntest trace",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &iError{
defaultErr: tt.fields.defaultErr,
trace: tt.fields.trace,
}
assert.Equalf(t, tt.want, e.Error(), "Error()")
})
}
}

func TestBError_SetMsg(t *testing.T) {
type fields struct {
err Error
data any
}
type args struct {
msg string
}
tests := []struct {
name string
fields fields
args args
want string
wantCode int
}{
{
name: "test error",
fields: fields{
err: &iError{
defaultErr: &defaultErr{
code: 100,
},
},
},
args: args{msg: "test error"},
want: "test error",
wantCode: 100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &BError{
err: tt.fields.err,
data: tt.fields.data,
}
e.SetMsg(tt.args.msg)
msg := e.Msg()
assert.Equalf(t, tt.want, msg, "SetMsg(%v)", tt.args.msg)
assert.Equalf(t, tt.wantCode, e.Code(), "SetMsg(%v)", tt.args.msg)
})
}
}

0 comments on commit 891264f

Please sign in to comment.