Skip to content

assertError function in maps section needs to be updated #861

@s-adhit

Description

@s-adhit

The current implementation of the assertError function doesn't handle all error cases safely. Specifically, if want is nil, calling want.Error() will result in a panic. This becomes problematic now that we’ve added custom errors like ErrNotFound and ErrWordExists.

Current implementation:

func assertError(t testing.TB, got, want error) {
	t.Helper()

	if got != want {
		t.Errorf("got error %q want %q", got, want)
	}
}

Suggestion:

func assertError(t testing.TB, got, want error) {
	t.Helper()

	switch {
	case got != nil && want == nil:
		t.Fatalf("got unexpected error: %q", got)
	case got == nil && want != nil:
		t.Fatalf("expected error %q but got none", want)
	case got != nil && got.Error() != want.Error():
		t.Errorf("got error %q, want %q", got, want)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions