diff options
| author | mo khan <mo.khan@gmail.com> | 2019-11-16 14:26:07 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2019-11-16 14:26:07 -0700 |
| commit | 7236a8c4dcf4879a0ed3f961c911cb75b3da64cd (patch) | |
| tree | 5e9a0a862f0717adef008ebeb31c4c51bcfb963c | |
| parent | 30ca3f00324cac25c9d757c9a3226d55dd545f85 (diff) | |
TestUpdate
| -rw-r--r-- | dictionary.go | 20 | ||||
| -rw-r--r-- | dictionary_test.go | 24 |
2 files changed, 42 insertions, 2 deletions
diff --git a/dictionary.go b/dictionary.go index e2e6c38..14eddd3 100644 --- a/dictionary.go +++ b/dictionary.go @@ -4,8 +4,9 @@ type Dictionary map[string]string type DictionaryError string var ( - ErrorNotFound = DictionaryError("could not find the word you were looking for") - ErrorWordExists = DictionaryError("cannot add word because it already exists") + ErrorNotFound = DictionaryError("could not find the word you were looking for") + ErrorWordDoesNotExist = DictionaryError("cannot update word because it does not exist") + ErrorWordExists = DictionaryError("cannot add word because it already exists") ) func (e DictionaryError) Error() string { @@ -35,3 +36,18 @@ func (d Dictionary) Add(word, definition string) error { return nil } + +func (d Dictionary) Update(word, definition string) error { + _, err := d.Search(word) + + switch err { + case ErrorNotFound: + return ErrorWordDoesNotExist + case nil: + d[word] = definition + default: + return err + } + + return nil +} diff --git a/dictionary_test.go b/dictionary_test.go index 393bc08..a3a9907 100644 --- a/dictionary_test.go +++ b/dictionary_test.go @@ -44,6 +44,30 @@ func TestAdd(t *testing.T) { }) } +func TestUpdate(t *testing.T) { + t.Run("existing word", func(t *testing.T) { + word := "test" + definition := "this is just a test" + + dictionary := Dictionary{word: definition} + newDefinition := "new definition" + err := dictionary.Update(word, newDefinition) + + assertNil(t, err) + assertDefinition(t, dictionary, word, newDefinition) + }) + + t.Run("new word", func(t *testing.T) { + word := "test" + definition := "this is just a test" + + dictionary := Dictionary{} + err := dictionary.Update(word, definition) + + assertError(t, err, ErrorWordDoesNotExist) + }) +} + func assertStrings(t *testing.T, got, want string) { t.Helper() |
