dict: refacto test for error handling
This commit is contained in:
parent
fc67cf14ff
commit
7e713f54de
@ -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)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user