From d402765f694ade8fd3a0da1b7a2f9d2fa4453957 Mon Sep 17 00:00:00 2001 From: Cr <631807682@qq.com> Date: Fri, 18 Mar 2022 20:11:23 +0800 Subject: [PATCH] test: fix utils.AssertEqual (#5172) --- tests/query_test.go | 4 +++- utils/tests/utils.go | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/query_test.go b/tests/query_test.go index 6542774a..af2b8d4b 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -583,7 +583,9 @@ func TestPluck(t *testing.T) { if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Order("name desc").Pluck("name", &names2).Error; err != nil { t.Errorf("got error when pluck name: %v", err) } - AssertEqual(t, names, sort.Reverse(sort.StringSlice(names2))) + + sort.Slice(names2, func(i, j int) bool { return names2[i] < names2[j] }) + AssertEqual(t, names, names2) var ids []int if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Pluck("id", &ids).Error; err != nil { diff --git a/utils/tests/utils.go b/utils/tests/utils.go index 817e4b0b..661d727f 100644 --- a/utils/tests/utils.go +++ b/utils/tests/utils.go @@ -83,20 +83,22 @@ func AssertEqual(t *testing.T, got, expect interface{}) { } if reflect.ValueOf(got).Kind() == reflect.Struct { - if reflect.ValueOf(got).NumField() == reflect.ValueOf(expect).NumField() { - exported := false - for i := 0; i < reflect.ValueOf(got).NumField(); i++ { - if fieldStruct := reflect.ValueOf(got).Type().Field(i); ast.IsExported(fieldStruct.Name) { - exported = true - field := reflect.ValueOf(got).Field(i) - t.Run(fieldStruct.Name, func(t *testing.T) { - AssertEqual(t, field.Interface(), reflect.ValueOf(expect).Field(i).Interface()) - }) + if reflect.ValueOf(expect).Kind() == reflect.Struct { + if reflect.ValueOf(got).NumField() == reflect.ValueOf(expect).NumField() { + exported := false + for i := 0; i < reflect.ValueOf(got).NumField(); i++ { + if fieldStruct := reflect.ValueOf(got).Type().Field(i); ast.IsExported(fieldStruct.Name) { + exported = true + field := reflect.ValueOf(got).Field(i) + t.Run(fieldStruct.Name, func(t *testing.T) { + AssertEqual(t, field.Interface(), reflect.ValueOf(expect).Field(i).Interface()) + }) + } } - } - if exported { - return + if exported { + return + } } } } @@ -107,6 +109,9 @@ func AssertEqual(t *testing.T, got, expect interface{}) { } else if reflect.ValueOf(expect).Type().ConvertibleTo(reflect.ValueOf(got).Type()) { expect = reflect.ValueOf(got).Convert(reflect.ValueOf(got).Type()).Interface() isEqual() + } else { + t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), expect, got) + return } } }