-
Notifications
You must be signed in to change notification settings - Fork 3
/
brain_test.go
99 lines (77 loc) · 2.17 KB
/
brain_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
package bot_test
import (
"testing"
"github.com/botopolis/bot"
"github.com/botopolis/bot/mock"
"github.com/stretchr/testify/assert"
)
func TestBrain(t *testing.T) {
assert := assert.New(t)
brain := bot.NewBrain()
key := "foo"
value := "bar"
var t1 string
brain.Set(key, value)
brain.Get(key, &t1)
assert.Equal(value, t1)
var t2 string
brain.Delete(key)
brain.Get(key, &t2)
assert.Equal("", t2)
}
func TestBrainGet(t *testing.T) {
assert := assert.New(t)
store := mock.NewStore()
brain := bot.NewBrain()
brain.SetStore(store)
called := map[string]int{}
store.GetFunc = func(key string, i interface{}) error {
called[key] = called[key] + 1
return nil
}
brain.Get("missing", nil)
assert.Equal(1, called["missing"], "uses the store when the value is not cached")
brain.Get("missing", nil)
assert.Equal(1, called["missing"], "caches the result from the first missing Get()")
brain.Set("present", nil)
brain.Get("present", nil)
assert.True(called["present"] == 0, "doesn't use the store when the value is cached")
}
func TestBrainSet(t *testing.T) {
assert := assert.New(t)
store := mock.NewStore()
brain := bot.NewBrain()
brain.SetStore(store)
var called bool
store.SetFunc = func(key string, i interface{}) error {
called = true
return nil
}
brain.Set("key", nil)
assert.True(called, "should also set in the store")
}
func TestBrainDelete(t *testing.T) {
assert := assert.New(t)
store := mock.NewStore()
brain := bot.NewBrain()
brain.SetStore(store)
var called bool
store.DeleteFunc = func(key string) error {
called = true
return nil
}
brain.Delete("key")
assert.True(called, "should also delete in the store")
}
func TestBrainKeys(t *testing.T) {
assert := assert.New(t)
brain := bot.NewBrain()
var noKeys []string
assert.Equal(noKeys, brain.Keys(), "should not have any keys")
brain.Set("A. key", nil)
assert.Equal([]string{"A. key"}, brain.Keys(), "should have one key")
brain.Set("C. another", nil)
assert.Equal([]string{"A. key", "C. another"}, brain.Keys(), "should have two keys")
brain.Set("B. alphabetized", nil)
assert.Equal([]string{"A. key", "B. alphabetized", "C. another"}, brain.Keys(), "should alphabetize keys")
}