-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move object equality functions to another package for clarity
- Loading branch information
Showing
5 changed files
with
139 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package equal | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
) | ||
|
||
// ObjectsAreEqual determines if two objects are considered equal. | ||
// | ||
// This function does no assertion of any kind. | ||
func ObjectsAreEqual(expected, actual interface{}) bool { | ||
if expected == nil || actual == nil { | ||
return expected == actual | ||
} | ||
|
||
exp, ok := expected.([]byte) | ||
if !ok { | ||
return reflect.DeepEqual(expected, actual) | ||
} | ||
|
||
act, ok := actual.([]byte) | ||
if !ok { | ||
return false | ||
} | ||
if exp == nil || act == nil { | ||
return exp == nil && act == nil | ||
} | ||
return bytes.Equal(exp, act) | ||
} | ||
|
||
// ObjectsAreEqualValues gets whether two objects are equal, or if their | ||
// values are equal. | ||
// | ||
// This function does no assertion of any kind. | ||
func ObjectsAreEqualValues(expected, actual interface{}) bool { | ||
if ObjectsAreEqual(expected, actual) { | ||
return true | ||
} | ||
|
||
actualType := reflect.TypeOf(actual) | ||
if actualType == nil { | ||
return false | ||
} | ||
expectedValue := reflect.ValueOf(expected) | ||
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) { | ||
// Attempt comparison after type conversion | ||
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package equal | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestObjectsAreEqual(t *testing.T) { | ||
cases := []struct { | ||
expected interface{} | ||
actual interface{} | ||
result bool | ||
}{ | ||
// cases that are expected to be equal | ||
{"Hello World", "Hello World", true}, | ||
{123, 123, true}, | ||
{123.5, 123.5, true}, | ||
{[]byte("Hello World"), []byte("Hello World"), true}, | ||
{nil, nil, true}, | ||
|
||
// cases that are expected not to be equal | ||
{map[int]int{5: 10}, map[int]int{10: 20}, false}, | ||
{'x', "x", false}, | ||
{"x", 'x', false}, | ||
{0, 0.1, false}, | ||
{0.1, 0, false}, | ||
{time.Now, time.Now, false}, | ||
{func() {}, func() {}, false}, | ||
{uint32(10), int32(10), false}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(fmt.Sprintf("ObjectsAreEqual(%#v, %#v)", c.expected, c.actual), func(t *testing.T) { | ||
res := ObjectsAreEqual(c.expected, c.actual) | ||
|
||
if res != c.result { | ||
t.Errorf("ObjectsAreEqual(%#v, %#v) should return %#v", c.expected, c.actual, c.result) | ||
} | ||
|
||
}) | ||
} | ||
|
||
// Cases where type differ but values are equal | ||
if !ObjectsAreEqualValues(uint32(10), int32(10)) { | ||
t.Error("ObjectsAreEqualValues should return true") | ||
} | ||
if ObjectsAreEqualValues(0, nil) { | ||
t.Fail() | ||
} | ||
if ObjectsAreEqualValues(nil, 0) { | ||
t.Fail() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters