stretchr/testify

Add NotEqualValues assertion and FailNowDiff

Open

#640 ouverte le 23 juil. 2018

Voir sur GitHub
 (1 commentaire) (0 réactions) (0 assignés)Go (1 704 forks)batch import
help wanted

Métriques du dépôt

Stars
 (25 958 stars)
Métriques de merge PR
 (Merge moyen 6j 15h) (2 PRs mergées en 30 j)

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...)
}

Guide contributeur