diff --git a/TODO.MD b/TODO.MD index 1efa597b9..a86547377 100644 --- a/TODO.MD +++ b/TODO.MD @@ -44,7 +44,6 @@ 1. 添加sqlite数据库的单元测试用例; 1. gredis增加cluster支持; - # DONE 1. gconv完善针对不同类型的判断,例如:尽量减少sprintf("%v", xxx)来执行string类型的转换; 2. ghttp.Server请求执行中增加服务退出的方法,不再执行后续操作; diff --git a/g/test/gtest/gtest.go b/g/test/gtest/gtest.go index 0b520e0a8..89256a481 100644 --- a/g/test/gtest/gtest.go +++ b/g/test/gtest/gtest.go @@ -32,12 +32,9 @@ func Case(t *testing.T, f func()) { // Assert checks and 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 and 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 and 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 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 + } } \ No newline at end of file diff --git a/geg/other/test.go b/geg/other/test.go index ed997310b..20c4a87fc 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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) } \ No newline at end of file