frankban / quicktest

Quick helpers for testing Go applications
MIT License
529 stars 27 forks source link

confusing failure when using qt.Equals with pointers #133

Closed josharian closed 2 years ago

josharian commented 2 years ago

https://go.dev/play/p/vYkG-W6-tI2

package main

import (
    "testing"

    qt "github.com/frankban/quicktest"
)

type T int

func TestFun(t *testing.T) {
    c := qt.New(t)
    a := new(T)
    b := new(T)
    c.Assert(a, qt.Equals, b)
}

Result:

 error:
          values are not equal
got:
          &main.T(0)
want:
          <same as "got">

That provided a needed bit of levity in my day, but it took me a while to figure out that the problem was that the two pointers were distinct. (This was made worse in my case by a String function that printed the complete internal state of the struct, removing the telltail & at the beginning.)

I'd suggest printing something like %p %s for got/want when they are both pointers. That'd make it much more obvious.

dsnet commented 2 years ago

For prior art, cmp.Diff prints something like the following:

  (*main.T)(
-   &⟪0xc0000c8140⟫0,
+   &⟪0xc0000c8148⟫0,
  )

It notices that both values would print as the same thing, and so it falls back to a more verbose printout that includes addresses to hopefully surface that these are different.

frankban commented 2 years ago

Tagged as v1.14.3.