stretchr/testify

Add NotEqualValues assertion and FailNowDiff

Open

#640 aberto em 23 de jul. de 2018

Ver no GitHub
 (1 comment) (0 reactions) (0 assignees)Go (1.704 forks)batch import
help wanted

Métricas do repositório

Stars
 (25.958 stars)
Métricas de merge de PR
 (Mesclagem média 6d 15h) (2 fundiu PRs em 30d)

Description

Currently there is an EqualValues assertion, which seems good for checking structs are the same. But if I want to test the negative case (NotEqualValues), there doesn't seem to be a good method. It would be basically the same as the EqualValues, though just changing the inner check:

From:

func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
..snip..
if !ObjectsAreEqualValues(expected, actual) {
..snip..

To:

func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
..snip..
if ObjectsAreEqualValues(expected, actual) {
..snip..

Perhaps a refactor to share the common code in an unexported method with a flag on the test case would be better too.

I can make my own using ObjectsAreEqual and Condition, but then I don't get the niceness of the diff format in the output, which I can't access to make my own similar (without copying code) as the diff functions aren't exported.

It would also be nice if there was a wrapper function to access the diff features, perhaps something like (based on the error diff code in EqualValues:

func FailNowDiff(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
	diff := diff(expected, actual)
	expected, actual = formatUnequalValues(expected, actual)
	return Fail(t, fmt.Sprintf("Not equal: \n"+
		"expected: %s\n"+
		"actual  : %s%s", expected, actual, diff), msgAndArgs...)
}

Guia do colaborador