-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_test.go
108 lines (86 loc) · 2.36 KB
/
dictionary_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"testing"
)
func TestSearch(t *testing.T) {
dictionary := Dictionary{"golang": "Go is all about type"}
t.Run("known word", func(t *testing.T) {
got, _ := dictionary.Search("golang")
want := "Go is all about type"
assertStrings(t, got, want)
})
t.Run("unknown word", func(t *testing.T) {
_, got := dictionary.Search("Java")
assertError(t, got, ErrNotFound)
})
}
func TestAdd(t *testing.T) {
t.Run("new word", func(t *testing.T) {
dictionary := Dictionary{}
word := "golang"
definition := "late have I known golang"
err := dictionary.Add(word, definition)
assertError(t, err, nil)
assertDefinition(t, dictionary, word, definition)
})
t.Run("existing word", func(t *testing.T) {
word := "golang"
definition := "this is just a test"
dictionary := Dictionary{word: definition}
err := dictionary.Add(word, "new test")
assertError(t, err, ErrWordExists)
assertDefinition(t, dictionary, word, definition)
})
}
func TestUpdate(t *testing.T) {
t.Run("existing word", func(t *testing.T) {
word := "golang"
definition := "Go is all about type"
newDefinition := "Go forever"
// add a word to the dictionary
dictionary := Dictionary{word: definition}
// update the dictionary
err := dictionary.Update(word, newDefinition)
assertError(t, err, nil)
assertDefinition(t, dictionary, word, newDefinition)
})
t.Run("new word", func(t *testing.T) {
word := "test"
definition := "Go is all about type"
dictionary := Dictionary{}
err := dictionary.Update(word, definition)
assertError(t, err, ErrWordDoesNotExist)
})
}
func TestDelete(t *testing.T) {
word := "golang"
definition := "Go is all about type"
dictionary := Dictionary{word: definition}
dictionary.Delete(word)
_, err := dictionary.Search(word)
if err != ErrNotFound {
t.Errorf("expected %q to be deleted", word)
}
}
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 %q want %q", got, want)
}
}
func assertDefinition(t testing.TB, dictionary Dictionary, word, definition string) {
t.Helper()
got, err := dictionary.Search("golang")
if err != nil {
t.Fatal("should find added word:", err)
}
if got != definition {
t.Errorf("got %q, want %q", got, definition)
}
}