Skip to content

Commit

Permalink
简化逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Aug 16, 2024
1 parent b8eb843 commit e74abe7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions erkrequire/erkrequire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
)

func NoError(t *testing.T, erk *errors.Error, msgAndArgs ...interface{}) {
if erk != nil { //这里必须在非空时才能让它去判断,否则即使是 nil 也会报错
require.NoError(t, erk, msgAndArgs...)
}
require.NoError(t, erk2erx(erk), msgAndArgs...)
}

func Error(t *testing.T, erk *errors.Error, msgAndArgs ...interface{}) {
require.Error(t, erk2erx(erk), msgAndArgs...) //这里必须传递个空才行,跟前面的情况相同
}

func erk2erx(erk *errors.Error) error {
if erk == nil {
require.Error(t, nil, msgAndArgs...) //这里必须传递个空才行,跟前面的情况相同
return nil //这里必须做这样的转换,因为两个 nil 是不一样的
}
return erk
}
6 changes: 3 additions & 3 deletions erkrequire/erkrequire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/go-kratos/kratos/v2/errors"
"github.com/orzkratos/erkkratos/erkrequire"
"github.com/orzkratos/erkkratos/internal/errors_example"
"github.com/stretchr/testify/require"
"github.com/yyle88/erero"
)

func TestNoError(t *testing.T) {
var erk *errors.Error
// require.NoError(t, erk) 这里是不对的
// 需要使用以下的函数
erkrequire.NoError(t, erk)
require.Error(t, erk) // 这里是不符合预期的
erkrequire.NoError(t, erk) // 需要使用这个函数
}

func TestError(t *testing.T) {
Expand Down

0 comments on commit e74abe7

Please sign in to comment.