-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys_test.go
125 lines (95 loc) · 3.45 KB
/
keys_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package keys_test
import (
"testing"
"github.com/go-franky/keys"
)
type T1 struct{}
func (km *T1) Get(k string) string { return "World" }
func (km *T1) Lookup(k string) (string, bool) { return "World", true }
func (km *T1) Set(k, v string) error { return nil }
type emptyMgr struct{}
func (km *emptyMgr) Get(k string) string { return "" }
func (km *emptyMgr) Lookup(k string) (string, bool) { return "", false }
func (km *emptyMgr) Set(k, v string) error { return nil }
func TestManager(t *testing.T) {
km := keys.NewKeyManager()
if v := km.Get("Hello"); v != "" {
t.Errorf("expected a manager to be initialized empty got %v", v)
}
if k, ok := km.Lookup("Hello"); k != "" || ok {
t.Errorf("expected a manager to be initialized empty for lookups, got %v - %v", k, ok)
}
km.Set("Hello", "World")
if k := km.Get("Hello"); k != "World" {
t.Errorf("expected %v, got %v", "World", k)
}
if k, ok := km.Lookup("Hello"); k != "World" || !ok {
t.Errorf("expected a manager to be initialized empty for lookups, got %v - %v", k, ok)
}
}
func TestMultiManager(t *testing.T) {
defaultMgr := keys.NewKeyManager()
empty := &emptyMgr{}
all := []keys.Manager{defaultMgr, empty}
var tAll = keys.MultiManager(all...)
if k, ok := tAll.Lookup("Non-Existient"); k != "" || ok {
t.Errorf("expected a combined manager to be initialized empty for lookups, got %v - %v", k, ok)
}
if k := tAll.Get("Non-Exisitent"); k != "" {
t.Errorf("expected empty, got %v", k)
}
// Setting tAll does not change the others
tAll.Set("Hey", "You")
if k, ok := tAll.Lookup("Hey"); k != "You" || !ok {
t.Errorf("expected a combined manager to be initialized empty for lookups, got %v - %v", "You", ok)
}
if k := tAll.Get("Hey"); k != "You" {
t.Errorf("expected %v, got %v", "You", k)
}
for _, mgr := range all {
if k, ok := mgr.Lookup("Non-Existient"); k != "" || ok {
t.Errorf("expected a combined manager to be initialized empty for lookups, got %v - %v", k, ok)
}
if k := mgr.Get("Non-Exisitent"); k != "" {
t.Errorf("expected empty, got %v", k)
}
}
// Reading any of the listed ones
tAll = keys.MultiManager(defaultMgr, &T1{})
if tAll.Get("Hello") != "World" {
t.Errorf("expected %v, got: %v", "World", tAll.Get("Hello"))
}
// Check the order
defaultMgr.Set("Hello", "This is my World")
if tAll.Get("Hello") != "This is my World" {
t.Errorf("expected %v, got: %v", "This is My World", tAll.Get("Hello"))
}
}
func TestMultiGetter(t *testing.T) {
defaultMgr := keys.NewKeyManager()
empty := &emptyMgr{}
all := []keys.Getter{defaultMgr, empty}
var tAll = keys.MultiGetter(all...)
if k := tAll.Get("Non-Exisitent"); k != "" {
t.Errorf("expected empty, got %v", k)
}
all = []keys.Getter{empty, &T1{}}
tAll = keys.MultiGetter(all...)
if k := tAll.Get("Hello"); k != "World" {
t.Errorf("expected %v, got %v", "World", k)
}
}
func TestMultiLookuper(t *testing.T) {
defaultMgr := keys.NewKeyManager()
empty := &emptyMgr{}
all := []keys.Lookuper{defaultMgr, empty}
var tAll = keys.MultiLookuper(all...)
if k, ok := tAll.Lookup("Non-Existient"); k != "" || ok {
t.Errorf("expected a combined manager to be initialized empty for lookups, got %v - %v", k, ok)
}
all = []keys.Lookuper{empty, &T1{}}
tAll = keys.MultiLookuper(all...)
if k, ok := tAll.Lookup("Hello"); k != "World" || !ok {
t.Errorf("expected a multi lookup to retur right value for key: %v, bool: %v; got key: %v, bool: %v", "World", true, k, ok)
}
}