dict: add update method
This commit is contained in:
		@ -3,8 +3,9 @@ package dictionary
 | 
				
			|||||||
type Dictionary map[string]string
 | 
					type Dictionary map[string]string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	ErrNotFound   = DictionaryErr("could not find the word you were looking for")
 | 
						ErrNotFound         = DictionaryErr("could not find the word you were looking for")
 | 
				
			||||||
	ErrWordExists = DictionaryErr("word exists")
 | 
						ErrWordExists       = DictionaryErr("word exists")
 | 
				
			||||||
 | 
						ErrWordDoesNotExist = DictionaryErr("word does not exist")
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type DictionaryErr string
 | 
					type DictionaryErr string
 | 
				
			||||||
@ -34,3 +35,16 @@ func (d Dictionary) Add(word, definition string) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d Dictionary) Update(word, definition string) error {
 | 
				
			||||||
 | 
						_, err := d.Search(word)
 | 
				
			||||||
 | 
						switch err {
 | 
				
			||||||
 | 
						case ErrNotFound:
 | 
				
			||||||
 | 
							return ErrWordDoesNotExist
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							d[word] = definition
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -70,3 +70,24 @@ func TestAdd(t *testing.T) {
 | 
				
			|||||||
		assertDefinition(t, dict, word, def)
 | 
							assertDefinition(t, dict, word, def)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestUpdate(t *testing.T) {
 | 
				
			||||||
 | 
						t.Run("update an existing word", func(t *testing.T) {
 | 
				
			||||||
 | 
							word := "test"
 | 
				
			||||||
 | 
							def := "this is just a test"
 | 
				
			||||||
 | 
							updated := "updated definition"
 | 
				
			||||||
 | 
							dict := Dictionary{word: def}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dict.Update(word, updated)
 | 
				
			||||||
 | 
							assertDefinition(t, dict, word, updated)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("update an non-existing word", func(t *testing.T) {
 | 
				
			||||||
 | 
							word := "test"
 | 
				
			||||||
 | 
							updated := "updated definition"
 | 
				
			||||||
 | 
							dict := Dictionary{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err := dict.Update(word, updated)
 | 
				
			||||||
 | 
							assertError(t, err, ErrWordDoesNotExist)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user