From 7e713f54de8cf3affa7399a1e3e71a55d9640880 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 17 Sep 2024 09:58:41 +0200 Subject: [PATCH] dict: refacto test for error handling --- dictionary/dictionary_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/dictionary/dictionary_test.go b/dictionary/dictionary_test.go index 5acd818..3ae8146 100644 --- a/dictionary/dictionary_test.go +++ b/dictionary/dictionary_test.go @@ -2,11 +2,19 @@ package dictionary import "testing" -func assertStrings(t testing.TB, got, want string) { +func assertStrings(t testing.TB, got, want, given string) { t.Helper() 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") want := "this is just a test" - assertStrings(t, got, want) + assertStrings(t, got, want, "test") }) t.Run("unknown word", func(t *testing.T) { _, err := dictionary.Search("undefined") - want := "could not find the word you were looking for" if err == nil { t.Fatal("expected to get an error") } - - assertStrings(t, err.Error(), want) + assertError(t, err, ErrNotFound) }) }