testify
testify copied to clipboard
How to assert the address of 2 pointers are (not) equal?
According to the docs, pointers are considered equals as long as their ref'd values are equal.
// NotEqual asserts that the specified values are NOT equal.
//
// assert.NotEqual(t, obj1, obj2)
//
// Pointer variable equality is determined based on the equality of the
// referenced values (as opposed to the memory addresses).
However, this can be difficult when testing clone functions because in addition to testing the referenced values, I also need to test the memory address are new and not the same.
type A struct {
i int
}
func (a *A) Clone() *A {
clone := *a
return &clone
}
func TestClone(t *testing.T) {
orig := &A{1}
copy := orig.Clone()
assert.Equal(t, *orig, *copy)
assert.NotEqual(t, orig, copy) // Fail, how to compare memory address values?
}
Are there any functions that compares memory addresses values of pointers?
@littledot As its a pointer, You could just use ==
operator to verify its different memory.
orig==copy
is false
and orig==orig
is true
.
Yes I can do == with assert.True(), but the default error log for assert.True() is vague due to a lack of context. Hence I'd like to know if there is a dedicated assert function for comparing memory addresses? Which will also pretty print the pointers on assertion failures.
What are your thoughts on a new assertion function assert.Same(*testing.T, interface{}, interface{})
that simply wraps the ==
check?
https://play.golang.org/p/ZEzrtMb7oQN
looks good, we also have to check the types of the two pointers, and report properly if its different, and not just print the different address.
Any updates on this?
Was looking for this today so I took a stab at it based on @littledot's start
NotSame
would also be great.
Can be closed, Same
and NotSame
are available.
@devdinu This issue was resolved. Could you close the issue, please?
Maybe you know how recursive doing Same or NotSame?