diff --git a/Go/dictionary/dictionary.go b/Go/dictionary/dictionary.go new file mode 100644 index 0000000..4f7940d --- /dev/null +++ b/Go/dictionary/dictionary.go @@ -0,0 +1,35 @@ +package dictionary + +import "errors" + +type Dictionary map[string]string + +var ( + ErrNotFound = errors.New("could not find the word you were looking for") + ErrWordExists = errors.New("cannot add word because it already exists") +) + +func (d Dictionary) Search(key string) (string, error) { + definition, ok := d[key] + + if !ok { + return "", ErrNotFound + } + + return definition, nil +} + +func (d Dictionary) Add(key, definition string) error { + _, err := d.Search(key) + + switch err { + case ErrNotFound: + d[key] = definition + case nil: + return ErrWordExists + default: + return err + } + + return nil +} diff --git a/Go/dictionary/dictionary_test.go b/Go/dictionary/dictionary_test.go new file mode 100644 index 0000000..fa4b7d8 --- /dev/null +++ b/Go/dictionary/dictionary_test.go @@ -0,0 +1,62 @@ +package dictionary + +import "testing" + +func TestSearch(t *testing.T) { + dictionary := Dictionary{"test": "this is just a test"} + + t.Run("known word", func(t *testing.T) { + got, _ := dictionary.Search("test") + want := "this is just a test" + + if got != want { + t.Errorf("got %q want %q given, %q", got, want, "test") + } + }) + + t.Run("unknown word", func(t *testing.T) { + _, err := dictionary.Search("unknown") + want := "could not find the word you were looking for" + + if err == nil { + t.Fatal("expected to get an error") + } + + if err.Error() != want { + t.Errorf("got %q want %q given, %q", err.Error(), want, "test") + } + }) +} + +func TestAdd(t *testing.T) { + t.Run("new word", func(t *testing.T) { + dictionary := Dictionary{} + dictionary.Add("test", "this is just a test") + + want := "this is just a test" + got, err := dictionary.Search("test") + if err != nil { + t.Fatal("should find added word:", err) + } + + if got != want { + t.Errorf("got %q want %q given, %q", got, want, "test") + } + }) + + t.Run("existing word", func(t *testing.T) { + key := "test" + definition := "this is just a test" + dictionary := Dictionary{key: definition} + err := dictionary.Add(key, "new test") + + if err != ErrWordExists { + t.Errorf("got %q want %q", err, "") + } + + got, _ := dictionary.Search(key) + if got != definition { + t.Errorf("got %q want %q given, %q", got, definition, key) + } + }) +}