dict: add Add function and tests

This commit is contained in:
vinchent 2024-09-17 10:10:07 +02:00
parent 7e713f54de
commit 58efd33b20
2 changed files with 27 additions and 1 deletions

View File

@ -13,3 +13,7 @@ func (d Dictionary) Search(word string) (string, error) {
} }
return result, nil return result, nil
} }
func (d Dictionary) Add(word, definition string) {
d[word] = definition
}

View File

@ -1,6 +1,8 @@
package dictionary package dictionary
import "testing" import (
"testing"
)
func assertStrings(t testing.TB, got, want, given string) { func assertStrings(t testing.TB, got, want, given string) {
t.Helper() t.Helper()
@ -36,3 +38,23 @@ func TestSearch(t *testing.T) {
assertError(t, err, ErrNotFound) assertError(t, err, ErrNotFound)
}) })
} }
func assertDefinition(t testing.TB, dict Dictionary, word, def string) {
t.Helper()
got, err := dict.Search(word)
if err != nil {
t.Fatal("Should find added word:", err)
}
assertStrings(t, got, def, word)
}
func TestAdd(t *testing.T) {
dict := Dictionary{}
word := "test"
def := "this is just a test"
dict.Add(word, def)
assertDefinition(t, dict, word, def)
}