Skip to content

Correct way to assert.Equal structs that contain functions pointers #1146

@bLuka

Description

@bLuka

Hello,

I'm having troubles comparing objects indirectly referencing function pointers.

For the reference, here's a minimal example:

package main

import (
        "testing"

        assert "github.com/stretchr/testify/assert"
)

type s struct {
        f func()
}

func TestAssert(t *testing.T) {
        var f = func() {}
        var obj = s{f}

        assert.Equal(t, obj, obj)
}

Testing this code leads to this error:

--- FAIL: TestAssert (0.00s)
    main_test.go:17: 
                Error Trace:    main_test.go:17
                Error:          Not equal: 
                                expected: main.s{f:(func())(0x571380)}
                                actual  : main.s{f:(func())(0x571380)}
                            
                                Diff:
                Test:           TestAssert

In my specific case, this function pointer field comes from a private nested structure I don't have the ability to manipulate.

Is there a way to compare structures indirectly embedding a function pointer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions