mirror of https://github.com/q191201771/naza
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
// Package assert 提供了单元测试时的断言功能,减少一些模板代码
|
|
//
|
|
// 代码参考了 https://github.com/stretchr/testify
|
|
//
|
|
package assert
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
)
|
|
|
|
type TestingT interface {
|
|
Errorf(format string, args ...interface{})
|
|
}
|
|
|
|
func Equal(t TestingT, expected interface{}, actual interface{}, msg ...string) {
|
|
if !equal(expected, actual) {
|
|
t.Errorf("%s expected=%+v, actual=%+v", msg, expected, actual)
|
|
}
|
|
return
|
|
}
|
|
|
|
func IsNotNil(t TestingT, actual interface{}, msg ...string) {
|
|
if isNil(actual) {
|
|
t.Errorf("%s expected not nil, but actual=%+v", msg, actual)
|
|
}
|
|
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
|
|
}
|
|
//if exp == nil || act == nil {
|
|
// return exp == nil && act == nil
|
|
//}
|
|
return bytes.Equal(exp, act)
|
|
}
|