Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ObjectsExportedFieldsAreEqual: Handle pointer and slice fields #1378

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,41 +80,62 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
//
// This function does no assertion of any kind.
func ObjectsExportedFieldsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
if isNil(expected) || isNil(actual) {
return expected == actual
}

expectedType := reflect.TypeOf(expected)
actualType := reflect.TypeOf(actual)

if expectedType != actualType {
return false
}

if expectedType.Kind() != reflect.Struct || actualType.Kind() != reflect.Struct {
expectedKind := expectedType.Kind()
actualKind := actualType.Kind()
if expectedKind != actualKind {
return false
}

expectedValue := reflect.ValueOf(expected)
actualValue := reflect.ValueOf(actual)

for i := 0; i < expectedType.NumField(); i++ {
field := expectedType.Field(i)
isExported := field.PkgPath == "" // should use field.IsExported() but it's not available in Go 1.16.5
if isExported {
var equal bool
if field.Type.Kind() == reflect.Struct {
equal = ObjectsExportedFieldsAreEqual(expectedValue.Field(i).Interface(), actualValue.Field(i).Interface())
} else {
equal = ObjectsAreEqualValues(expectedValue.Field(i).Interface(), actualValue.Field(i).Interface())
switch expectedKind {
case reflect.Struct:
for i := 0; i < expectedType.NumField(); i++ {
field := expectedType.Field(i)
isExported := field.PkgPath == "" // should use field.IsExported() but it's not available in Go 1.16.5
if isExported {
expectedElem := expectedValue.Field(i).Interface()
actualElem := actualValue.Field(i).Interface()
if !ObjectsExportedFieldsAreEqual(expectedElem, actualElem) {
return false
}
}
}
return true

case reflect.Interface, reflect.Ptr:
expectedElem := expectedValue.Elem().Interface()
actualElem := actualValue.Elem().Interface()
return ObjectsExportedFieldsAreEqual(expectedElem, actualElem)

if !equal {
case reflect.Array, reflect.Slice:
expectedLen := expectedValue.Len()
if expectedLen != actualValue.Len() {
return false
}
for i := 0; i < expectedLen; i++ {
expectedElem := expectedValue.Index(i).Elem().Interface()
actualElem := actualValue.Index(i).Elem().Interface()
if !ObjectsExportedFieldsAreEqual(expectedElem, actualElem) {
return false
}
}
return true

default:
return ObjectsAreEqualValues(expectedValue.Interface(), actualValue.Interface())
}
return true
}

// ObjectsAreEqualValues gets whether two objects are equal, or if their
Expand Down
27 changes: 27 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ func TestObjectsExportedFieldsAreEqual(t *testing.T) {
foo interface{}
}

type S3 struct {
Exported1 *Nested
Exported2 *Nested
}

type S4 struct {
Exported1 []*Nested
}

intValue := 1

cases := []struct {
expected interface{}
actual interface{}
Expand All @@ -180,6 +191,22 @@ func TestObjectsExportedFieldsAreEqual(t *testing.T) {
{S{1, Nested{2, 3}, 4, Nested{5, 6}}, S{1, Nested{"a", 3}, 4, Nested{5, 6}}, false},
{S{1, Nested{2, 3}, 4, Nested{5, 6}}, S2{1}, false},
{1, S{1, Nested{2, 3}, 4, Nested{5, 6}}, false},

{S3{&Nested{1, 2}, &Nested{3, 4}}, S3{&Nested{1, 2}, &Nested{3, 4}}, true},
{S3{nil, &Nested{3, 4}}, S3{nil, &Nested{3, 4}}, true},
{S3{&Nested{1, 2}, &Nested{3, 4}}, S3{&Nested{1, 2}, &Nested{3, "b"}}, true},
{S3{&Nested{1, 2}, &Nested{3, 4}}, S3{&Nested{1, "a"}, &Nested{3, "b"}}, true},
{S3{&Nested{1, 2}, &Nested{3, 4}}, S3{&Nested{"a", 2}, &Nested{3, 4}}, false},
{S3{&Nested{1, 2}, &Nested{3, 4}}, S3{}, false},
{S3{}, S3{}, true},

{S4{[]*Nested{{1, 2}}}, S4{[]*Nested{{1, 2}}}, true},
{S4{[]*Nested{{1, 2}}}, S4{[]*Nested{{1, 3}}}, true},
{S4{[]*Nested{{1, 2}, {3, 4}}}, S4{[]*Nested{{1, "a"}, {3, "b"}}}, true},

{Nested{&intValue, 2}, Nested{&intValue, 2}, true},
{Nested{&Nested{1, 2}, 3}, Nested{&Nested{1, "b"}, 3}, true},
{Nested{&Nested{1, 2}, 3}, Nested{nil, 3}, false},
}

for _, c := range cases {
Expand Down