From 58efd33b206497724e78a3d42052eaa78fa49375 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 17 Sep 2024 10:10:07 +0200 Subject: [PATCH] dict: add Add function and tests --- dictionary/dictionary.go | 4 ++++ dictionary/dictionary_test.go | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/dictionary/dictionary.go b/dictionary/dictionary.go index 30ea5bb..b04ece0 100644 --- a/dictionary/dictionary.go +++ b/dictionary/dictionary.go @@ -13,3 +13,7 @@ func (d Dictionary) Search(word string) (string, error) { } return result, nil } + +func (d Dictionary) Add(word, definition string) { + d[word] = definition +} diff --git a/dictionary/dictionary_test.go b/dictionary/dictionary_test.go index 3ae8146..4e35081 100644 --- a/dictionary/dictionary_test.go +++ b/dictionary/dictionary_test.go @@ -1,6 +1,8 @@ package dictionary -import "testing" +import ( + "testing" +) func assertStrings(t testing.TB, got, want, given string) { t.Helper() @@ -36,3 +38,23 @@ func TestSearch(t *testing.T) { 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) +}