MCPcopy
hub / github.com/go-gorm/gorm / AssertEqual

Function AssertEqual

utils/tests/utils.go:30–123  ·  view source on GitHub ↗
(t *testing.T, got, expect interface{})

Source from the content-addressed store, hash-verified

28}
29
30func AssertEqual(t *testing.T, got, expect interface{}) {
31 if !reflect.DeepEqual(got, expect) {
32 isEqual := func() {
33 if curTime, ok := got.(time.Time); ok {
34 format := "2006-01-02T15:04:05Z07:00"
35
36 if curTime.Round(time.Second).UTC().Format(format) != expect.(time.Time).Round(time.Second).UTC().Format(format) && curTime.Truncate(time.Second).UTC().Format(format) != expect.(time.Time).Truncate(time.Second).UTC().Format(format) {
37 t.Errorf("%v: expect: %v, got %v after time round", utils.FileWithLineNum(), expect.(time.Time), curTime)
38 }
39 } else if fmt.Sprint(got) != fmt.Sprint(expect) {
40 t.Errorf("%v: expect: %#v, got %#v", utils.FileWithLineNum(), expect, got)
41 }
42 }
43
44 if fmt.Sprint(got) == fmt.Sprint(expect) {
45 return
46 }
47
48 if reflect.Indirect(reflect.ValueOf(got)).IsValid() != reflect.Indirect(reflect.ValueOf(expect)).IsValid() {
49 t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), expect, got)
50 return
51 }
52
53 if valuer, ok := got.(driver.Valuer); ok {
54 got, _ = valuer.Value()
55 }
56
57 if valuer, ok := expect.(driver.Valuer); ok {
58 expect, _ = valuer.Value()
59 }
60
61 if got != nil {
62 got = reflect.Indirect(reflect.ValueOf(got)).Interface()
63 }
64
65 if expect != nil {
66 expect = reflect.Indirect(reflect.ValueOf(expect)).Interface()
67 }
68
69 if reflect.ValueOf(got).IsValid() != reflect.ValueOf(expect).IsValid() {
70 t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), expect, got)
71 return
72 }
73
74 if reflect.ValueOf(got).Kind() == reflect.Slice {
75 if reflect.ValueOf(expect).Kind() == reflect.Slice {
76 if reflect.ValueOf(got).Len() == reflect.ValueOf(expect).Len() {
77 for i := 0; i < reflect.ValueOf(got).Len(); i++ {
78 name := fmt.Sprintf(reflect.ValueOf(got).Type().Name()+" #%v", i)
79 t.Run(name, func(t *testing.T) {
80 AssertEqual(t, reflect.ValueOf(got).Index(i).Interface(), reflect.ValueOf(expect).Index(i).Interface())
81 })
82 }
83 } else {
84 name := reflect.ValueOf(got).Type().Elem().Name()
85 t.Errorf("%v expects length: %v, got %v (expects: %+v, got %+v)", name, reflect.ValueOf(expect).Len(), reflect.ValueOf(got).Len(), expect, got)
86 }
87 return

Callers 2

checkFieldFunction · 0.92
AssertObjEqualFunction · 0.70

Calls 5

FileWithLineNumFunction · 0.92
LenMethod · 0.80
ValueMethod · 0.65
NameMethod · 0.65
TypeMethod · 0.65

Tested by 1

checkFieldFunction · 0.74