dict: refacto test for error handling

This commit is contained in:
vinchent 2024-09-17 09:58:41 +02:00
parent fc67cf14ff
commit 7e713f54de

View File

@ -2,11 +2,19 @@ package dictionary
import "testing" import "testing"
func assertStrings(t testing.TB, got, want string) { func assertStrings(t testing.TB, got, want, given string) {
t.Helper() t.Helper()
if got != want { if got != want {
t.Errorf("got %q want %q given %q", got, want, "test") t.Errorf("got %q want %q given %q", got, want, given)
}
}
func assertError(t testing.TB, got, want error) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
} }
} }
@ -17,16 +25,14 @@ func TestSearch(t *testing.T) {
got, _ := dictionary.Search("test") got, _ := dictionary.Search("test")
want := "this is just a test" want := "this is just a test"
assertStrings(t, got, want) assertStrings(t, got, want, "test")
}) })
t.Run("unknown word", func(t *testing.T) { t.Run("unknown word", func(t *testing.T) {
_, err := dictionary.Search("undefined") _, err := dictionary.Search("undefined")
want := "could not find the word you were looking for"
if err == nil { if err == nil {
t.Fatal("expected to get an error") t.Fatal("expected to get an error")
} }
assertError(t, err, ErrNotFound)
assertStrings(t, err.Error(), want)
}) })
} }