testify icon indicating copy to clipboard operation
testify copied to clipboard

Print underlying object in ptr comparison

Open mchlp opened this issue 2 years ago • 6 comments

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 in assert/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

mchlp avatar Oct 21 '22 21:10 mchlp

Thank you for your contribution :)

boyan-soubachov avatar Oct 23 '22 20:10 boyan-soubachov

@boyan-soubachov Would be great if you could take a look at this and merge if it looks good!

mchlp avatar Dec 02 '22 15:12 mchlp

@boyan-soubachov Would be great if you could take a look and merge when you get the chance!

mchlp avatar Apr 14 '23 04:04 mchlp

The MR looks good @boyan-soubachov

dropdevrahul avatar May 03 '23 12:05 dropdevrahul

@boyan-soubachov please have a look

boindil avatar Sep 27 '23 13:09 boindil

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.

mchlp avatar Dec 13 '23 20:12 mchlp