smarty / assertions

Fluent assertion-style functions used by goconvey and gunit. Can also be used in any test or application.
Other
100 stars 34 forks source link

ShouldResemble can fail when there's no visual diff between the inputs #50

Closed srabraham closed 1 year ago

srabraham commented 1 year ago

This was initially filed as an issue on goconvey: https://github.com/smartystreets/goconvey/issues/665 But really it's a problem in this assertions code.

Assuming you read that issue first, and especially https://github.com/smartystreets/goconvey/issues/665#issuecomment-1489390117 , the right fix would be for the diff to report which types are different. That would be a little involved, especially if anyone has managed to rely on the exact output format that is provided currently. A lazier fix would be something like the below

func ShouldResemble(actual interface{}, expected ...interface{}) string {
    if message := need(1, expected); message != success {
        return message
    }

    if matchError := oglematchers.DeepEquals(expected[0]).Matches(actual); matchError != nil {
        renderedExpected, renderedActual := render.Render(expected[0]), render.Render(actual)
        message := fmt.Sprintf(shouldHaveResembled, renderedExpected, renderedActual)

        if renderedExpected != renderedActual {
            message += composePrettyDiff(renderedExpected, renderedActual)
        } else {
            message += "No visual diff, but there is a difference in type within expected versus actual"
        }
        return serializer.serializeDetailed(expected[0], actual, message)
    }

    return success
}

FYI @riannucci

mdwhatcott commented 1 year ago

I'd welcome a PR with code similar to what @srabraham suggested above (including test cases please).