improve nil checks for gtest.Assert*

This commit is contained in:
John
2019-04-09 23:12:22 +08:00
parent 8b3eb5b02d
commit 85c2ed1bf2
3 changed files with 27 additions and 26 deletions

View File

@ -44,7 +44,6 @@
1. 添加sqlite数据库的单元测试用例
1. gredis增加cluster支持
# DONE
1. gconv完善针对不同类型的判断例如尽量减少sprintf("%v", xxx)来执行string类型的转换
2. ghttp.Server请求执行中增加服务退出的方法不再执行后续操作

View File

@ -32,12 +32,9 @@ func Case(t *testing.T, f func()) {
// Assert checks <value> and <expect> EQUAL.
func Assert(value, expect interface{}) {
rvValue := reflect.ValueOf(value)
rvExpect := reflect.ValueOf(expect)
if rvValue.Kind() == reflect.Ptr {
if rvValue.IsNil() {
value = nil
}
if isNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
if err := compareMap(value, expect); err != nil {
@ -53,13 +50,10 @@ func Assert(value, expect interface{}) {
// AssertEQ checks <value> and <expect> EQUAL, including their TYPES.
func AssertEQ(value, expect interface{}) {
// Value assert.
rvValue := reflect.ValueOf(value)
rvExpect := reflect.ValueOf(expect)
if rvValue.Kind() == reflect.Ptr {
if rvValue.IsNil() {
value = nil
}
}
if isNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
if err := compareMap(value, expect); err != nil {
panic(err)
@ -79,13 +73,10 @@ func AssertEQ(value, expect interface{}) {
// AssertNE checks <value> and <expect> NOT EQUAL.
func AssertNE(value, expect interface{}) {
rvValue := reflect.ValueOf(value)
rvExpect := reflect.ValueOf(expect)
if rvValue.Kind() == reflect.Ptr {
if rvValue.IsNil() {
value = nil
}
}
if isNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
if err := compareMap(value, expect); err == nil {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v != %v`, value, expect))
@ -259,11 +250,9 @@ func Fatal(message...interface{}) {
func compareMap(value, expect interface{}) error {
rvValue := reflect.ValueOf(value)
rvExpect := reflect.ValueOf(expect)
if rvValue.Kind() == reflect.Ptr {
if rvValue.IsNil() {
value = nil
}
}
if isNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
if rvValue.Kind() == reflect.Map {
if rvExpect.Len() == rvValue.Len() {
@ -336,4 +325,15 @@ func getBacktrace(skip...int) string {
}
}
return backtrace
}
// isNil checks whether <value> is nil.
func isNil(value interface{}) bool {
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.Ptr, reflect.Func:
return rv.IsNil()
default:
return value == nil
}
}

View File

@ -1,10 +1,12 @@
package main
import (
"fmt"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/test/gtest"
)
func test() []byte {
return nil
}
func main() {
fmt.Println(gtime.Now().Format(`Y-m-j G:i:su`))
gtest.Assert(test(), nil)
}