Skip to content

Commit

Permalink
Support pointer on top-level for 'TestEqualExportedValues' & Get rid …
Browse files Browse the repository at this point in the history
…of duplicate 'ObjectsExportedFieldsAreEqual'
  • Loading branch information
HaraldNordgren committed May 19, 2023
1 parent 4c93d8f commit 141f1f6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
19 changes: 0 additions & 19 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ func copyExportedFields(expected interface{}) interface{} {
}
}

// ObjectsExportedFieldsAreEqual determines if the exported (public) fields of two objects are
// considered equal. This comparison of only exported fields is applied recursively to nested data
// structures.
//
// This function does no assertion of any kind.
func ObjectsExportedFieldsAreEqual(expected, actual interface{}) bool {
expectedCleaned := copyExportedFields(expected)
actualCleaned := copyExportedFields(actual)
return ObjectsAreEqualValues(expectedCleaned, actualCleaned)
}

// ObjectsAreEqualValues gets whether two objects are equal, or if their
// values are equal.
func ObjectsAreEqualValues(expected, actual interface{}) bool {
Expand Down Expand Up @@ -566,14 +555,6 @@ func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs ..
return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...)
}

if aType.Kind() != reflect.Struct {
return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", aType.Kind(), reflect.Struct), msgAndArgs...)
}

if bType.Kind() != reflect.Struct {
return Fail(t, fmt.Sprintf("Types expected to both be struct \n\t%v != %v", bType.Kind(), reflect.Struct), msgAndArgs...)
}

expected = copyExportedFields(expected)
actual = copyExportedFields(actual)

Expand Down
9 changes: 6 additions & 3 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type S6 struct {
unexported string
}

func TestObjectsExportedFieldsAreEqual(t *testing.T) {
func TestEqualExportedValues(t *testing.T) {

intValue := 1

Expand Down Expand Up @@ -219,6 +219,8 @@ func TestObjectsExportedFieldsAreEqual(t *testing.T) {
{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},
{&Nested{1, 2}, &Nested{1, "b"}, true},
{&Nested{1, 2}, &Nested{"a", 2}, false},

{
Nested{map[interface{}]*Nested{nil: nil}, 2},
Expand Down Expand Up @@ -249,7 +251,8 @@ func TestObjectsExportedFieldsAreEqual(t *testing.T) {

for _, c := range cases {
t.Run(fmt.Sprintf("ObjectsExportedFieldsAreEqual(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
res := ObjectsExportedFieldsAreEqual(c.expected, c.actual)
mockT := new(testing.T)
res := EqualExportedValues(mockT, c.expected, c.actual)

if res != c.result {
t.Errorf("ObjectsExportedFieldsAreEqual(%#v, %#v) should return %#v", c.expected, c.actual, c.result)
Expand Down Expand Up @@ -342,7 +345,7 @@ func TestCopyExportedFields(t *testing.T) {
}
}

func TestEqualExportedValues(t *testing.T) {
func TestEqualExportedValuesDiffs(t *testing.T) {
cases := []struct {
value1 interface{}
value2 interface{}
Expand Down

0 comments on commit 141f1f6

Please sign in to comment.