summaryrefslogtreecommitdiff
path: root/dictionary_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'dictionary_test.go')
-rw-r--r--dictionary_test.go46
1 files changed, 36 insertions, 10 deletions
diff --git a/dictionary_test.go b/dictionary_test.go
index 89cd37b..393bc08 100644
--- a/dictionary_test.go
+++ b/dictionary_test.go
@@ -23,18 +23,25 @@ func TestSearch(t *testing.T) {
}
func TestAdd(t *testing.T) {
- dictionary := Dictionary{}
- dictionary.Add("test", "this is just a test")
+ t.Run("new word", func(t *testing.T) {
+ dictionary := Dictionary{}
+ key := "test"
+ value := "this is just a test"
+ err := dictionary.Add(key, value)
- want := "this is just a test"
- got, err := dictionary.Search("test")
- if err != nil {
- t.Fatal("should find added word:", err)
- }
+ assertNil(t, err)
+ assertDefinition(t, dictionary, key, value)
+ })
- if want != got {
- t.Errorf("got %q want %q", got, want)
- }
+ t.Run("existing word", func(t *testing.T) {
+ key := "test"
+ value := "this is just a test"
+ dictionary := Dictionary{key: value}
+ err := dictionary.Add(key, "new test")
+
+ assertError(t, err, ErrorWordExists)
+ assertDefinition(t, dictionary, key, value)
+ })
}
func assertStrings(t *testing.T, got, want string) {
@@ -44,3 +51,22 @@ func assertStrings(t *testing.T, got, want string) {
t.Errorf("got %q want %q", got, want)
}
}
+
+func assertDefinition(t *testing.T, dictionary Dictionary, key, expected string) {
+ t.Helper()
+
+ actual, err := dictionary.Search("test")
+ if err != nil {
+ t.Fatal("should find added key:", err)
+ }
+
+ if expected != actual {
+ t.Errorf("actual %q want %q", actual, expected)
+ }
+}
+
+func assertNil(t *testing.T, actual error) {
+ if actual != nil {
+ t.Errorf("expected nil got %q", actual)
+ }
+}