Skip to content

Commit

Permalink
add maps
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-toriya committed Sep 2, 2024
1 parent 15029bf commit 320e113
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
60 changes: 60 additions & 0 deletions maps/dictionary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package maps

type Dictionary map[string]string

const (
ErrNotFound = DictionaryErr("word is not found")
ErrWordExist = DictionaryErr("word already exists")
ErrWordDoesNotExist = DictionaryErr("cannot update word because it does not exist")
)

type DictionaryErr string

func (e DictionaryErr) Error() string {
return string(e)
}

func (d Dictionary) Search(word string) (string, error) {
definition, ok := d[word]

if !ok {
return "", ErrNotFound
}

return definition, nil

}

func (d Dictionary) Add(word, definition string) error {
_, err := d.Search(word)

switch err {
case ErrNotFound:
d[word] = definition
case nil:
return ErrWordExist
default:
return err
}

return nil
}

func (d Dictionary) Update(word, newDefinition string) error {
_, err := d.Search(word)

switch err {
case ErrNotFound:
return ErrWordDoesNotExist
case nil:
d[word] = newDefinition
default:
return err
}

return nil
}

func (d Dictionary) Delete(word string) {
delete(d, word)
}
132 changes: 132 additions & 0 deletions maps/dictionary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package maps

import (
"testing"
)

func TestSearch(t *testing.T) {

dictionary := Dictionary{"test": "this is just a test"}

t.Run("key in the dictionary", func(t *testing.T) {

got, _ := dictionary.Search("test")
want := "this is just a test"

assertStrings(t, got, want)
})

t.Run("key is not in the dictionary", func(t *testing.T) {

_, err := dictionary.Search("notHere")

if err == nil {
t.Fatal("expected to get an error")
}

assertError(t, err, ErrNotFound)

})

}

func TestAdd(t *testing.T) {
t.Run("new word", func(t *testing.T) {
dictionary := Dictionary{}
word := "test"
definition := "this is just a test"

err := dictionary.Add(word, definition)

assertError(t, err, nil)
assertDefinition(t, dictionary, word, definition)

})

t.Run("existing word", func(t *testing.T) {
word := "test"
definition := "this is just a test"
dictionary := Dictionary{word: definition}

err := dictionary.Add(word, "new definition for the existing word")

assertError(t, err, ErrWordExist)
assertDefinition(t, dictionary, word, definition)

})

}

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 for the existing word"

dictionary.Update(word, newDefinition)

assertDefinition(t, dictionary, word, newDefinition)

})

t.Run("word doesn't exist", func(t *testing.T) {
dictionary := Dictionary{}

err := dictionary.Update("unknown", "its definition")

assertError(t, err, ErrWordDoesNotExist)
})

}

func TestDelete(t *testing.T) {
t.Run("existing word", func(t *testing.T) {
word := "test"
dictionary := Dictionary{word: "this is just a test"}

dictionary.Delete(word)

_, err := dictionary.Search(word)
assertError(t, err, ErrNotFound)

})

t.Run("word doesn't exist", func(t *testing.T) {
dictionary := Dictionary{}

err := dictionary.Update("unknown", "its definition")

assertError(t, err, ErrWordDoesNotExist)
})

}

func assertStrings(t testing.TB, got, want string) {
t.Helper()

if got != want {
t.Errorf("got %q want %q", got, want)
}
}

func assertError(t testing.TB, got, want error) {
t.Helper()

if got != want {
t.Errorf("got error %q want error %q", got, want)
}
}

func assertDefinition(t testing.TB, dictionary Dictionary, word, definition string) {
t.Helper()

got, err := dictionary.Search(word)

if err != nil {
t.Fatal("should find added word:", err)
}

assertStrings(t, got, definition)
}

0 comments on commit 320e113

Please sign in to comment.