testify
testify copied to clipboard
Print underlying object in ptr comparison
Summary
Prints out the underlying value of a pointer to a basic type in addition to the pointer address when the comparison between two basic type pointers fails.
Changes
- Updated
truncatingFormat
function inassert/assertions.go
with a flag that control whether or not the underlying value of basic types are also printed in addition to their address - Updated
assert/assertions_test.go
to fix unit tests that were broken and added additional unit test to cover added code
Motivation
As described in Issue #1118, when the comparison between two pointers fails, the underlying value is not always printed. This gives the impression that it is the address of the pointers that are being compared, which is incorrect, since it is actually the underlying values of the pointers that are being compared.
After further research, we discovered that non-basic types such as maps and structs already print the underlying value when the %#v
specifier is used for formatting, so their underlying value is already being printed. However, for basic types such as int, bool, and string, the %#v
specifier only prints out the pointer address. Our change will ensure that the underlying value of these basic type pointers are printed as well.
For example, this is the original output when comparing two int pointers:
Error: Not equal:
expected: (*int)(0x14000120df0)
actual : (*int)(0x14000120df8)
And this is the new output when comparing two int pointers:
Error: Not equal:
expected: (*int)(0x14000120df0) 5
actual : (*int)(0x14000120df8) 0
Similarly, this is the new output when comparing two string and bool pointers:
Error: Not equal:
expected: (*string)(0x140001132d0) hello
actual : (*string)(0x140001132e0)
Error: Not equal:
expected: (*bool)(0x14000120fa8) true
actual : (*bool)(0x14000120fa9) false
However, we do not print out the underlying value of basic type pointers when a comparison is being made between two different pointer types. This is the same behaviour as before. For example, for a comparison between an int pointer and a string pointer, this is the output:
Error: Not equal:
expected: *string((*string)(0x140000997c0))
actual : *int((*int)(0x140000a7608))
Related issues
Closes #1118 Closes #1273
Thank you for your contribution :)
@boyan-soubachov Would be great if you could take a look at this and merge if it looks good!
@boyan-soubachov Would be great if you could take a look and merge when you get the chance!
The MR looks good @boyan-soubachov
@boyan-soubachov please have a look
Rebased on top of what is currently on master
and builds are passing on my local branch (https://github.com/mchlp/testify/actions/runs/7200906284), but are failing here.
@dolmen Do you know why this might be happening? The build failures seem to be very inconsistent (they will pass some runs, but will fail some other runs with the exact same commit). The test of 1.20 failed, but the build of 1.20 passed.