dict: add Add function and tests
This commit is contained in:
parent
7e713f54de
commit
58efd33b20
@ -13,3 +13,7 @@ func (d Dictionary) Search(word string) (string, error) {
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d Dictionary) Add(word, definition string) {
|
||||||
|
d[word] = definition
|
||||||
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package dictionary
|
package dictionary
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func assertStrings(t testing.TB, got, want, given string) {
|
func assertStrings(t testing.TB, got, want, given string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
@ -36,3 +38,23 @@ func TestSearch(t *testing.T) {
|
|||||||
assertError(t, err, ErrNotFound)
|
assertError(t, err, ErrNotFound)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertDefinition(t testing.TB, dict Dictionary, word, def string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
got, err := dict.Search(word)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Should find added word:", err)
|
||||||
|
}
|
||||||
|
assertStrings(t, got, def, word)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAdd(t *testing.T) {
|
||||||
|
dict := Dictionary{}
|
||||||
|
|
||||||
|
word := "test"
|
||||||
|
def := "this is just a test"
|
||||||
|
|
||||||
|
dict.Add(word, def)
|
||||||
|
assertDefinition(t, dict, word, def)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user