gorm/tests/utils.go

58 lines
1.6 KiB
Go
Raw Normal View History

2020-02-02 09:40:44 +03:00
package tests
import (
"reflect"
"testing"
2020-03-02 18:43:34 +03:00
"time"
2020-02-02 09:40:44 +03:00
)
2020-03-04 06:32:36 +03:00
func AssertObjEqual(t *testing.T, r, e interface{}, names ...string) {
2020-02-02 09:40:44 +03:00
for _, name := range names {
got := reflect.Indirect(reflect.ValueOf(r)).FieldByName(name).Interface()
2020-03-04 06:32:36 +03:00
expect := reflect.Indirect(reflect.ValueOf(e)).FieldByName(name).Interface()
t.Run(name, func(t *testing.T) {
AssertEqual(t, got, expect)
})
}
}
2020-02-02 09:40:44 +03:00
2020-03-04 06:32:36 +03:00
func AssertEqual(t *testing.T, got, expect interface{}) {
if !reflect.DeepEqual(got, expect) {
isEqual := func() {
2020-03-02 18:43:34 +03:00
if curTime, ok := got.(time.Time); ok {
format := "2006-01-02T15:04:05Z07:00"
2020-03-04 06:32:36 +03:00
if curTime.Format(format) != expect.(time.Time).Format(format) {
t.Errorf("expect: %v, got %v", expect.(time.Time).Format(format), curTime.Format(format))
2020-03-02 18:43:34 +03:00
}
2020-03-04 06:32:36 +03:00
} else if got != expect {
t.Errorf("expect: %#v, got %#v", expect, got)
2020-03-02 18:43:34 +03:00
}
2020-02-02 09:40:44 +03:00
}
2020-03-04 06:32:36 +03:00
2020-05-23 11:38:13 +03:00
if got == expect {
return
}
if reflect.Indirect(reflect.ValueOf(got)).IsValid() != reflect.Indirect(reflect.ValueOf(expect)).IsValid() {
t.Errorf("expect: %+v, got %+v", expect, got)
return
}
2020-03-04 06:32:36 +03:00
if got != nil {
got = reflect.Indirect(reflect.ValueOf(got)).Interface()
}
if expect != nil {
expect = reflect.Indirect(reflect.ValueOf(expect)).Interface()
}
if reflect.ValueOf(got).Type().ConvertibleTo(reflect.ValueOf(expect).Type()) {
got = reflect.ValueOf(got).Convert(reflect.ValueOf(expect).Type()).Interface()
isEqual()
} else if reflect.ValueOf(expect).Type().ConvertibleTo(reflect.ValueOf(got).Type()) {
expect = reflect.ValueOf(got).Convert(reflect.ValueOf(got).Type()).Interface()
isEqual()
}
2020-02-02 09:40:44 +03:00
}
}