Commit 7236a8c

mo khan <mo.khan@gmail.com>
2019-11-16 21:26:07
TestUpdate
1 parent 30ca3f0
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
+}
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()