2024-09-11 11:48:59 +00:00
|
|
|
package dictionary
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2024-09-17 07:58:41 +00:00
|
|
|
func assertStrings(t testing.TB, got, want, given string) {
|
2024-09-17 07:55:36 +00:00
|
|
|
t.Helper()
|
2024-09-11 12:07:00 +00:00
|
|
|
|
|
|
|
if got != want {
|
2024-09-17 07:58:41 +00:00
|
|
|
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)
|
2024-09-11 12:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:58:41 +00:00
|
|
|
assertStrings(t, got, want, "test")
|
2024-09-17 07:55:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown word", func(t *testing.T) {
|
|
|
|
_, err := dictionary.Search("undefined")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected to get an error")
|
|
|
|
}
|
2024-09-17 07:58:41 +00:00
|
|
|
assertError(t, err, ErrNotFound)
|
2024-09-17 07:55:36 +00:00
|
|
|
})
|
2024-09-11 11:48:59 +00:00
|
|
|
}
|