testify icon indicating copy to clipboard operation
testify copied to clipboard

assert: print underlying objects when comparing pointers values

Open sjiekak opened this issue 4 years ago • 11 comments

Example

package main

import (
	"github.com/stretchr/testify/require"
	"testing"
)

func TestPointerToValue(t *testing.T) {
	var a int = 1
	var b int = 2
	require.EqualValues(t, &a, &b, "The two numbers should be the same.")

}

The output is

=== RUN   TestPointerToValue
    prog.go:13: 
        	Error Trace:	prog.go:13
        	Error:      	Not equal: 
        	            	expected: (*int)(0xc0000189b8)
        	            	actual  : (*int)(0xc0000189c0)
        	Test:       	TestPointerToValue
        	Messages:   	The two numbers should be the same.
--- FAIL: TestPointerToValue (0.00s)

It would be incredible to have the actual values being compared printed as well. For instance expected: (*int)(0xc0000189b8) could be expected: (*int)(0xc0000189b8) 1.

Currently, I am abusing this helper

func equalValuesPrettyPrint(t testing.TB, expected, actual interface{}) bool {
	return assert.EqualValues(t, expected, actual, "exp=%v, got=%v", reflect.Indirect(reflect.ValueOf(expected)), reflect.Indirect(reflect.ValueOf(actual)))
} 

sjiekak avatar Oct 01 '21 09:10 sjiekak

Can I take this issue?

rajkumarGosavi avatar Oct 22 '21 11:10 rajkumarGosavi

hi, any updates on this?

slavaavr avatar Mar 29 '22 21:03 slavaavr

Hi I can take this one on, if @rajkumarGosavi isn't working on this. @slavaavr I assume we just want to show the values of the basic datatypes like int and bool and the like? And not structs? Or do we want everything?

jyturley avatar Apr 26 '22 18:04 jyturley

@jyturley he there. Initially, I thought about printing pointer values for any type in the right way (without memory addresses). But then I checked library implementation and found out that they use fmt.Sptintf("%#v"), so it will be too hard to implement this feature from the scratch

slavaavr avatar Apr 27 '22 10:04 slavaavr

Will be taking this since this issue has been idle for a while.

mchlp avatar Oct 21 '22 17:10 mchlp

Any update?

tomasweigenast avatar Feb 18 '23 12:02 tomasweigenast

This is still relevant, would be thrilled to have this feature implemented. Is anybody able to pick it up? @rajkumarGosavi @mchlp @slavaavr

NikitaPersefoni avatar Dec 13 '23 01:12 NikitaPersefoni

The original PR that closes this issue seems to still be open and waiting for approve + merge: https://github.com/stretchr/testify/pull/1287

The changes are now out-of-date and need to be rebased on top of the most up to date changes on master. WIll take a look and see if I can the builds working again with those changes on top of master.

mchlp avatar Dec 13 '23 05:12 mchlp

The PR is now rebased on top of master and checks are passing on my fork, but are failing on the PR. Additional discussion on the PR: https://github.com/stretchr/testify/pull/1287#issuecomment-1854687817

mchlp avatar Dec 13 '23 20:12 mchlp