go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
743 stars 54 forks source link

False positive on error field with different real types but same string #41

Closed daniel-nichter closed 4 years ago

daniel-nichter commented 4 years ago
    var err1 primKindError = "abc"
    var err2 error = fmt.Errorf("abc")
    t1 := tWithError{
        Error: err1,
    }
    t2 := tWithError{
        Error: err2,
    }

Those should be equal, but as of v1.0.5 they're not:

--- FAIL: TestErrorDifferentTypesSameString (0.00s)
    deep_test.go:1336: expected zero diffs, got 1: [Error: deep_test.primKindError != *errors.errorString]

because,

    if aType.Implements(errorType) && bType.Implements(errorType) {
        if (!aElem || !a.IsNil()) && (!bElem || !b.IsNil()) {
            aString := a.MethodByName("Error").Call(nil)[0].String()
            bString := b.MethodByName("Error").Call(nil)[0].String()
            if aString != bString {
                c.saveDiff(aString, bString)
                return
            }
        }
    }

we don't return from that block if they're equal, we keep comparing which results in the false positive on their different underlying type.

daniel-nichter commented 4 years ago

On second thought, this isn't a bug per-se. These would be different to reflect.DeepEqual because different types are inherently not equal. What this introduces is a more high-level, abstract equality for error based only on their Error() string values. Will hold off on this for now.