stretchr/testify

Add NotEqualValues assertion and FailNowDiff

Open

#640 geöffnet am 23. Juli 2018

Auf GitHub ansehen
 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)Go (1.704 Forks)batch import
help wanted

Repository-Metriken

Stars
 (25.958 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 15h) (2 gemergte PRs in 30 T)

Beschreibung

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

Contributor Guide