|
|
@ -9,10 +9,7 @@
|
|
|
|
// package assert 提供了单元测试时的断言功能,减少一些模板代码
|
|
|
|
// package assert 提供了单元测试时的断言功能,减少一些模板代码
|
|
|
|
package assert
|
|
|
|
package assert
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import "github.com/q191201771/naza/pkg/nazareflect"
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 单元测试中的 *testing.T 和 *testing.B 都满足该接口
|
|
|
|
// 单元测试中的 *testing.T 和 *testing.B 都满足该接口
|
|
|
|
type TestingT interface {
|
|
|
|
type TestingT interface {
|
|
|
@ -27,7 +24,7 @@ func Equal(t TestingT, expected interface{}, actual interface{}, msg ...string)
|
|
|
|
if h, ok := t.(tHelper); ok {
|
|
|
|
if h, ok := t.(tHelper); ok {
|
|
|
|
h.Helper()
|
|
|
|
h.Helper()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !equal(expected, actual) {
|
|
|
|
if !nazareflect.Equal(expected, actual) {
|
|
|
|
t.Errorf("%s expected=%+v, actual=%+v", msg, expected, actual)
|
|
|
|
t.Errorf("%s expected=%+v, actual=%+v", msg, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -38,37 +35,8 @@ func IsNotNil(t TestingT, actual interface{}, msg ...string) {
|
|
|
|
if h, ok := t.(tHelper); ok {
|
|
|
|
if h, ok := t.(tHelper); ok {
|
|
|
|
h.Helper()
|
|
|
|
h.Helper()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isNil(actual) {
|
|
|
|
if nazareflect.IsNil(actual) {
|
|
|
|
t.Errorf("%s expected not nil, but actual=%+v", msg, actual)
|
|
|
|
t.Errorf("%s expected not nil, but actual=%+v", msg, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isNil(actual interface{}) bool {
|
|
|
|
|
|
|
|
if actual == nil {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
v := reflect.ValueOf(actual)
|
|
|
|
|
|
|
|
k := v.Kind()
|
|
|
|
|
|
|
|
if k == reflect.Chan || k == reflect.Map || k == reflect.Ptr || k == reflect.Interface || k == reflect.Slice {
|
|
|
|
|
|
|
|
return v.IsNil()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func equal(expected, actual interface{}) bool {
|
|
|
|
|
|
|
|
if expected == nil {
|
|
|
|
|
|
|
|
return isNil(actual)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exp, ok := expected.([]byte)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return reflect.DeepEqual(expected, actual)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
act, ok := actual.([]byte)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.Equal(exp, act)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|