-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcache_test.go
64 lines (52 loc) · 1.4 KB
/
cache_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
package memstore
import (
"reflect"
"testing"
)
func Test_newCache(t *testing.T) {
cache := newCache()
if cache == nil {
t.Error("newCache() got nil")
}
}
func Test_cache_value(t *testing.T) {
cache := newCache()
cache.setValue("key1", valueType{"subkey1": "value1"})
cache.setValue("key2", nil)
cache.setValue("key3", valueType{"subkey3": nil})
tests := []struct {
name string
key string
want valueType
wantOk bool
}{
{"Existing key", "key1", valueType{"subkey1": "value1"}, true},
{"Existing key with nil value type", "key2", nil, true},
{"Existing key and subkey with nil value", "key3", valueType{"subkey3": nil}, true},
{"Not existing key", "thereisnokey", nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, gotOk := cache.value(tt.key)
if gotOk != tt.wantOk {
t.Errorf("cache.value(%v) got ok = %v, want %v", tt.key, gotOk, tt.wantOk)
}
if gotOk && !reflect.DeepEqual(got, tt.want) {
t.Errorf("cache.value(%v) got = %v, want %v", tt.key, got, tt.want)
}
})
}
}
func Test_cache_delete(t *testing.T) {
cache := newCache()
cache.setValue("key1", valueType{"subkey1": "value1"})
_, gotOk := cache.value("key1")
if !gotOk {
t.Error(`cache.value("key1") got ok = false, want true`)
}
cache.delete("key1")
_, gotOk = cache.value("key1")
if gotOk {
t.Error(`cache.value("key1") got ok = true, want false`)
}
}