Skip to content

Add EqualEnum function #912

@Nokel81

Description

@Nokel81

Problem:

Currently when asserting a specific enum value is set I have been using either assert.Equal(t, val1, val1) or assert.Equal(t, val1.String(), val2.String()).

The first is, imo, better from the point of view of correctness but the second one produces better error messages. I know that I could implement GoString() but that has several problems.

  1. It seems like changing code for an implementation detail in my testing package.
  2. Some of the enums I am checking are defined in my dependencies which are out of my control, so I can't even define GoString() even if I wanted to.

Proposal:

Add the following functions in both testify/assert and testify/require:

func EqualEnum(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
    ...
}

func EqualEnumf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
    ...
}

func NotEqualEnum(t *testing.T, expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
    ...
}

func NotEqualEnumf(t *testing.T, expected interface{}, actual interface{}, msg string, args ...interface{}) {
    ...
}

These functions would act very similarly to Equal currently, but in the error output formatting would use the String() function ("%v") not the GoString() function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions