go-by-test/dictionary/dictionary_test.go

33 lines
673 B
Go
Raw Normal View History

2024-09-11 11:48:59 +00:00
package dictionary
import "testing"
2024-09-11 12:07:00 +00:00
func assertStrings(t testing.TB, got, want string) {
2024-09-17 07:55:36 +00:00
t.Helper()
2024-09-11 12:07:00 +00:00
if got != want {
t.Errorf("got %q want %q given %q", got, want, "test")
}
}
2024-09-11 11:48:59 +00:00
func TestSearch(t *testing.T) {
2024-09-11 13:22:42 +00:00
dictionary := Dictionary{"test": "this is just a test"}
2024-09-11 11:48:59 +00:00
2024-09-17 07:55:36 +00:00
t.Run("known word", func(t *testing.T) {
got, _ := dictionary.Search("test")
want := "this is just a test"
2024-09-11 11:48:59 +00:00
2024-09-17 07:55:36 +00:00
assertStrings(t, got, want)
})
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)
})
2024-09-11 11:48:59 +00:00
}